1 回答

TA貢獻2080條經驗 獲得超4個贊
當透視投影對稱時,給定(y軸)視場角FOV、縱橫比aspectRatio以及到近平面(NEAR_PLANE)和遠平面(FAR_PLANE)的距離,然后視錐體的8個角點在視圖中空間可以計算:
float aspectRatio = (float)Display.getWidth() / (float)Display.getHeight();
float y_scale = 1.0f / (float)Math.tan( Math.toRadians(FOV / 2.0) );
float x_scale = y_scale * aspectRatio;
float near_x = NEAR_PLANE * x_scale;
float near_y = NEAR_PLANE * y_scale;
float far_x = FAR_PLANE * x_scale;
float far_y = FAR_PLANE * y_scale;
Vector3f left_bottom_near = new Vector3f(-near_x, -near_y, FAR_PLANE);
Vector3f right_bottom_near = new Vector3f( near_x, -near_y, FAR_PLANE);
Vector3f left_top_near = new Vector3f(-near_x, near_y, FAR_PLANE);
Vector3f right_top_near = new Vector3f( near_x, near_y, FAR_PLANE);
Vector3f left_bottom_far = new Vector3f(-far_x, -far_y, FAR_PLANE);
Vector3f right_bottom_far = new Vector3f( far_x, -far_y, FAR_PLANE);
Vector3f left_top_far = new Vector3f(-far_x, far_y, FAR_PLANE);
Vector3f right_top_far = new Vector3f( far_x, far_y, FAR_PLANE);
如果您想知道世界空間中視錐體的 8 個角點,則必須將這些點從視圖空間轉換到世界空間。
從世界空間轉換到視圖空間的矩陣,就是“視圖”矩陣??梢詮囊晥D空間轉換到世界空間的矩陣是逆視圖矩陣(invert())。
逆視圖矩陣是由視圖位置、視圖方向和視圖的向上向量定義的矩陣。Matrix4f可以設置如下:
Vector3f eyePos; // position of the view (eye position)
Vector3f targetPos; // the point which is looked at
Vector3f upVec; // up vector of the view
Vector3f zAxis = Vector3f.sub(eyePos, targetPos, null);
zAxis.normalise();
Vector3f xAxis = Vector3f.cross(upVec, zAxis, null);
xAxis.normalise();
Vector3f yAxis = Vector3f.cross(zAxis, upVec, null);
Matrix4f inverseView = new Matrix4f();
inverseView.m00 = xAxis.x; inverseView.m01 = xAxis.y; inverseView.m02 = xAxis.z; inverseView.m03 = 0.0f;
inverseView.m10 = yAxis.x; inverseView.m11 = yAxis.y; inverseView.m12 = yAxis.z; inverseView.m13 = 0.0f;
inverseView.m20 = zAxis.x; inverseView.m21 = zAxis.y; inverseView.m22 = zAxis.z; inverseView.m23 = 0.0f;
inverseView.m30 = eyePos.x; inverseView.m31 = eyePos.y; inverseView.m32 = eyePos.z; inverseView.m33 = 1.0f;
注意,視圖空間的坐標系是右手系,其中X軸指向左側,Y軸指向上方,然后Z軸指向視圖外(注意在右手系中Z 軸是 X 軸和 Y 軸的叉積)。
最后角點可以通過矩陣變換:
例如
Vector4f left_bottom_near_v4 = new Vector4f(
left_bottom_near.x, left_bottom_near.y, left_bottom_near.z, 1.0f
);
Vector4f left_bottom_near_world_v4 = new Vector4f();
Matrix4f.transform(Matrix4f left, left_bottom_near_v4, left_bottom_near_world_v4);
Vector3f left_bottom_near_world = new Vector3f(
left_bottom_near_world_v4 .x, left_bottom_near_world_v4 .y, left_bottom_near_world_v4 .z
);
添加回答
舉報