# View space lighting problem?

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

## Recommended Posts

This is the shader (single directional light):

VS_OUT VShader(VS_IN input)
{
VS_OUT output = (VS_OUT)0;
output.position = mul(WorldViewProj, input.position);
output.normal = mul((float3x3)NormalTransf, input.normal);

return output;
}

{
float3 normal = normalize(input.normal);
float3 diffuse = 0.6 * saturate(dot(normal, -SunDirection));

return float4(0.25 + diffuse, 1);
}


NormalTransf is the transpose of the inverse world * view matrix:

D3DXMatrixInverse(&NormalTransf, 0, &(World * mCamera.GetViewMatrix()));
D3DXMatrixTranspose(&NormalTransf, &NormalTransf);

And SunDirection is converted to view space like this:

D3DXVECTOR3 SunDirection(0, 0, 1);
D3DXVECTOR4 vec;
D3DXVec3Transform(&vec, &SunDirection, &mCamera.GetViewMatrix());
SunDirection = D3DXVECTOR3(vec.x, vec.y, vec.z);

I'm guessing the problem is with the SunDirection code. Except i'm doing what this says:

The problem is that the light follows the camera... I tried multiplying by different matrices and the closest i got is when i multiplied by NormalTransf, except the y and z values of SunDirection get switched.

Edited by Waaayoff

##### Share on other sites

SunDirection is direction vector

D3DXVec3Transform docs:

This function transforms the vector, pV (x, y, z, 1), by the matrix pM.

Notice the 1, it must be function with 0 in that place so try D3DXVec3TransformNormal

This function transforms the vector (pV->x, pV->y, pV->z, 0) by the matrix pointed to by pM.

##### Share on other sites

Yep that did it, thanks

I thought i tried this when i used D3DXVec3TransformCoord but i guess not.

Edit: Yea D3DXVec3TransformCoord uses w = 1 too.

Edited by Waaayoff

• 18
• 19
• 11
• 21
• 16