# learn-opengl **Repository Path**: zmx0142857/learn-opengl ## Basic Information - **Project Name**: learn-opengl - **Description**: learn opengl - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-06-15 - **Last Updated**: 2024-02-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: demo ## README # Learn OpenGL https://learnopengl.com/Getting-started/OpenGL ## quick start 安装 GLFW, 这个库用于绘制窗口. 类似的库还有 GLUT, SDL, SFML 等. $ sudo pacman -S glfw-x11 安装 OpenGL 发行版, 如 Mesa, NVidia, AMD 的任一个. $ sudo pacman -S mesa 现在 `/usr/lib` 目录下应该有 `libglfw.so` 和 `libGL.so` 两个文件. 运行 `glxinfo | grep version` 查看 OpenGL 版本, 确保为 3.3 或更高. 下载 GLM (OpenGL Mathematics) 库, 然后把 `glm/glm` 目录拷贝到 `learn-opengl/include` 目录中. $ git clone https://github.com/g-truc/glm --depth=1 编译并运行: $ make # 编译 $ bin/main # 运行 ## starter code ### GLAD 下载 GLAD: 打开 https://glad.dav1d.de/, 选择 - Language: C/C++ - Specification: OpenGL - API: gl: Version 3.3 或者更高 - Profile: Core - Options: Generate a loader 生成并下载 `glad.zip` (`include` 和 `src` 目录已经包含在内). ```text glad ├── include │   ├── glad │   │   └── glad.h │   └── KHR │   └── khrplatform.h ├── Makefile └── src ├── glad.c └── main.cpp ``` ### 编写代码 `Makefile` ```Makefile CC = g++ IDIR = include CFLAGS = -I$(IDIR) LINKS = -lglfw -lGL -lX11 -lpthread -lXrandr -lXi -ldl # $@ 表示目标 # $^ 表示所有依赖 bin/main: obj/main.o obj/glad.o mkdir -p bin $(CC) $(LINKS) -o $@ $^ obj/glad.o: src/glad.c mkdir -p obj $(CC) $(CFLAGS) -c -o $@ $^ obj/main.o: src/main.cpp mkdir -p obj $(CC) $(CFLAGS) -c -o $@ $^ ``` `src/main.cpp` ```c++ #include #include int main() { return 0; } ``` > Be sure to include GLAD before GLFW. The include file for GLAD includes > the required OpenGL headers behind the scenes (like GL/gl.h) so be sure > to include GLAD before other header files that require OpenGL (like > GLFW). 运行 `make`, 尝试编译项目. ### 一个黑窗口 继续修改代码: `src/main.cpp` ```c++ #include #include #include const int width = 800; const int height = 600; // 窗口大小改变时的回调 void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); } int main() { glfwInit(); // 配置 GLFW glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // for MacOS // 创建窗口 GLFWwindow* window = glfwCreateWindow(width, height, "LearnOpenGL", NULL, NULL); if (!window) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); // 初始化 GLAD if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failed to initialize GLAD" << std::endl; return -1; } // 初始化 viewport glViewport(0, 0, width, height); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); // 既然是图形程序, 就要有消息循环 while (!glfwWindowShouldClose(window)) { glfwSwapBuffers(window); // 双缓冲 glfwPollEvents(); // 派发事件 } glfwTerminate(); return 0; } ``` 编译并运行, 如果一切顺利, 将出现一个黑窗口. ### 输入控制与渲染 `src/main.cpp` ```c++ void processInput(GLFWwindow *window) { // 按下 esc 关闭窗口 if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); } void render(GLFWwindow *window) { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); // rgba glClear(GL_COLOR_BUFFER_BIT); // or GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT } while (!glfwWindowShouldClose(window)) { + processInput(window); // 处理输入 + render(window); // 渲染 glfwSwapBuffers(window); // 双缓冲 glfwPollEvents(); // 派发事件 } ``` 这个程序将得到一个蓝色背景.