Copying two sets of vertices into one vbuffer

Started by
5 comments, last by Programmer16 19 years, 7 months ago
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
Advertisement
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.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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-
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.
________________
"I'm starting to think that maybe it's wrong to put someone who thinks they're a Vietnamese prostitute on a bull"       -- Stan from South Park
Lab74 Entertainment | Project Razor Online: the Future of Racing (flashsite preview)
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.
Quote:Original post by Programmer16
If 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.

Damn, I'm an idiot. Thanks Coder, it works now!

Thanks to everybody else too!

This topic is closed to new replies.

Advertisement