目標是把螢幕像素中心轉成相機空間中的射線方向。假設畫面為 width×heightwidth \times height、寬高比 A=width/heightA=width/height,投影平面位於 z=1z=-1

  1. 加上 0.5 取像素中心,轉換到 [0,1][0,1]Xndc=(Xscreen+0.5)/widthX_{ndc}=(X_{screen}+0.5)/widthYndc=(Yscreen+0.5)/heightY_{ndc}=(Y_{screen}+0.5)/height
  2. 轉換到 [1,1][-1,1]X=2Xndc1X=2X_{ndc}-1Y=2Yndc1Y=2Y_{ndc}-1
  3. 用寬高比修正 XX
  4. 乘上 tan(FOV/2)\tan(FOV/2),得到投影平面上的位置

最後射線方向為 (Xfinal,Yfinal,1)(X_{final},Y_{final},-1),正規化後即可使用。若要改變觀察方向,再把方向乘上相機旋轉矩陣;方向向量不要套用平移。

目标是把屏幕像素中心转换为相机空间中的射线方向。设画面为 width×heightwidth \times height、宽高比 A=width/heightA=width/height,投影平面位于 z=1z=-1

  1. 加上 0.5 取得像素中心,并转换到 [0,1][0,1]
  2. 转换到 [1,1][-1,1]
  3. 使用宽高比修正 XX
  4. 乘以 tan(FOV/2)\tan(FOV/2),得到投影平面上的位置

最终射线方向为 (Xfinal,Yfinal,1)(X_{final},Y_{final},-1),归一化后即可使用。改变观察方向时,把方向乘以相机旋转矩阵;方向向量不应应用平移。

The goal is to convert a screen-pixel center into a ray direction in camera space. Let the image size be width×heightwidth \times height, the aspect ratio be A=width/heightA=width/height, and the projection plane be at z=1z=-1.

  1. Add 0.5 to address the pixel center and map it to [0,1][0,1]
  2. Remap the result to [1,1][-1,1]
  3. Correct XX using the aspect ratio
  4. Multiply by tan(FOV/2)\tan(FOV/2) to recover a point on the projection plane

The ray direction is (Xfinal,Yfinal,1)(X_{final},Y_{final},-1). Normalize it before use. To change the view direction, multiply the direction by the camera rotation matrix; do not apply translation to a direction vector.

画面上のピクセル中心をカメラ空間のレイ方向へ変換します。画面サイズを width×heightwidth \times height、アスペクト比を A=width/heightA=width/height、投影面を z=1z=-1 とします。

  1. 0.5 を加えてピクセル中心を取り、[0,1][0,1] へ変換します
  2. 結果を [1,1][-1,1] へ変換します
  3. アスペクト比で XX を補正します
  4. tan(FOV/2)\tan(FOV/2) を掛け、投影面上の位置を求めます

レイ方向は (Xfinal,Yfinal,1)(X_{final},Y_{final},-1) です。使用前に正規化します。視線方向を変える場合はカメラの回転行列を掛けますが、方向ベクトルへ平行移動を適用してはいけません。

Xfinal=(2Xscreen+0.5width1)Atan(fovx2)X_{final}=\left(2\frac{X_{screen}+0.5}{width}-1\right)A\tan\left(\frac{fov_x}{2}\right)

Yfinal=(2Yscreen+0.5height1)tan(fovy2)Y_{final}=\left(2\frac{Y_{screen}+0.5}{height}-1\right)\tan\left(\frac{fov_y}{2}\right)

1
2
Vector3f direction = Vector3f(x, y, -1).normalized();
Vector3f rotatedDirection = cameraRotation * direction;

參考資料

Xfinal=(2Xscreen+0.5width1)Atan(fovx2)X_{final}=\left(2\frac{X_{screen}+0.5}{width}-1\right)A\tan\left(\frac{fov_x}{2}\right)

Yfinal=(2Yscreen+0.5height1)tan(fovy2)Y_{final}=\left(2\frac{Y_{screen}+0.5}{height}-1\right)\tan\left(\frac{fov_y}{2}\right)

1
2
Vector3f direction = Vector3f(x, y, -1).normalized();
Vector3f rotatedDirection = cameraRotation * direction;

参考资料

Xfinal=(2Xscreen+0.5width1)Atan(fovx2)X_{final}=\left(2\frac{X_{screen}+0.5}{width}-1\right)A\tan\left(\frac{fov_x}{2}\right)

Yfinal=(2Yscreen+0.5height1)tan(fovy2)Y_{final}=\left(2\frac{Y_{screen}+0.5}{height}-1\right)\tan\left(\frac{fov_y}{2}\right)

1
2
Vector3f direction = Vector3f(x, y, -1).normalized();
Vector3f rotatedDirection = cameraRotation * direction;

References

Xfinal=(2Xscreen+0.5width1)Atan(fovx2)X_{final}=\left(2\frac{X_{screen}+0.5}{width}-1\right)A\tan\left(\frac{fov_x}{2}\right)

Yfinal=(2Yscreen+0.5height1)tan(fovy2)Y_{final}=\left(2\frac{Y_{screen}+0.5}{height}-1\right)\tan\left(\frac{fov_y}{2}\right)

1
2
Vector3f direction = Vector3f(x, y, -1).normalized();
Vector3f rotatedDirection = cameraRotation * direction;

参考資料