# Unable to do volume rendering on external VTK render window

**URL:** https://discourse.vtk.org/t/unable-to-do-volume-rendering-on-external-vtk-render-window/14459
**Category:** Support
**Created:** [August 22, 2024, 1:25pm UTC](https://discourse.vtk.org/t/unable-to-do-volume-rendering-on-external-vtk-render-window/14459 "2024-08-22T13:25:59Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![CK008](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ck008/32/9205_2.png) [@CK008](https://discourse.vtk.org/u/CK008)
#### Post date: [August 22, 2024, 1:25pm UTC](https://discourse.vtk.org/t/unable-to-do-volume-rendering-on-external-vtk-render-window/14459/1 "2024-08-22T13:25:59Z")

</div>

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](https://gitlab.kitware.com/vtk/vtk/-/blob/0b7d6de3dddbcdb5dd9267a9586d59962464819e/Rendering/External/Testing/Cxx/TestGLUTRenderWindow.cxx)

```auto
        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](https://github.com/Chahat08/Win32_OpenGL/blob/vtk/main.cpp)). 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/](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](https://github.com/Chahat08/Win32_OpenGL/blob/volume_rendering/main.cpp).

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

---

<div class="post-metadata">

### Author: ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)
#### Post date: [August 22, 2024, 2:11pm UTC](https://discourse.vtk.org/t/unable-to-do-volume-rendering-on-external-vtk-render-window/14459/2 "2024-08-22T14:11:40Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![CK008](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ck008/32/9205_2.png) [@CK008](https://discourse.vtk.org/u/CK008)
#### Post date: [August 22, 2024, 3:31pm UTC](https://discourse.vtk.org/t/unable-to-do-volume-rendering-on-external-vtk-render-window/14459/3 "2024-08-22T15:31:40Z")

</div>

Hi @jaswantp , thank you for the reply.

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

---

<div class="post-metadata">

### Author: ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)
#### Post date: [August 22, 2024, 4:01pm UTC](https://discourse.vtk.org/t/unable-to-do-volume-rendering-on-external-vtk-render-window/14459/4 "2024-08-22T16:01:26Z")

</div>

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](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`.

---

<div class="post-metadata">

### Author: ![CK008](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ck008/32/9205_2.png) [@CK008](https://discourse.vtk.org/u/CK008)
#### Post date: [August 28, 2024, 6:20pm UTC](https://discourse.vtk.org/t/unable-to-do-volume-rendering-on-external-vtk-render-window/14459/5 "2024-08-28T18:20:17Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)
#### Post date: [August 28, 2024, 6:23pm UTC](https://discourse.vtk.org/t/unable-to-do-volume-rendering-on-external-vtk-render-window/14459/6 "2024-08-28T18:23:49Z")

</div>

Do you see any shader errors?

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

---

<div class="post-metadata">

### Author: ![CK008](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ck008/32/9205_2.png) [@CK008](https://discourse.vtk.org/u/CK008)
#### Post date: [August 28, 2024, 6:30pm UTC](https://discourse.vtk.org/t/unable-to-do-volume-rendering-on-external-vtk-render-window/14459/7 "2024-08-28T18:30:24Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)
#### Post date: [August 28, 2024, 6:40pm UTC](https://discourse.vtk.org/t/unable-to-do-volume-rendering-on-external-vtk-render-window/14459/8 "2024-08-28T18:40:12Z")

</div>

> [@CK008](#):
>
> However, I can not see anything being passed to the shaders for the volume rendering example.

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

---

<div class="post-metadata">

### Author: ![CK008](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ck008/32/9205_2.png) [@CK008](https://discourse.vtk.org/u/CK008)
#### Post date: [August 28, 2024, 7:14pm UTC](https://discourse.vtk.org/t/unable-to-do-volume-rendering-on-external-vtk-render-window/14459/9 "2024-08-28T19:14:44Z")

</div>

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/](https://www.kitware.com/new-in-vtk-rendering-in-external-immersive-environments/)

---

<div class="post-metadata">

### Author: ![sankhesh](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sankhesh/32/73_2.png) [@sankhesh](https://discourse.vtk.org/u/sankhesh)
#### Post date: [August 28, 2024, 7:38pm UTC](https://discourse.vtk.org/t/unable-to-do-volume-rendering-on-external-vtk-render-window/14459/10 "2024-08-28T19:38:27Z")

</div>

@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.
