Skip to main content
gamedev.net
🔒 Locked

Diffuse Lighting in Shader - Visual Problems

Started by SiS-Shadowman Apr 10, 2006 at 5:54 AM 1 replies 450+ views
Original Post
SiS-Shadowman
SiS-Shadowman
Well, after i've searched for documents on the web for different illumination models, i tried to implement diffuse lighting in my terrain viewer, but the result is very ... you can see for yourself: the terrain consists of 256 Patches, wich you can clearly see at the "shadowed" side of the terrain. i don't know what is wrong here. i'll simpli post the code for the shader:

float4x4 matWorldViewProjection;
float4x4 matRotation;
texture SkinTexture;
float4 WireFrameColor;
float4 vecLightDir = {0.0, 1.0, 1.0, 1.0};
float4 ambient = {0.05, 0.0, 0.0, 1.0};

struct VS_OUTPUT
{
	float4 Pos : POSITION;
	float2 Tex0 : TEXCOORD0;
	float2 Tex1 : TEXCOORD1;
	float4 Hemi : Color0;
};

VS_OUTPUT VS(float4 Pos: POSITION, float3 Normal: NORMAL, float2 Tex0 : TEXCOORD0, float2 Tex1 : TEXCOORD1)
{
	VS_OUTPUT Out = (VS_OUTPUT) 0;
	
    Out.Pos	 = mul(Pos, matWorldViewProjection);
	Out.Tex0 = Tex0; 
	Out.Tex1 = Tex1;
	Out.Hemi = saturate(dot(vecLightDir, normalize(mul(Normal, matRotation))));

	return Out;
}

sampler SkinSampler = sampler_state
{
	texture = (SkinTexture);
	MIPFILTER = LINEAR;
	MINFILTER = LINEAR;
	MAGFILTER = LINEAR;
};

float4 PS(float2 Tex0 : TEXCOORD0, float4 Hemi : Color0) : COLOR
{
	return ambient + tex2D(SkinSampler, Tex0) * Hemi;
	//return tex2D(SkinSampler, Tex0);
}

technique DiffuseLighting
{
	pass P1
	{
		CullMode = CCW;

 		VertexShader = compile vs_1_1 VS();
        PixelShader  = compile ps_1_1 PS();
	}
}

i think the code is right, but i don't know why there are clearly visible edges.
jollyjeffers
jollyjeffers
You're obviously going to get linear interpolation across the triangles, so any error in the per-vertex calculation will show up even more. By the looks of things, it's probably something wrong with your normals. Dark/light areas usually mean that the normal isn't pointing in the correct direction...

What is matRotation set to? Try changing it to the inverse transpose, or possibly just the identity and see what happens.

When debugging normals/tangents/binormals I find it useful to have a visual guide - it's not too hard to fill up a vertex buffer with a D3DPT_LINELIST arrangement - one line for each normal. It's often quite easy to spot problems by this kind of visual inspection.

hth
Jack
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |
SiS-Shadowman
SiS-Shadowman
matRotation is (obviously) the rotation matrix of the terrain.
i need it because it would be standing on its head ( i had to rotate the terrain by PI to let it point at the right direction ^^



hm. the normals at the edge of each border seem to differ.
i think the problem might be, that i create the terrain vertexbuffer ( along with the normals ) and it's indexbuffer in each patch, and the neighbouring normals ( of the neighbouring patches ) aren't taken into account, when the normals for each patch are created.
i'm not certain, but would it be better if i calc positions, indices and normals first and let each patch take out from a buffer, what they need?

[Edited by - SiS-Shadowman on April 10, 2006 7:32:13 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.