Sign in to follow this  
GaryNas

Transforming Normals

Recommended Posts

GaryNas    134
Say I create a triangle (D3DFVF_XYZ | D3DFVF_NORMAL) as follows:

{0.0, 0.0, 0.0, 0.0, 0.0, -1.0}
{0.0, 1.0, 0.0, 0.0, 0.0, -1.0}
{1.0, 0.0, 0.0, 0.0, 0.0, -1.0}

Here, I'm saying that the normals point down the negative z axis. This is great in the models initial position, but what happens if I rotate the cube via 90 degrees? The normals are still -1.0. Am I supposed to be transforming the normals everytime I transform the vertices? Thanks, Gary

Share this post


Link to post
Share on other sites
If you transform manually, you'll need to transform the normals too, yes. If you specify a world transform to rotate your cube, the normals will be rotated correctly for you.

ie:
pDev->SetTransform(D3DTS_WORLD, &matrix);
is all you need to do.

If you do scales and shears you'll also want
pDev->SetRenderState(D3DRS_NORMALIZENORMALS, true);

If you modify your cube with the world transform, you can stop reading now, if you'd like.


Normals technically should be transformed by transpose of the inverse of the world matrix, which is what the fixed pipeline will do for you.

If you're transforming manually, you'll need
D3DXMATRIX iworld, itworld;
D3DXMatrixInverse(&iworld, 0, &world);
D3DXMatrixTranspose(&itworld, &iworld);
D3DXVec3TransformNormal(&newnormal, &oldnormal);
D3DXVec3Normalize(&newnormal, &newnormal);

If you don't scale each axis differently, or use shears, but just plain rotation, you can get away with just using the world matrix as is to transform normals.

When using a shader, either cast your matrix to a float3x3, or use a normal of (nx,ny,nz,0), or in asm use a dp3 for your matrix multiplies. This ensures you just rotate your normals, and not translate them too.

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