   Filling vertex buffer without memcpy
  Ive been looking at the tutorial that comes with the samples in Directx SDK in the Direct3d folder and the following section of code came up in the lighting part of the tutorial. Normally I assumed that the memcpy function would be needed to fill the vertex buffer but im not sure how the buffer is filled in this code. Can someone please explain the code to me? (I get all the stuff about drawing the cylinder)HRESULT InitGeometry(){    // Create the vertex buffer.    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 50 * 2 * sizeof( CUSTOMVERTEX ),0, D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )    {   return E_FAIL;    }    // Fill the vertex buffer. We are algorithmically generating a cylinder    // here, including the normals, which are used for lighting.    CUSTOMVERTEX* pVertices;    if( FAILED( g_pVB->Lock( 0, 0, ( void** )&pVertices, 0 ) ) )   return E_FAIL;    for( DWORD i = 0; i < 50; i++ )    {   FLOAT theta = ( 2 * D3DX_PI * i ) / ( 50 - 1 );   pVertices[2 * i + 0].position = D3DXVECTOR3( sinf( theta ), -1.0f, cosf( theta ) );   pVertices[2 * i + 0].normal = D3DXVECTOR3( sinf( theta ), 0.0f, cosf( theta ) );   pVertices[2 * i + 1].position = D3DXVECTOR3( sinf( theta ), 1.0f, cosf( theta ) );   pVertices[2 * i + 1].normal = D3DXVECTOR3( sinf( theta ), 0.0f, cosf( theta ) );    }    g_pVB->Unlock();    return S_OK;}
  When you lock a vertex buffer, you get given a pointer to the first byte in the vertex buffer. Often you'll use memcpy() to simply copy from an array of vertices you have into that buffer.That code just replaces the vertices in place, updating two vertices at a time (2*i+0 and 2*i+1). In this case, it's walking around a circle in 50 steps, writing in the top and bottom vertex for that part of the cylinder.   ;  I think I understand, the third parrameter in Lock() is the array I want the buffer to store. I understand how to use it but im still abit unclear on how it works and what exactly the third parammeter does.Also is there any specific reason DWORD was used for i and not int or float?edit: what happened to eppo's post?also I understand how they stored the vertex points in the array but not how it got to the buffer   ;Quote:Original post by eflatmajorI think I understand, the third parrameter in Lock() is the array I want the buffer to store. I understand how to use it but im still abit unclear on how it works and what exactly the third parammeter does.Also is there any specific reason DWORD was used for i and not int or float?edit: what happened to eppo's post?DWORD is an unsigned int   ;Quote:Original post by eflatmajorI think I understand, the third parrameter in Lock() is the array I want the buffer to store.Not really. The third parameter is there so Lock() can give you back a pointer to the memory block.That is, there's a block of data somewhere in memory that Lock() gets for you. Using the third parameter, Lock then gives you a pointer to that memory (the pointer is passed by pointer).Quote:Also is there any specific reason DWORD was used for i and not int or float?Not really. DWORD on Windows is an unsigned integer that's always 32-bits, IIRC. Maybe that's not a guarantee, but there's no way Microsoft would change that anyway.   ;  I deleted my post because it said exactly the same thing as Evil Steve's did.Note that g_pVB is not the actual buffer. Just an object that encapsulates the buffer. Lock() gives you the address to the actual bytes that store the vertices.   ;  I sorta get it now but I guess I'll just stick to using memcpy until I can get my head around it completely. I just liked how this method didnt require me to define the size of the array before it started.   ;  It sounds like the thing you're missing is that in C++ (which it gets from C), arrays are treated as pointers to contiguous blocks of memory.There really isn't any distinction between arrays and pointers in C/C++.  That's one of the things that makes code analysis hard, actually.   