OpenGL Loading Library: Difference between revisions
(→GL3W: Changed variable names in example) |
(Added SDK info.) |
||
Line 49: | Line 49: | ||
cout<<GLMajorVer<<"."<<GLMinorVer<<endl; | cout<<GLMajorVer<<"."<<GLMinorVer<<endl; | ||
</source> | </source> | ||
== Unofficial OpenGL SDK == | |||
The [http://glsdk.sourceforge.net/ Unofficial OpenGL SDK] comes with a component for loading OpenGL functions. This component, called GL Load, has a C and C++ interface for loading OpenGL functions. It also provides different headers for different OpenGL versions. It even has headers that remove compatibility enums and functions for versions of OpenGL 3.1 or greater. | |||
Here is a code example: | |||
<source lang="cpp"> | |||
#include <glload/gl_3_3.h> //OpenGL version 3.3, core profile. C-style functions. | |||
#include <glload/gll.h> //The C-style loading interface. | |||
//Include headers for FreeGLUT/GLFW/other GL tools. | |||
int main(int argc, char *argv[]) | |||
{ | |||
//Initialize OpenGL and bind the context | |||
if(LoadFunctions() == LS_LOAD_FAILED) | |||
//exit in some way | |||
//Loading succeeded. Now use OpenGL functions. | |||
//Render stuff | |||
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER); | |||
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER); | |||
... | |||
} | |||
</source> | |||
GL Load even offers special headers for C++ code, that moves as much of OpenGL as possible into a namespace. The equivalent code in the C++ interface is as follows: | |||
<source lang="cpp"> | |||
#include <glload/gl_3_3.hpp> //OpenGL version 3.3, core profile. C++-style functions. | |||
#include <glload/gll.hpp> //The C-style loading interface. | |||
//Include headers for FreeGLUT/GLFW/other GL tools. | |||
int main(int argc, char *argv[]) | |||
{ | |||
//Initialize OpenGL and bind the context | |||
if(glload::LoadFunctions() == glload::LS_LOAD_FAILED) | |||
//exit in some way | |||
//Loading succeeded. Now use OpenGL functions. | |||
//Render stuff | |||
GLuint vertShader = gl::CreateShader(gl::GL_VERTEX_SHADER); | |||
GLuint fragShader = gl::CreateShader(gl::GL_FRAGMENT_SHADER); | |||
... | |||
} | |||
</source> | |||
[[Category:Related Toolkits & APIs]] | [[Category:Related Toolkits & APIs]] |
Revision as of 22:10, 1 October 2011
An Extension Loading Library is a library that loads pointers to OpenGL functions at runtime, core as well as extensions. This is required to access functions from OpenGL versions above 1.1 on most platforms. Extension loading libraries also abstracts away the difference between the loading mechanisms on different platforms.
Most extension loading libraries override the need to include gl.h at all. Instead, they provide their own header that must be used. Most extension loading libraries use code generation to construct the code that loads the function pointers and the included headers.
GLEW
The OpenGL Extension Wrangler library provides access to all GL entrypoints. It supports Windows, MacOS X, Linux, and FreeBSD.
GLEW's problem is that it calls glGetString(GL_EXTENSIONS) which causes GL_INVALID_ENUM on GL 3.2 forward compatible context as soon as glewInit() is called. It also doesn't fetch the function pointers. The solution is for GLEW to use glGetStringi instead. The current version of GLEW is 1.6.0 but they still haven't corrected it. The only fix is to use glewExperimental for now :
glewExperimental=TRUE;
GLenum err=glewInit();
if(err!=GLEW_OK)
{
//Problem: glewInit failed, something is seriously wrong.
cout<<"glewInit failed, aborting."<<endl;
}
You might still get GL_INVALID_ENUM (depending on the version of GLEW you use), but at least GLEW ignores glGetString(GL_EXTENSIONS) and gets all function pointers.
If you are creating a GL context the old way or if you are creating a backward compatible context for GL 3.2+, then you don't need glewExperimental.
GLee
Currently seems defunct. Hasn't been updated in a while.
GL3W
The GL3W library focuses on the core profile of OpenGL 3 and 4. It only loads the core entrypoints for these OpenGL versions (and the extensions, of course). It supports Windows, Linux, and FreeBSD.
GL3W relies on Python 2.6 for its code generation. Unlike other extension loaders, GL3W actually does the code generation on your machine. This is based on downloading and parsing the gl3.h file from the OpenGL Registry website.
On the one hand, this means that it is always up-to-date, more or less. On the other hand, this also makes it beholden to the format of gl3.h (which has no true format), as well as requiring that the user of GL3W have a Python 2.6 installation.
GL3W is used like this
//****Create a GL 3.x or GL 4.x core context
//****Make the context current (for example, on Windows you need to call wglMakeCurrent())
//****Now let's have GL3W get the GL function pointers
GLint GLMajorVer, GLMinorVer;
if(gl3wInit())
{
cout<<"GL3W failed to get GL function pointers."<<endl;
return;
}
glGetIntegerv(GL_MAJOR_VERSION, &GLMajorVer);
glGetIntegerv(GL_MINOR_VERSION, &GLMinorVer);
cout<<GLMajorVer<<"."<<GLMinorVer<<endl;
Unofficial OpenGL SDK
The Unofficial OpenGL SDK comes with a component for loading OpenGL functions. This component, called GL Load, has a C and C++ interface for loading OpenGL functions. It also provides different headers for different OpenGL versions. It even has headers that remove compatibility enums and functions for versions of OpenGL 3.1 or greater.
Here is a code example:
#include <glload/gl_3_3.h> //OpenGL version 3.3, core profile. C-style functions.
#include <glload/gll.h> //The C-style loading interface.
//Include headers for FreeGLUT/GLFW/other GL tools.
int main(int argc, char *argv[])
{
//Initialize OpenGL and bind the context
if(LoadFunctions() == LS_LOAD_FAILED)
//exit in some way
//Loading succeeded. Now use OpenGL functions.
//Render stuff
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
...
}
GL Load even offers special headers for C++ code, that moves as much of OpenGL as possible into a namespace. The equivalent code in the C++ interface is as follows:
#include <glload/gl_3_3.hpp> //OpenGL version 3.3, core profile. C++-style functions.
#include <glload/gll.hpp> //The C-style loading interface.
//Include headers for FreeGLUT/GLFW/other GL tools.
int main(int argc, char *argv[])
{
//Initialize OpenGL and bind the context
if(glload::LoadFunctions() == glload::LS_LOAD_FAILED)
//exit in some way
//Loading succeeded. Now use OpenGL functions.
//Render stuff
GLuint vertShader = gl::CreateShader(gl::GL_VERTEX_SHADER);
GLuint fragShader = gl::CreateShader(gl::GL_FRAGMENT_SHADER);
...
}