Jump to content
  • Advertisement
Sign in to follow this  
Endemoniada

World Inverse Transpose

This topic is 2563 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

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:

// diffuse vertex shader

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 vshader(VS_INPUT input)
{
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;
}


float4 pshader(VS_OUTPUT input) : COLOR
{
return input.color;
}

Please help.

Share this post


Link to post
Share on other sites
Advertisement
Hey, I figured it out.

It works when I do:

mul(World,input.normal);

instead of:

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 this post


Link to post
Share on other sites
Multiplication with matricies is not commutative.

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 this post


Link to post
Share on other sites
Hi,
Instead of:
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 this post


Link to post
Share on other sites
Instead of:
float4 N=normalize(mul(float4(input.normal.xyz, 0),World));
use
float3 N=normalize( mul( input.normal.xyz, (float3x3)World));
They both actually compile to the same assembly code (last I tested) biggrin.gif

Share this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!