Sign in to follow this  

Copying two sets of vertices into one vbuffer

This topic is 4856 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

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4856 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this