1

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
aadhithyan
  • 49
  • 10

3 Answers3

2

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!

Zedka29
  • 127
  • 4
1

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

perivesta
  • 3,417
  • 1
  • 10
  • 25
0

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);
}
aadhithyan
  • 49
  • 10
  • Thank you for your feedback. I am glad you found this way around. Just know it is a short term solution, and I strongly encourage you to switch to working with a core OpenGL context :) – Zedka29 Nov 20 '18 at 16:04
  • @Zedka29 what do you mean by core OpenGL Context (like vertex and fragment shaders or something else) ? Can you please explain in short or share the link for the content ? – aadhithyan Nov 21 '18 at 06:38
  • I mean, you should not use anything that has been marked as deprecated, i.e only use the OpenGL 3.3 and above API. No more glEnableClientState, no more glPushMatrix, glPopMatrix. Which leads you to be on your own when it comes to linear algebra and matrix stacks. A known math library for that purpose is glm. Have a look at the link I gave you (the tutorial). It is only teaching you the current way of doing things. You'll see that the syntax of GLSL changes a bit too. – Zedka29 Nov 21 '18 at 12:43