View space lighting problem?

Started by
1 comment, last by Waaayoff 10 years, 8 months ago

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.

"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
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.

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.

"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "

This topic is closed to new replies.

Advertisement