Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

What tips or tricks do you have when it comes to making the OpenGL more efficient?

share|improve this question

closed as too broad by bummzack, Byte56 Aug 4 '13 at 23:55

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Are you looking for tips regarding 2D, 3D or both? –  Dennis Munsie Jul 22 '10 at 14:08
    
Any tip will do! –  thyrgle Jul 22 '10 at 18:37

9 Answers 9

Use VBO rather than glStart()/glEnd(). This saves cycles in transferring the data to the GPU's memory.

share|improve this answer
6  
More than that, glBegin and glEnd were deprecated in later revisions and were completely removed (and therefore will not work) in versions 3.x and above. If you request an OpenGL 3.x context you will be unable to use them completely. –  SAHChandler Jul 22 '10 at 6:59
    
DirectX actually forces this upon you, it doesn't have an equivalent of glStart()/glEnd(). –  knight666 Jul 22 '10 at 7:18
    
@SAHChandler I didn't know that. I didn't realized it was deprecated. –  MrValdez Jul 22 '10 at 7:43
2  
@SAHChandler: That sucks! I learned all my drawing with glBegin() and glEnd(). Noooo!!! –  thyrgle Jul 23 '10 at 1:21

These are from Apple's documentation references on OpenGL but every little bit helps right? ;)

OpenGL Performance Optimization : The Basics
Optimizing OpenGL Data Throughput on OS X

(This last one is a sample application)
Vertex Optimization

There are also a very large number of videos available on the subject (Also from Apple) that talk about OpenGL extensions. Some of them are OS X specific, but a few others are more generalized and could most likely be applied to Windows and Linux.

You can find the videos here. You do require iTunes and an Apple account to view them however (but they are free! :D)

share|improve this answer

The standard tip of profile, profile, profile applies just as well here, although the tools are slightly different.

gDEBugger is great at helping you see exactly what's going on under the hood. It's expensive, though.

share|improve this answer
2  
Just wanted to add that it is now free. –  DMan Dec 19 '10 at 18:06

Well, it really, really depends on what you're planning to do, but here are the basics:

  • Your CPU is very precious while your GPU is a hungry beast. Offset as much as you can to the GPU. Nowadays you can do this with excellent libraries like OpenCL, CUDA and Thrust.

  • Do your lighting in a shader. Not only will this help retain your sanity, but it allows you to very cool things like toon shaders and deferred lighting.

  • Particles suck. Even when using OpenGL point sprites.

share|improve this answer

Minimize your state changes. On a lot of platforms, each state change potentially flushes the pipeline. It's much better to batch similar items together and change state once.

share|improve this answer
    
+1, this was what I was going to say –  Oskar Jul 12 '12 at 9:31

Minimize OpenGL commands is the key. So you must batch all drawing commands. For 2d games, it is a good idea to use Altas Texture and draw all sprites sharing the same texture in one opengl command.

share|improve this answer
    
Be careful. Less and bigger batches are useful, but depending on the target platform "too big" batches will decrease performance. –  Andreas Jul 22 '10 at 14:37
glEnable(GL_CULL_FACES);

Simple one liner that applies back face culling. This is excellent for trimming down polygon count. Also look into Frustum/Occlusion culling for reducing poly counts.

Another thing to look into is SIMD for your vectors and quaternions (mostly used in camera rotations). If you don't want to mess with ASM then check out libSIMD which is a great library for handling SIMD in C.

share|improve this answer

Don't use glGet* when you can access local variables, e.g.:

  • Don't query textures size from OpenGL, store them on loading in variables
  • Don't query line widths / points sizes from OpenGL when you can store the changes in your application

These calls are really slow

Move unused stuff outside of glBegin - glEnd clause (be it glColor or glNormal or anything else) if it does not changes inside. OpenGL is a state machine, it uses the last value.

share|improve this answer

Batch, batch, batch!! (Ab)use the GPU to render as much as possible in each draw call. Games are mostly limited by the amount of state changes, not so much by the amount of triangles.

share|improve this answer