3/28/2007

Normalizing vectors.

As far as Dr.Zovil knows, the way vectors are normalized determines which shading model a shader uses and two typical cases are as follows.

normalizing vectors in 'vertex shader' >> Gouraud shading

//vertex shader
vertexOutput VS(vertexInput IN)
{
vertexOutput OUT ;

OUT.hPosition = mul(float4(IN.position,1.0), worldViewProjection);

float3 worldVertPos = mul(float4(IN.position,1.0f), world).xyz ;
float3 lightVector = normalize(float4(lightPos - worldVertPos, 1.0f)) ;
float3 worldNormal = normalize(mul(IN.normal, worldInverseTranspose));

OUT.diff = dot(worldNormal, lightVector);

return OUT ;
}


//pixel shader
float4 PS(vertexOutput IN) : COLOR
{

float4 diffuseContrib = IN.diff* Kd* lightColor ;
float4 result = diffuseContrib;

return result ;
}



normalizing vectors in 'pixel shader' >> Phong shading.

//vertex shader
vertexOutput VS(vertexInput IN)
{
vertexOutput OUT ;

OUT.hPosition = mul(float4(IN.position,1.0), worldViewProjection);

float3 worldVertPos = mul(float4(IN.position,1.0f), world).xyz ;
OUT.lightVector = float4(lightPos - worldVertPos, 1.0f) ;

OUT.worldNormal = mul(IN.normal, worldInverseTranspose);

return OUT ;
}


//pixel shader
float4 PS(vertexOutput IN) : COLOR
{
float4 N = normalize(IN.worldNormal);
float4 L = normalize(IN.lightVector);
float diff = saturate(dot(N,L));
float4 diffuseContrib = diff* Kd* lightColor ;
float4 result = diffuseContrib;

return result ;
}






He doesn't know why there is difference between two shading models.

Is it an inherent difference between the two shading models?
Or, did he make some mistakes in wriing the shaders?
If you have the answer, please let him know!!!

1 comment:

Bence said...

Instead of worldInverseTranspose he should use worldViewinverseTranspose. :)

Tag