OpenGL environment construction [turn]
table of Contents
Download GLFW
- Download link: https://www.glfw.org/download.html
- Unzip, then create a new build folder in the folder, and then use
- Use Cmake
- Source directory: the directory where you just downloaded and unzipped the GLFW
- Output directory: the build directory newly created in the decompression directory just now
- Then click Configure in the lower left corner of CMake, and click Finish should be as shown in the figure:
- If you click Configure here, there are three reasons why you don't see the picture like this: 1. The GLFW download is wrong. 2. The source directory and target directory are configured incorrectly. 3. The CMake download is wrong.
- After all issues are resolved, click Generate .
- Then go to our new Build directory, which is the output directory, find GLFW.sln, and open it with VS2019 (other versions are also available, the official tutorial uses 2015).
- Then you can compile. After compiling , the glfw3.lib file will be generated in the src/Debug directory .
- [Note] Placed in 32nd place
Establish a test project case
- After opening VS and creating a new project, you need to configure three places:
- Include directory: include directory of the downloaded GLFW
- Library directory: the src/Debug directory under the build directory we generated
- The glfw3.lib will be generated in the src/Debug directory of the build directory. The last step is as follows:
Download Glad
- Address: https://glad.dav1d.de/
- Click GENERATE in the lower right corner, select the .zip file, download and unzip it.
- After decompression, copy all the files in his include folder to the include folder we configured above, and copy the .c file directly to the project.
- Click GENERATE in the lower right corner, select the .zip file, download and unzip it.
- After decompression, copy all the files in his include folder to the include folder we configured above , and copy the .c file directly to the project.
- Run it and it will be done without error
- Test code:
#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() { glfwInit(); 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(); 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(); } glfwTerminate(); return 0; } void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); }