Copying two sets of vertices into one vbuffer

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

Recommended Posts

I'm trying to get the vertices from my two cubes into one vertexbuffer and I can't figure out how to do it. This is what I have:
IDirect3DVertexBuffer9* pBuffer = NULL;
int iSize = CubeOne.Size() + CubeTwo.Size();

pDevice->CreateVertexBuffer(iSize, 0, VertexFVF, D3DPOOL_DEFAULT, &pBuffer, NULL);

void* pVertices = NULL;
pBuffer->Lock(0, 0, (void**)&pVertices, 0);
memcpy(pVertices, CubeOne.GetVertices(), CubeOne.Size());
memcpy(&(pVertices + CubeOne.Size()), CubeTwo.GetVertices(), CubeTwo.Size());
pBuffer->Unlock();


I'm pretty sure that the problem is with my second memcpy() call. Any help would be appreciated! Thanks

Share on other sites
What's not working? Is it crashing? (If so, is it crashing on the second memcpy?) Is it not drawing the cubes correctly? (If so, what does draw?)

From your code, I don't immediately see anything wrong. I'm assuming all your size variables, functions, etc. are all in bytes.

Share on other sites
Hey,

I'm not 100% sure on this, but this code looks familiar to something I did with my network code when it came to constructing my final message. For the second memcpy call try something like this:

memcpy( pVertices[ CubeOne.size() ], CubeTwo.GetVertices(), CubeTwo.GetSize() );

This should tell memcpy to start at the index immediately following the last vertex (you may have to subtract one depending on how you handle your size) and copy CubeTwo.Size number of vertices.

Permafried-

Share on other sites
the error I think is:

memcpy(&(pVertices + CubeOne.Size()), CubeTwo.GetVertices(), CubeTwo.Size());

you are taking the address of the address of the end point of cube1 data in the vbuffer. 2 levels of indirection. It oughta be crashing.
remove the & operator.

Share on other sites
If I remove the & operator it tells me "void*" unknown size.

Okay, I've tracked it down to giving me an error on the pBuffer->Unlock() function.

Share on other sites
Quote:
 Original post by Programmer16If I remove the & operator it tells me "void*" unknown size.

Pointer arithmetic - adding/subtracting/incrementing/decrementing pointers - requires knowing the size of the pointee type so that the address is incremented succesfully.

i.e. Basically, when you say:
int arr[5];int* pointer = arr;pointer += 3;

The pointer += 3 increments the address pointed to by the pointer by the value "3 * sizeof(int)" (i.e. multiplied by the size of the pointee type).

In short, either declare pVertices to be of your vertex type, or typecast it in the memcpy.

Share on other sites
Damn, I'm an idiot. Thanks Coder, it works now!

Thanks to everybody else too!

• 47
• 11
• 17
• 11
• 13