#### Archived

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

# VertexBuffer with Dynamic Vertexes

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

## Recommended Posts

How do I fill a vertex buffer with a dynamic number of vertexes? Here is a nice non-working example:
int Triangles = 2; //dynamic number

int TrianglePoints = Triangles * 3;
VERTEX *sVertices = new VERTEX[TrianglePoints];

//vertex information

for(int i=0; i < TrianglePoints; ++i)
{
if((i+1)%3 == 0)
{
long x = (rand()%10) - 5;
long y = (rand()%10) - 5;
long z = (rand()%10) - 5;
sVertices[i-2].vPosition = D3DXVECTOR3(-1 + x,-1 + y, z);
sVertices[i-1].vPosition = D3DXVECTOR3(-1 + x, 1 + y, z);
sVertices[i-0].vPosition = D3DXVECTOR3( 0 + x, 0 + y, z);
}
}

VOID* pVertices;
g_pD3DDevice->CreateVertexBuffer( TrianglePoints * sizeof(VERTEX), 0, FVF_VERTEX, D3DPOOL_MANAGED, g_pD3DVB_VERTEX, 0);
g_pD3DVB_VERTEX->Lock(0, sizeof(sVertices), &pVertices, 0);
memcpy(pVertices, sVertices, sizeof(sVertices));
g_pD3DVB_VERTEX->Unlock();

SafeDeleteArray(sVertices);
Anyone?

##### Share on other sites
g_pD3DVB_VERTEX->Lock(0, sizeof(sVertices), &pVertices, 0);
memcpy(pVertices, sVertices, sizeof(sVertices));

sVertices is a dynamic array/pointer. I believe the use of the sizeof operator will give you 4 (the actuall size of the pointer). This is contrary to the sizeof operator use on a satic array in which case it gives the actaull size of the array.

So what you should try is.

g_pD3DVB_VERTEX->Lock(0, TrianglePoints * sizeof(VERTEX), &pVertices, 0);
memcpy(pVertices, sVertices, TrianglePoints * sizeof(VERTEX));

##### Share on other sites
Thank you very much sir, it worked

1. 1
2. 2
3. 3
4. 4
Rutin
18
5. 5

• 13
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632927
• Total Posts
3009248
• ### Who's Online (See full list)

There are no registered users currently online

×