Sign in to follow this  
Waaayoff

View space lighting problem?

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;
}

float4 PShader(VS_OUT input) : SV_TARGET
{	
	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 this post


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


Link to post
Share on other sites

Yep that did it, thanks smile.png

 

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

 

Edit: Yea D3DXVec3TransformCoord uses w = 1 too.

Edited by Waaayoff

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this