Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I read about how to get the direction vector in raycasting, for example on this site:

They first render the mesh with front face culling and then with back face culling. And then subtract the backface from the front to get at a direction vector for each pixel.

But is this not to much work to get the direction vector, is it not more simpler and faster to just take the vertex position(in world coordinates) and subtract the camera position in the fragment shader to get the direction vector? This should give the exact same answer but we skip the backface and frontface rendering.

share|improve this question
up vote 4 down vote accepted

The issue here is that there is no "vertex position" to compare against the camera position; the article you mention is talking about a raytracing implementation which doesn't feed model vertices and triangles to a shader in the usual way; instead, it would feed the GPU just one big 2D quad to cover the whole screen, and the rendering of model data would then be performed within the pixel shader applied to that single quad. (In this case, the model data is being provided as a large translucent 3D texture for the ray-tracing to sample through)

The approach described basically seems to be a clever method of generating the direction for a ray through every pixel of the screen, which ordinarily requires some trigonometry per pixel to generate. By using this approach, it seems to be possible to get those same rays using some pre-calculated renders and a simple vector subtraction per pixel. Of course, I haven't actually tried this approach to verify that it works, but the approach seems plausible to me.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.