I need help (with Scaling)...

Started by
7 comments, last by mfolino 19 years, 4 months ago
Its kinda odd. Let me just post the code: D3DXMATRIX matTrans; D3DXMATRIX matRotate; D3DXMATRIX matScale; D3DXMatrixIdentity(&matScale); D3DXMatrixTranslation(&matTrans, pos.x, pos.y, pos.z); D3DXMatrixRotationY(&matRotate, D3DXToRadian(1.0f)); D3DXMatrixMultiply(&matTrans, &matRotate, &matTrans); D3DXMatrixScaling(&matScale, 0.5f, 0.5f, 0.5f); D3DXMatrixMultiply(&matTrans, &matScale, &matTrans); When I create the object with this code, it doesn't even show up. If I remove the two last lines, the object shows up, but is absolutely huge (even though the 3D model is tiny). Any ideas why my scaling isnt working? Any help you guys can offer would be awesome. Thanks in advance.
Advertisement
What happens if you do swap the last two arguments to the last matrix multiply (ie: perform scaling after translation and rotation rather than before). It may be that you're just not moving the mesh into view anymore because you've scaled the translation.
Im sorry, I posted the code the DOESNT show anything. This is the code that renders the object, but HUGELY (and as if Im not using the scale factor at all...ie. if I take out the first two lines, the result is exactly the same).

D3DXMatrixScaling(&matScale, 0.5f, 0.5f, 0.5f);
D3DXMatrixMultiply(&matTrans, &matScale, &matTrans);
D3DXMatrixTranslation(&matTrans, pos.x, pos.y, pos.z);
D3DXMatrixRotationY(&matRotate, D3DXToRadian(1.0f));
D3DXMatrixMultiply(&matTrans, &matRotate, &matTrans);

So, if I use this:

D3DXMatrixTranslation(&matTrans, pos.x, pos.y, pos.z);
D3DXMatrixRotationY(&matRotate, D3DXToRadian(1.0f));
D3DXMatrixMultiply(&matTrans, &matRotate, &matTrans);
D3DXMatrixScaling(&matScale, 0.5f, 0.5f, 0.5f);
D3DXMatrixMultiply(&matTrans, &matScale, &matTrans);

I get nothing on the screen. All the variables seem placed correctly, Im just not sure what Im doing wrong here.
This is what I wanted you to try...

D3DXMatrixTranslation(&matTrans, pos.x, pos.y, pos.z);
D3DXMatrixRotationY(&matRotate, D3DXToRadian(1.0f));
D3DXMatrixMultiply(&matTrans, &matRotate, &matTrans);
D3DXMatrixScaling(&matScale, 0.5f, 0.5f, 0.5f);
D3DXMatrixMultiply(&matTrans, &matTrans, &matScale);

The order matrices are multiplied is important.

Swapping the arguments of the last matrix multiply. In your first code snippet on your latest post you scale a translation matrix that hasn't been set up yet, then you overwrite it with a plain translation matrix... so yeah, the scale has no effect. It's as if you did this.

float s,t,r;

s = scale;
t *= s;
t = pos;
r = rot;
t *= r;

Notice how you're multiplying t before assigning a value, then overwriting the result completely anyway.
The problem with your first code I think lies here
|
| D3DXMatrixScaling(&matScale, 0.5f, 0.5f, 0.5f);
| D3DXMatrixMultiply(&matTrans, &matScale, &matTrans);
+> D3DXMatrixTranslation(&matTrans, pos.x, pos.y, pos.z);
D3DXMatrixRotationY(&matRotate, D3DXToRadian(1.0f));
D3DXMatrixMultiply(&matTrans, &matRotate, &matTrans);

This will overwrite what was in matTrans (ie your scale) with just your Translation variables. Instead, try something like :

D3DXMatrixIdentity(&matWorld);
D3DXMatrixScaling(&matScale, 0.5f, 0.5f, 0.5f);
D3DXMatrixTranslation(&matTrans, pos.x, pos.y, pos.z);
D3DXMatrixRotationY(&matRotate, D3DXToRadian(1.0f));
D3DXMatrixMultiply(&matWorld, &matRotate, &matScale);
D3DXMatrixMultiply(&matWorld, &matWorld, &matTrans);

Hope this helps,
Steele.
I thought you were just scaling and translating. Sorry for the incorrect info. I have deleted the original post.
** boolean010 **
To illustrate the point about the order of operations, I'll clarify it a bit here why it matters.

Imagine you have a vertex at 2,3,4. Lets just worry about x for now. We translate by 40, and scale by 0.5.

2, translate by 40 = 42, scale by 0.5 = 21
vs
2, scale by 0.5 = 1, translate by 40 = 41.

Vastly different answers based on the order of operations. The order you multiply matrices determines the order of operations.
The order of multiplication with matrices is imperative. You must multiply scale with rotation first. You then take the result of that and multiply with the translation matrix. Do it out of order and your result will be incorrect.
Ok, I feel a little dumb with regards to the ordering, I know that it matters, and yet I still screwed it up! Anyhow, thanks guys for the help, it seems I was just being an idiot! Thanks again.

This topic is closed to new replies.

Advertisement