• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Luke Cragg

Diffuse light on mesh not working - normals missing / wrong

8 posts in this topic

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

0

Share this post


Link to post
Share on other sites
	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.

0

Share this post


Link to post
Share on other sites

	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.

1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0