leg*_*s2k 1 c++ floating-point graphics raytracing
在我正在进行的光线追踪分配中,我将计算从相机拍摄的光线的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));
Run Code Online (Sandbox Code Playgroud)
给了我一个不正确的拉伸图像,我花了好几个小时才把它弄好,这是在发现简化它之后同样的方程式之后.
最终重新排列的等式是:
float OffsetX = tan(FovY / 2.0f) * (2.0f / height) * (col - (width / 2.0f));
Run Code Online (Sandbox Code Playgroud)
我试过调试,两个方程的结果确实不同.
会有某种圆整错误吗?有人可以向我解释这个怪癖吗?
#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;
}
Run Code Online (Sandbox Code Playgroud)
我猜:宽度和高度是整数.
当你这样做时:
(width / height)
Run Code Online (Sandbox Code Playgroud)
你得到整数除法,它会丢弃结果的小数部分.如果您改为:
((double)width / height)
Run Code Online (Sandbox Code Playgroud)
然后这两个结果几乎相同.
另外,您可以进一步简化表达式:
tan(FovY / 2.0f) * (2.0f*col - width) / height
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
230 次 |
| 最近记录: |