parsing .obj and loading in directx 9

Started by
2 comments, last by Amr0 9 years, 5 months ago

so i have parsed all the data in the obj file and stored them in vectors

std::vector<DWORD> indices;
std::vector<D3DXVECTOR3> vertPos;
std::vector<D3DXVECTOR3> vertNorm;
std::vector<D3DXVECTOR2> vertTexCoord;

I am abit stuck creating the VertexBuffer from the vector and also the indexbuffer using these functions

d3ddev->CreateIndexBuffer()
d3ddev->CreateVertexBuffer()

i know most of what the params are, i just dont know how to store the actual data in to them from vectors

for loading your own custom cube for example you do


   // create an index buffer interface called i_buffer
    d3ddev->CreateIndexBuffer(36*sizeof(short),
                              0,
                              D3DFMT_INDEX16,
                              D3DPOOL_MANAGED,
                              &i_buffer,
                              NULL);

    // lock i_buffer and load the indices into it
    i_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, indices, sizeof(indices));
    i_buffer->Unlock();

 // create a vertex buffer interface called v_buffer
    d3ddev->CreateVertexBuffer(24*sizeof(CUSTOMVERTEX),
                               0,
                               CUSTOMFVF,
                               D3DPOOL_MANAGED,
                               &v_buffer,
                               NULL);

    VOID* pVoid;    // a void pointer

    // lock v_buffer and load the vertices into it
    v_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, vertices, sizeof(vertices));
    v_buffer->Unlock();

my attempt would be

thanks if you can help

:)
Advertisement

First: don't hard-code numbers! E.g.,


... 24*sizeof(CUSTOMVERTEX) ... // no
// instead use
... numVerts * sizeof( ... ) ... // yes

Even better, calculate the size you need for both CreateVertexBuffer and memcpy.


int numBytesInBuffer = numVerts * sizeof(CUSTOMVERTEX);

... CreateVertexBuffer( numBytesInBuffer, ... );
...
memcpy(pVoid, vertices, numBytesInBuffer );

With regard to your unasked question: assuming you're asking about "i just dont know how to store the actual data in to them from vectors" :

1. Check that all the data you imported is consistent. I.e., vertPos.size() == vertNorm.size(), and vertPos.size() == vertTexCoord.size()

2. Create a std::vector<CUSTOMVERTEX> vertices;

Then, something like:


CUSTOMVERTEX vert;
for(size_t v = 0; v < vertPos.size(); ++v)
{
   vert.pos = vertPos[v];
   vert.norm = vertNorm[v];
   vert.tc = vertTexCoords[v];
   vertices.push_back(vert);
}
int numBytesInBuffer = vertices.size() * sizeof(CUSTOMVERTEX);
...
std::vector<int16_t> idx;
for(size_t i=0; i < indices.size(); ++i)
{
   idx.push_back( (int16_t) indices[i] );
}
// create your vertex and index buffer
// then
memcpy( pVoid, &vertices[0], numBytesInBuffer );
// similarly for your index buffer

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

The algorithm is available in the DirectX June 2010 SDK, where the obj file's format is implemented.

You would simply use it.

The algorithm is available in the DirectX June 2010 SDK, where the obj file's format is implemented.

You would simple use it.

The implementation in the DX SDK is flawed - it breaks on most obj files.

I have written a DX9 obj loader in the past which works with all obj files I've tested it with, including files exporting from Maya and zBrush. If I remember correctly though, it doesn't create an index buffer and instead copies vertices when needed in the vertex buffer. The point was to test the obj loader and not the translation from obj to DX, so I'm not sure how useful it would be to you. Here it is anyway: http://code-section.com/entry/13/cdx9-obj-loader-sample

This topic is closed to new replies.

Advertisement