我試圖將一些 c 代碼移植到 C# 中,除了透視圖外,一切正常。
在 c 中,代碼看起來像這樣,它可以正常作業而不會失真。
DirectX::XMMatrixTranspose(
DirectX::XMMatrixRotationZ(100) *
DirectX::XMMatrixRotationX(200) *
DirectX::XMMatrixTranslation(0,0,4) *
DirectX::XMMatrixPerspectiveLH(DirectX::XMConvertToRadians(180),pAdapter->aspectRatio,0.5f,10)
和這樣的 C# 代碼
Matrix4x4.Transpose(
Matrix4x4.CreateRotationZ(100) *
Matrix4x4.CreateRotationX(200) *
Matrix4x4.CreateTranslation(0, 0, 4) *
Matrix4x4.CreatePerspective(1.39626f, 1.7777777777777777777777777777778f, 0.5f,10)
);
我會假設這兩者都會做完全相同的事情,但是當我洗掉它呈現的透視圖時,C# 代碼根本不會出現,但一切都按預期進行了拉伸。
我試圖重新制作導致這種情況的 dxmath 的來源,它現在渲染得更遠但它仍然被拉伸。
Matrix4x4 perspective = new Matrix4x4();
float ViewWidth = 1.39626f;
float ViewHeight = 1.7777777777777777777777777777778f;
float NearZ = 0.5f;
float FarZ = 10.0f;
float TwoNearZ = NearZ NearZ;
float fRange = FarZ / (NearZ - FarZ);
perspective.M11 = TwoNearZ / ViewWidth;
perspective.M22 = TwoNearZ / ViewHeight;
perspective.M33 = fRange;
perspective.M43 = -fRange * NearZ;
data.matrix = perspective * data.matrix;
我不確定問題出在哪里,我在另一篇文章中讀到 matrix4x4 是右手的,所以我嘗試了同樣的方法,但它根本沒有渲染。任何幫助表示贊賞。
uj5u.com熱心網友回復:
您使用的透視矩陣看起來很奇怪。我建議您使用 OpenGL 檔案中更經典的實作。另外,請注意,您沒有M44將透視矩陣的值顯式設定為,0.0f而該值設定1.0f為默認單位矩陣。這會導致錯誤的透視矩陣。
float frustumDepth = farDist - nearDist;
float oneOverDepth = 1 / frustumDepth;
perspective.M22 = (float)(1.0f / Math.Tan(0.5f * fovRadians));
perspective.M11 = 1.0f * perspective.M22 / aspectRatio;
perspective.M33 = farDist * oneOverDepth;
perspective.M34 = 1.0f;
perspective.M43 = (farDist * nearDist) * oneOverDepth;
perspective.M44 = 0.0f; //< replace 1.0f to 0.0f
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/484070.html
上一篇:如何從列舉類中獲取隨機值?
