2012-11-25 23 views
1

我正在实施基本的射线示踪剂,所以我正在阅读理论和其他实现。这里是我目前提到的代码射线示踪剂中射线产生的计算

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的表达式。

请有人帮忙澄清。

问候, 莫伊拉

回答

1

好,所以如果我理解这一点,你有你的框架平行于XY平面,并在原点坐在Z = -1,用你的相机/眼。现在您正在尝试查找每个像素中心的X和Y位置。是对的吗?因此x + 0.5偏移到当前像素的中心,这很好。除以像素总数,即可得出整个帧的走向百分比:好。乘以2,然后减1,然后在-1到1的范围内,这相当于从中心到左边的100%,以及从中心到右边的100% -versa)。现在乘以宽度的一半(fovDist),因此它将您放置在左半边宽度和右半边宽度之间。所以这很好。最后,你乘以长宽比,因为fovDist真的是高度的一半,而不是necc。宽度的一半。因此,如果您的宽高比为2:1,则它应该比宽高比较宽,因此您想要将像素水平分布,因此您在这方面也都很出色。

你已经建立了相同的方式,除了倒置,因为扫描线通常以另一种方式。所以那看起来也很好。

所以它工作,你只是想了解它?或者它工作不正常?如果它不能正常工作,你会看到什么样的行为?

如果工作不正常,跳出来的唯一的事情就是所有不同的数字类型。例如,您的invWidthinvHeight变量都将整数1除以某个值。我不记得C++如何处理这个问题,但是可能你在这里被整数除法截断为0。同样,你在xx和yy表达式中有很多整数。不知道编译器是否可以正常工作,但如果工作不正常,我会从那里开始。

+0

非常感谢您的答复。该计划确实有效。我想了解这个实现,因为我需要自己做一个。 – Moira