参考文档:主页 - LearnOpenGL CN (learnopengl-cn.github.io)
GLFW:是一个专门针对OpenGL的C语言库,它提供了一些渲染物体所需的最低限度的接口。它允许用户创建OpenGL上下文、定义窗口函数以及处理用户输入。
GLAD:由于OpenGL驱动版本众多,它大多数函数的位置都无法在编译时确定下来,需要在运行时查询。所以任务就落在了开发者身上,开发者需要在运行时获取函数地址并将其保存在一个函数指针中供以后使用。幸运的是,有些库能简化此过程,其中GLAD是目前最新,也是最流行的库。
第一步:输出黑色窗口
在初始化glad之前要先用glfwCreateWindow函数创建窗口对象
源代码如下:
# include <iostream>
# include <glad/glad.h>
# include <GLFW/glfw3.h>
using namespace std;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
int main()
{
//对glfw进行初始化和配置
//----------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//用glfw创建窗口
//--------------
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
cout << "Failed to create GLFW window" << endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
//每当调整窗口大小的时候告诉glfw调用framebuffer_size_callback函数
//---------------------------------------------------------------
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
//用glad加载所有OpenGL函数指针
//----------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
cout << "Failed to initialize GLAD" << endl;
return -1;
}
glViewport(0, 0, 800, 600);
//渲染循环
//--------
while (!glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
//glfw:无论何时窗口大小改变(通过操作系统或是用户改变)这个回调函数执行
//---------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
第二步:把屏幕设置为了类似黑板的深蓝绿色
源代码如下:
# include <iostream>
# include <glad/glad.h>
# include <GLFW/glfw3.h>
using namespace std;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
int main()
{
//对glfw进行初始化和配置
//----------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//用glfw创建窗口
//--------------
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
cout << "Failed to create GLFW window" << endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
//每当调整窗口大小的时候告诉glfw调用framebuffer_size_callback函数
//---------------------------------------------------------------
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
//用glad加载所有OpenGL函数指针
//----------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
cout << "Failed to initialize GLAD" << endl;
return -1;
}
glViewport(0, 0, 800, 600);
//渲染循环
//--------
while (!glfwWindowShouldClose(window))
{
//输入
//----
processInput(window);
//渲染
//----
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//交换缓冲,检查并调用事件
//------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
//当渲染结束后需要正确释放/删除之前的分配的所有资源。
//--------------------------------------------------
glfwTerminate();
return 0;
}
//glfw:无论何时窗口大小改变(通过操作系统或是用户改变)这个回调函数执行
//---------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
//确保视口与新窗口尺寸相匹配;请注意,宽度和高度将明显大于在视网膜显示器上指定的宽度和高度
//---------------------------------------------------------------------------------------
glViewport(0, 0, width, height);
}
//处理所有输入:查询GLFW该帧是否按下/释放了相关键,并做出相应的反应
//----------------------------------------------------------------
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}