一、GLFW下载
(根据自己电脑配置下载)
二、链接GLFW
1、新建工程,然后鼠标右键工程->添加库->外部库->选择库路径
2、在 .pro 文件中添加如下代码
LIBS += -lopengl32 -luser32
三、代码验证
#include <GLFW/glfw3.h>
//窗口大小变化
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
qDebug() << width << " " << height;
glViewport(0, 0, width, height);
}
//按键
void processInput(GLFWwindow *window)
{
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
int main(int argc, char *argv[])
{
glfwInit();
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); //mac os
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
qDebug() << "Failed to create GLFW window";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
qDebug() << "Failed to initialize GLAD";
return -1;
}
glViewport(0, 0, 800, 600);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Poll for and process events */
glfwPollEvents();
/* Swap front and back buffers */
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
资源下载:点击跳转
觉得有帮助的话,打赏一下呗。。