glm matrix conversion for DirectX

Posted by niktehpui on Stack Overflow See other posts from Stack Overflow or by niktehpui
Published on 2012-12-10T10:43:46Z Indexed on 2012/12/10 11:06 UTC
Read the original article Hit count: 1549

Filed under:
|
|
|
|

For on of the coursework specification I need to work with DirectX, so I tried to implement a DirectX Renderer in my small cross-platform framework (to have it optionally available for Windows). Since I want to stick to my dependencies I want use glm for vector/matrix/quaternions math.

The vectors seem to be fully compatible with DirectX, but the glm::mat4 is not working properly in DirectX Effects Framework. I assumed the reason is that DirectX uses row majors layouts and OpenGL column majors (although if I remember right internally in HLSL DX uses column major as well), so I transposed the matrix, but I still get no proper results compared to using XNA-Math.

XNA-Version of the code (works):

XMMATRIX world = XMMatrixIdentity();
XMMATRIX view  = XMMatrixLookAtLH(XMVectorSet(5.0, 5.0, 5.0, 1.0f),  XMVectorZero(), XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f));
XMMATRIX proj  = XMMatrixPerspectiveFovLH(0.25f*3.14f, 1.25f, 1.0f, 1000.0f);
XMMATRIX worldViewProj = world*view*proj;
m_fxWorldViewProj->SetMatrix(reinterpret_cast<float*>(&worldViewProj));

This works flawlessly and displays the expected colored cube.

GLM-Version (does not work):

glm::mat4 world(1.0f);
glm::mat4 view = glm::lookAt(glm::vec3(5.0f, 5.0f, 5.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 proj = glm::perspective(0.25f*3.14f, 1.25f, 1.0f, 1000.0f);
glm::mat4 worldViewProj = glm::transpose(world*view*proj);
m_fxWorldViewProj->SetMatrix(glm::value_ptr(worldViewProj));

Displays nothing, screen stays black.

I really would like to stick to glm on all platforms.

© Stack Overflow or respective owner

Related posts about c++

Related posts about matrix