本文主要是介绍LearnOpenGL #01 Hello Window,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
搭建好OpenGL环境之后测试
#include <glad/glad.h>
#include <GLFW/glfw3.h>#include <iostream>void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);// Set the screen size
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;int main()
{// Initialize GLFWglfwInit();// Configure GLFWglfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);// This line is used to allow initialization code to work on Mac OS XglfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);// GLFW window creationGLFWwindow* 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: manage all OpenGL function pointersif (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){std::cout << "Failed to initialize GLAD" << std::endl;return -1;}// render loop: keep running until we let GLFW stop// check if GLFW has been instructed to closewhile (!glfwWindowShouldClose(window)){// inputprocessInput(window);// renderglClearColor(0.2f, 0.3f, 0.3f, 1.0f);glClear(GL_COLOR_BUFFER_BIT);// swap the color bufferglfwSwapBuffers(window);// check if poll IO events (keys pressed/released, mouse moved etc.)glfwPollEvents();}// glfw: terminate, clearing all previously allocated GLFW resources.glfwTerminate();return 0;
}// 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);
}// glfw: whenever the window size changed (by OS or user resize) this callback function executes
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{// make sure the viewport matches the new window dimensions; note that width and// height will be significantly larger than specified on retina displays.glViewport(0, 0, width, height);
}
OpenGL
OpenGL一般被认为是一个API(Application Programming Interface , 应用程序编程接口),包含了一系列可以操作图形、图像的函数。然而,OpenGL本身 并不是一个API ,它仅仅是一个由 Khronos组织 制定并维护的 规范 (Specification)。
OpenGL自身是一个巨大的 状态机 (State Machine):一系列的变量描述OpenGL此刻应当如何运行。OpenGL的状态通常被称为OpenGL上下文(Context)。我们通常使用如下途径去更改OpenGL状态:设置选项,操作缓冲。最后,我们使用当前OpenGL上下文来渲染。
GLFW
GLFW是一个专门针对OpenGL的C语言库,它提供了一些 渲染物体所需的最低限度的接口 。它允许用户创建OpenGL上下文,定义窗口参数以及处理用户输入,这正是我们需要的。
GLEW
GLEW是一个跨平台开源的C/C++扩展加载库。GLEW提供了OpenGL中许多核心或扩展函数。
GLAD
GLAD管理OpenGL的函数指针。 因为OpenGL只是一个标准/规范,具体的实现是由驱动开发商针对特定显卡实现的。由于OpenGL驱动版本众多,它大多数函数的位置都无法在编译时确定下来,需要在运行时查询。所以任务就落在了开发者身上, 开发者需要在运行时获取函数地址并将其保存在一个函数指针中供以后使用 。取得地址的方法 因平台而异。 有些库能简化此过程,其中 GLAD 是目前最新,也是最流行的库。
相关函数
glfwInit函数:初始化GLFW
glfwWindowHint函数:配置GLFW。 第一个参数代表选项的名称,我们可以从很多以 GLFW_ 开头的枚举值中选择;第二个参数接受一个整形,用来设置这个选项的值。
如果使用的是Mac OS X系统,需要加下面这行代码到初始化代码中,配置才能起作用:
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwCreateWindow 函数:需要窗口的宽和高作为它的前两个参数。第三个参数表示这个窗口的名称(标题)。 这个函数将会返回一个 GLFWwindow 对象,我们会在其它的GLFW操作中使用到。
glViewport函数:设置窗口维度。 前两个参数控制窗口左下角的位置。第三个和第四个参数控制渲染窗口的宽度和高度(像素)。
glfwWindowShouldClose函数:在我们每次循环的开始前检查一次GLFW是否被要求退出,如果是的话该函数返回 true 然后渲染循环便结束了,之后为我们就可以关闭应用程序了。
glfwPollEvents函数:检查有没有触发什么事件(比如键盘输入、鼠标移动等)、更新窗口状态,并调用对应的回调函数(可以通过回调方法手动设置)。
glfwSwapBuffers函数:交换颜色缓冲(它是一个储存着GLFW窗口每一个像素颜色值的大缓冲),它在这一迭代中被用来绘制,并且将会作为输出显示在屏幕上。
glfwTerminate 函数:在 渲染循环结束后我释放/删除之前的分配的所有资源。
glClearColor函数 :设置清空屏幕所用的颜色。
glClear 函数:清除颜色缓冲,整个颜色缓冲都会被填充为 glClearColor 里所设置的颜色。
参考:
[1] The OpenGL Extension Wrangler Library http://glew.sourceforge.net
[2] LearnOpenGL CN - 创建窗口 https://learnopengl-cn.github.io/01%20Getting%20started/02%20Creating%20a%20window/
[3] LearnOpenGL CN - 你好,窗口 https://learnopengl-cn.github.io/01%20Getting%20started/03%20Hello%20Window/
这篇关于LearnOpenGL #01 Hello Window的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!