首先说一下自学的网址: https://learnopengl-cn.github.io/ 这个也是主要资料。
1.GLFW下载与配置:
下载地址:https://www.glfw.org/download.html
建议下载32位的,据说64位会有很多问题所以我就先不用了。
下载后里面有很多VS版本的类库,至于用哪一个就看使用的VS版本了。
打开VS创建一个空的C++项目,然后再解决方案中右击项目打开属性页。
2.GLAD下载与配置:
打开网址 http://glad.dav1d.de/ 按以下配置进行选择:
选完后
在生成后的页面下载生成后.zip文件:
把解压后的include里面的两个文件夹复制到glfw的include文件夹下,当然你也可以再创建个项目进行外链。
把src下的文件glad.c复制到自己的项目中去.按以下步骤把glfw3.lib和opengl32.lib加入链接器。
3.测试:
创建.cpp文件把下面代码复制进去,看是否出现了窗口。
#include <glad/glad.h> #include <GLFW/glfw3.h> #include<iostream> using namespace std; void framebuffer_size_callback(GLFWwindow* window, int width, int height); void processInput(GLFWwindow *window); int main() { glfwInit(); //设置glfw的版本为3.3 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) { std::cout << "Failed to create GLFW window" << std::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)) { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // 输入 processInput(window); // 渲染指令 // 检查并调用事件,交换缓冲 glfwPollEvents(); glfwSwapBuffers(window); } glfwTerminate(); return 0; } //当窗口改变时调用 void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); } //给窗体注册输入事件 void processInput(GLFWwindow *window) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { glfwSetWindowShouldClose(window, true); } }