I am writing a simple raytracer/raycaster in c#. I have expirence in the past with Vectors, so I wrote a class called Vector3D, as you can see in the code below. I also wrote a class to handle the Rays. For now, I am concerned about ensuring that the rays, are orginating from the camera, and being casted to all of the pixels on the screen, then to objects in front of the camera in the scene. I have expiremented with writing text to the Output (Debug.WriteLine), though it hard to see if it is really working. Would the following code be appropriate, or would you recommend another method or a site to refer/guide me?
for (int x = 0; x < sizeofoutput.Width; x++)
{
for (int y = 0; y < sizeofoutput.Height; y++)
{
Vector3D lookat = new Vector3D(sizeofoutput.Width / 2, sizeofoutput.Height / 2, 0);
Vector3D lookatrev = new Vector3D(lookat.X * -1, lookat.Y * -1, 0);
Vector3D tmp2 = lookatrev + new Vector3D(x, y, 0);
Vector3D campos = new Vector3D(0, 2, -6); // camera position.
Vector3D raydir = tmp2 - campos; // ray goes into a pixel.
Vector3D rayorg = campos; // ray starts at camera.
Ray ray = new Ray(rayorg); // create the ray from the data provided.
ray.Direction = raydir;
for (int c = 0; c < sceneobj.Length; c++)
{
// find object and render!
}
}
}
Your camera seems to be firing rays down the z-axis, if this is what you were after it looks good. Note that your ray's direction is not normalized, which may or may not be a problem later on. You have to keep it in mind and act accordingly. Anyway, I think for the very basics Phantom's tutorial is what got me started (and addicted!) with ray tracing, and I think it might be a good read for you.