Advertisement Jump to content
Sign in to follow this  

Transforming Normals

This topic is 4950 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

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
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!