Sign in to follow this  

Who wants to be a millionnaire (DirectX Matrixes edition)

Recommended Posts

Hi, i'm your host for tonight. This will be a special edition, so no choice of answers. (heartbeating music starts..) So let's start with the 100$ question : What is the "practical" purpose of this function :D3DXMatrixTranspose ? By practical I mean show an example and explain why that function must be used. Don't answer me it switches columns to rows I know, because knowing that does not tell me when to use this function. Now the 200$ question : From DirectX SDK : [­...]You can minimize the number of required calculations by concatenating your world and view matrices into a world-view matrix that you set as the world matrix, and then setting the view matrix to the identity. [...] For clarity, in this documentation Direct3D samples rarely employ this optimization. For clarity, could you show an example (a simple one thanks) of concatenating a world and view matrix? Right now i'm using them separately. This is it for the first part of the show.. Come back next week!

Share this post

Link to post
Share on other sites
D3DXMatrixTranspose(&mat, &mat);
pDev->SetVertexShaderConstant(&mat, 20, 4);

Why? Because then a matrix multiply becomes

dp4 r0.x, v0, c20
dp4 r0.y, v0, c21
dp4 r0.z, v0, c22
dp4 r0.w, v0, c23

Also, technically, if you want to accurately transform normals in software (or in a shader), you should use the transpose of the inverse of the world transform.

D3DXMatrixInverse(&iworld, 0, &world);
D3DXMatrixTranspose(&itworld, &iworld);
D3DXVec3TransformNormal(&normworld, &norm, &itworld);

concat = world * view;
D3DXMatrixMultiply(&concat, &world, &view);
pDev->SetTransform(D3DTS_WORLD, &concat);
pDev->SetTransform(D3DTS_VIEW, &ident);

I'm waiting for my $300 dollars.

Share this post

Link to post
Share on other sites
You do not have to use every function that is there in the SDK. If you have not had a need to use transpose, then maybe that is ok.

By the way, on a more serious note, transpose is a quick way to compute INVERSE for a rotation only matrix. (== 4th row = 0 0 0 1)

And you need inverse for transforming everything back into object space from world space.

Share this post

Link to post
Share on other sites
Original post by Jacob Roman
Can we use a life line? LOL!!! I knew of the transpose thing for many years and I've never seen it used nor do I know even to this day of where it is used. Does anyone know?

So why is the transpose useful? Is fast inverse for rotation-only matrices its only use?

PS: Here in the UK £ = about 1.6 $ so WWTBAM is better here!

Share this post

Link to post
Share on other sites
We've got a winner!

So far you've won $300 dollars, Namethatnobodyelsetook,
but you know how it's working, nobody's get off that seat
until maybe passed the 64,000$ mark.

I'm sure you can't wait for the 400$ question.

Next one will be soon. Final answer.

Share this post

Link to post
Share on other sites
Original post by Axiverse
If you set one of the view/proj matricies to ident, will it not multiply throught it? or will it multiply through it anyways?

Personally, I'd expect DX to multiply the matrices together itself. It's slightly more work than testing for identity, and can save lots of work per vertex. If you're doing a shader, you can multiply them if you want, or leave them seperate.

We use coords in worldspace for height fog and lightmap UV generation (world x and z * scale factor), allowing the lightmap to affect moving objects. If we didn't do height fog, we could do lightmap UVs as a texture transform with the inverse of view and then scaled and do that on cameraspaceposition, but really, it all works out to the same number of matrix multiplies, so why bother making the CPU calculate the inverse of the view transform.

If you use the fixed pipeline, I'm sure it multiplies world and view, and if you're using shaders, it's up to you. If you don't need data in worldspace, go for it... mix those matrices. If you do need worldspace data, it's not a big deal. It's only 3 shader instructions.

Share this post

Link to post
Share on other sites
Normally you'd have

// transform to worldview, or worldviewproj
dp4 r1.x, v0, c20
dp4 r1.y, v0, c21
dp4 r1.z, v0, c22
dp4 r1.w, v0, c23
// now r1 = viewpos or viewprojpos

instead you now have
// transform to world
dp4 r0.x, v0, c17
dp4 r0.y, v0, c18
dp4 r0.z, v0, c19
mov r0.w, v0.w
// transform to view or viewproj
dp4 r1.x, r0, c20
dp4 r1.y, r0, c21
dp4 r1.z, r0, c22
dp4 r1.w, r0, c23
// now r0 = worldpos
// now r1 = viewpos or viewprojpos

Ok, so it MAY be 4 instructions to have an extra matrix mutliply (because you need to copy over the W... or use a 4X4 matrix, wasting a constant register).

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this