• 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
cozzie

HLSL add tangents for normal mapping

3 posts in this topic

Hi,

I've started adding normal mapping in my 3d engine. To do this I've cut the process in steps to implement it 'one time right' and exactly understand what I'm doing.

 

1. Add tangents and binormals to my models, exported from 3ds max, using KW-Xport

2. Add tangents and binormals in both my vertex declaration and shader VS_INPUT

3. continue implementing normal mapping in my HLSL shader (add second textureSampler, convert to tangent space etc.)

 

Now my thought would be that I could do step 1 and 2, try if the end result of rendering stays OK/ the same and then continue.

But unfortunately, after I did this, lighting is 'gone', meaning meshes are no longer visible. Draw calls are made though, but quite a bit less, you can see this by the numbers/ statistics in the top left corner.

 

My questions:

1. is my assumption right that I can add tangents and binormals to my X files/ meshes, vtx declaration and VS_INPUT, and keep the actual PS/VS the same with similar results as before?

2. Maybe I've done something wrong in step 1 and 2. I've pasted the changes below

3. Before the changes my position and normal in the VS_INPUT were float4, changed them to float3 what would be logical. Could this be an issue?

 

Before changes:

// Vertex declaration
typedef struct TVERTEX
{ 
	D3DXVECTOR3	position;
	D3DXVECTOR3	normal;
	float    	tex[2];		// vs d3dxvector3
} TVERTEX;

const D3DVERTEXELEMENT9 vtxdecl[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()
};

// loading meshes, relevant part of code

	if(FAILED(D3DXLoadMeshFromXA(pXfilename.c_str(), 
								 D3DXMESH_MANAGED, //SYSTEMMEM, // DYNAMIC??? SYSTEMMEM FOR DYNAMIC OBJECTS??
								 mD3ddev,
								 &mAdjacencyBuffer,
								 &mMaterialBuffer,
								 NULL,
								 &mSubMeshSize,
								 &mMesh))) return false;
	
	if(!(mMesh->GetFVF() & D3DFVF_NORMAL))
	{
		MessageBox(NULL, err_nonormals_xfile.c_str(), err_windowtext.c_str(), MB_OK);
		return false;
	}

// the VS_INPUT in the effect file (shader)
struct VS_INPUT
{
	float4 Pos		: POSITION0;
	float4 Normal	: NORMAL0;
	float2 TexCoord	: TEXCOORD0;
};


After the changes:

// Vertex declaration
typedef struct TVERTEX
{ 
	D3DXVECTOR3	position;
	D3DXVECTOR3	normal;
	float    	tex[2];	
	D3DXVECTOR3 binormal;
	D3DXVECTOR3 tangent;
} TVERTEX;

const D3DVERTEXELEMENT9 vtxdecl[6] =
{
	{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},
	{0, 32, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BINORMAL, 0},
	{0, 44, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0},
	D3DDECL_END()
};

// loading meshes
	D3DVERTEXELEMENT9 decl[MAX_FVF_DECL_SIZE];
	mMesh->GetDeclaration(decl);
	
	bool normalsFound = false;
	bool tangentsFound = false;
	bool binormalsFound = false;

	for(unsigned int index=0;index<D3DXGetDeclLength(decl);++index)
	{
		if(decl[index].Usage == D3DDECLUSAGE_NORMAL) normalsFound = true;
		if(decl[index].Usage == D3DDECLUSAGE_TANGENT) tangentsFound = true;
		if(decl[index].Usage == D3DDECLUSAGE_BINORMAL) binormalsFound = true;
	}
	if(!normalsFound || !tangentsFound || !binormalsFound)
	{
		MessageBox(NULL, err_nonormals_xfile.c_str(), err_windowtext.c_str(), MB_OK);
		return false;
	}

// VS_INPUT in effect/ shader
struct VS_INPUT
{
	float3 Pos		: POSITION0;
	float3 Normal	: NORMAL0;
	float2 TexCoord	: TEXCOORD0;
	float3 BiNormal	: BINORMAL0;
	float3 Tangent	: TANGENT0;
};

Any input is really appreciated.

I'm planning not to move on before I fix or understand this fully.

 

Screenshot before/after:

http://www.sierracosworth.nl/gamedev/tangents_before.jpg

http://www.sierracosworth.nl/gamedev/tangents_after.jpg

 

I double checked the vertex declaration returned from my mesh file with the one I've created.

They are the same.

 

Any ideas?

Edited by cozzie
0

Share this post


Link to post
Share on other sites

Found it, it was the float3 for position in my VS_INPUT.

When I change it back to float4, everything works fine.

 

Actually 2 following questions/ curious;

 

- why did this solve it/ what's the difference?

(I thought it didn't make a difference, unless it affects vertex shader calculations, but there are no compilation errors)

- would it be bad, performance wise, to ALWAYS send binormals/tangents to my shader, even when I don't do actual normal mapping in the VS/PS?

(or would you define different VS_INPUTS in the different shaders/effects, one with and one without normal mapping)

0

Share this post


Link to post
Share on other sites

Although this is getting a conversion with myself, my first NormalMapping test succeeded (see below).

Not perfect yet, looks a bit to 'pixeled' and lighting not 100% OK yet, but getting there.

 

The actual normal mapping code (HLSL):

// Vertex shader
	// Worldspace to Tangent space for normalMapping
	Out.WorldToTangent[0] = mul(normalize(input.Tangent), World);
	Out.WorldToTangent[1] = mul(normalize(input.Binormal), World);
	Out.WorldToTangent[2] = mul(normalize(input.Normal), World);


// Pixel shader
	float3 normal = tex2D(normalMapSampler, input.TexCoord);
	normal = normalize(mul(normal, input.WorldToTangent));
//	float3 normal = normalize(input.Normal);


normalmapping.jpg

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