shader and matrix transform

Started by
4 comments, last by DieterVW 14 years, 9 months ago
hy, i have this PART OF shader:

PS_INPUT VS( VS_INPUT input )
{
    
    PS_INPUT output = (PS_INPUT)0;
   

    output.Pos = mul(input.Pos,worldMatrix);
    output.Pos = mul( output.Pos, ViewMatrix );
    output.Pos = mul( output.Pos, projMatrix );
     
    output.normal=mul(input.Normal,worldMatrix);
	output.normal=normalize(mul(output.normal,ViewMatrix));
    output.normal=normalize(mul(output.normal,projMatrix));
      
    return output;
}


float4 PS( PS_INPUT In) : SV_Target
{
   float4 colLight = float4(1, 1, 1, 1);
   float4 MaterialColor = float4(0, 1, 0, 1);
   float3 lightDirection = float3(0,0,-21);
   float3 L = normalize(lightDirection.xyz);
   float4 D = saturate( dot(In.normal,L) * colLight * MaterialColor);
   return D;

 }

i disabled the cull mode but some faces appear black , i think is a normal problem , is correct to multiply the normal in the VS for the word , view and projection matrix? Thanks.
Advertisement
Quote:Original post by giugio
hy, i have this PART OF shader:

*** Source Snippet Removed ***

i disabled the cull mode but some faces appear black , i think is a normal problem , is correct to multiply the normal in the VS for the word , view and projection matrix?

Thanks.


You shouldn't be transforming a normal at all, it's just a directional vector.

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

Quote:Original post by deadstar

You shouldn't be transforming a normal at all, it's just a directional vector.


That is not true, your normal needs to be transformed by the rotational portion of the transformation matrix. There are many threads on gamedev and else where discussing how to do this.



Quote:Original post by DieterVW
Quote:Original post by deadstar

You shouldn't be transforming a normal at all, it's just a directional vector.


That is not true, your normal needs to be transformed by the rotational portion of the transformation matrix. There are many threads on gamedev and else where discussing how to do this.


Ah of course, my mistake.

Don't listen to me giugio, it's been a long day ;)

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

output.normal=mul(input.Normal,(float3x3)worldMatrix); <- if you use float3x3 this uses just the rotation part of the matrix, this is what you need

This is all you need. Dont multiply your normal by the view-proj matrix at all, only the world transform is needed. And in case your input normal isnt normalized, normalize it.

Quote:Original post by n3Xus
output.normal=mul(input.Normal,(float3x3)worldMatrix); <- if you use float3x3 this uses just the rotation part of the matrix, this is what you need

This is all you need. Dont multiply your normal by the view-proj matrix at all, only the world transform is needed. And in case your input normal isnt normalized, normalize it.


This only works if your transformation matrix contains no scaling.

This topic is closed to new replies.

Advertisement