Sign in to follow this  
wswqwps

Vertex decls about the shader

Recommended Posts

wswqwps    122
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 this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by wswqwps
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]???
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 wswqwps
And 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 this post


Link to post
Share on other sites
jollyjeffers    1570
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 this post


Link to post
Share on other sites
Mike nl    390
Quote:
Original post by Evil Steve
Quote:
Original post by wswqwps
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]???
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 this post


Link to post
Share on other sites
wswqwps    122
Quote:
Original post by Mike nl
Quote:
Original post by Evil Steve
Quote:
Original post by wswqwps
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]???
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 this post


Link to post
Share on other sites
wswqwps    122
Quote:
Original post by Evil Steve
Quote:
Original post by wswqwps
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]???
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 wswqwps
And 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 this post


Link to post
Share on other sites
wswqwps    122
Quote:
Original post by jollyjeffers
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


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

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

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