# comp.graphics.algorithms

## Subject: Understanding raytracing code

Hi,
And I'm having problem understanding this raytracing thing.
I have typed this from the sourcecode that was available and
I wonder if someone could help me explain how this code work?

This code will generate a "hole" that is mapped with a texture.
The resolution is 320x200. And I see that the code loops through
every pixel, and calculates some map that will be stored in
textcoord. But I don't understand how the hole is created. So
I would really appreciate if someone could explain this for me.
Thanks!

textcoord = new (unsigned char)[128000];
long offs = 0;
for (int j=-100; j<100; j++) {
for (int i=-160; i<160; i++) {
float dx = (float)i / 200;
float dy = (float)-j / 200;
float dz = 1;

// normalize them
float d = 20 / sqrt(dx*dx + dy*dy + 1);
dx *= d;
dy *= d;
dz *= d;
// start interpolation at origin
float x = 0;
float y = 0;
float z = 0;
// set original precision
d = 16;
// interpolate along ray
while (d>0) {
// continue until we hit a wall
while (((x-get_x_pos(z))*(x-get_x_pos(z))+(y-get_y_pos(z))*
(y-get_y_pos(z)) < get_radius(z)) && (z<1024)) {
x += dx;
y += dy;
z += dz;
}
// reduce precision and reverse direction
x -= dx; dx /= 2;
y -= dy; dy /= 2;
z -= dz; dz /= 2;
d -= 1;
}
// calculate the texture coordinates
x -= get_x_pos(z);
y -= get_y_pos(z);
float ang = atan2(y, x) * 256 / M_PI;
unsigned char u = (unsigned char)ang;
unsigned char v = (unsigned char)z;
// store texture coordinates
textcoord[offs] = u;
textcoord[offs+1] = v;
offs += 2;
}

{
return 128;
}

float get_y_pos(float f)
{
return - 16 * sin(f * M_PI / 256);
}

float get_x_pos(float f)
{
return - 16 * sin(f * M_PI / 256);
}