我正在实现一个基本的光线追踪器,所以我正在阅读理论和其他实现。这是我目前所指的代码
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);
}
}
我正在对我理解的内容发表评论,但我被困在 xx 和 yy 的计算上。
我知道通过简单的三角函数宽度 = 2 *(从相机到视图平面的垂直距离)* tan(fov / 2)。但我无法弄清楚 T xx 和 T yy 的表达式。
请有人帮忙澄清一下。
问候,莫伊拉