我正在实施基本的射线示踪剂,所以我正在阅读理论和其他实现。这里是我目前提到的代码射线示踪剂中射线产生的计算
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的表达式。
请有人帮忙澄清。
问候, 莫伊拉
非常感谢您的答复。该计划确实有效。我想了解这个实现,因为我需要自己做一个。 – Moira