# My memcpy/vertexbuffer question,

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

## Recommended Posts

Before people read this and think, why doesn't he just try it, i have. Umm the thing is i have groups of 36 vertices stored in different locations, there is a reason why for this but thats not relevent. What i want to do is copy these straight into a vb. The problem im facing is the way that data is copied into a vb, the only way i know how. The only method i have so far is to copy all the verts into 1 big group of vertices, then to copy that group into the actual vb, hope this makes some sense the code:
	int buffInd = 0;
for ( int i = 0; i < 1000; i++)
{
if (bGuide.inUse == true)
{
memcpy(&finalBuffer[buffInd], &bGuide.pBL->v[0], sizeof(bGuide.pBL->v));
buffInd += 36;
}
else
break;
}

VOID* pVertices;
vb->Lock( 0, sizeof(finalBuffer), (void**)&pVertices, 0 );
memcpy( pVertices, finalBuffer, sizeof(finalBuffer) );
vb->Unlock();


so i loop through the 1000 locations where these groups of 36 verts are copy them into finalBuffer[], then copy finaleBuffer[] into the vb afterwards. i want to optimise this by instead of using finalBuffer[], just copy straight into the vb. I dont understand quite why this code uses: VOID* pVertices; Is that just an open ended section of memory? I want to be able to index through the vb so like vb[0], vb[3] so i can copy the verts into the location i want. what should i use instead of VOID* pVertices, ace?

##### Share on other sites
memcpy is used for copying ranges from one location of memory to another. It is not necessary for copying individual elements. The purpose of the IDirect3DVertexBuffer9::Lock() is to provide you with a pointer to the beginning of a range of memory that you can safely write to. That means that you can index from that pointer using the subscript operator/notation to access/write individual elements.

What all that verbiage means is that after you've called IDirect3DVertexBuffer9::Lock(), you can do
pVertices = <ptr_to_vertex_data>;

Hope that helped.

##### Share on other sites
Use MYVERTEX* pVertices; so the pointer is the size of MYVERTEX and not VOID so each [] is size-MYVERTEX.

##### Share on other sites
You'll have to cast the pVertices to something else in order to index it.

For example:
void *pVertices;MyVertex *pVB;int total = 0;// Lock somewhere before thispVB = (MyVertex*)pVertices;  // Cast to indexable typememcpy(&pVB[total], source1, sizeof(MyVertex)*numverts1);whereIPutThem1 = total; // You can use this in DrawIndexedPrimitivetotal += numverts1;memcpy(&pVB[total], source2, sizeof(MyVertex)*numverts2);whereIPutThem2 = total; // You can use this in DrawIndexedPrimitivetotal += numverts2;...// Unlock somewhere after you copy

obviously you'd have proper structures, not just variables with numbers tacked onto the end, but you get the idea.

##### Share on other sites
judging by the code i already had, can it be improved upon quite nicely?

##### Share on other sites
i just tested and i tried to cahnge the code so that the

VOID* pVertices
was

GENERICVERTEX* pVertices

then tried indexing into it, and it said it cant convert

pVertices[0] to VOID*

thanx for that source snippet BTW but i cant quite follow it.

Any chance on a more worded reflection on that source?
thanx :)
ace

##### Share on other sites
SOLVED!!!!!

thanx everyone

PS i was being a complete LUMMOX!

• 18
• 29
• 11
• 21
• 16