Hi guys, I’m trying to implement a backwards ray trace and I’m a little lost. I know that I have to send a ray through every pixel and see if it collides with something. I have written a class for a ray that contains an origin and a direction, but I’m not sure how to apply it to go through every pixel, see if it collides with anything, get its color, store it and then display it. if anyone has any input, please let me know.

Start from the point of view. Then trace rays from this point of view which go threw any pixel of your screen then continue these rays to see which object it interacts.

Then regarding the nature of these objects, you’ll need to refract, reflect the ray to know the color behind (in case of transparency), and/or if the object is lit or in shadow.

Google for more details about ray tracers.

In fact Ray Tracing has nothing to do with OpenGL. One might use OpenGL to show the result generated by a Ray Tracer in Real Time or something like this.

Ray Tracing is commonly used as a offscreen rendering due to the quality of final image and time spend to generate a single frame.

When I started in Ray Tracing world (and this was last year) I use OpenGL to actually view the rays across the scene. Ofcourse I was dealing with a small amount of rays just for learning process.

I think the most important concept regarding ray tracing is to understand its basic concepts like, physics of light, what is a ray and what kind of rays a ray tracer must deal with (primary, shadow, reflective, refractive) and the last, but most important, how do compute collisions between rays and objects.

Collision detection is a key concept behind ray tracing. One need to implement collion between ray and objects as fast as possible. Many data structures are used together with ray tracing in order to minimize such time (i.e. BVH, KD-Trees, OcTrees), but this is an advanced topic and you should start with the basic ones.

As basics, don’t forget to learn about 3D vectors, matrix multiplications, homogenous coordinates, and projections.

Thanks for the help guys. I understand the process of shooting a ray through each pixel on the screen and then seeing if it collides with anything, but what I am confused is the actual syntax and code for doing this. I know that to shoot a ray through a pixel, you need to take into account the aspect ratio and the field of view, but I don’t how to apply those to run through every point and then check if it collides with anything.

Did you learn about the concepts I mentioned above ?

```
for (y=0;y<height;h++) {
for (x=0;x<width;x++) {
color[x,y]=shoot(unprojectPixelToWorldVector(x,y));
}
}
```

Hi ZbuffeR,

Yes, I am familiar with vectors, matrices, and coordinates, but I am less familiar with projections.

I think I figured out how to send a ray through each pixel, but, I am still a little confused on how to do the intersect calculation. I know that I have to check if the ray is contained in a triangle, vertex, or sphere, but I don’t know how.

This is an OpenGL forum, not a ray tracing forum. And OpenGL is a rasterizer, not a ray tracer. Google is probably more likely to find you answers. Just search for things like “ray triangle intersection.”

Or just download Yafray and look through its source code.

These forums (even if biased toward realtime raytracing) seem more suited for the topic :

http://ompf.org/

Thanks for all your help guys, I’ll check out those other resources.

I’ve written one (I’m sure many here have). Lots of fun!

Best advice in one sentence to give you is buy Peter Shirley’s Realistic Ray Tracing. The intro material walks you though this step-by-step, including what classes to provide and implementation tips. I personally like the 1st edition better than the 2nd edition for this intro material. Excellent jumpstart material and a great book! Highly recommended.

I still use the classes and techniques described in there in my real-time graphics exploits to keep 3D transform math simple, readable, and easily understandable. Oftentimes 3D developers’ transform math ends up looking like obfuscated Perl mess – go back a week later and, …what the heck! What’s all this mess do? :sorrow:

Then once you get your “Ray Tracing legs” and want to get into more complex stuff, go for this: Physically Based Rendering, 2nd edition. You can download the ray tracer described in the book for free on their web site along with sample scenes and render them on your PC: http://www.pbrt.org . Version 2 supports multicore so if you have a hyperthreaded quad-core for instance it’ll auto-launch 8 tracing threads and trace rays like crazy.