how could i get the correct texture coordinate in HLSL

Started by
6 comments, last by gwihlidal 17 years, 6 months ago
here is my definition in code (TEX0): #define D3DFVF_MYVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX0) and here is my vertex shader .fx file (TEXCOORD0): VS_OUTPUT VS(float4 Pos : POSITION, float3 Normal : NORMAL, float2 Tex : TEXCOORD0) but i get the wrong texture coordinate, when i change D3DFVF_TEX0 to D3DFVF_TEX1 , then the result is right. then i try TEX2->TEXCOORD2, TEX1->TEXCOORD1, TEX0->TEXCOORD2, TEX0->TEXCOORD, the result are all wrong. how could i get the right texture coordinate if i set 2 or more coordinate in the vertex declaration, and how could i know which one is map to which one. Thank for any help, Alan.
Advertisement
1) You should probably use a VertexDeclaration as opposed to Flexible Vertex Format since FVFs are obsolete\deprecated and eventually get translated into Vertex Declarations anyways.

2) What does your vertex structure look like?

3) How are you setting your texture sampler?

4) What is "TEX2->TEXCOORD2, TEX1->TEXCOORD1, TEX0->TEXCOORD2, TEX0->TEXCOORD"?


Here's an example vertex declaration and how to set it (instead of using an FVF)

D3DVERTEXELEMENT9 decl[] =
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
{ 0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
{ 0, 28, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()
};


if( FAILED( hr = m_pd3dDevice->CreateVertexDeclaration( decl, &m_pVertexDeclaration ) ) )
return hr;


m_pd3dDevice->SetVertexDeclaration( m_pVertexDeclaration );



But if you want to use an FVF for simplicity for now:

struct MESH_VERTEX
{
D3DXVECTOR3 position; // vertex position
D3DXVECTOR3 normal; // vertex normal
DWORD color;
D3DXVECTOR2 tex1;
};
#define D3DFVF_MESHVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_DIFFUSE|D3DFVF_TEX1)


That's how you can define a vertex with a position, normal, diffuse color, and a single texture coordinate. For two textures (follow pattern for more)

struct MESH_VERTEX
{
D3DXVECTOR3 position; // vertex position
D3DXVECTOR3 normal; // vertex normal
DWORD color;
D3DXVECTOR2 tex1;
D3DXVECTOR2 tex2;
};
#define D3DFVF_MESHVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_DIFFUSE|D3DFVF_TEX1|D3DFVF_TEX2)

Hope that helps!
Great help and deeply appreciate.

"TEX2"->"TEXCOORD2", means
"FVFs (D3DFVF_TEX2)"->"The input of vertex shader (float2 Tex : TEXCOORD2)"

here is my vertex struct:
typedef struct My_Vertex{	float x;	float y;	float z;	float normalx; 	float normaly; 	float normalz;	float tu;	float tv;} Vertex_t;


here is my texture sampler setting
sampler ColorMapSampler = sampler_state{	Texture = <ColorMap>;	MinFilter = Linear;	MagFilter = Linear;	MipFilter = Linear;	AddressU = Clamp;	AddressV = Clamp;};


so, if my FVFs have two texture coordinate settings:
D3DFVF_TEX1|D3DFVF_TEX2

in .fx file
The vertex shader:
VS_OUTPUT VS(....., float2 Tex : TEXCOORD?, float2 Tex1 : TEXCOORD?)

how could i know "?" is 0 or 1 or 2 or....

Thank,

Alan.
In the FVF the number is a count. If you have 2 texcoords, you say TEX2. You don't say TEX1 | TEX2, as that would work out to telling D3D that you have 3 texcoord instead.

For an FVF the texcoord indices start at 0 and count up by 1.
D3DFVF_TEX2 would be TEXCOORD0 and TEXCOORD1.

You can also use D3DFVF_TEXCOORDSIZEn(m) which sets the m'th coord to size n. ie:

TEX2 | TEXCOORDSIZE3(1)
would be
float2 uv : TEXCOORD0;
float3 uvw : TEXCOORD1;

When using declarations, the last number, called the USAGEINDEX, indicates which TEXCOORD it maps to.
Great help and deeply appreciate.

Now i know how to correct my mind to get the right texture coordinate.

and sorry for my poor english.

Thank,

Alan.
Yes, sorry it is the count, though as far as I remember you won't get 3 textures with that, you'll just get two as the HLSL Vertex Declaration interpreter just uses the highest texcount value perceived. So TEX1 | TEX2 should work, though it's redundant as you pointed out.

And like I mentioned earlier, I wouldn't use FVFs as they are obsolete\deprecated and just as pointed out, they are hard to distinguish what exactly is being mapped. There's also inescapable problems when building advanced shaders that need more constants\samplers than the FVFs support (parallax mapping for example).

~Graham
Thank for this mention, i am just weird that i can't find a FVFs for TANGENT when doing bump mapping. Now i know what's wrong with me. i have changed my FVFs to vertex declaration (the performance also be improved because of the little changed).

Thank,

Alan.

[Edited by - ppp456 on September 27, 2006 6:03:33 AM]
Yup, np :)

TANGENT is a perfect example of where FVFs become obsolete\deprecated. FVFs were designed for the fixed function pipeline which was fairly rigid in what you could tell the GPU. Vertex Declaration were pretty much designed for the programmable pipeline.

~Graham

This topic is closed to new replies.

Advertisement