Creating a new CMakeLists.txt for building and using vtkOSPRay* with c++

I successfully built OSPRay and VTK 9/8 with it and the Rendering vtkExamples and Tests/Testing are working. I am having difficulty updating my own CMakeLists.txt file for c++ to “make” the includes and libraries known but I have been building VTK programs until now. I looked at the CMakeLists.txt files in the example/test directories but I don’t yet understand how to make the OSPray parts work because they are dependent on CMake files higher in the VTK build directory structure (when I cut and pasted subparts). I did browse this forum for solutions, but most pertain to how incorporate OSPRay in the build itself, not the usage. Please advise. Thanks

Hi @Spacetracker

find_package(VTK) should be enough. Do you intend to use ospray directly in your project of just VTKRaytracing classes ?

Best,

Not unless necessary; I just want to tell the renderer to use the ray tracer in place of OpenGL sometimes. I will go to the classes more directly only if I need to. I am using ImplicitFunctions and am sampling them, but would like to have clouds of gridded pressures in a volume eventually. I am looking for a good example of switching the renderer to ray tracing for volume rendering (instead of surface rendering now). Can you point me to some?

I typically look at the example CMake files to determine what packages I needed and assumed I would need the same here. The OSPRay CMakes themselves are more complicated. I first tried adding some OSPRay-specific classes to find_packages put ran into trouble. Without them, I have added three vtkOSPRay* includes to my source without complaint, so they must be under another package I have already loaded.

Oh, and I am software rendering on a headless server.

Thanks

Then this sound like you have OSPRay questions, not VTK questions. VTK do not provide OSPRay feature, it provides raytracing features through OSPRay.

If you want to use OSPRay directly, you need to do just that.

Then this sound like you have OSPRay questions, not VTK questions. VTK do not provide OSPRay feature, it provides raytracing features through OSPRay.

I am sorry I was not clear. I certainly don’t want to program OSPRay calls from that API at all, or control many or any of the variables. My question is totally about how to use VTK for ray tracing that was built with OSRPray. Inside of VTK, there are vtkOSPRay calls directly, and another level inside the renderers that may not call for doing even that. I am getting confused from the old and new examples of how to do that properly.

Thanks again.

Did you try using the OSPRay examples testing ?

https://gitlab.kitware.com/vtk/vtk/-/tree/master/Rendering/RayTracing/Testing/Cxx

I should have asked, “How to switch between OpenGL and ray tracing rendering?” But I was getting stuck on problems and finding a good example.

Yes, I have been going through those examples. That is where I am now. I just thought I saw a simpler example before. I will work on that today. Thanks.

BTW. I am already looking at the Testing examples. I am also looking for “usage” examples. There is one old Ray Casting example, but no OSPRay usage examples yet.

From: https://kitware.github.io/vtk-examples/site/
“Hundreds of tests are distributed with the toolkit source. The tests reside in ‘‘Kit’’/Testing directories (for example, Filters/Points/Testing) in the source distribution. However, these tests are meant to exercise the toolkit rather than illustrate how to use it. For the most part, the tests are not good educational resources.”

but are better than nothing, as there is not ospray examples available.

VTK examples is (also) an open source project, open for anyone to create new ones, sadly no OSPRay examples were ever submited.

OK. I am back to the original question. How to include the ospray package in my CMake file.

I built all of Testing and ran all of the RayTracing ones interactively. Those will help me with the source code. But I am still stuck on getting ospray properly loaded in the CMakeLists.txt file.

Even though I added the ospray libraries to my path I still get vtk OSPRAY errors:

Undefined symbols for architecture x86_64:
  "vtkOSPRayPass::New()", referenced from:
      _main in AlveoliMPIVolumeRay.cxx.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[2]: *** [AlveoliMPIVolumeRay] Error 1
make[1]: *** [CMakeFiles/AlveoliMPIVolumeRay.dir/all] Error 2
make: *** [all] Error 2

I have looked at all three levels of CMake files under RayTracing. When I cut and paste the part I think I need, I get this error:

