Using OpenGL extensions On Windows

后端 未结 4 429
独厮守ぢ
独厮守ぢ 2020-12-13 05:51

I want to use the functions exposed under the OpenGL extensions. I\'m on Windows, how do I do this?

相关标签:
4条回答
  • 2020-12-13 05:59

    @Kronikarz: From the looks of it, GLEW seems to be the way of the future. NVIDIA already ships it along with its OpenGL SDK. And its latest release was in 2007 compared to GLEE which was in 2006.

    But, the usage of both libraries looks almost the same to me. (GLEW has an init() which needs to be called before anything else though.) So, you don't need to switch unless you find some extension not being supported under GLEE.

    0 讨论(0)
  • 2020-12-13 06:04

    GL3W is a public-domain script that creates a library which loads only core functionality for OpenGL 3/4. It can be found on github at:

    https://github.com/skaslev/gl3w

    GL3W requires Python 2.6 to generate the libraries and headers for OpenGL; it does not require Python after that.

    0 讨论(0)
  • 2020-12-13 06:23

    Easy solution: Use GLEW. See how here.

    Hard solution: If you have a really strong reason not to use GLEW, here's how to achieve the same without it:

    Identify the OpenGL extension and the extension APIs you wish to use. OpenGL extensions are listed in the OpenGL Extension Registry.

    Example: I wish to use the capabilities of the EXT_framebuffer_object extension. The APIs I wish to use from this extension are:

    glGenFramebuffersEXT()
    glBindFramebufferEXT()
    glFramebufferTexture2DEXT()
    glCheckFramebufferStatusEXT()
    glDeleteFramebuffersEXT()
    

    Check if your graphic card supports the extension you wish to use. If it does, then your work is almost done! Download and install the latest drivers and SDKs for your graphics card.

    Example: The graphics card in my PC is a NVIDIA 6600 GT. So, I visit the NVIDIA OpenGL Extension Specifications webpage and find that the EXT_framebuffer_object extension is supported. I then download the latest NVIDIA OpenGL SDK and install it.

    Your graphic card manufacturer provides a glext.h header file (or a similarly named header file) with all the declarations needed to use the supported OpenGL extensions. (Note that not all extensions might be supported.) Either place this header file somewhere your compiler can pick it up or include its directory in your compiler's include directories list.

    Add a #include <glext.h> line in your code to include the header file into your code.

    Open glext.h, find the API you wish to use and grab its corresponding ugly-looking declaration.

    Example: I search for the above framebuffer APIs and find their corresponding ugly-looking declarations:

    typedef void (APIENTRYP PFNGLGENFRAMEBUFFERSEXTPROC) (GLsizei n, GLuint *framebuffers); for GLAPI void APIENTRY glGenFramebuffersEXT (GLsizei, GLuint *);
    

    All this means is that your header file has the API declaration in 2 forms. One is a wgl-like ugly function pointer declaration. The other is a sane looking function declaration.

    For each extension API you wish to use, add in your code declarations of the function name as a type of the ugly-looking string.

    Example:

    PFNGLGENFRAMEBUFFERSEXTPROC glGenFramebuffersEXT;
    PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebufferEXT;
    PFNGLFRAMEBUFFERTEXTURE2DEXTPROC glFramebufferTexture2DEXT;
    PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC glCheckFramebufferStatusEXT;
    PFNGLDELETEFRAMEBUFFERSEXTPROC glDeleteFramebuffersEXT;
    

    Though it looks ugly, all we're doing is to declare function pointers of the type corresponding to the extension API.

    Initialize these function pointers with their rightful functions. These functions are exposed by the library or driver. We need to use wglGetProcAddress() function to do this.

    Example:

    glGenFramebuffersEXT = (PFNGLGENFRAMEBUFFERSEXTPROC) wglGetProcAddress("glGenFramebuffersEXT");
    glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC) wglGetProcAddress("glBindFramebufferEXT");
    glFramebufferTexture2DEXT = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC) wglGetProcAddress("glFramebufferTexture2DEXT");
    glCheckFramebufferStatusEXT = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC) wglGetProcAddress("glCheckFramebufferStatusEXT");
    glDeleteFramebuffersEXT = (PFNGLDELETEFRAMEBUFFERSEXTPROC) wglGetProcAddress("glDeleteFramebuffersEXT");
    

    Don't forget to check the function pointers for NULL. If by chance wglGetProcAddress() couldn't find the extension function, it would've initialized the pointer with NULL.

    Example:

    if (NULL == glGenFramebuffersEXT || NULL == glBindFramebufferEXT || NULL == glFramebufferTexture2DEXT
        || NULL == glCheckFramebufferStatusEXT || NULL == glDeleteFramebuffersEXT)
    {
        // Extension functions not loaded!
        exit(1);
    }
    

    That's it, we're done! You can now use these function pointers just as if the function calls existed.

    Example:

    glGenFramebuffersEXT(1, &fbo);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorTex[0], 0);
    

    Reference: Moving Beyond OpenGL 1.1 for Windows by Dave Astle — The article is a bit dated, but has all the information you need to understand why this pathetic situation exists on Windows and how to get around it.

    0 讨论(0)
  • 2020-12-13 06:24

    A 'Very strong reason' not to use GLEW might be that the library is not supported by your compiler/IDE. E.g: Borland C++ Builder.

    In that case, you might want to rebuild the library from source. If it works, great, otherwise manual extension loading isnt as bad as it is made to sound.

    0 讨论(0)
提交回复
热议问题