OpenGL Distilled

7.2. Versions

The OpenGL specification has been revised several times since its inception in 1992. The OpenGL ARB assigns a version number to each major revision and publishes its specification. Download current and past OpenGL specifications at the OpenGL Web site: http://www.opengl.org. Appendices C through I of The OpenGL Graphics System summarize the changes to each version of the specification.

Unfortunately, there is significant lag time between the approval of a new version of the specification and widespread industry adoption of that new version. As a result, most multiplatform OpenGL applications must support several OpenGL versions at runtime. To accomplish this, employ the tactics described in the section "Extensions" earlier in this chapter. To reiterate:

  • Use a development environment that contains definitions and declarations corresponding to the latest OpenGL version that your application requires.

  • Check for the supported version at runtime using glGetString ( GL_VERSION ).

  • Conditionally obtain version-specific entry points, and execute version-specific code only if the version is supported in the runtime environment.

In concept, version-specific code is essentially the same as extension-specific code.

Your development environment must contain the definitions and declarations for the latest OpenGL version that your application uses. You can't compile code that uses OpenGL version 2.0 features if your development environment doesn't contain the version 2.0 enumerant definitions and command entry-point prototypes. If your OpenGL vendor doesn't provide a development environment for the latest version, obtain glext.h from the OpenGL Extension Registry Web site.

Although glext.h allows you to compile new code, linking is still an issue because the linker can't resolve the latest version-specific entry points if the OpenGL stub library is out of date. Fortunately, the same mechanism available for obtaining extension entry points at runtime also lets your application obtain any entry point in the underlying OpenGL implementation. This allows you to declare function pointers to current OpenGL features and to obtain function addresses by using dlsym(), glXGetProcAddress(), or wglGetProcAddress() just as though it were an extension entry point.

In fact, if you intend to deploy your application in Microsoft Windows, your application must employ this technique for all OpenGL features after version 1.1.[2] The reason is that Microsoft operating systems include only OpenGL version 1.1 support in opengl32.dll. Correspondingly, the opengl32.lib stub library also contains only version 1.1 symbols.

[2] At the WinHEC 2005 conference, Microsoft announced plans to upgrade base OpenGL support to version 1.4 in a future release of its operating system.

Chapter 1, "An Introduction to OpenGL," contains example code for checking the OpenGL runtime version using glGetString( GL_VERSION ). The OGLDif class in the example code cooks the OpenGL version string down to a simple enumerant, such as Ver14 if the OpenGL runtime version is 1.4. This enumerant allows fast runtime version checking. The following code snippet, for example, shows how to execute cube map code conditionally only if the OpenGL runtime version is 1.3 or later:

if (ogld::OGLDif::instance()->getVersion() >= ogld::Ver13) // Use cube mapping

The OGLDif class uses the singleton design pattern. It calls glGetString( GL_VERSION ) only in its constructor. The getVersion() method simply returns the cooked version enumerant.

Finally, the example code ogld namespace declares function pointers for all OpenGL entry points added after version 1.1, and the OGLDif constructor initializes them if the runtime version supports the entry point.

Категории