0
\$\begingroup\$

I have an issue with the transform matrix of my cube. When I change its position (world matrix) and rotate the camera, it is getting deformed.

Cube deformation bug

Here's my shader update code:

void IShader::UpdateShader()
{
    XMMATRIX view= XMLoadFloat4x4(&m_viewMtx);    //The camera's view matrix (using XMMatrixLookAt())
    XMMATRIX persp = XMLoadFloat4x4(&m_perspMtx);    //The camera's perspective matrix
    XMMATRIX world = XMLoadFloat4x4(geo->GetWorldMatrix());    //Here, get the cube's world position (it is updated using keyboard input in another class)
    // GetWorldMatrix() returns m_worldMtx

    XMMATRIX m = view * persp * world;

    m = XMMatrixTranspose(m);

    VERTEX_BASIC_CONSTANT_t k;
    k.mWorldViewPersp = m;    //The vertex shader only contains one float4x4 in the cbuffer...

    D3D11_MAPPED_SUBRESOURCE map;
    memset(&map, 0, sizeof(map));

    ID3D11Buffer* context = sh->GetVSCBuffer();
    m_pD3DCtx->Map(
        context,
        0,
        D3D11_MAP_WRITE_DISCARD,
        0,
        &map
    );

    memcpy(map.pData, &k, sizeof(k));

    m_pD3DCtx->Unmap(
        context,
        0
    );
}

Here is how I update the cube's position:

void Geo::UpdatePos(VEC3 offset)
{
    XMMATRIX t = XMMatrixTranslation(offset.x, offset.y, offset.z);

    XMStoreFloat4x4(&m_worldMtx, t);
}

This deformation issue only affects the cube, not the floor.

\$\endgroup\$
0
\$\begingroup\$

Tldr: debug if your cube is where you expect it to be, then check your mvp multiplication order. And make sure to first rotate and then translate the viewmatrix as it seems to be an orbiting rotation around identity.

General guide on debugging transformations and colours:

  1. Start local Verify your cubes expected world only transformation by translation transforming it from identity to a known location like 2,3,3. If the cubes transformed matrix matches your expectation you can rule that out as a problem.

  2. Know your expected SRT order If you plan to use more than translations the order of operations starts to matter because only translation is a commutative operation. Scale first, then rotate, and last translate.

  3. Know your matrix layout aka handedness Is it row major or column major order matrices that you work with? In opengl it's column major PVM with right handedness, positive z towards screen. In DirectX it's row major MVP with left handedness, positive z further away from screen.

  4. Sketch an easy scene on paper that consists of 3 cubes Each cube has a unique colour like r,g and b. Place every cube from left to right but the middle one a bit further along z. Is the middle cube further or nearer to your screen? Do the colours match the order of the cubes? Is it rgba, argb or bgra colours that you're dealing with?

\$\endgroup\$
  • \$\begingroup\$ Thank you @D3d_dev, the problem was the following: The multiplication order of the matrices was incorrect. The correct order is: world * view * persp. \$\endgroup\$ – cattapaw Jul 17 '19 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.