# comp.graphics.algorithms

## Subject: Re: Understanding raytracing code

On Wed, 17 May 2006 22:10:48 GMT, john wrote:

>When you say that the ray originates from somewhere behind the image,
>is that the same position as that of my eyes?

Well yeah sorta. Usually you define the position of "the eye"
(camera), but in this code, it is a bit convoluted.

Imagine that your camera is positioned at Vc = (0, 0, Zc) (c for
camera), and the image plane is in the x, y plane with z = 0. That
means the position of the pixels in the image is at Vp = (Xp, Yp, 0)
(p for pixel).

The view vector from the camera to a given pixel is then

Vview = Vp - Vc

Which gives us

(Xp - 0, Yp - 0, 0 - Zc)

Now the code initializes the view vector the following way:

float dx = (float)i / 200;
float dy = (float)-j / 200;
float dz = 1;

This means that 0 - Zc = 1, hence Zc = -1.

The 200 used for dividing the i and j variables are just to bring the
x and y values into proportion with the value of Zc. Since the view
vector is normalized, only the relative magnitudes matter. You could
have used i and j as is, and used Zc = 200.

This approach is called "ray marching" I belive. Another (and probably
more efficient, in this case) way would be to solve the equations for
a ray-cylinder intersection directly. Ray marching is usually used for
things such as ray tracing volumetric effects, afaik.

- Asbjørn