亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

是否可以僅使用:aspectRatio、y_scale、x_scal

是否可以僅使用:aspectRatio、y_scale、x_scal

九州編程 2022-06-23 19:25:23
我正在創建一個游戲,我用 aspectRatio、y_scale、x_scale 和 frustum_length 計算視圖矩陣,所以我想知道是否有一種方法可以只用這些變量來計算視錐。變量:float aspectRatio = (float)Display.getWidth() / (float)Display.getHeight();float y_scale = (float) ((1f / Math.tan(Math.toRadians(FOV / 2f))) * aspectRatio);float x_scale = y_scale / aspectRatio;float frustum_length = FAR_PLANE - NEAR_PLANE;
查看完整描述

1 回答

?
犯罪嫌疑人X

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

);


查看完整回答
反對 回復 2022-06-23
  • 1 回答
  • 0 關注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號