world matrix problem

Started by
6 comments, last by gnmgrl 11 years, 9 months ago
Hello guys, im getting a pretty wierd problem.
When I try to move my model by changing its worldmatrix, it gets draw some frames at the 0 0 0 position, and some other frams as totally messed up something. Same for rotation, but scaling works fine.

[source lang="cpp"]
D3DXMatrixIdentity(&World);
D3DXVECTOR3 rotaxisX = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
D3DXVECTOR3 rotaxisY = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
D3DXVECTOR3 rotaxisZ = D3DXVECTOR3(0.0f, 0.0f, 1.0f);

D3DXMATRIX temprot1, temprot2, temprot3;
D3DXMatrixRotationAxis(&temprot1, &rotaxisX, 0);
D3DXMatrixRotationAxis(&temprot2, &rotaxisY, 0);
D3DXMatrixRotationAxis(&temprot3, &rotaxisZ, 0);

Rotation = temprot1 *temprot2 * temprot3;


D3DXMatrixTranslation(&Translation, 0.0f, 10.0f, 0.0f);
D3DXMatrixScaling(&Scale, 0.02f, 0.02f, 0.02f);
//Set objs world space using the transformations
World = Translation * Rotation * Scale;
[/source]


Shader looks like this:


[source lang="cpp"]cbuffer cbPerObject
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};

// Change the position vector to be 4 units for proper matrix calculations.
input.position.w = 1.0f;

// Calculate the position of the vertex against the world, view, and projection matrices.
output.position = mul(input.position, worldMatrix);
output.position = mul(output.position, viewMatrix);
output.position = mul(output.position, projectionMatrix);
[/source]

I really cant see what is going wrong here. If all matrixchanges are 0, then its drawn fine.
Has it something to do with the math? The constantbuffer is set fine thought, light is set the same way and it works fine.

It looks like this (switching between these two frames randomly, ignore the terrain):
http://imageshack.us...3/32408839.png/
http://imageshack.us.../42/failjf.png/
Advertisement
one thing I notice is that you do "World = Translate * Rotate * Scale", and in the shader "output.position = input.position * World". As a result, you first translate the point, then rotate it about the origin, and then scale it w.r.t the origin. It's more conventional to first scale, then rotate and finally translate, so try changing the code to "World = Scale * Rotate * Translate".
Thank you, but that didnt do any good =(
The object now gets some times drawn in the right position, but the wierd flickering didnt change.
When I encounter issues like this, my first go-to is PIX (and more recently, nVidia Paraller nSight). It's very helpful in disassembling the device state for evaluating that everything is as you'd expect.
Well, I AM currently trying to figure out the problem with PIX myself, but I really cant see where the problem lies ..
I added two pictures to my first post.

Edit: Im through with analysing the buffers in PIX. I still cant see what the problem is, everything seems to be just fine....
Perhaps you're accidentally transposing some of the matrices, and you end up processing the translation elements of the matrix to affect the .w component of the output. This bug causes results that look a bit like the second image - vertices flying towards "infinity" in one direction.
You are right. When I delete the line that transposes the worldmatrix, it keeps looking like pic2. Now I need to figure why the matrix only gets transposed in some frames, while in others not. Here is the drawingcode:


D3DXMatrixTranspose(&World, &World);
D3DXMatrixTranspose(&camView, &camView);
D3DXMatrixTranspose(&camProjection, &camProjection);
// Lock the constant buffer so it can be written to.
D3D11_MAPPED_SUBRESOURCE mappedResource;
d3d11DevCon->Map(matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
cbPerObject* dataPtr;
dataPtr = (cbPerObject*)mappedResource.pData;
// Copy the matrices into the constant buffer.
dataPtr->worldMatrix = World;
dataPtr->viewMatrix = camView;
dataPtr->projectionMatrix = camProjection;
// Unlock the constant buffer.
d3d11DevCon->Unmap(matrixBuffer, 0);
d3d11DevCon->VSSetConstantBuffers(0, 1, &matrixBuffer);
I fixed it, but I dont know what caused it exactly. I have to call this code EVERY frame, not only once (I have no idea why)
[source lang="cpp"]D3DXMatrixIdentity(&World);
D3DXVECTOR3 rotaxisX = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
D3DXVECTOR3 rotaxisY = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
D3DXVECTOR3 rotaxisZ = D3DXVECTOR3(0.0f, 0.0f, 1.0f);

D3DXMATRIX temprot1, temprot2, temprot3;
D3DXMatrixRotationAxis(&temprot1, &rotaxisX, 0);
D3DXMatrixRotationAxis(&temprot2, &rotaxisY, 0);
D3DXMatrixRotationAxis(&temprot3, &rotaxisZ, 0);

Rotation = temprot1 *temprot2 * temprot3;


D3DXMatrixTranslation(&Translation, 0.0f, 10.0f, 0.0f);
D3DXMatrixScaling(&Scale, 0.02f, 0.02f, 0.02f);
//Set objs world space using the transformations
World = Scale * Rotation * Translation;[/source]

Now it works, but can you tell me why?

This topic is closed to new replies.

Advertisement