【问题标题】:calculation for ray generation in ray tracer光线追踪器中的光线生成计算
【发布时间】:2012-11-13 07:18:13
【问题描述】:

我正在实现一个基本的光线追踪器,所以我正在阅读理论和其他实现。这是我目前所指的代码

    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);
        }
    }

我正在输入我理解的 cmets,但我卡在 xx 和 yy 的计算上。

我知道通过简单的三角函数宽度 = 2 *(从相机到视图平面的垂直距离)* tan (fov / 2)。但我无法弄清楚 T xx 和 T yy 的表达式。

请有人帮忙澄清一下。

问候, 莫伊拉

【问题讨论】:

    标签: raytracing


    【解决方案1】:

    好的,如果我理解这一点,您的框架平行于 XY 平面,并且位于 Z=-1,您的相机/眼睛位于原点。现在您正试图找到每个像素中心的 X 和 Y 位置。对吗?

    所以x + 0.5 将您偏移到当前像素的中心,这很好。除以总像素数得出整个帧的百分比:好。乘以 2,然后减去 1,您在 -1 到 1 的范围内,分别对应于从中心到左侧的 100% 和从中心到右侧的 100%(或反之亦然) - 反之亦然)。现在乘以宽度的一半 (fovDist),因此它会将您置于左侧二分之一宽度和右侧二分之一宽度之间的某个位置。所以这一切都很好。最后,你乘以纵横比,因为 fovDist 实际上是高度的一半,而不是 necc。宽度的一半。所以如果你的纵横比是 2:1,它应该比它的高更宽,所以你想把你的像素水平分散,这样你也很好。

    除了倒置之外,您的 yy 设置方式相同,因为扫描线通常采用相反的方式。所以看起来也不错。

    那么它是否有效,而您只是想理解它?或者它不能正常工作?如果它无法正常工作,您会看到什么样的行为?

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

    【讨论】:

    • 非常感谢您的回复。该程序确实有效。我试图理解这个实现,因为我需要自己做一个。
    猜你喜欢
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    相关资源
    最近更新 更多