在windows中,使用GLFW创建窗口的流程如下:
- 包含头文件
#include "GLFW/glfw3.h"
#pragma comment(lib, "glfw3.lib")
- 初始化窗口
glfwInit() - 配置GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- 创建窗口对象
GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", nullptr, nullptr);
- 设置上下文
glfwMakeContextCurrent(window);
GLAD
GLAD是用来管理OpenGL的函数指针的,所以在调用任何OpenGL的函数之前我们需要初始化GLAD。
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
cout << "Failed to initialize GLAD" << endl;
return -1;
}
Viewport
在渲染之前,需要告诉OpenGL窗口大小的坐标位置,通过glViewport函数来设置。
渲染循环
在程序中添加一个while循环,让程序退出时才关闭窗口。
//render loop
while (!glfwWindowShouldClose(window))
{
processInput(window);
//等待事件
glfwPollEvents();
//render
glClearColor(0.2f, 0.3f, 0.4f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//swap
glfwSwapBuffers(window);
}
- glfwWindowShouldClose函数在我们每次循环的开始前检查一次GLFW是否被要求退出,如果是的话该函数返回true然后渲染循环便结束了,之后为我们就可以关闭应用程序了。
- glfwPollEvents函数检查有没有触发什么事件(比如键盘输入、鼠标移动等)、更新窗口状态,并调用对应的回调函数(可以通过回调方法手动设置)。
- glfwSwapBuffers函数会交换颜色缓冲(它是一个储存着GLFW窗口每一个像素颜色值的大缓冲),它在这一迭代中被用来绘制,并且将会作为输出显示在屏幕上。
退出清理
为了释放所有资源,需要使用glfwTerminate释放所有资源。
按键处理
使用GLFW的函数能处理键盘输入。当输入为ESC键时,WindowShouldClose设置为true,下次while循环会退出,然后执行退出的逻辑。
void processInput(GLFWwindow *window)
{
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
最终程序
ggl.h头文件,包含一些常用的头文件和lib
#pragma once
#include <iostream>
#include "glad/glad.h"
#include "GLFW/glfw3.h"
//包含GLFW库
#pragma comment(lib, "glfw3.lib")
//包含opengl库
#pragma comment(lib, "opengl32.lib")
main.cpp
#include<iostream>
using namespace std;
#include "ggl.h"
//键盘按键的回调函数
void processInput(GLFWwindow *window);
//窗口改变的回调
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
int main()
{
//初始化glfw库,使用前必须初始化
if (!glfwInit())
{
cout << "Failed to init GLFW" << endl;
return -1;
}
//配置GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//
GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", nullptr, nullptr);
if (window == nullptr)
{
cout << "Failed to open GLFW window" << endl;
glfwTerminate();
return -1;
}
//设置上下文
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
//GLAD管理opengl函数指针,在调用opengl之前需要先初始化GLAD
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
cout << "Failed to initialize GLAD" << endl;
return -1;
}
//render loop
while (!glfwWindowShouldClose(window))
{
processInput(window);
//等待事件
glfwPollEvents();
//render
glClearColor(0.2f, 0.3f, 0.4f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//swap
glfwSwapBuffers(window);
}
//释放资源
glfwTerminate();
return 0;
}
void processInput(GLFWwindow * window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
void framebuffer_size_callback(GLFWwindow * window, int width, int height)
{
glViewport(0, 0, width, height);
}
效果: