daily pastebin goal
68%
SHARE
TWEET

Untitled

a guest Dec 15th, 2018 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "stdafx.h"
  2. #include "tutorials.h"
  3. #include "raytracer.h"
  4. #include "structs.h"
  5. #include "texture.h"
  6. #include "mymath.h"
  7.  
  8. /* error reporting function */
  9. void error_handler( void * user_ptr, const RTCError code, const char * str )
  10. {
  11.     if ( code != RTC_ERROR_NONE )
  12.     {
  13.         std::string descr = str ? ": " + std::string( str ) : "";
  14.  
  15.         switch ( code )
  16.         {
  17.         case RTC_ERROR_UNKNOWN: throw std::runtime_error( "RTC_ERROR_UNKNOWN" + descr );
  18.         case RTC_ERROR_INVALID_ARGUMENT: throw std::runtime_error( "RTC_ERROR_INVALID_ARGUMENT" + descr ); break;
  19.         case RTC_ERROR_INVALID_OPERATION: throw std::runtime_error( "RTC_ERROR_INVALID_OPERATION" + descr ); break;
  20.         case RTC_ERROR_OUT_OF_MEMORY: throw std::runtime_error( "RTC_ERROR_OUT_OF_MEMORY" + descr ); break;
  21.         case RTC_ERROR_UNSUPPORTED_CPU: throw std::runtime_error( "RTC_ERROR_UNSUPPORTED_CPU" + descr ); break;
  22.         case RTC_ERROR_CANCELLED: throw std::runtime_error( "RTC_ERROR_CANCELLED" + descr ); break;
  23.         default: throw std::runtime_error( "invalid error code" + descr ); break;
  24.         }
  25.     }
  26. }
  27.  
  28. /* adds a single triangle to the scene */
  29. unsigned int add_triangle( const RTCDevice device, RTCScene scene )
  30. {
  31.     // geometries are objects that represent an array of primitives of the same type, so lets create a triangle
  32.     RTCGeometry mesh = rtcNewGeometry( device, RTC_GEOMETRY_TYPE_TRIANGLE );
  33.  
  34.     // and depending on the geometry type, different buffers must be bound (typically, vertex and index buffer is required)
  35.  
  36.     // set vertices in the newly created buffer
  37.     Vertex3f * vertices = ( Vertex3f * )rtcSetNewGeometryBuffer(
  38.         mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof( Vertex3f ), 3 );
  39.     vertices[0].x = 0; vertices[0].y = 0; vertices[0].z = 0;
  40.     vertices[1].x = 2; vertices[1].y = 0; vertices[1].z = 0;
  41.     vertices[2].x = 0; vertices[2].y = 3; vertices[2].z = 0;
  42.  
  43.     // set triangle indices
  44.     Triangle3ui * triangles = ( Triangle3ui * )rtcSetNewGeometryBuffer( mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof( Triangle3ui ), 1 );
  45.     triangles[0].v0 = 0; triangles[0].v1 = 1; triangles[0].v2 = 2;
  46.  
  47.     // see also rtcSetSharedGeometryBuffer, rtcSetGeometryBuffer       
  48.  
  49.     /*
  50.     The parametrization of a triangle uses the first vertex p0 as base point, the vector (p1 - p0) as u-direction and the vector (p2 - p0) as v-direction.
  51.     Thus vertex attributes t0, t1, t2 can be linearly interpolated over the triangle the following way:
  52.  
  53.     t_uv = (1-u-v)*t0 + u*t1 + v*t2 = t0 + u*(t1-t0) + v*(t2-t0)
  54.     */
  55.  
  56.     // sets the number of slots (vertexAttributeCount parameter) for vertex attribute buffers (RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE)
  57.     rtcSetGeometryVertexAttributeCount( mesh, 2 );
  58.  
  59.     // set vertex normals
  60.     Normal3f * normals = ( Normal3f * )rtcSetNewGeometryBuffer( mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0, RTC_FORMAT_FLOAT3, sizeof( Normal3f ), 3 );
  61.     normals[0].x = 0; normals[0].y = 0; normals[0].z = 1;
  62.     normals[1].x = 0; normals[1].y = 0; normals[1].z = 1;
  63.     normals[2].x = 0; normals[2].y = 0; normals[2].z = 1;
  64.  
  65.     // set texture coordinates
  66.     Coord2f * tex_coords = ( Coord2f * )rtcSetNewGeometryBuffer( mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 1, RTC_FORMAT_FLOAT2, sizeof( Coord2f ), 3 );
  67.     tex_coords[0].u = 0; tex_coords[0].v = 1;
  68.     tex_coords[1].u = 1; tex_coords[1].v = 1;
  69.     tex_coords[2].u = 0; tex_coords[2].v = 0;
  70.  
  71.     // changes to the geometry must be always committed
  72.     rtcCommitGeometry( mesh );
  73.  
  74.     // geometries can be attached to a single scene
  75.     unsigned int geom_id = rtcAttachGeometry( scene, mesh );
  76.     // release geometry handle
  77.     rtcReleaseGeometry( mesh );
  78.  
  79.     return geom_id;
  80. }
  81.  
  82. /* generate a single ray and get the closest intersection with the scene */
  83. int generate_and_trace_ray( RTCScene & scene )
  84. {
  85.     // setup a primary ray
  86.     RTCRay ray;
  87.     ray.org_x = 0.1f; // ray origin
  88.     ray.org_y = 0.2f;
  89.     ray.org_z = 2.0f;
  90.     ray.tnear = FLT_MIN; // start of ray segment
  91.  
  92.     ray.dir_x = 0.0f; // ray direction
  93.     ray.dir_y = 0.0f;
  94.     ray.dir_z = -1.0f;
  95.     ray.time = 0.0f; // time of this ray for motion blur
  96.  
  97.     ray.tfar = FLT_MAX; // end of ray segment (set to hit distance)
  98.  
  99.     ray.mask = 0; // can be used to mask out some geometries for some rays
  100.     ray.id = 0; // identify a ray inside a callback function
  101.     ray.flags = 0; // reserved
  102.  
  103.     // setup a hit
  104.     RTCHit hit;
  105.     hit.geomID = RTC_INVALID_GEOMETRY_ID;
  106.     hit.primID = RTC_INVALID_GEOMETRY_ID;
  107.     hit.Ng_x = 0.0f; // geometry normal
  108.     hit.Ng_y = 0.0f;
  109.     hit.Ng_z = 0.0f;
  110.  
  111.     // merge ray and hit structures
  112.     RTCRayHit ray_hit;
  113.     ray_hit.ray = ray;
  114.     ray_hit.hit = hit;
  115.  
  116.     // intersect ray with the scene
  117.     RTCIntersectContext context;
  118.     rtcInitIntersectContext( &context );
  119.     rtcIntersect1( scene, &context, &ray_hit );
  120.  
  121.     if ( ray_hit.hit.geomID != RTC_INVALID_GEOMETRY_ID )
  122.     {
  123.         // we hit something
  124.         RTCGeometry geometry = rtcGetGeometry( scene, ray_hit.hit.geomID );
  125.         Normal3f normal;
  126.         // get interpolated normal
  127.         rtcInterpolate0( geometry, ray_hit.hit.primID, ray_hit.hit.u, ray_hit.hit.v,
  128.             RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0, &normal.x, 3 );
  129.         // and texture coordinates
  130.         Coord2f tex_coord;
  131.         rtcInterpolate0( geometry, ray_hit.hit.primID, ray_hit.hit.u, ray_hit.hit.v,
  132.             RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 1, &tex_coord.u, 2 );
  133.  
  134.         printf( "normal = (%0.3f, %0.3f, %0.3f)\n", normal.x, normal.y, normal.z );
  135.         printf( "tex_coord = (%0.3f, %0.3f)\n", tex_coord.u, tex_coord.v );
  136.     }
  137.  
  138.     return EXIT_SUCCESS;
  139. }
  140.  
  141. /* simple tutorial how to find the closest intersection with a single triangle using embree */
  142. int tutorial_1( const char * config )
  143. {
  144.     RTCDevice device = rtcNewDevice( config );
  145.     error_handler( nullptr, rtcGetDeviceError( device ), "Unable to create a new device.\n" );
  146.     rtcSetDeviceErrorFunction( device, error_handler, nullptr );
  147.  
  148.     ssize_t triangle_supported = rtcGetDeviceProperty( device, RTC_DEVICE_PROPERTY_TRIANGLE_GEOMETRY_SUPPORTED );
  149.  
  150.     // create a new scene bound to the specified device
  151.     RTCScene scene = rtcNewScene( device );
  152.  
  153.     // add a single triangle geometry to the scene
  154.     unsigned int triangle_geom_id = add_triangle( device, scene );
  155.  
  156.     // commit changes to scene
  157.     rtcCommitScene( scene );
  158.  
  159.     generate_and_trace_ray( scene );
  160.  
  161.     // release scene and detach with all geometries
  162.     rtcReleaseScene( scene );
  163.  
  164.     rtcReleaseDevice( device );
  165.  
  166.     return EXIT_SUCCESS;
  167. }
  168.  
  169. /* texture loading and texel access */
  170. int tutorial_2()
  171. {
  172.     // create texture
  173.     Texture texture( "../../../data/test4.png" );
  174.     Color3f texel = texture.get_texel( ( 1.0f / texture.width() ) * 2.5f, 0.0f );
  175.     printf( "(r = %0.3f, g = %0.3f, b = %0.3f)\n", texel.r, texel.g, texel.b );
  176.  
  177.     return EXIT_SUCCESS;
  178. }
  179.  
  180. /* raytracer mainloop */
  181. int tutorial_3( const std::string file_name, const char * config )
  182. {
  183.     //SimpleGuiDX11 gui( 640, 480 );
  184.     //gui.MainLoop();
  185.     //Raytracer raytracer(640, 480, deg2rad(45.0), Vector3(175, -140, 130), Vector3(0, 0, 35), config);
  186.     Raytracer raytracer(640, 480, deg2rad(40.0),Vector3(40, -940, 250), Vector3(0, 0, 250), config);
  187.     //Raytracer raytracer( 640, 480, deg2rad( 45.0 ),Vector3( 0, -50,50), Vector3( 0, 0, 0 ), config );
  188.     raytracer.LoadScene( file_name );
  189.     raytracer.MainLoop();
  190.  
  191.     return EXIT_SUCCESS;
  192. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top