【发布时间】:2016-06-08 11:59:12
【问题描述】:
在我正在做的光线追踪任务中,我必须计算从相机拍摄的光线的 X 偏移量;偏移量计算是这样的
FovY 作为输入给出;我记得在读取变量时将其转换为弧度。
OffsetX = tan (FovX / 2) * ((col - (width / 2)) / (width / 2))
FovX = tan(FovY / 2) * aspect = tan(FovY / 2) * (width / height)
代入原方程并编写代码:
float OffsetX = tan(FovY / 2.0f) * (width / height) * ((col - (width / 2.0f)) / (width / 2.0f));
给了我一个不正确的拉伸图像,我花了几个小时才把它弄好,这是在发现相同的方程在简化后也可以工作之后。
最终重排方程为:
float OffsetX = tan(FovY / 2.0f) * (2.0f / height) * (col - (width / 2.0f));
我尝试调试,两个方程的结果确实不同。
会不会有某种四舍五入的错误?有人可以向我解释一下这个怪癖吗?
#include <cmath>
#include <iostream>
#include <cstdint>
using namespace std;
int main()
{
const float PI = 3.1415f;
const uint32_t width = 160, height = 120;
const auto fovy = (30.0f * (PI / 180.0f));
size_t j = 0;
auto alpha = (tan(fovy / 2.0f) * (width / height)) * (((j + 0.5f) - (width / 2.0f)) / (width / 2.0f));
cout << alpha << endl;
alpha = tan(fovy / 2.0f) * (2.0f / height) * ((j + 0.5f) - (width / 2.0f));
cout << alpha << endl;
}
【问题讨论】:
-
您能否提供一个完整的可运行示例,显示计算结果的两种方式,并显示两种方法不同的一些输入。它会从你的问题中消除很多歧义。
-
除非你可以展示两段不同的代码来重现与你有关的任何行为,否则很难提供帮助
-
当我发现需要它时,我花了几分钟用代码更新它;其中有人对这个问题投了反对票:(
标签: c++ graphics floating-point raytracing