Visual Studio + GLFW + GLAD – Zhihu (zhihu.com)

Step1. Download the compiled GLFW library

To download: www.glfw.org/download.ht… . \

OpenGL environment configuration generally choose Win32, so this tutorial will be equipped with Win32 platform development environment.

Step2. Download the GLAD library

GLAD is an open source library that solves the cumbersome problem we mentioned above. GLAD’s configuration is slightly different from most open source libraries in that it uses an online service. Here we are able to tell GLAD which version of OpenGL we need to define and load all relevant OpenGL functions based on this version.

Open the GLAD online service, set the Language to C/C++, and in the API options, select the OpenGL(GL) version above 3.3 (we will use 3.3 in the tutorial, but newer versions will work fine). Then set the Profile to Core and make sure the option to Generate a Loader is selected. You can ignore the Extensions for now. When you’re done, click the Generate button to Generate the library file.

Fill in the information as shown below:

Click Generate to finish

The following file is generated:

Click to download the compression package, which contains the following files:

Step3. Configure the Visual Studio project

Create a VC++ empty project :(name it whatever you want) \

Open the project properties screen and configure the following:

  1. The contents of the include file are in the include folder of the glad and GLFW file packages downloaded just now:
  • Note that the second should be opengL /glad/include

  1. The library directory contains the corresponding VS version content (VS2017) in the GLFW folder:

  1. The content of the link is:
opengl32.lib
glfw3.lib
Copy the code

Copy glad. C from the SRC in the GLAD package to the project when the configuration is complete:

To create a CPP file, I created main and the contents are as follows:

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;

void framebuffer_size_callback(GLFWwindow* window, int width, int height);

int main(a) {
    glfwInit(a);glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow *window = glfwCreateWindow(800.600."LearnOpenGL".NULL.NULL);
    if (window == NULL) {
        cout << "Failed to create GLFW window" << endl;
        glfwTerminate(a);return - 1;
    }
    glfwMakeContextCurrent(window);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return - 1;
    }

    glViewport(0.0.800.600);

    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    while (!glfwWindowShouldClose(window)) {
        glfwSwapBuffers(window);
        glfwPollEvents(a); }glfwTerminate(a);return 0;
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
    glViewport(0.0, width, height);
}
Copy the code

If it can run successfully, the configuration is successful, and the running result is as follows: \

Other problems

If the project does not report an error but fails to run, please check whether your project is a 32-bit project: \

Also, ensure that your system supports OpenGL3.3 or later. Otherwise, the application may crash or have unexpected errors. If you want to view the OpenGL version, run GlXInfo on Linux, or use another tool (such as OpenGL Extension Viewer) on Windows. If your OpenGL version is younger than 3.3, check to see if your graphics card supports OpenGL 3.3+ (if not, your graphics card is too old), and update your drivers and graphics card if necessary.