Search code examples
nvidiaoptix

NVidia OptiX: Load an OBJ in the PathTracer example


In the path tracer example in the SDK I want to add an OBJ file to trace, so I went over to the loadGeometry() function, and right after the last parallelogram creation, I added this code block

OptiXMesh mesh;
mesh.context = context;
loadMesh(mesh_file, mesh);
gis.push_back(mesh.geom_instance);
//setMaterial(gis.back(), diffuse, "diffuse_color", white);

note that gis is a GeometryInstance vector.

When I run it, the display window opens, and immediately closes and I get the following exceptions:

Exception thrown at 0x00007FFA2856A388 in optixPathTracer.exe: Microsoft C++ exception: optix::TypeMismatch at memory location 0x0000000E29EFF030.
Exception thrown at 0x00007FFA2856A388 in optixPathTracer.exe: Microsoft C++ exception: optix::Exception at memory location 0x0000000E29EFF5C0.

If I comment out the modified code block, it works fine.

How can I load an OBJ file to the tracer? Do I need to add something in the shaders / RT_PROGRAMs side? Thank in advance!

P.S. I know that the loadMesh() function takes care of the material, but since the program doesn't work, I tried to set a material just like it's shown for all the other GeometryInstances, as demonstrated in the code block above.


Solution

  • About intersect function:

    In the path tracer example the scene is made of parallelograms. In the parallelogram.cu file the intersect function is called by the OptiX pipeline to detect if a ray intersects a parallelogram (4 points shape). In OptixPathTracer.cpp, in the createParallelogram method, the intersection program is set to be the function in parallelogram.cu file.

    When you load a mesh, it is (most commonly) made of triangles, so the intersect function that is being used is not appropriate to the geometry of the mesh (and I guess that explains the TypeMismatch error, but the message is not very clear and you did not post a complete example that allows to reproduce the error).

    How to fix:

    If you look at the ray casting example, in OptixRaycastingContext.cu there's an intersect function that's made for triangles. You should most probably copy that triangle intersection function to your cu file (don't forget to rename it: there's already an intercept function there that is used for parallelograms), then when you create the Geometry object for your mesh, call setIntersectionProgram with the triangle intersection function as parameter.

    The other way:

    You can also start working on the mesh viewer example and change the raytracing code (cu files) to do path tracing. It is a good exercice to understand how OptiX works.