我正在尝试为 raymarching 编写一个 opencl 内核。一切正常,除了生成的图像中有明显的鱼眼失真,如本例所示:(这应该是一个立方体)
问题在于我为每条射线构造方向向量的方式。
目前,我将相机的方向作为俯仰和偏航(在我的代码中)给pitch
内核yaw
。
然后基于 fov ( fov
)、内核正在计算的像素坐标 (ix
和iy
) 以及整个帧的宽度和高度 ( width
, height
),我得到了光线方向的俯仰和偏航。
最后,我使用前面计算给出的俯仰和偏航构造一个单位向量。
(varfloat
表示浮点或双精度取决于内核是以双精度还是单浮点精度运行)
对于上图,fov
Pi/3 和width
和height
都是 500。
unsigned int ix = get_global_id(0);
unsigned int iy = get_global_id(1);
//PROBLEM LIES IN THESE 3 LINES:
varfloat y = yaw - fov/2.0 + fov*ix/width; //get yaw using horizontal fov
varfloat f = (fov * height) / width; //calculate vertical fov from horizontal fov scaled proportionally based on width and height
varfloat p = pitch - f/2.0 + f*iy/height; //get pitch using vertical fov
varfloat3 direction = {sin(y)*cos(p), cos(y)*cos(p), sin(p)}; //unit vector for ray direction
有人能告诉我应该如何计算方向矢量的俯仰和偏航以消除失真吗?