Coding Makes the Matworld Go 'Round

Recommended Posts

So I've been following the coding in this book and basically it's going through a huge chapter all about graphics and I'm taking it slowly to take it piece by piece. Well while I was reading I was following along with my own code and comparing it to the code on the cd, while the CD has some extra stuff for the more advanced portions of the chapter what I have should work (I think). Basically all it is is a Verts[4] in a square list. I have my projection and view transformations in and put in a world transformation of 'matWorld'. I didn't do anything with the matWorld except declare it and run a SetTransform on it. This is the code the cd had:
D3DXMatrixRotationZ(&matWorld, (float)timeGetTime() / 1000.0f);
g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
I didn't want the MatrixRotation so I only had the SetTransform and my box didn't appear. I commented that single line out and bam the square appeared. Can anyone help clear up why nothing would show when I took out that code? Maybe explain a bit more about why it's required to code the three transformations instead of DX making a single function that does all of it for the programmer?

Share on other sites
stevenmarky    369
Hello programmermattc,
D3DXMatrixRotationZ creates a matrix, so if you comment out that line you may have a empty matrix being passed into SetTransform (I could confirm this if you show me the code where matWorld is created).
This could be the reason your box isn't showing up.

Keep D3DXMatrixRotationZ commented, but try adding a new line above SetTransform:
D3DXMatrixIdentity(&matWorld);

Share on other sites
Thats what I was thinking too but I thought if it transformed a blank matrixthe program would error out. I had this a few lines above:
D3DXMATRIX matWorld;

Adding that Identity worked! Thanks! Any idea now why I wouldn't have to apply my other two transformations to the World so DX would understand how to render it in context? Is an Identity Matrix normally used for a world matrix if something like a rotation isn't used?

Share on other sites
stevenmarky    369
"Maybe explain a bit more about why it's required to code the three transformations instead of DX making a single function that does all of it for the programmer?"

- Are you asking why there is a function for each rotation axis?
Probably because the programmer may want to rotate in different orders, also rotation through all axis is more expensive, but there are functions to do this; D3DXMatrixRotationYawPitchRoll and D3DXMatrixRotationAxis.

Share on other sites
ItsDoh    162
An identity matrix is kind of like '1' for multiplication. Anything times 1 ='s the same number you had. Well anything times an identity matrix is the same matrix. So basically, you start with the identity matrix and then perform your transformations in the ordr you want. With a plain matrix you'd end up with multiplications against 0, and losing data. For comparison an 'empty' matrix would be:

0000
0000
0000
0000

whereas an identity matrix is

1000
0100
0010
0001

Share on other sites
Yeah I read a bunch about different matrices like that a couple weeks ago but I guess I didn't understand what the use for it was in gaming / coding.