Archived

This topic is now archived and is closed to further replies.

bentry

About the offset in D3DVERTEXELEMENT9

Recommended Posts

bentry    122
There is a sample in the Dx9 doc(Programmable Stream Model ): // multistream vertex // stream 0, pos, diffuse, specular struct POSCOLORVERTEX { FLOAT x, y, z; DWORD diffColor, specColor; }; #define D3DFVF_POSCOLORVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_SPECULAR) // stream 1, tex coord 0 struct TEXC0VERTEX { FLOAT tu1, tv1; }; #define D3DFVF_TEXC0VERTEX (D3DFVF_TEX1) // stream 2, tex coord 1 struct TEXC1VERTEX { FLOAT tu2, tv2; }; #define D3DFVF_TEXC1VERTEX (D3DFVF_TEX0) // multitexture - multistream D3DVERTEXELEMENT9 dwDecl3[] = { { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 }, { 0, 12, D3DDECLTYPE_D3DCOLOR, 3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 }, { 0, 28, D3DDECLTYPE_D3DCOLOR, 3DDECLMETHOD_DEFAULT, /** ¡«¡«specColor''s offset£½28 *****/ D3DDECLUSAGE_COLOR, 1 }, { 1, 36, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, /** ¡«¡« Tex coord1''s offset£½36 *****/ D3DDECLUSAGE_TEXCOORD, 0 }, { 2, 44, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() }; /////////////////////////////////////////////////////////////////////////////////////////// Why the offset of specColor is 28 but not 12 + sizeof(diffColor) = 12+4 = 16? And the offset of Tex coord1 is so odd too. Is it wrong?

Share this post


Link to post
Share on other sites
aker_jus    122
The offsets for the vertex elements fo like this:

positionoffset = 0
coloroffset = positionoffset + sizeof(positiontype) <-- which is Float3, so 3 * 4 = 12 for color offset.

normaloffset = positionoffset + sizeof(positiontype) .... same

texoffset = what_is_behind_uv_offset + sizeof(thattype)

so if before ur UV u have a normal, which would be float3, u would have an offset of:
texoffset = 12 + sizeof(float)*3 = 24

You are working from top to bottom on the vertex structure. There is a function in C u can use to get the offsets, offsetof (structure, member)

returns an int i think. But above, I gave u the algorithm that that function uses, so u can understand it.

Share this post


Link to post
Share on other sites
bentry    122
Thanks a lot.But in that example it really haven't any thing before the color. Should i resever the space of that element even if it's not used?

[edited by - bentry on March 22, 2004 9:33:19 PM]

Share this post


Link to post
Share on other sites
I''m not sure how aker got coloroffset and normaloffset as the same value.

Anyway, no, you aren''t and should not reserving space. Just describe where the data is in your structure.

For what you gave, I''d expect

0, 0, Float3, default, position, 0
0, 12, d3dcolor, default, color, 0
0, 16, d3dcolor, default, color, 1

1, 0, float2, default, texcoord, 0
2, 0, float2, default, texcoord, 1

The FVF of the third stream (stream2) looks wrong, but since you can''t use the FVF I wouldn''t worry about it.

Share this post


Link to post
Share on other sites