Jump to content
  • Advertisement
Sign in to follow this  
jkristia

problem with basinc light

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

Hi, I have been working on getting basinc light working for the last several evenings, but I'm not able to get the correct behavior, and I would like some help understanding what I'm doing wrong.

 

The objects vertices are transformed to world space at the time of creating the geometry, so positions are in world space. The normal are calculated from the positions, which I think should be correct.

 

Here is my simple shader code



cbuffer cbTransform : register(b0)
{
	float4x4 gWorld;
	float4x4 gView;
	float4x4 gViewProj;
	float3 gEyePos;
	float filler;
}

cbuffer cbObjColor : register(b1)
{
	float4 objColor;
	float4x4 objInvTransform;
}

struct VS_IN
{
	float4 pos : POSITION;
	float4 normal : NORMAL;
};

struct PS_IN
{
	float4 posH : SV_POSITION;
	float4 posWorld : POSITION;
	float4 col : COLOR;
	float4 normal : NORMAL;
};

PS_IN VS( VS_IN input )
{
	PS_IN output = (PS_IN)0;
	output.posWorld = input.pos;
	output.posH = mul(input.pos, gViewProj);
	input.normal.w = 0;
	output.normal = input.normal;
	output.normal.w = 0;
	output.col = objColor;
	return output;
}

float4 PS( PS_IN input ) : SV_Target
{
	float3 gDirectionalLightPos = float3(1,1,-1);
	float4 gAmbColor = input.col; // ambient color
	float  gAmbIntensity = 0.0;

	float4 inputnormal = input.normal;
	float4 inputpos = input.posWorld;
	float4 inputcolor = input.col;
	float4 lightpos = float4(gDirectionalLightPos,0);

	float4 n = normalize(inputnormal);
	float4 L = normalize(lightpos);

	// ambient
	float4 cAmb = saturate(gAmbColor * gAmbIntensity);
	// diffuse component
	float4 sDiff = float4(1,1,1,1); // color of diffuse light
	float4 mDiff = inputcolor;		// color of the object
	float4 cDiffuse = saturate((sDiff * mDiff) * clamp(dot(n, L), 0, 1));

	return saturate(cAmb + cDiffuse);
	//return input.col;
}

The problem is that when the camera is at 1,1,-1 the cylinder is not lit

 

[attachment=18103:img1.png]

 

but when moved to -1,1-1 the cylinder is lit, but it is lit 'almost' evenly from all sides

 

[attachment=18104:img2.png]

 

Anyone can spot my mistake, because I can't.

Any help is very much appreciated.

 

 

Share this post


Link to post
Share on other sites
Advertisement

Shouldn't you use the Light Vector instead of the light position?

 

EDIT. Remember to multiply the normal by the world matrix.

 

Btw. Just tested it out in FX Composer, and it turns out fine, so I don't know what the problem could be, other than the world matrix.

Edited by Migi0027

Share this post


Link to post
Share on other sites

I think the position becomes the light vector.

E.g. if I place the light at 1,1,1, then isn't the normalised vector also norm(1,1,1) ? - or did I get that wrong.

 

One thing that tells me it is a problem with the normal is that if I moved the cylinder to the center (no translation) then it is lit as expected regardless of where I place the camera.

 

Edit:

This is where I change from pos to vector, just didn't name it correct.

 

float4 lightpos = float4(gDirectionalLightPos,0);

Edited by jkristia

Share this post


Link to post
Share on other sites

>>EDIT. Remember to multiply the normal by the world matrix.

Hmm, I was thinking since I generate the normals after the vertices have been translated to world space that I would not have to transform them, but let me give that a try.

 

I remember downloading FX Composer at some point, but haven't tried it yet (still very new to 3D and DirectX). I will give that a try, might be easier than shooting in the dark as I'm kind of doing now.

Edited by jkristia

Share this post


Link to post
Share on other sites

hmm, if I instead of transforming the vertices into world space at the time of creating the geometry, keep it in local space and then let the vertex shader do the transformation, then it seems to work.

 

I don't understand this.

 

If I calculate a normal from a triangle by doing

 

vector0 = normalized(Vertex0 - Vertex1)

vector1 = normalized(Vertex0 - Vertex2)

N = cross(vector0, vector1)

 

Then why does it matter if the vertices are in local space or world space, as long as I only apply a translate, e.g. offset the x value by a given amount. The normal should still be the same - shouldn't it ?

Share this post


Link to post
Share on other sites

[attachment=18130:img3.png]

this is what it looks like if I create the vertices in model space and let the vertex shade translate the vertices and normals into world space.

I still don't understand why it doesn't work if I create it directly in world space.

Share this post


Link to post
Share on other sites
I think you have to normalize the final normal vector, not the two triangle-edge vectors: N = normalized(cross(edge1, edge2));

Share this post


Link to post
Share on other sites

Argh - last night I had time to debug this, and I found my silly !@#$% mistake.

At some point I had changed the InputLayout, and by mistake (ARGH!!) I had the Normal offset = 0, same as the position, which of course is why it behaved so strange. Once fixed basic diffuse light works as expected, regardless of the objects position (of course). Guess I wont make that mistake again.

 

[attachment=18172:img.png]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!