Building objects within OptiX

Hello team!

I have studied the different OptiX samples and tried modifying some of them. A question for you though. I have made a copy of the OptixWhitted sample and inside my copy I would like to add a cube but I am not quite how to do this. I have seen how a cube is made for example within the sample “OptixPathTracer” and I have also successfully created a cube inside of that sample but it seems like different samples use different techniques to create geometrical objects so I am not quite sure on how to go about to create a cube within my copy of “OptixWhitted”. Any help regarding this?

Thanks in advance!

The optixWhitte example is special in that it defines two custom geometric primitives (parallelogram, sphere) which build a super simple scene by hardcoding the sphere and floor parallelogram positions inside the source code. It doesn’t even use the built-in triangles.

The optixPathTracer is building the very little geometry inside its scene by simply hardcoding the vertices for the objects in some global arrays (see g_vertices) and builds triangles from those.

Means you would need to learn how to build your cube from triangles as shown inside the optixPathTracer and then enhance the createGeometry() function to also create a triangle geometry. That in turn cannot be put together with custom primitives into the same geometry accelerations structure (GAS), which means you’d need an instance acceleration structure (IAS) on top of the custom and triangle GAS. That requires understanding of the shader binding table (SBT) a little more, because that needs to be adjusted for the additional geometry as well.

I would recommend looking at the examples which use built-in triangles first and then figure out how to implement a Whitted renderer with those. Then you can still learn how to handle custom geometric primitives like spheres and combine everything in your version of a Whitted renderer.

You would normally build more complex objects with code at runtime or load arbitrary models from scene file formats.
The optixMeshViewer shows how to do the latter. It can load glTF models (most of them), build the acceleration structures of the geometry, and supports the material.

For other examples generating geometry at runtime, maybe have a look at my OptiX examples which have functions creating a simple box from 12 triangles, tessellated planes, spheres and a torus as well as loading arbitrary mesh geometry from any file format supported by ASSIMP.

Look for Box.cpp, Plane.cpp, Sphere.cpp, Torus.cpp here

The Assimp.cpp in the more advanced examples contains the code loading polygonal mesh data.

This data is all loaded into host structures first and then the geometry accelerations structures are built from that.
For the runtime generated objects, that always happens with the same createGeometry() function at the end.

Please take care to not mix and match the different geometry representations in these SDK resp. open-source examples. Some care needs to be taken to match the vertex attributes to the calculations inside the closesthit or anyhit programs accessing these the current hit point.

Hello again. I wanted to try building the more advanced samples in the OptiX_Apps file.
I have followed your steps successfully when building the examples but when I get to the step where I use the “make” command, it loads until about 28% then I get this error:

[ 28%] Linking CXX executable …/…/bin/intro_driver
/usr/bin/ld: /usr/local/lib/libglfw3.a(posix_thread.c.o): undefined reference to symbol ‘pthread_getspecific@@GLIBC_2.2.5
/usr/bin/ld: /lib/x86_64-linux-gnu/ error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [apps/intro_driver/CMakeFiles/intro_driver.dir/build.make:456: bin/intro_driver] Error 1
make[1]: *** [CMakeFiles/Makefile2:244: apps/intro_driver/CMakeFiles/intro_driver.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

How do I resolve this?

With kind regards, Emil.

I’m not using Linux myself and I have not heard of this error from people who build the examples under Linux before.

From the error message that looks as if GLFW 3 requires functions from the pthread library.

Searching the internet showed similar link errors about pthread in other projects and the recommended change was to add pthread to the CMAKE_CXX_FLAGS.
So you could try inserting the following line into the root CMakeList.txt before line 24 for the Unix build settings:
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread" )

1 Like

I have tried to add it, but I still failed. The compiler reported that: c++: error: pthread: no such file or directory.

Using set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread" ) before line 24 is OK. Thank you!

“-pthread” not “pthread”

OK, right. Thanks. I corrected it in the original post.
I added that line inside the repository now while I updated the examples to support the newly released OptiX SDK 7.4.0 as well.