Skip to main content
gamedev.net
🔒 Locked

MD2 loader

Started by Thirthe Sep 2, 2008 at 6:51 AM 1 replies 1.7k views
Original Post
Thirthe
Thirthe
So I wanted to make an MD2 loader... Well, I started off easy, by loading only the 1st frame using those horrid glBegin()/glEnd() functions. And it worked! I could load the Q2 models nicely. But then I wanted to render the models with VA (and then later on, VBO) method and now I'm seeing incomplete or corrupted models rendered on the screen. But, I should say, the MD2 models that were actually quite primitive (pyramids, cubes, hex prisms...) I had no problem rendering correctly. screenshot: http://i34.tinypic.com/2vudi7n.jpg code:

    index_num           = model->header->num_tris * 3;
    vertex_num          = model->header->num_vertices * 3;

    indices             = new GLubyte[ index_num ];
    vertices            = new GLfloat[ vertex_num ];
    vertices_original   = new GLfloat[ vertex_num ];
    color               = new GLfloat[ vertex_num ];

    int j;
    for( int i = 0, j = 0; i < model->header->num_tris; ++i )
    {
        indices[j++] = model->triangle.vertexIndices[0];
        indices[j++] = model->triangle.vertexIndices[1];
        indices[j++] = model->triangle.vertexIndices[2];
    }

    // TODO: remember, there can be more than 1 frame
    for( int i = 0, j = 0; i < model->header->num_vertices; ++i )
    {
        vertices_original[j++] = (model->frame[0].vertices.vertex[0] * model->frame[0].scale[0]) + model->frame[0].translate[0];
        vertices_original[j++] = (model->frame[0].vertices.vertex[1] * model->frame[0].scale[1]) + model->frame[0].translate[1];
        vertices_original[j++] = (model->frame[0].vertices.vertex[2] * model->frame[0].scale[2]) + model->frame[0].translate[2];
    }

 mode_primitive  = GL_TRIANGLES;
    coords[0]       = x; // user-defined origin coords
    coords[1]       = y;
    coords[2]       = z;



rendering:

    for( unsigned int i = 0U; i <= vertex_num - 3; i += 3 )
    {
        vertices     = coords[0] + vertices_original;
        vertices[i+1U]  = coords[1] + vertices_original[i+1U];
        vertices[i+2U]  = coords[2] + vertices_original[i+2U];
    }

    glEnableClientState( GL_COLOR_ARRAY );
    glEnableClientState( GL_VERTEX_ARRAY );

    glColorPointer( 3, GL_FLOAT, 0, color );
    glVertexPointer( 3, GL_FLOAT, 0, vertices );

    glDrawElements( mode_primitive, index_num, GL_UNSIGNED_BYTE, indices );

    glDisableClientState( GL_VERTEX_ARRAY );
    glDisableClientState( GL_COLOR_ARRAY );



NOTE: 'vertices_original' stores model's vertices on the local coordinate system 'vertices' stores model's vertices on the global (world) coordinate system and 'coords' are the model's origin coords of the global (world) coordinate system so basically coords + vertices_original positions the object into the world. P.S.: I'm kind of new to 3D, so I'm not sure if I used the phrases correctly :) [Edited by - Thirthe on September 2, 2008 7:11:13 AM]
davek20
davek20
I believe the problem is with your indices. I had the exact same problem when using vertex arrays and VBOs.

You need to change the type of the indices from GLubyte to GLushort (or GLuint if it still is not displaying properly). The GLubyte only goes up to 255, so if there is more than 255 distinct vertices in the model, the indices will overflow and the model will turn into "triangle soup".
Thirthe
Thirthe
davek20: If you will decide to visit Central Europe, please find me so that I can kiss you :D
Thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.