Ray Tracing

Hi,

Im looking to implement a ray tracer using the opengl renderer. Whilst thinking this idea through i ran into a problem…

If i have a start point for the ray and i calculate its direction how can i check whether this ray collides with an object in my scene?

Thanks

I don’t know if this is the answer you’re looking for, but maybe it can help. when doing collision detection, you want to do it gradually i.e. first do a simple test like a sphere ray intersection test, to quickly discard some objects and then do more thorough test like ray-triangle on the ones who pass the first test.

So what you want to do is to surround all your objects with a sphere that is large enough to enclose the entire object. Then when you have the ray you test all the objects bounding spheres with the ray. The objects that do not pass this test, i.e. do not collide are discarded. The others are put into a list of potential collisions. These objects are then tested again. This time you do a triangle-ray collision test. First check if the ray collides with the triangles plane. If so, then check if the intersection point is within the triangles area. If this is also the case, then the ray and the object have collided. Then you have to figure out how far down the ray the colision point is. You store this distance for all collisions. When you have all these distances you can find the closest collision point. This is done simply by taking the collision point with the smallest distance to the ray origin. You could put more steps into the collision testing, by using a box after the sphere for example. This could speed up the tests in some cases.

Hope this helps you in some way. I didn’t put any math here because I can’t remember it all just now, but if you want I could look it up.

  1. From memory www.gametutorials.com has a tutorial on ray/poly collision/intersection.

  2. Gamasutra has lots of (pretty basic) articles on object collision.

  3. Google probably has a hundred billion links of which a few might be relevant.

  4. Select buffers will allow you to calculate collisions on individual objects but this is (a) a BASIC OpenGL question and (b) of no use in ray tracing.

  5. A pen and paper and knowledge of the equation of a plane, how to represent a sphere, the equation of a line etc. will do wonders for your education and self well being.

  6. This is an Advanced OpenGL forum and none of the above has any relevancy here.

Are you guys sure that he isn’t asking about actually using OpenGL for raytracing, not just the display of a raytraced image?

There was a paper on this at last year’s siggraph. I believe that the scene is stored in a 3D texturemap and rays are “cast” within the programmable pixel shader to determine intersections.

http://graphics.stanford.edu/papers/rtongfx/

– Zeno

Originally posted by Zeno:
Are you guys sure that he isn’t asking about actually using OpenGL for raytracing, not just the display of a raytraced image?

Irrespective of how he wants to implement it, calculation of ray/polygon/mesh intersection is not Opengl… (And judging by the question asked he is not asking how to implement it in OpenGL, he is asking how to calculate the intersection of his ray with elements in his scene).

Originally posted by rgpc:
[b] Irrespective of how he wants to implement it, calculation of ray/polygon/mesh intersection is not Opengl… (And judging by the question asked he is not asking how to implement it in OpenGL, he is asking how to calculate the intersection of his ray with elements in his scene).

[/b]

actually, those are the days fragment programs start to get useful for such stuff not that he wants to do it with fragment programs…

Originally posted by davepermen:
actually, those are the days fragment programs start to get useful for such stuff not that he wants to do it with fragment programs…

Now there’s a good idea. I’ll just go write one on my geforce 3.

(And then wait a week for the results…)

Originally posted by rgpc:
[b] Now there’s a good idea. I’ll just go write one on my geforce 3.

(And then wait a week for the results…)[/b]

not even the r300 or nv30 chips are really useful for it… once we have full branching in pixelshaders, nothings gonna stop us

So presumably the reason for using a fragment program is that it will be much faster. Any idea how much faster it would be? Is this likely to make ray traced computer games viable?

Originally posted by Adrian:
So presumably the reason for using a fragment program is that it will be much faster. Any idea how much faster it would be? Is this likely to make ray traced computer games viable?

I don’t think it would be viable for games yet, but it looks promising, given the rate of increase of speed of GPU’s relative to CPU’s. Take a look at this slide:

http://graphics.stanford.edu/papers/rtongfx/slides/slide29.html

I think once better branching, stacks, memory access etc gets into fragment shaders, so that it starts looking like a proper RISC CPU, things will get interesting.

The real advantage of GPUs in this context (besides massive parallellism and bandwidth) is that the low level operations are very useful for vector & matrix math (e.g. dot3, 1/sqrt, etc), so I think in the future the GPU may very well be used for (near) real time ray tracing.

I still think it will always be a trade-off between CPU/GPU though, since most scenes are probably much more complex than can be handled efficiently in a GPU. At least gross culling and data preparation will be handled by the CPU.

Even with todays/yesterdays hardware, you can do limited HW accelleration with OpenGL by doing first hit culling with OpenGL, eliminating the need for bounding boxes etc.

heh, I was doing a raytracing test today.

On a 9700, I’ve got it to do this:

For each pixel of each object, reflect the eye vector in the normal, then do a ray-sphere intersection test with that reflection vector and an arbitrary sphere. Then work out the sphere intersection point and normal, then do diffuse and specular lighting on the result. Basically I’ve got a raytraced lit sphere reflection in a pixel shader.

It’s completely useless as it’s 1 pass per reflected sphere, but it’s quite fun