• Advertisement
Sign in to follow this  

View space lighting problem?

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

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
Advertisement

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
Sign in to follow this  

  • Advertisement