# 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.

## 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 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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 9
• 15
• 14
• ### Forum Statistics

• Total Topics
634070
• Total Posts
3015333
×