Lagging problem

Hey everyone, I’m new at openGL, and I’m currently working on a project that draws about 15 obj files (which I have successfully imported using codes I’ve downloaded) and redraws them every frame (because of animation). However, it’s extremely laggy. It displays like only 2 frames per second. I’m wondering if there’s a way of fixing this? I’m thinking it’s because it takes so long to draw each of the mesh but I dunno how to animate it w/o having to redraw the mesh every frame.

Thanks in advance =D

Hi Powers,

Can you please show some of the code used for the drawing?

Yep thanks :smiley:

Here’s the function that draws the imported .obj files. These actually aren’t my codes, it came w/ the obj importer:

void DrawModelUsingFixedFuncPipeline()
{
const ModelOBJ::Mesh *pMesh = 0;
const ModelOBJ::Material *pMaterial = 0;
const ModelOBJ::Vertex *pVertices = 0;
ModelTextures::const_iterator iter;

for (int i = 0; i < g_model.getNumberOfMeshes(); ++i)
{
    pMesh = &g_model.getMesh(i);
    pMaterial = pMesh->pMaterial;
    pVertices = g_model.getVertexBuffer();

    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, pMaterial->ambient);
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, pMaterial->diffuse);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, pMaterial->specular);
    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, pMaterial->shininess * 128.0f);

    if (g_enableTextures)
    {
        iter = g_modelTextures.find(pMaterial->colorMapFilename);

        if (iter == g_modelTextures.end())
        {
            glDisable(GL_TEXTURE_2D);
        }
        else
        {
            glEnable(GL_TEXTURE_2D);
            glBindTexture(GL_TEXTURE_2D, iter->second);
        }
    }
    else
    {
        glDisable(GL_TEXTURE_2D);
    }

    if (g_model.hasPositions())
    {
        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(3, GL_FLOAT, g_model.getVertexSize(),
            g_model.getVertexBuffer()->position);
    }

    if (g_model.hasTextureCoords())
    {
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glTexCoordPointer(2, GL_FLOAT, g_model.getVertexSize(),
            g_model.getVertexBuffer()->texCoord);
    }

    if (g_model.hasNormals())
    {
        glEnableClientState(GL_NORMAL_ARRAY);
        glNormalPointer(GL_FLOAT, g_model.getVertexSize(),
            g_model.getVertexBuffer()->normal);
    }

    glDrawElements(GL_TRIANGLES, pMesh->triangleCount * 3, GL_UNSIGNED_INT,
        g_model.getIndexBuffer() + pMesh->startIndex);

    if (g_model.hasNormals())
        glDisableClientState(GL_NORMAL_ARRAY);

    if (g_model.hasTextureCoords())
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);

    if (g_model.hasPositions())
        glDisableClientState(GL_VERTEX_ARRAY);
}

}

I don’t completely understand it though. Would it help if I took out some of the texture & lighting stuff? Since I don’t really care about those in this particular program.

Are you using double buffer?

Yep I believe so

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

Do you have something like this in your main function?

Nope, I’ll try that =P

Works like a charm :smiley: Thank you so much :smiley:

Besides that you have to write glutSwapBuffers(); when you finish drawing all the scene :slight_smile: