Once a program is written, it often can take just as long to optimize the code and data files as it did to write the code in the first place. This is especially true when attempting to improve OpenGL® performance. Many factors can affect performance, from the size and format of texture files to the amount of geometry in a scene, to the rendering options employed during rasterization. Actually adding code can make a program run faster if that code is meant to detect and avoid special functions that are not supported by a graphics card. Good programming practices and an understanding of common OpenGL® performance bottlenecks can go a long way toward improving the speed and quality of an OpenGL® application.
One area in which great improvements to OpenGL® performance can be gained is within the data structures of the application itself. OpenGL® scenes and animation usually require an extensive amount of data trees, data structures and arrays. Employing container structures and search algorithms that use the least time possible while still fulfilling the needs of the application can speed up how quickly data are processed and moved to the graphics processing unit (GPU) for display. Understanding how OpenGL® requires data to be formatted also can help, because some compile-time optimizations could occur depending on the language used.
Texture files are a common area in which OpenGL® performance can be improved. These image files should have pixel dimensions that are only powers of two, even if the hardware does not require it. They also should be optimized inside an image editor to be as small as possible. Generally, animated or moving objects do not need textures that are as detailed as objects that remain still. Using the smallest textures possible without sacrificing too much quality can greatly increase the frame rate.
One common pitfall, especially for new programmers or artists, is to use too much geometry. There are a number of tricks that can help to reduce the number of polygons in a model without sacrificing detail. One mistake often made is to use the geometry of a model to represent details that can much more efficiently be shown in a texture image. Most models are actually very simple, and the complex features are really implemented using bump maps, normals and texture mapping. Simplified, optimized models will increase OpenGL® performance by reducing the number of calculations that need to be performed on each vertex.
For some applications, it can be beneficial to program for the lowest common denominator when it comes to hardware. Some high-end graphics cards implement OpenGL® features that are incredibly exciting but not supported by most other cards. By using few extensions and relying on hardware acceleration only for basic tasks, OpenGL® performance can be improved across nearly all systems, preventing situations in which a certain line of graphics cards are incapable of achieving an acceptable frame rate.
Profiling software also is very important when attempting to enhance OpenGL® performance. A profiler will measure how long it takes to execute each line of code, each function, and each operation within a program. This can be an incredibly effective way to identify a bottleneck's location. If a certain function is taking longer than others, then it can be targeted for optimization. This can sometimes lead to extensive re-writes of certain code segments but, ultimately, it can create a program that runs as smoothly as possible.