Jump to content

  • Log In with Google      Sign In   
  • Create Account


Diffuse light on mesh not working - normals missing / wrong


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Mclankyman   Members   -  Reputation: 110

Like
0Likes
Like

Posted 22 April 2013 - 06:33 AM

Hi everyone. I have a sphere model in .x format with normals, prooven by opening the directX viewer. I have loaded the model into a D3D9 Mesh using just D3DXLoadMeshFromX() and have also tried to clone the mesh with a defined vertex declaration. But I cant get my diffuse lighting to work.

 

 

 

 

//my models only need Normals, UV and position.

D3DVERTEXELEMENT9 tempElem[4]=

{

{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},

{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL , 0},

{0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},

D3DDECL_END()

};

 

meshSys->CloneMesh(D3DXMESH_SYSTEMMEM,tempElem,m_D3DDevice,&m_mesh);

 

 

/******************************************************STRUCTURES**********************/
struct VS_INPUT
{
	float4 Position : POSITION;
	float3 Normal 	: NORMAL;
	float2 UV 		: TEXCOORD0;
};

struct VS_OUTPUT
{
	float4 Position 	 : POSITION;
	float2 UV 	         : TEXCOORD0;
	float4 WorldPos 	 : TEXCOORD1;
	float4 WorldNormal  	 : TEXCOORD2;
	
};
/****************************************************************************************/


/****************************************************SHADERS**********************/
VS_OUTPUT vs_default(VS_INPUT In)
{
	VS_OUTPUT Out;
	
	//TRANSFORM VERTEX INFO
	Out.Position = mul( In.Position, gWVP );
	Out.WorldPos = mul( In.Position, gWorldMatrix);
	Out.WorldNormal = mul(float4(In.Normal,0.0), gWorldMatrix);
	Out.UV = In.UV;
	
	return Out;
}

float4 ps_default(VS_OUTPUT In) : COLOR0
{
	//////////////////////////////////////////////
	//CALCULATE LIGHT AMOUNT
	//////////////////////////////////////////////
	
	//PIXEL TO LIGHT POSITION
	float3 PixelToLightVec = gLightPosition - In.WorldPos;
	//COMPUTE SQUARED DISTANCE
	float LenSq = dot( PixelToLightVec, PixelToLightVec );
	PixelToLightVec = normalize(gLightPosition - In.WorldPos);
	float3 WorldNormal = normalize(In.WorldNormal.xyz);
	

	float3 LightAmount   = gRange * saturate( dot( WorldNormal, PixelToLightVec ) ) * gLightColour *(gLightFalloff * gLightFalloff) / LenSq;
	float3 reflection     = normalize((PixelToLightVec + gCameraPos) * 0.5);
	float3 LightSpecular  = LightAmount * pow( max( dot(WorldNormal, reflection), 0.0000001 ), gSpecularPower );

	//////////////////////////////////////////////
	//CALCULATE DIRECTIONAL LIGHT AMOUNT
	/////////////////////////////////////////////
	
	float3 DirectionalLight    = gDirectionalLightColour * max( dot(WorldNormal,   normalize(gDirectionalLight)), 0.0 );
	reflection         	   = normalize((gDirectionalLight + gCameraPos) * 0.5);
	float3 DirectionalSpecular = DirectionalLight * pow( max( dot(WorldNormal, reflection), 0.0000001 ), gSpecularPower );
	
	//////////////////////////////////////////////
	//TOTAL LIGHT = RETURNED COLOUR
	//////////////////////////////////////////////
	
	float3 DiffuseLight  = gAmbientColour + DirectionalLight + LightAmount;
	float3 SpecularLight = DirectionalSpecular + LightSpecular;
	
	float4 colour       = (tex2D(DiffuseSampler, In.UV) * float4(DiffuseLight,1.0f)) + (gSpecularMaterial * float4(SpecularLight,1.0f));
	return colour;	
}
/**************************************************************************************/

 

 There is the shader which I use. The lights are in world space and so I transform the normal and the position into world space. I am only rotating and translating the mesh so I thought I didnt require the transpose inverse matrix.  The problem is the sphere appears with ambient lighting only. I have entered a vector where 'WorldNormal' is, manually in the DP calculations and this can brighten the sphere so the problem must be with the normals passed in from the mesh or the transformation from Vertex Shader, not the lights or their calculations. Any ideas.

 

Thank you



Sponsor:

