Using modern OpenGL - hls333555/OpenGL GitHub Wiki

If we want to call OpenGL functions inside our c++ code, we need to get them from somewhere. Since OpenGL functions are implemented inside graphics drivers, if we want to use any kind of functionality that is newer than OpenGL 1.1, we need to get into those drivers, pull out the functions and call them - basically is to get the function declarations and then link against the functions as well, so we need to access the driver dll files and retrieve function pointers to the functions inside those libraries.

GLEW is a library which provides an OpenGL API specification - function declarations, constants etc. The behind-the-scene implementation of the library - those c/cpp files will identify what graphics driver you are using, find the appropriate dll file and load all the function pointers. The library does not implement those OpenGL functions, it just access the functions that are already on the computer in binary form.

Here are the main steps:

  • Download GLEW precompiled binaries from GLEW. (If you want to create a game engine or a series of project utilizing OpenGL, you can download the source code instead of the binaries)
  • Copy and paste GLEW files into our "Dependencies" folder
  • Add include path, lib path and lib file to the project configuration like above
  • Navigate to GLEW Documentation to see how to use it

The followings are the issues you may encounter:

  • If you put #include <GL/glew.h> below #include <GLFW/glfw3.h> and compile, an error, saying gl.h included before glew.h, will occur.

    To fix this, simply exchange the include order

  • If you try to call glewInit();, you will get a link error.

    You can dig into this by navigating to its definition in glew.h where you will find a GLEWAPI macro defining the return type, after jumping to its definition, you will find the reason that you did not define the GLEW_STATIC macro.

    image

    To fix this, just add GLEW_STATIC to Preprocessor Definitions

  • If you put glewInit() above glfwCreateWindow(), and replace it with the following code to check whether GLEW has initialized successfully, you will see the error illustrated below:

    if (glewInit() != GLEW_OK)
    {
        std::cout << "Glew Init Error!" << std::endl;
    }

    If you take a look at the GLEW Documentation, you will find the answer:

    First you need to create a valid OpenGL rendering context and call glewInit() to initialize the extension entry points.

    To fix this, just move the above code below glfwMakeContextCurrent().

⚠️ **GitHub.com Fallback** ⚠️