代码并不长,直接上代码说明
//
// Created by user on 8/12/22.
//
#include "glad/glad.h"#include #include
#include
using namespace std;void framebuffer_size_callback(GLFWwindow *window, int width, int height);void processInput(GLFWwindow *window) {if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)glfwSetWindowShouldClose(window, true);
}int main() {glfwInit();glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endifGLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);if (window == nullptr) {std::cout << "Failed to create GLFW window" << std::endl;glfwTerminate();return -1;}glfwMakeContextCurrent(window);if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {std::cerr << "gladLoadGLLoader failed\n";return -1;}glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);for (; !glfwWindowShouldClose(window);) {processInput(window);glClearColor(0.8f, 0.3f, 0.3f, 1.0f);glClear(GL_COLOR_BUFFER_BIT);glfwSwapBuffers(window);glfwPollEvents();}glfwTerminate();return 0;
}void framebuffer_size_callback(GLFWwindow *window, int width, int height) {glViewport(0, 0, width, height);
}
其中:
#include "glad/glad.h"
#include
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwInit
初始化 glfw 库,在调用任何 glfw 函数前都应该先做初始化glfwWindowHint
用来配置 glfw。上述代码告诉 glfw 我们要使用 OpenGL 3.3 版本,且希望是 Core-profileGLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);
if (window == nullptr) {std::cout << "Failed to create GLFW window" << std::endl;glfwTerminate();return -1;
}
glfwMakeContextCurrent(window);
glfwCreateWindow
创建窗口。800 表示宽,600 表示高,“LearnOpenGL” 为窗口标题。glfwCreateWindow
同时也会创建 OpenGL contextglfwMakeContextCurrent(window)
设置当前线程的 OpenGL contextif (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {std::cerr << "gladLoadGLLoader failed\n";return -1;
}
gladLoadGLLoader
初始化 GLADvoid framebuffer_size_callback(GLFWwindow *window, int width, int height) {glViewport(0, 0, width, height);
}
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glViewport
用来调整渲染窗口的大小。glViewport
函数前两个参数控制窗口左下角的位置。第三个和第四个参数控制渲染窗口的宽度和高度(像素)。framebuffer_size_callback
注册到窗口中,使得当窗口大小发生变化时,glfw 能够回调该函数从而修改渲染窗口的大小void processInput(GLFWwindow *window) {if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)glfwSetWindowShouldClose(window, true);
}for (; !glfwWindowShouldClose(window);) {processInput(window);glfwSwapBuffers(window);glfwPollEvents();
}
glfwWindowShouldClose
如果该函数返回true然后渲染循环便结束了,之后为我们就可以关闭应用程序了processInput
处理用户输入,如果用户按下 esc 键,也会退出glfwSwapBuffers
函数会交换颜色缓冲(它是一个储存着GLFW窗口每一个像素颜色值的大缓冲),它在这一迭代中被用来绘制,并且将会作为输出显示在屏幕上glfwPollEvents
函数检查有没有触发什么事件(比如键盘输入、鼠标移动等)、更新窗口状态,并调用对应的回调函数(可以通过回调方法手动设置)glfwTerminate();
glfwTerminate
释放/删除之前的分配的所有资源虽然这个例子很简单,但已经有了 OpenGL 程序的基本框架了,步骤如下: