When do I need to use an OpenGL function loader?

后端 未结 3 963
北恋
北恋 2020-12-03 08:16

I\'m a bit confused about when exactly I need to use an OpenGL function loader like GLEW. In general, it seems like you first obtain a window and valid OpenGL context and th

3条回答
  •  误落风尘
    2020-12-03 09:11

    Function loaders are only needed on Windows and Linux. Here's a quick overview of how you build for various OpenGL versions on different platforms.

    Windows

    The Windows development tools only contain headers for OpenGL 1.1. The conspiracy theorists would probably claim that Microsoft is not interested in making the use of OpenGL easy because it wants developers to use a proprietary API instead.

    For anything beyond 1.1, you need to load the entry points dynamically by calling wglGetProcAddress(). Libraries like GLEW provide header files for higher OpenGL versions, and encapsulate the logic for loading the entry points.

    Linux

    I haven't done OpenGL programming on Linux. From what I hear, it requires function loading similar to Windows. I'll defer to @datenwolf's answer for the details.

    Mac OS

    Mac OS supports two main OpenGL feature sets:

    • OpenGL 2.1 with legacy features. This is used by including .
    • OpenGL 3.x and higher, Core Profile only. Used by including .

    In both cases, you don't need any dynamic function loading. The header files contain all the declarations/definitions for the maximum version that can be supported, and the framework you link against (using -framework OpenGL) resolves the function names.

    The maximum version you can use at build time is determined by the platform SDK you build against. By default, this is he platform SDK that matches the OS of your build machine. But you can change it by using the -isysroot build option.

    At runtime, the machine has to run at least the OS matching the platform SDK used at build time, and you can only use features up to the version supported by the GPU. You can get an overview of what version is supported on which hardware on:

    https://developer.apple.com/opengl/capabilities/ http://support.apple.com/en-us/HT202823

    Android, NDK

    With native code on Android, you choose the OpenGL version while setting up the context and surface. Your code then includes the desired header (like or ) and links against the matching libraries. There is no dynamic function loading needed.

    If the target device does not support the version you are trying to use, the context creation will fail. You can have an entry in the manifest that prevents the app from being installed on devices that will not support the required ES version.

    Android, Java

    This is very similar to the NDK case. The desired version is specified during setup, e.g. while creating a GLSurfaceView.

    The GLES20 class contains the definitions for ES 2.0. GLES30 derives from GLES20, and adds the additional definitions for ES 3.0.

    iOS

    Not surprisingly, this is very similar to Mac OS. You include the header file that matches the desired OpenGL ES version (e.g. ), link against the framework, and you're all done.

    Also matching Mac OS, the maximum version you can build against is determined by the platform SDK version you choose. Devices you want to run on then have to use at least the OS version that matches this platform SDK version, and support the OpenGL ES version you are using.

    One main difference is obviously that you cross compile the app on a Mac. iOS uses a different set of platform SDKs with different headers and frameworks, but the overall process is pretty much the same as building for Mac OS.

提交回复
热议问题