#2 cephalo   Members   -  Reputation: 539

Like
0Likes
Like

Posted 22 April 2013 - 06:28 PM

	Out.WorldNormal = mul(float4(In.Normal,0.0), gWorldMatrix);

Not an expert here, but does this cause your normal to become degenerate? I would guess the w component would need to be 1.0 or else the matrix mul will scale it to zero, and then when you try to normalize it, you can't.



#3 Jason Z   Crossbones+   -  Reputation: 4905

Like
1Likes
Like

Posted 22 April 2013 - 08:16 PM

	Out.WorldNormal = mul(float4(In.Normal,0.0), gWorldMatrix);

Not an expert here, but does this cause your normal to become degenerate? I would guess the w component would need to be 1.0 or else the matrix mul will scale it to zero, and then when you try to normalize it, you can't.

Actually that should be ok, since he is normalizing only the xyz component of the WorldNormal.  Even so, cephalo's advice is valid - you should treat vectors like vectors and only do the 3x3 rotations on them...

 

Have you tried to output the normal vectors instead of doing the regular shading?  That will let you investigate if the normals are making it to your shader, and if they are properly transformed to world space.  Do something like this:

 

cbuffer Transforms
{
	matrix WorldViewProjMatrix;	
	matrix WorldViewMatrix;
};


struct VS_INPUT
{
	float3 position : POSITION;
	float2 tex		: TEXCOORDS0;
	float3 normal	: NORMAL;
};

struct VS_OUTPUT
{
	float4 position : SV_Position;
	float4 depth	: COLOR;
};


VS_OUTPUT VSMAIN( in VS_INPUT v )
{
	VS_OUTPUT output;
	output.position = mul( float4( v.position, 1.0f ), WorldViewProjMatrix );
	
	float3 ViewSpaceNormals = mul( float4( v.normal, 0.0f ), WorldViewMatrix ).xyz;
	output.depth.xyz = ViewSpaceNormals * 0.5f + 0.5f;

	output.depth.w = output.position.w / 25.0f;
		
	return output;
}

float4 PSMAIN( in VS_OUTPUT input ) : SV_Target
{
	return input.depth;
}


Then you can check the output colors visually and determine if the values are correct (i.e. full red means the normal is point in the +X direction, and so on).  That should help determine if the issue is in the shader or in the data coming to the shader.



#4 Mclankyman   Members   -  Reputation: 110

Like
0Likes
Like

Posted 23 April 2013 - 01:01 AM

Ok good idea. I'll give it a go. thanks



#5 Mclankyman   Members   -  Reputation: 110

Like
0Likes
Like

Posted 23 April 2013 - 01:31 AM

Well the sphere is grey when I transform the normals but multicoloured (as expected) when I just render the normal as it came in the model. So it's something to do with the transformation for sure. I am going to try and use the transpose inverse matrix of the world matrix instead, even though I just thought that was necessary with non-uniform scaling.



#6 Mclankyman   Members   -  Reputation: 110

Like
0Likes
Like

Posted 23 April 2013 - 01:35 AM

Worked straight away. Case closed. Do these forums have like a topic finished option or anything.



#7 Jason Z   Crossbones+   -  Reputation: 4905

Like
0Likes
Like

Posted 23 April 2013 - 04:42 AM

The closest thing is that people edit the title of the thread to begin with "[SOLVED]".

 

Have you inspected your world transformation to see why you need to do the inverse transpose?  Where are the matrices coming from that could possibly have some non-uniform scaling applied to it?



#8 Mclankyman   Members   -  Reputation: 110

Like
0Likes
Like

Posted 23 April 2013 - 08:03 AM

Well i create a matrix using MatrixScaling(x,y,z)  SCALING

 

then have world as 

 

(SCALING * TRANSFORMATION)

 

so the only problem I could think of is that the w value of a normal is not scaled. But that confuses me a little.



#9 belfegor   Crossbones+   -  Reputation: 2559

Like
0Likes
Like

Posted 23 April 2013 - 09:58 AM

Normal is something like a "direction vector". usually when you want to transform direction vectors you need to set 'w' component to 0 so that translation part of transformation matrix isn't included into output. Position is, well position (point in space) and you set 1 to include translation part.

 

float3 normal = mul( float4( v.normal, 0.0f ), World ).xyz;
or
float3 normal = mul( v.normal, float3x3(World) );

Edited by belfegor, 23 April 2013 - 10:00 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS