PDA

View Full Version : Ray Tracing



MrWendell1982
02-23-2003, 10:30 AM
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

Anders
02-23-2003, 11:10 AM
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.

rgpc
02-23-2003, 04:14 PM
1. From memory www.gametutorials.com (http://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.

Zeno
02-23-2003, 08:52 PM
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

rgpc
02-23-2003, 10:53 PM
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).

davepermen
02-24-2003, 02:10 AM
Originally posted by rgpc:
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).


actually, those are the days fragment programs start to get useful for such stuff http://www.opengl.org/discussion_boards/ubb/biggrin.gif not that he wants to do it with fragment programs..

rgpc
02-24-2003, 03:30 AM
Originally posted by davepermen:
actually, those are the days fragment programs start to get useful for such stuff http://www.opengl.org/discussion_boards/ubb/biggrin.gif 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. http://www.opengl.org/discussion_boards/ubb/tongue.gif

(And then wait a week for the results...)

davepermen
02-24-2003, 08:00 AM
Originally posted by rgpc:
Now there's a good idea. I'll just go write one on my geforce 3. http://www.opengl.org/discussion_boards/ubb/tongue.gif

(And then wait a week for the results...)

http://www.opengl.org/discussion_boards/ubb/biggrin.gif not even the r300 or nv30 chips are really useful for it.. once we have full branching in pixelshaders, nothings gonna stop us http://www.opengl.org/discussion_boards/ubb/biggrin.gif

Adrian
02-24-2003, 10:04 AM
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?

Zeno
02-24-2003, 12:14 PM
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

marcus256
02-25-2003, 12:22 AM
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.

Matt Halpin
02-25-2003, 07:29 AM
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. http://www.opengl.org/discussion_boards/ubb/smile.gif

It's completely useless as it's 1 pass per reflected sphere, but it's quite fun http://www.opengl.org/discussion_boards/ubb/smile.gif