写在前面
通过本节,你可以毫不费力的--->创建一个窗口
OpenGL中窗口,即载体
导入头文件
#include <glad/glad.h>
#include <GLFW/glfw3.h>
初始化glfw窗口
1 glfwInit(); 2 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 3 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 4 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 5 #ifdef __APPLE__ 6 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 7 #endif
创建窗口
1 //创建窗口 2 GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); 3 // 防止创建失败 4 if (window == NULL){ 5 std::cout<< "Failed to create glfw window" << std::endl; 6 glfwTerminate(); 7 return -1; 8 } 9 glfwMakeContextCurrent(window); 1011 12 if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) 13 { 14 std::cout << "Failed to initialize GLAD" << std::endl; 15 return -1; 16 }
窗口长存
1 // 避免闪退,监听输入事件 2 while(!glfwWindowShouldClose(window)) 3 { 4 if(glfwGetKey(window, GLFW_KEY_ESCAPE == GLFW_PRESS)) 5 glfwSetWindowShouldClose(window, true); 6 7 glfwPollEvents(); 8 }
总结
这一节是入门章节,就是先搞一个窗口出来,方便我们以后的测试,下一节开始实现具体的渲染