I am implementing a basic ray tracer so I am reading up on theory and other implementations. Here is the code that I am currently referring to
template<typename T>
void render(const std::vector<Sphere<T> *> &spheres)
{
int width = 800, height = 800;//define width and height of the screen
Vector3d<T> *image = new Vector3d<T>[width * height], *pixel = image;
T invWidth = 1 / T(width), invHeight = 1 / T(height);
T fov = 90, aspectratio = width / T(height);//defining field of view angle and aspect ratio
T fovDist = tan(M_PI * 0.5 * fov / T(180));//Calculates half screen width / perpendicular distance to the camer
a position
// For each ray crossing a pixel on the view screen.
for (int y = 0; y < height; ++y) //For each scan line
{
for (int x = 0; x < width; ++x, ++pixel) //for each pixel on a scan line
{
//Calculate the x position using centre of the pixel.
/*By simple trig (width/2)/perpendicular distance to camera = tan (fov/2)
=>width = distance * tan (fov/2)
*/
T xx = (2 * ((x + 0.5) * invWidth) - 1) * fovDist * aspectratio;
T yy = (1 - 2 * ((y + 0.5) * invHeight)) * fovDist;//Calculate the y position
Vector3d<T> raydir(xx, yy, -1);
raydir.normalize();
*pixel = trace(Vector3d<T>(0), raydir, spheres, 0);
}
}
I am putting in comments of what I understand but I am stuck on the calculation of xx and yy.
I understand that by simple trigonometry width = 2 * (perpendicular distance from camera to the view plane) * tan (fov / 2). But I am not able to figure out the expression for T xx and T yy.
Please can someone help clarify.
Regards, Moira