Unable to do volume rendering on external VTK render window

I have an existing Win32 OpenGL rendering engine. I decided to integrate VTK into it to extend it’s functionality to volume rendering. This seemed very possible with VTK’s vtkRenderingExternal module.

The below is what I used to test at first and I was able to recreate this GLUT example in my Win32 application.
https://gitlab.kitware.com/vtk/vtk/-/blob/0b7d6de3dddbcdb5dd9267a9586d59962464819e/Rendering/External/Testing/Cxx/TestGLUTRenderWindow.cxx

        vtkNew<vtkExternalOpenGLRenderWindow> renderWindow;
        externalVTKWidget->SetRenderWindow(renderWindow.GetPointer());

        vtkNew<vtkCallbackCommand> callback;
        callback->SetCallback(MakeCurrentCallback);
        renderWindow->AddObserver(vtkCommand::WindowMakeCurrentEvent, callback.GetPointer());

        vtkNew<vtkPolyDataMapper> mapper;
        vtkNew<vtkActor> actor;
        actor->SetMapper(mapper.GetPointer());
        vtkRenderer* renderer = externalVTKWidget->AddRenderer();
        renderer->AddActor(actor.GetPointer());

        vtkNew<vtkCubeSource> cs;
        mapper->SetInputConnection(cs->GetOutputPort());
        renderer->ResetCamera();

So, this worked and I was able to get a cube (whole code at: Win32_OpenGL/main.cpp at vtk · Chahat08/Win32_OpenGL · GitHub). However, when I tried extending the same to volume rendering, it didn’t work:

renderer->AddViewProp(volume);

For full context, this is the MedicalDemo4 volume rendering example from the VTK examples website (https://examples.vtk.org/site/Cxx/Medical/MedicalDemo4/). My full code for testing volume rendering is at Win32_OpenGL/main.cpp at volume_rendering · Chahat08/Win32_OpenGL · GitHub.

I’m using OpenGL 3.3 with custom shaders and Win32 for window creation and management.

Hello @CK008

Thank you for sharing the code.

I’m using OpenGL 3.3 with custom shaders and Win32 for window creation and management.

This maybe the problem.

I see that you’re using gladLoadGL. Can you regenerate the glad.c files to target OpenGL 4.5 instead?

Hi @jaswantp , thank you for the reply.

I tried with OpenGL 4.5 and still am unable to render the volume.

Probably because you’re creating a 1.1 opengl context using wglCreateContext. Instead you might have to use wgl extensions wglChoosePixelFormatARB and wglCreateContextAttribsARB like done in vtkWin32OpenGLRenderWindow https://gitlab.kitware.com/vtk/vtk/-/blob/master/Rendering/OpenGL2/vtkWin32OpenGLRenderWindow.cxx#L577.

For the extensions, you can generate wgl.h and wgl.c as well from glad site and call gladLoaderLoadWGL before gladLoadGL.

Hey, I tried again, this time creating the win32 app as you suggested. Again, I was able to get the cube to render but the volume still does not show up.

It would definitely help if I can get some example or tutorial where volume rendering is done in an external opengl context.

Do you see any shader errors?

Otherwise, it could be that the camera is incorrectly configured. Can you try renderer->ResetCamera() and renderer->ResetCameraClippingRange()?

I tried them both, it doesn’t work. And there’s no errors either.

So, the interesting thing is that I used RenderDoc to analyse the buffer contents of my application. For the cube example, I can clearly see the geometry being passed to the vertex shader.

However, I can not see anything being passed to the shaders for the volume rendering example. And it’s definitely don’t the case that the volume is not being read or parsed correctly, because I can see the volume bounds using volume->GetBounds() and they are correct.

Could you use vtkSmartVolumeMapper instead of vtkFixedPointVolumeRayCastMapper? The fixed point mapper is software based, it doesn’t use OpenGL.

Is replacing vtkNew<vtkFixedPointVolumeRayCastMapper> volumeMapper; with vtkNew<vtkSmartVolumeMapper> volumeMapper; sufficient? Just doing that is still not rendering the volume.

I’m new to VTK so am unsure if I’m doing things the right way. The image from this post is the only clue I have that makes it seem like volume rendering in an external context is even possible. But, since I can’t find any examples demonstrating it, I’m feeling really struck at this point.

https://www.kitware.com/new-in-vtk-rendering-in-external-immersive-environments/

@CK008 Are you getting any errors? Or is it just that you’re not seeing any rendering? If so, my guess is that your camera parameters are clipping the volume away. Could you make sure that your OpenGL camera frustum is set up properly to encapsulate the volume bounds?

Switching to vtkSmartVolumeMapper is definitely the right way to go forward.