Performance Issue while using QGLWidget with Qt5

921 views Asked by At

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

3

There are 3 answers

3
aadhithyan On BEST ANSWER

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);
}
0
perivesta On

You are using OpenGLs immediate mode which is very slow for large numbers of vertices und should almost never be used. Use the retained mode instead. See this answer for more detail: https://stackoverflow.com/a/6734071

0
Zedka29 On

You are using the fixed pipeline instead of the programmable one, where you tell to each stage of the rendering process, what should be done, and nothing more. Among other noticeable differences that I encourage you to research (research "modern opengl", which will lead you to doing OpenGL 3.3 and above type of work).

The old fixed pipeline is terribly inefficient, when the computer has to talk to the graphics card for every geometries while rendering. By contrast, the modern programmable pipeline allows you to push the data of the models to render into the VRAM, from where it will be directly accessed during rendering (very fast memory accesses).

You also get rid of the generic ways of "doing stuff", that are mechanically slower than customized ones.

Also, I encourage you to use QOpenGLWidget instead of the former QGLWidget class. As mentioned in http://doc.qt.io/qt-5/qglwidget.html, this class is obsolete.

Modern OpenGL quick start:

http://www.opengl-tutorial.org/

So, you are not doing anything "wrong". You are just not using the current technology. Have fun!