# World Inverse Transpose

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

## Recommended Posts

Hi guys,

I'm rendering a cube with a basic diffuse vertex shader. It works fine when I transform the normal with the WorldInverseTranspose but does not work at all when I transform with the World. I am only doing translation, not scaling or even rotation. Most tutorials I see transform the normal by the World so I don't see what I can be doing wrong. I'm using D3D9. Here is the shader code:

float4x4 World;
// float4x4 WorldInverseTranspose;
float4x4 WorldViewProjection;

float4 LightDirection;

struct VS_INPUT
{
float4 position : POSITION;
float4 normal : NORMAL;
};

struct VS_OUTPUT
{
float4 position : POSITION;
float4 color : COLOR;
};

{
VS_OUTPUT output;

output.position=mul(input.position,WorldViewProjection);

float4 N=normalize(mul(input.normal,World));
float4 L=normalize(LightDirection);

float NL=saturate(dot(N,L));

output.color=float4(NL,NL,NL,1);

return output;
}

{
return input.color;
}

##### Share on other sites
Hey, I figured it out.

It works when I do:

mul(World,input.normal);

mul(input.normal,World);

Will someone please explain, in layman's terms, why that works. Thanks guys.

EDIT: actually, that doesn't seem to work when rotation is applied.

##### Share on other sites
Multiplication with matricies is not [url="http://en.wikipedia.org/wiki/Commutative_property"]commutative[/url].

Instead of using [font="Courier New"]input.normal[/font], you want to use [font="Courier New"]float4(input.normal.xyz, 0.0)[/font]. The reason is that the 4th component of the vector acts as a scaling factor for the translation part of the matrix. You only want your normals to be rotated (not translated!), so a 4th component of zero should be used.

##### Share on other sites
Thanks alot Hodgman, I never would have figured that out on my own.

##### Share on other sites
Hi,
float4 N=normalize(mul(float4(input.normal.xyz, 0),World));
use
float3 N=normalize( mul( input.normal.xyz, (float3x3)World));
and float3 for light direction (.w component is not used anyway)
This saves some inst.

##### Share on other sites
float4 N=normalize(mul(float4(input.normal.xyz, 0),World));
use
float3 N=normalize( mul( input.normal.xyz, (float3x3)World));[/quote]They both actually compile to the same assembly code (last I tested) [img]http://public.gamedev.net/public/style_emoticons/default/biggrin.gif[/img]

##### Share on other sites
Yeah the compiler will aggressively optimize out multiplications with 0 or 1, or additions with 0, etc. You can take advantage of this to make your code more intuitive, especially if you're auto-generating many permutations of shaders.

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628642
• Total Posts
2983985

• 10
• 18
• 20
• 13
• 9