# Ray Tracing in VTK - Capabilities in surface and volume Rendering - Own Implementation

**URL:** https://discourse.vtk.org/t/ray-tracing-in-vtk-capabilities-in-surface-and-volume-rendering-own-implementation/11308
**Category:** Support
**Tags:** python
**Created:** [April 26, 2023, 11:42am UTC](https://discourse.vtk.org/t/ray-tracing-in-vtk-capabilities-in-surface-and-volume-rendering-own-implementation/11308 "2023-04-26T11:42:27Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![DMelenberg](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dmelenberg/32/6926_2.png) [@DMelenberg](https://discourse.vtk.org/u/DMelenberg)
#### Post date: [April 26, 2023, 11:42am UTC](https://discourse.vtk.org/t/ray-tracing-in-vtk-capabilities-in-surface-and-volume-rendering-own-implementation/11308/1 "2023-04-26T11:42:28Z")

</div>

Hello All,

I am new to the community and have only recently started reading up on vtk and need support on the possibilities of this great library.

Namely, I want to develop my own ray tracer in Python using the `vtk` library. As I understood it according to the following post, the own implementation of a ray tracer is relatively easy possible without creating new classes, provided that the data (assuming medical CBCT data) has already been converted to a **surface model** with e.g. `vtkMarchingCubes` [From Ray Casting to Ray Tracing with Python and VTK | PyScience (wordpress.com)](https://pyscience.wordpress.com/2014/10/05/from-ray-casting-to-ray-tracing-with-python-and-vtk/) One advantage of this approach is yes, that one can very easily perform cut tests with the `OBBTree` class. However, a disadvantage is that if you want to represent different tissue types, such as bone and soft tissue with different material properties, you should first segment each tissue type of a dataset and define its own material property. (As far as I understood 🙂)

Furthermore, both researches didn’t show me that this possibility to use existing vtk classes can also be used for the implementation of an own ray-tracing volume rendering algorithm. For this I only found that you have to create your own derivation of the class `vtkVolumeMappers`, in which you can then integrate the functions. Is this also possible in Python? And to what extent can I merge the functions of other libraries, like `pyoptix` with the `vtk` functionalities to achieve my goals? Or in the same way with paraview?

Thank you very much in advance and I appreciate any answer!

Best regards David

---

<div class="post-metadata">

### Author: ![Jens\_Munk\_Hansen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jens_munk_hansen/32/1215_2.png) [@Jens\_Munk\_Hansen](https://discourse.vtk.org/u/Jens_Munk_Hansen)
#### Post date: [April 26, 2023, 10:44pm UTC](https://discourse.vtk.org/t/ray-tracing-in-vtk-capabilities-in-surface-and-volume-rendering-own-implementation/11308/2 "2023-04-26T22:44:29Z")

</div>

Many questions at once. Do you want to implement your own ray tracer or would it be sufficent to play around with an open-source ray tracer? Have you looked into `vtkOSPRay`? To see how things fit into the VTK framework, I believe it is a good place to start.

---

<div class="post-metadata">

### Author: ![DMelenberg](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dmelenberg/32/6926_2.png) [@DMelenberg](https://discourse.vtk.org/u/DMelenberg)
#### Post date: [April 27, 2023, 12:56pm UTC](https://discourse.vtk.org/t/ray-tracing-in-vtk-capabilities-in-surface-and-volume-rendering-own-implementation/11308/3 "2023-04-27T12:56:31Z")

</div>

I thank you for the quick reply. exactly, I want to write my own RayTracing algorithm on volume data. I also read something about `vtkRenderPass`? Could you implement your own renderer using this interface?  
I left it at `vtkOSPRay` just the name for now without reading further.

Thank you very much !!!

---

<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: [May 2, 2023, 2:21pm UTC](https://discourse.vtk.org/t/ray-tracing-in-vtk-capabilities-in-surface-and-volume-rendering-own-implementation/11308/4 "2023-05-02T14:21:05Z")

</div>

Hi @DMelenberg, I suggest looking at how the current implementation of the volume mapper is written. The main code is in OpenGL shaders composed by a header file called "vtkVolumeShaderComposer`. The composer is used by `vtkOpenGLGPUVolumeRayCastMapper` to dynamically create the vertex and fragment shaders that cast rays through the volume. If there are certain improvements you’d like to make, then there is the ability to replace parts of the shader code with your own.

Understanding how the current volume mapping is done should hopefully help you figure out your next steps. As suggested, Intel OSPRay’s integration in VTK should also give you more ideas.
