Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do ray tracing in modern OpenGL?

So I'm at a point that I should begin lighting my flatly colored models. The test application is a test case for the implementation of only latest methods so I realized that ideally it should be implementing ray tracing (since theoretically, it might be ideal for real time graphics in a few years).

But where do I start?

Assume I have never done lighting in old OpenGL, so I would be going directly to non-deprecated methods.

The application has currently properly set up vertex buffer objects, vertex, normal and color input and it correctly draws and transforms models in space, in a flat color.

Is there a source of information that would take one from flat colored vertices to all that is needed for a proper end result via GLSL? Ideally with any other additional lighting methods that might be required to complement it.

like image 889
j riv Avatar asked Sep 08 '10 11:09

j riv


People also ask

Can you do ray tracing in OpenGL?

OpenGL currently does not support hardware boosted Ray tracing. DirectX 12 on windows does have support for it. It is recommended to wait a few more years before creating a Ray tracing only renderer although it is possible using DirectX 12 with current desktop and laptop hardware.

Does Vulkan support ray tracing?

Ray-tracing using Vulkan can either be achieved through GPU compute or using dedicated ray-tracing cores. Drivers with support for Vulkan ray-tracing are already available for both Nvidia and AMD GPUs, starting with the AMD Radeon Adrenalin 20.11.

Can ray tracing be used in real-time graphics?

Ray tracing is a method of graphics rendering that simulates the physical behavior of light. Thought to be decades away from reality, NVIDIA has made real-time ray tracing possible with NVIDIA RTX™ the first-ever real-time ray-tracing GPU—and has continued to pioneer the technology since.


1 Answers

I would not advise to try actual ray tracing in OpenGL because you need a lot hacks and tricks for that and, if you ask me, there is not a point in doing this anymore at all. If you want to do ray tracing on GPU, you should go with any GPGPU language, such as CUDA or OpenCL because it makes things a lot easier (but still, far from trivial).

To illustrate the problem a bit further: For raytracing, you need to trace the secondary rays and test for intersection with the geometry. Therefore, you need access to the geometry in some clever way inside your shader, however inside a fragment shader, you cannot access the geometry, if you do not store it "coded" into some texture. The vertex shader also does not provide you with this geometry information natively, and geometry shaders only know the neighbors so here the trouble already starts. Next, you need acceleration data-structures to get any reasonable frame-rates. However, traversing e.g. a Kd-Tree inside a shader is quite difficult and if I recall correctly, there are several papers solely on this problem. If you really want to go this route, though, there are a lot papers on this topic, it should not be too hard to find them.

A ray tracer requires extremely well designed access patterns and caching to reach a good performance. However, you have only little control over these inside GLSL and optimizing the performance can get really tough.

Another point to note is that, at least to my knowledge, real time ray tracing on GPUs is mostly limited to static scenes because e.g. kd-trees only work (well) for static scenes. If you want to have dynamic scenes, you need other data-structures (e.g. BVHs, iirc?) but you constantly need to maintain those. If I haven't missed anything, there is still a lot of research currently going on just on this issue.

like image 138
zerm Avatar answered Oct 05 '22 22:10

zerm