Jump to content

  • Log In with Google      Sign In   
  • Create Account

HLSL add tangents for normal mapping


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
3 replies to this topic

#1 cozzie   Members   -  Reputation: 1776

Like
0Likes
Like

Posted 27 January 2014 - 04:25 PM

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, 27 January 2014 - 04:55 PM.


Sponsor:

#2 cozzie   Members   -  Reputation: 1776

Like
0Likes
Like

Posted 28 January 2014 - 01:19 PM

Anyone?

#3 cozzie   Members   -  Reputation: 1776

Like
0Likes
Like

Posted 28 January 2014 - 02:23 PM

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)



#4 cozzie   Members   -  Reputation: 1776

Like
0Likes
Like

Posted 28 January 2014 - 03:11 PM

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






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