I'm trying to write a simple geometry loader that loads data from a .OBJ file. The vertices, and indices are stored in vectors. That's all fine. However, when it comes to filling the index and vertex buffers, the examples I learnt from use standard arrays, not vectors.
So, how do I load the vertex and index data from the vectors and into the corresponding ID3D10Buffer buffers? I'm a bit nervous about using memcpy( ) with vectors to be honest.
struct VERTEX
{
VERTEX( void ) : Position.x( 0.0f ), Position.Y( 0.0f ), Position.z( 0.0f )
{
}
D3DXVECTOR3 Position;
};
// The vertex and index buffers...
ID3D10Buffer *VertexBuffer( NULL ), *IndexBuffer( NULL );
// Vectors to hold the vertex and index data from the .OBJ file.
// Lets assume that the vectors are filled with data from the file.
vector< short > Indices;
vector< VERTEX > Vertices;
// This method will load the data from both vectors and into the
// corresponding instance of ID3D10Buffer.
HRESULT CopyVectorData( void )
{
// Copy data from Vertices...
if( VertexBuffer == NULL )
{
// Do copy operation here...
}
// Copy the data from Indices...
if( IndexBuffer == NULL )
{
// Do copy operation here...
}
}
int WinMain( /* WinMain's arguments here... */ )
{
}
Using the example code above, how should I copy the data from the vectors and into the buffers?
Yes, memcpy for sturctures is not good. You need to copy it field by field. That doesn't seem to be too hard. If you write the 'ID3D10Buffer' immediatly to the device you don't need an array otherwise you need to allocate an array.
Btw why do you have 2 vectors? Why not putting it in 1 struct for 1 vector?
I ran into this exact same problem today, trying to write a simple OBJ file loader.
I don't know the number of vertices before importing, so I'm using vectors too, to add vertices and indices dynamically.
So since the number of vertices is not static, I can't create an array with it.
Could I just create an array int indices[1], then zeromemory(&indices, sizeof(int * num_indices)), and then use it as tough it has num_indices fields?
Apparently, since vectors store their data in contiguous memory, you can treat them as arrays by simply getting the pointer to the first element, like so:
//my indices are stored in vector<short> faces
short *indices_array = &faces[0];
Now to see if this works with directx...
Edit: it does :)