Jump to content
  • Advertisement

Archived

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

EvilCrap

vb and ib question

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, i think i have heard that its possible to use mutliple vbs, and have one store positions and texture coords, and another store normals.. and then draw using index lists. is this possible, anyone know of an example?

Share this post


Link to post
Share on other sites
Advertisement
I''d like to know to, in fact I''d like to now if there was a way to store position in one, UV in another, and normals in a third, that would rock.

Bobboau, bringing you products that work... in theory

Share this post


Link to post
Share on other sites
Yes, this is possible. But you have to remember that you can have only one index buffer. So this means that all the data for a particular vertex must be contained at the same index position in their respective buffers.

For example, you can draw a triangle with positions, normals, and texture coordinates in different buffers:

D3DXVECTOR3 pVertices[3] = {
D3DXVECTOR3(-1.0f, -1.0f, 0.0f),
D3DXVECTOR3(0.0f, 1.0f, 0.0f),
D3DXVECTOR3(1.0f, -1.0f, 0.0f)
};
D3DXVECTOR3 pNormals[3] = {
D3DXVECTOR3(0.0f, 0.0f, -1.0f),
D3DXVECTOR3(0.0f, 0.0f, -1.0f),
D3DXVECTOR3(0.0f, 0.0f, -1.0f)
}
FLOAT pTexCoords[6] = {
0.0f, 1.0f,
0.5f, 0.0f,
1.0f, 1.0f
}

m_pd3dDevice->CreateVertexBuffer(3*sizeof(D3DXVECTOR3), 0, D3DFVF_XYZ, D3DPOOL_DEFAULT, &pPositionVB);
m_pd3dDevice->CreateVertexBuffer(3*sizeof(D3DXVECTOR3), 0, D3DFVF_NORMAL, D3DPOOL_DEFAULT, pNormalVB);
m_pd3dDevice->CreateVertexBuffer(6*sizeof(FLOAT), 0, D3DFVF_TEX1|D3DFVF_TEXCOORDSIZE2(0), D3DPOOL_DEFAULT, pTexCoordsVB );

So now when you create an index buffer, using the index buffer to access the data in each of these vertex buffers will return the proper data for the particular vertex you are dealing with.

I haven''t tried it out yet, but locking these vertex buffers and filling them with the right data should give you what you want. You can then set each of these vertex buffers in a different stream.

Remember that using multiple streams requires you to use vertex shaders.

neneboricua

Share this post


Link to post
Share on other sites
quote:
Original post by neneboricua19
Remember that using multiple streams requires you to use vertex shaders.

Actually, although the docs don''t say much about FVFs in this context, you can indeed use basic FVFs. I was experimenting with streams back in December or January, I think, and I got them working quite nicely without ever having to touch shaders. It''s still a bit more complicated than the basic way of using FVFs, but it''s still doable without too much effort. I don''t have the code with me here at work, else, I''d show some of it.

Share this post


Link to post
Share on other sites
quote:
Original post by Agony
Actually, although the docs don''t say much about FVFs in this context, you can indeed use basic FVFs. I was experimenting with streams back in December or January, I think, and I got them working quite nicely without ever having to touch shaders. It''s still a bit more complicated than the basic way of using FVFs, but it''s still doable without too much effort. I don''t have the code with me here at work, else, I''d show some of it.

Really? I''d be interested to see how it''s done. I haven''t seen any info on this in the docs but I could have overlooked it somewhere. Always cool to learn something new

neneboricua

Share this post


Link to post
Share on other sites
I''ll try to remember to post some of the code this evening, then... I don''t have a clue how I figured out how to do it in the first place. Maybe somewhere on the internet...

Share this post


Link to post
Share on other sites
Here are some important clips from my project. I don''t remember everything about it, but hopefully it makes reasonable sense.
//Declarations

LPDIRECT3DVERTEXDECLARATION9 gpVertexDeclaration_PDT;
LPDIRECT3DVERTEXDECLARATION9 gpVertexDeclaration_PNDT;
LPDIRECT3DVERTEXDECLARATION9 gpVertexDeclaration_PNDST;
LPDIRECT3DVERTEXDECLARATION9 gpVertexDeclaration_PDTT;
LPDIRECT3DVERTEXDECLARATION9 gpVertexDeclaration_PNDTT;
LPDIRECT3DVERTEXDECLARATION9 gpVertexDeclaration_PNDSTT;
//

LPDIRECT3DVERTEXBUFFER9 gpVBPosition;
LPDIRECT3DVERTEXBUFFER9 gpVBNormal;
LPDIRECT3DVERTEXBUFFER9 gpVBDiffuse;
LPDIRECT3DVERTEXBUFFER9 gpVBSpecular;
LPDIRECT3DVERTEXBUFFER9 gpVBTexture0;
LPDIRECT3DVERTEXBUFFER9 gpVBTexture1;
//

LPDIRECT3DINDEXBUFFER9 gpIB;
//

const DWORD D3DFVF_PDT = (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE2(0) );
const DWORD D3DFVF_PNDT = (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE2(0) );
const DWORD D3DFVF_PNDST = (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_SPECULAR | D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE2(0) );
const DWORD D3DFVF_PDTT = (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX2 | D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEXCOORDSIZE2(1));
const DWORD D3DFVF_PNDTT = (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX2 | D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEXCOORDSIZE2(1));
const DWORD D3DFVF_PNDSTT = (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_SPECULAR | D3DFVF_TEX2 | D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEXCOORDSIZE2(1));
//

const D3DVERTEXELEMENT9 s_VertexDeclaration_PDT[] = //Position, Diffuse, Texture