CMake Error at []vtkINSTALL/lib/cmake/vtk-9.1/vtkObjectFactory.cmake:93 (message):
  The `vtk_object_factory_configure` function needs to be run within a module
  context.
Call Stack (most recent call first):
  CMakeLists.txt:41 (vtk_object_factory_configure)

This is what I copied over. How LITTLE do I need? I don’t want the RTX or denoiser, etc anyway.

#to add OSPRay renderer
vtk_object_factory_declare(
  BASE vtkOSPRayVolumeInterface
  OVERRIDE vtkOSPRayVolumeMapper)

vtk_object_factory_configure(
  SOURCE_FILE vtk_object_factory_source
  HEADER_FILE vtk_object_factory_header
  EXPORT_MACRO "VTKRENDERINGRAYTRACING_EXPORT")

option(VTK_ENABLE_OSPRAY "Enable OSPRay RayTracing backend" ON)
cmake_dependent_option(VTKOSPRAY_ENABLE_DENOISER
  "build OSPRay Renderer using OpenImageDenoise" OFF
  "VTK_ENABLE_OSPRAY" OFF)

option(VTK_ENABLE_VISRTX "Enable VisRTX RayTracing backend" OFF)
if (VTK_ENABLE_VISRTX)
  list(APPEND sources RTWrapper/VisRTX/VisRTXBackend.cxx)
endif ()

vtk_module_add_module(VTK::RenderingRayTracing
  HEADER_DIRECTORIES
  CLASSES ${classes}
  SOURCES ${vtk_object_factory_source} ${sources}
  NOWRAP_CLASSES  ${nowrap_classes}
  NOWRAP_HEADERS  ${nowrap_headers}
  PRIVATE_HEADERS ${vtk_object_factory_header})

if (VTK_ENABLE_OSPRAY)
  vtk_module_find_package(
    PACKAGE ospray
    VERSION 2.1)

  vtk_module_link(VTK::RenderingRayTracing
    PUBLIC
      ospray::ospray
      )
  if (VTKOSPRAY_ENABLE_DENOISER)
    vtk_module_find_package(
      PACKAGE OpenImageDenoise
      FORWARD_VERSION_REQ MINOR)
    vtk_module_definitions(VTK::RenderingRayTracing
      PRIVATE
        VTKOSPRAY_ENABLE_DENOISER)
    vtk_module_link(VTK::RenderingRayTracing
      PRIVATE
        OpenImageDenoise)
  endif ()

  # TODO: FindOSPRay should do this.
  # OSPRay_Core uses MMTime which is in it's own special library.
  if (WIN32)
    vtk_module_link(VTK::RenderingRayTracing
      PRIVATE
        Winmm)
  endif ()
  vtk_module_definitions(VTK::RenderingRayTracing
    PRIVATE
      VTK_ENABLE_OSPRAY
      OSPRAY_VERSION_MINOR=${ospray_VERSION_MINOR})
endif ()

# VisRTX
if (VTK_ENABLE_VISRTX)
  vtk_module_find_package(
    PACKAGE VisRTX
    CONFIG_MODE)
  vtk_module_link(VTK::RenderingRayTracing
    PUBLIC
      VisRTX_DynLoad)
  vtk_module_definitions(VTK::RenderingRayTracing
    PRIVATE
      VTK_ENABLE_VISRTX)
endif ()

So let me ask the same question, do you want to use OSPray directly or do you want to use VTK raytracing classes ?

Using the VTK ray tracing classes has really been my only goal. I do not want to write any OSPRay code. I built VTK with it and ran the OSPray tests. It seems to be as simple as this:

vtkSmartPointer ospray = vtkSmartPointer::New();
vtkNew renderer;
renderer->SetPass(ospray);

My question has always been this. I am already building a VTK program. What do I need to add to my CmakeList.txt file to get past the first link error? I attempted to add items from the Testing CMakeList.txt file to do that, but got the other error.

Never mind. It was easier than I thought. But I still learned from this. Thanks.