This topic is 3476 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I want to replace this code with vertex shader:
m_pd3dDevice->SetFVF( D3DFVF_XYZRHW| D3DFVF_DIFFUSE| D3DFVF_TEX2 );


so I declared a vertex declaration like this:
const D3DVERTEXELEMENT9 FVF_POS_T_TEX1[] =
{
{ 0, 0,	D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{ 0, 16,D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1}, //Or it should be 0???
D3DDECL_END()
};


In the SetFVF() function it used a [D3DFVF_TEX2] flag, but I do not know what I should use to identify the D3DDECLUSAGE_TEXCOORD : [0] or [1]??? And how to write the vertex entry function?(Assume the window is 1280Wx720H)
struct VS_OUT_POS_TEX1
{
float4 	ProjPos	:	POSITION;
float2	ProjUV1	:	TEXCOORD1;// Or TEXCOORD0?
};
VS_OUT_POS_TEX1 vsMainPosTTex1(	float4	ObjPos	:	POSITION,
float2	ObjUV1	:	TEXCOORD1)// Or TEXCOORD0?
{
VS_OUT_POS_TEX1 Out = (VS_OUT_POS_TEX1)0;
Out.ProjPos.x = (ObjPos.x / (1280/2.0)) -1;
Out.ProjPos.y = -(ObjPos.y / (720/2.0)) +1;
Out.ProjPos.z = ObjPos.z;
Out.ProjPos.w = 1;
Out.ProjUV1 = ObjUV1;
return Out;
}


Does this right? Thanks very much.

##### Share on other sites
Quote:
 Original post by wswqwpsIn the SetFVF() function it used a [D3DFVF_TEX2] flag, but I do not know what I should use to identify the D3DDECLUSAGE_TEXCOORD : [0] or [1]???
It should be 0. That number means that it's the first set of texture coordinates. If you had two sets of texture corodinates, you'd put that line in twice, and change the last parameter.

Quote:
 Original post by wswqwpsAnd how to write the vertex entry function?(Assume the window is 1280Wx720H)*** Source Snippet Removed ***Does this right? Thanks very much.
All your TEXCOORD1's should be TEXCOORD0 (For the reason above). I'm not really sure about the rest of the shader code unfortunately - have you tried it to see if it works?

##### Share on other sites
Have you considered just using projection-space coordinates? That way you get resolution-independent 2D geometry and can avoid having ugly 1280/2.0 style constants...

By using projection space you can defined everything in the application in [-1,+1] range and forget about handling different resolutions. The main case where it gets complicated is if you need pixel-accurate 2D, but even that can be handled if you must.

hth
Jack

##### Share on other sites
Quote:
Original post by Evil Steve
Quote:
 Original post by wswqwpsIn the SetFVF() function it used a [D3DFVF_TEX2] flag, but I do not know what I should use to identify the D3DDECLUSAGE_TEXCOORD : [0] or [1]???
It should be 0. That number means that it's the first set of texture coordinates. If you had two sets of texture corodinates, you'd put that line in twice, and change the last parameter.

Actually, he has 2 sets. D3DFVF_TEX2 means he has two sets of texture coordinates. Their sizes would be specified with D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEXCOORDSIZE2(1), for instance. But since he left those out, it default to two floats per set.

Also, he uses transformed vertices (D3DFVF_XYZRHW), so IMO, including the D3DFVF_DIFFUSE, the vertex declaration should be:
const D3DVERTEXELEMENT9 FVF_POS_T_TEX1[] = {  { 0,  0, D3DDECLTYPE_FLOAT4,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITIONT, 0},  { 0, 16, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR,     0},  { 0, 20, D3DDECLTYPE_FLOAT2,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD,  0},    { 0, 28, D3DDECLTYPE_FLOAT2,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD,  1},  D3DDECL_END()};

And then of course use TEXCOORD0 and TEXCOORD1 in the shader.

(Also, I prefer using offsetof() instead of hard-coding the byte offsets in the vertex)

##### Share on other sites
Quote:
Original post by Mike nl
Quote:
Original post by Evil Steve
Quote:
 Original post by wswqwpsIn the SetFVF() function it used a [D3DFVF_TEX2] flag, but I do not know what I should use to identify the D3DDECLUSAGE_TEXCOORD : [0] or [1]???
It should be 0. That number means that it's the first set of texture coordinates. If you had two sets of texture corodinates, you'd put that line in twice, and change the last parameter.

Actually, he has 2 sets. D3DFVF_TEX2 means he has two sets of texture coordinates. Their sizes would be specified with D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEXCOORDSIZE2(1), for instance. But since he left those out, it default to two floats per set.

Also, he uses transformed vertices (D3DFVF_XYZRHW), so IMO, including the D3DFVF_DIFFUSE, the vertex declaration should be:
*** Source Snippet Removed ***

And then of course use TEXCOORD0 and TEXCOORD1 in the shader.

(Also, I prefer using offsetof() instead of hard-coding the byte offsets in the vertex)

Thanks, But I am confused. The SetFVF() function only use a D3DFVF_TEX2 flag without D3DFVF_TEX1 flag. Does the declaration still need two D3DDECLUSAGE_TEXCOORD flags?

##### Share on other sites
Quote:
Original post by Evil Steve
Quote:
 Original post by wswqwpsIn the SetFVF() function it used a [D3DFVF_TEX2] flag, but I do not know what I should use to identify the D3DDECLUSAGE_TEXCOORD : [0] or [1]???
It should be 0. That number means that it's the first set of texture coordinates. If you had two sets of texture corodinates, you'd put that line in twice, and change the last parameter.

Quote:
 Original post by wswqwpsAnd how to write the vertex entry function?(Assume the window is 1280Wx720H)*** Source Snippet Removed ***Does this right? Thanks very much.
All your TEXCOORD1's should be TEXCOORD0 (For the reason above). I'm not really sure about the rest of the shader code unfortunately - have you tried it to see if it works?

Unfortunately, it works but appears terrible.

##### Share on other sites
Quote:
 Original post by jollyjeffersHave you considered just using projection-space coordinates? That way you get resolution-independent 2D geometry and can avoid having ugly 1280/2.0 style constants...By using projection space you can defined everything in the application in [-1,+1] range and forget about handling different resolutions. The main case where it gets complicated is if you need pixel-accurate 2D, but even that can be handled if you must.hthJack

Sorry, could you show me some sample code? Thanks.

[Edited by - wswqwps on August 20, 2008 10:36:56 PM]