{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 1, 0, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
{ 2, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()
};

const D3DVERTEXELEMENT9 s_VertexDeclaration_PNDT[] = //Position, Normal, Diffuse, Texture

{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 1, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
{ 2, 0, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
{ 3, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()
};
//

const D3DVERTEXELEMENT9 s_VertexDeclaration_PNDST[] = //Position, Normal, Diffuse, Specular, Texture

{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 1, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
{ 2, 0, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
{ 3, 0, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 1 },
{ 4, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()
};
//

const D3DVERTEXELEMENT9 s_VertexDeclaration_PDTT[] = //Position, Diffuse, Texture, Texture

{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 1, 0, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
{ 2, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
{ 3, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 },
D3DDECL_END()
};
//

const D3DVERTEXELEMENT9 s_VertexDeclaration_PNDTT[] = //Position, Normal, Diffuse, Texture, Texture

{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 1, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
{ 2, 0, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
{ 3, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
{ 4, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 },
D3DDECL_END()
};
//

const D3DVERTEXELEMENT9 s_VertexDeclaration_PNDSTT[] = //Position, Normal, Diffuse, Specular, Texture, Texture

{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 1, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
{ 2, 0, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
{ 3, 0, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 1 },
{ 4, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
{ 5, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 },
D3DDECL_END()
};
//

HRESULT CreateDirectX()
{
//Direct3D, Device, etc...

//...

//...

//Creating the buffers and such

gpDevice->CreateVertexBuffer(MAX_VERTICES * sizeof(D3DXVECTOR3), D3DUSAGE_DYNAMIC, D3DFVF_XYZ, D3DPOOL_DEFAULT, &gpVBPosition, NULL);
gpDevice->CreateVertexBuffer(MAX_VERTICES * sizeof(D3DXVECTOR3), D3DUSAGE_DYNAMIC, D3DFVF_NORMAL, D3DPOOL_DEFAULT, &gpVBNormal, NULL);
gpDevice->CreateVertexBuffer(MAX_VERTICES * sizeof(D3DCOLOR) , D3DUSAGE_DYNAMIC, D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &gpVBDiffuse, NULL);
gpDevice->CreateVertexBuffer(MAX_VERTICES * sizeof(D3DCOLOR) , D3DUSAGE_DYNAMIC, D3DFVF_SPECULAR, D3DPOOL_DEFAULT, &gpVBSpecular, NULL);
gpDevice->CreateVertexBuffer(MAX_VERTICES * sizeof(D3DXVECTOR2), D3DUSAGE_DYNAMIC, D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE2(0), D3DPOOL_DEFAULT, &gpVBTexture0, NULL);
gpDevice->CreateVertexBuffer(MAX_VERTICES * sizeof(D3DXVECTOR2), D3DUSAGE_DYNAMIC, D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE2(0), D3DPOOL_DEFAULT, &gpVBTexture1, NULL);
//

gpDevice->CreateIndexBuffer(MAX_INDICES * sizeof(WORD), D3DUSAGE_DYNAMIC, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &gpIB, NULL);
//

gpDevice->CreateVertexDeclaration(s_VertexDeclaration_PDT, &gpVertexDeclaration_PDT);
gpDevice->CreateVertexDeclaration(s_VertexDeclaration_PDT, &gpVertexDeclaration_PNDT);
gpDevice->CreateVertexDeclaration(s_VertexDeclaration_PDT, &gpVertexDeclaration_PNDST);
gpDevice->CreateVertexDeclaration(s_VertexDeclaration_PDT, &gpVertexDeclaration_PDTT);
gpDevice->CreateVertexDeclaration(s_VertexDeclaration_PDT, &gpVertexDeclaration_PNDTT);
gpDevice->CreateVertexDeclaration(s_VertexDeclaration_PDT, &gpVertexDeclaration_PNDSTT);
//...

}
//

HRESULT Draw()
{
//Set states

//...

if (/*Using Location, Diffuse, 1 Texture*/)
{
gpDevice->SetFVF(D3DFVF_PDT);
gpDevice->SetVertexDeclaration(gpVertexDeclaration_PDT);
gpDevice->SetStreamSource(0, gpVBPosition, 0, sizeof(D3DXVECTOR3));
gpDevice->SetStreamSource(1, gpVBDiffuse, 0, sizeof(D3DCOLOR) );
gpDevice->SetStreamSource(2, gpVBTexture0, 0, sizeof(D3DXVECTOR2));
gpDevice->SetIndices(gpIB);
}
else if (/*Using Location, Normal, Diffuse, 1 Texture*/)
{
gpDevice->SetFVF(D3DFVF_PNDT);
gpDevice->SetVertexDeclaration(gpVertexDeclaration_PNDT);
gpDevice->SetStreamSource(0, gpVBPosition, 0, sizeof(D3DXVECTOR3));
gpDevice->SetStreamSource(1, gpVBNormal, 0, sizeof(D3DXVECTOR3));
gpDevice->SetStreamSource(2, gpVBDiffuse, 0, sizeof(D3DCOLOR) );
gpDevice->SetStreamSource(3, gpVBTexture0, 0, sizeof(D3DXVECTOR2));
gpDevice->SetIndices(gpIB);
}
else if (...)
//...

//...

//Lock appropriate buffers

//Fill them like normal

//Render them like normal

}

Share this post


Link to post
Share on other sites
In the code sample, I see calls to SetFVF but then right afterwards, there are calls to SetVertexDeclaration, which completely overwrites the call to SetFVF.

< ponders things for a bit >

Ok, after digging through the docs more, I found that I was assuming that vertex declarations could only be used with vertex shaders and that FVF flags were only to be used with the fixed function pipeline. This was a mistake on my part.

It seems that if you create a vertex declaration and specify that certain data will come from different streams, you can still use the fixed function pipeline.

That''s good info to know. Thanks for the sample Agony.

neneboricua

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!