https://learnopengl-cn.github.io/
入门-你好窗口
#include<glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include<stdio.h>
#include<math.h>
#pragma region 函数_变量声明
GLFWwindow* window; //窗口指针
const unsigned int SCR_WIDTH = 800; //屏幕初始宽度设定
const unsigned int SCR_HEIGHT = 600; //屏幕初始高度设定
void init();
void processInput(GLFWwindow* window);
void mouse_callback(GLFWwindow* window, double xPos, double yPos);
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
#pragma endregion
int main()
{
init();
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);//画图模式设置边框or填充 GL_LINE
//渲染循环(Render Loop)
while (!glfwWindowShouldClose(window))
{
processInput(window); //有事件触发的输入
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glfwSwapBuffers(window); //交换颜色缓冲
glfwPollEvents(); //检查触发事件
}
glfwTerminate(); //释放
return 0;
}
#pragma region 初始化
void init()
{
//initialize
glfwInit();//initialize glfw
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);//VERSION
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);//MODE
#ifdef __APPLE__//mac系统需要
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
//创建窗口
window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);//creat window指针前面全局定义
//检验窗口
if (window == NULL)
{
printf("err:windows is NULL");
glfwTerminate();
return;
}
//绑定上下文和窗口
glfwMakeContextCurrent(window);
//注册回调函数
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); //鼠标输入模式
glfwSetMouseButtonCallback(window, mouse_button_callback); //鼠标点击反馈
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); //窗口大小反馈
//检验glad
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
printf("err:glad is NULL ");
glfwTerminate();
return;
}
}
#pragma endregion
#pragma region 事件响应
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
if (action == GLFW_PRESS) switch(button)
{
case GLFW_MOUSE_BUTTON_LEFT:
glfwSetCursorPosCallback(window, mouse_callback);
break;
default:
return;
}
if (action != GLFW_PRESS)
{
}
return;
}
void mouse_callback(GLFWwindow* window, double xPos, double yPos)
{
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
#pragma endregion