80 lines
2.0 KiB
C++
80 lines
2.0 KiB
C++
#include <glad/glad.h>
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include <iostream>
|
|
|
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
|
void processInput(GLFWwindow* window);
|
|
|
|
// settings
|
|
const unsigned int SCR_WIDTH = 800;
|
|
const unsigned int SCR_HEIGHT = 600;
|
|
|
|
|
|
int main()
|
|
{
|
|
// 初始化 GLFW
|
|
glfwInit();
|
|
// 配置 GLFW : 版本 3.3 ,使用 Core-profile 模式
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
// MACOS 平台需要启用这一条
|
|
#ifdef _APPLE_
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|
#endif // _APPLE_
|
|
|
|
// 创建窗口对象
|
|
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
|
|
if (window == NULL)
|
|
{
|
|
std::cout << "Failed to create GLFW window" << std::endl;
|
|
glfwTerminate();
|
|
return -1;
|
|
}
|
|
glfwMakeContextCurrent(window);
|
|
// 注册帧缓冲变化时的回调函数
|
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
|
|
|
// 初始化 GLAD : load all OpenGL function pointers
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
|
{
|
|
std::cout << "Failed to initialize GLAD" << std::endl;
|
|
return -1;
|
|
}
|
|
|
|
// 渲染循环 (Render Loop)
|
|
while (!glfwWindowShouldClose(window))
|
|
{
|
|
// input
|
|
processInput(window);
|
|
|
|
// 渲染指令
|
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); // 状态设置函数
|
|
glClear(GL_COLOR_BUFFER_BIT); // 状态使用函数
|
|
|
|
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
|
|
glfwSwapBuffers(window);
|
|
glfwPollEvents();
|
|
}
|
|
|
|
// glfw: terminate, clearing all previously allocated GLFW resources
|
|
glfwTerminate();
|
|
|
|
return 0;
|
|
}
|
|
|
|
// 窗口大小改变时的回调函数
|
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
|
{
|
|
// 视口
|
|
glViewport(0, 0, width, height);
|
|
}
|
|
|
|
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
|
|
void processInput(GLFWwindow* window)
|
|
{
|
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
|
glfwSetWindowShouldClose(window, true);
|
|
} |