How to do ray tracing in modern OpenGL?
Asked Answered
R

6

41

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.

Reeducate answered 8/9, 2010 at 11:28 Comment(0)
B
45

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.

Bestial answered 8/9, 2010 at 11:44 Comment(4)
Btw: google scholar is your friend on stuff like this. How about scholar.google.de/scholar?q=glsl+ray+tracingBestial
What do you mean "acceleration data structures"?Unpeople
In this context, an acceleration data structure accelerates a query as to whether the world is occupied by an object at a particular 3D-position. It's accelerated vs. O(n), iterating through the list of objects.Markettamarkey
You can use OpenGL compute shaders to write your ray tracers. It can be easier to get started since one would already be familiar with writing GLSL code. This can be quite helpful: ci.i.u-tokyo.ac.jp/~hachisuka/tdf2015.pdf Though I would recommend using SSBOs for storing your data instead of textures.Witmer
P
30

You may be confusing some things.

OpenGL is a rasterizer. Forcing it to do raytracing is possible, but difficult. This is why raytracing is not "ideal for real time graphics in a few years". In a few years, only hybrid systems will be viable.

So, you have three possibities.

  • Pure raytracing. Render only a fullscreen quad, and in your fragment shader, read your scene description packed in a buffer (like a texture), traverse the hierarchy, and compute ray-triangles intersections.
  • Hybrid raytracing. Rasterize your scene the normal way, and use raytracing in your shader on some parts of the scene that really requires it (refraction, ... but it can be simultated in rasterisation)
  • Pure rasterization. The fragment shader does its normal job.

What exactly do you want to achieve ? I can improve the answer depending on your needs.

Anyway, this SO question is highly related. Even if this particular implementation has a bug, it's definetely the way to go. Another possibility is openCL, but the concept is the same.

Presume answered 8/9, 2010 at 11:40 Comment(1)
seems Microsoft (for DirectX) and NVIDIA have just released real-time raytracing APIs, judging from latest Unreal Engine announcementsNuris
C
5

Opengl (glsl) can be used for ray (path) tracing. however there are few better options: Nvidia OptiX (Cuda toolkit -- cross platform), directx 12 (with Nvidia ray tracing extension DXR -- windows only), vulkan (nvidia ray tracing extension VKR -- cross platform, and widely used), metal (only works on MacOS), falcor (DXR, VKR, OptiX based framework), Intel Embree (CPU ray tracing only).

Crabbing answered 4/1, 2022 at 23:31 Comment(1)
I don't know why I receive -1, I clearly answered the question, since 2010 when the question asked, many things has been changed. OpenGL has not been updated much, but NVIDIA worked on vulkan, DX to support rtx cores.Crabbing
Y
4

As for 2019 ray tracing is an option for real time rendering but requires high end GPUs most users don't have. Some of these GPUs are designed specifically for ray tracing. OpenGL currently does not support hardware accelerated 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. Support from mobile may take a while.

Yenta answered 16/3, 2019 at 22:0 Comment(0)
L
1

I found some of the other answers to be verbose and wordy. For visual examples that YES, functional ray tracers absolutely CAN be built using the OpenGL API, I highly recommend checking out some of the projects people are making on https://www.shadertoy.com/ (Warning: lag)

Loony answered 1/5, 2021 at 16:49 Comment(0)
P
0

To answer the topic: OpenGL has no RTX extension, but Vulkan has, and interop is possible. Example here: https://github.com/nvpro-samples/gl_vk_raytrace_interop

As for the actual question: To light the triangles, there are tons of techniques, look up for "forward", "forward+" or "deferred" renderers. The technique to be used depends on you goal. The simplest and most good-looking these days, is image based lighting (IBL) with physically based shading (PBS). Basically, you use a cubemap and blur it more or less depending on the glossiness of the object. For a simple object viewer you don't need more.

Placido answered 31/3, 2022 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.