Why My Program Is Running Slow Whenever I Use Heavy OBJ Files?

Hello Professionals,

Good day. I am trying to use different OBJ files to my program in order to read the details of OBJ files (particularly the faces and vertices), then I try to view them using the OpenGL viewer. When I use basic OBJs such as dodecahedron, it works okay (not slow), but when I am using bunny, armadillo, etc., it is extremely slow. What should I do to improve my program?

It would probably be easier to help you if you posted the code that you were using for loading your file on to the GPU, as well as the code that you’re using to render it.

Firstly, using more detailed models will always be slower than using less detailed models, because your computer must do more work to draw them. Assuming that you’re doing everything else correctly, there may be nothing you can do about this. It woudl be expected behaviour, in other words.

Secondly, it may be the case that you’re not doing everything else correctly. You mention .obj files, so that suggests that there is a high probability that you are using old-fashioned glBegin/glEnd code (and this is the kind of information that you really should be giving in your original post). If so, you should be aware that glBegin/glEnd code is actually the slowest code path in OpenGL. Bu you’ll really need to confirm what you’re doing before anybody can say any more or give you pointers for the correct direction to go in.

[QUOTE=mhagain;1290717]Firstly, using more detailed models will always be slower than using less detailed models, because your computer must do more work to draw them. Assuming that you’re doing everything else correctly, there may be nothing you can do about this. It woudl be expected behaviour, in other words.

Secondly, it may be the case that you’re not doing everything else correctly. You mention .obj files, so that suggests that there is a high probability that you are using old-fashioned glBegin/glEnd code (and this is the kind of information that you really should be giving in your original post). If so, you should be aware that glBegin/glEnd code is actually the slowest code path in OpenGL. Bu you’ll really need to confirm what you’re doing before anybody can say any more or give you pointers for the correct direction to go in.[/QUOTE]

Oh Yes, I am using the glBegin/glEnd pattern. I actually don’t know how will I make it more faster. If the glBegin/glEnd is the “slowest” code path, is there any faster alternative method for this?

glDrawElements. Preferably with the data in vertex buffer objects (VBOs).

Yes, this basically.

For the benefit of the OP: there are a number of places where code to draw objects might run slow. These include:

(1) Sending drawing commands from the CPU to the GPU.
(2) Transforming vertices.
(3) Shading fragments.

In the case of glBegin/glEnd code, you’re going to be slow on number (1) here: sending drawing commands from the CPU to the GPU. There are a number of reasons for this, including that a huge number of OpenGL calls are needed, that a large number of primitives (i.e. triangles, quads, fans, strips, etc) are specified, and that data must be re-sent to the GPU each frame, whether it changes or not.

In worst case scenarios a program could completely bottleneck on these commands, so that you never see any performance, even if you’ve otherwise got the fastest GPU money can buy. This is one reason why, for example, older games might not run faster on new GPUs.

OpenGL provides a few ways to fix this. Older versions of OpenGL (1.1) provided vertex arrays, which allowed objects to be drawn with very few commands: a handful of commands to set up data sources, then a single command to draw, and maybe a handful more to switch everything off when done. In extreme cases, the overhead to draw an object might change from tens or hundreds of thousands of OpenGL calls to no more than 20 or so.

Using this approach we still have the bottleneck where all of the data must be sent and re-sent from the CPU to the GPU each frame, even if it doesn’t change. So a slightly less old version of OpenGL (1.5) introduced buffer objects, of which the first type was the vertex buffer object, which allowed vertex data to stay resident on the GPU (and gave you more control over when in the frame data that needs to change gets sent).

All of this is a total re-write of your drawing code, and is not something that I or anybody else can easily guide you through over the course of a few forum responses. You need to go looking for tutorials, in other words. If you have difficulties with them, then some more specific and focussed questions would be something we could help with.