qtopenglqglwidget

Performance Issue while using QGLWidget with Qt5


I'm trying to develop an application which will be used for the visualization of 3D objects and its simulations. In this I have to draw 'n' number of objects (may be a triangle, rectangle or some other non-convex polygons) with individual color shades.For this I'm using QGLWidget in Qt5 (OS - Windows 7/8/10).

structure used for populating objects information:

typedef struct {
  QList<float> r,g,b;
  QList<double> x,y,z;
}objectData;

The number of objects and their corresponding coordinate values will be read from a file.

paintGL function:

void paintGL() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(25, GLWidget::width()/(float)GLWidget::height(), 0.1, 100);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0,0,5,   0,0,0,   0,1,0);
    glRotatef(140, 0.0, 0.0, 1.0);
    glRotatef(95, 0.0, 1.0, 0.0);
    glRotatef(50, 1.0, 0.0, 0.0);
    glTranslated(-1.0, 0.0, -0.6);
    drawObjects(objData, 1000)
}

Drawing of Objects Function:

void drawObjects(objectData objData,int objCnt) {
    glPushMatrix();
    glBegin(GL_POLYGON);
    for(int i = 0; i < objCnt; i++) {
        glColor3f(objData.r[i],objData.g[i],objData.b[i] );
        glVertex3d(objData.x[i],objData.y[i],objData.z[i]);
    }
    glEnd();
    glFlush();
    glPopMatrix();
}

Issue: Now, when the number of objects to be drawn exceeds a certain maximum value (for example say n = 5000), the application speed gradually decreases. I'm unable to use QThread since it already inherits QGLWidget.

Please suggest how to improve the performance of the application when number of objects count is higher. I don't know where I'm doing mistake.

Screenshot of that sample:

Sample image which contains number of objects in mesh view


Solution

  • Thank you @dave and @Zedka9. It works fine for me when I started to use the intermediate mode in openGL. I have modified the drawObject function like this

    Drawing of Objects Function:

    After organizing and copying the vertices and colors to these buffers

    GLfloat vertices[1024*1024],colors[1024*1024];

    int vertArrayCnt; // number of verticies

    void drawObjects(void) {
        glEnableClientState(GL_COLOR_ARRAY);
        glEnableClientState(GL_VERTEX_ARRAY);
        glColorPointer(3, GL_FLOAT, 0, colors);
        glVertexPointer(3, GL_FLOAT, 0, vertices); 
        glPushMatrix();
        glDrawArrays(GL_TRIANGLES, 0, vertArrayCnt); 
        glPopMatrix();
        glDisableClientState(GL_VERTEX_ARRAY);  // disable vertex arrays
        glDisableClientState(GL_COLOR_ARRAY);
    }