Hello Window

1 函数详解

  1. glfwInit();该函数是用来初始化GLFW

  2. glfwWindowHint();是用来指定OpenGL版本的,例如,该函数一般连续使用两次,指定OpenGL的大版本和小版本,举个例子,glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);

    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);两次使用该函数,指定了OpenGL的版本是3.3, glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);指定了使用OpenGL的核心模式。可以看出,这个函数接受两个参数,第一个参数是你要做什么?第二个参数是你要做什么的值。

  3. glfwCreateWindow(宽, 高, 名字(str), NULL, NULL)。创建窗口

  4. glfwTerminate(); This function destroys all remaining windows and cursors, restores any modified gamma ramps and frees any other allocated resources. Once this function is called, you must again call glfwInit successfully before you will be able to use most GLFW functions.

    If GLFW has been successfully initialized, this function should be called before the application exits. If initialization fails, there is no need to call this function, as it is called by glfwInit before it returns failure.一句话说,这个函数会摧毁所有的东西。

  5. glfwMakeContextCurrent(window); This function makes the OpenGL or OpenGL ES context of the specified window current on the calling thread. A context can only be made current on a single thread at a time and each thread can have only a single current context at a time.

    By default, making a context non-current implicitly forces a pipeline flush. On machines that support GL_KHR_context_flush_control, you can control whether a context performs this flush by setting the GLFW_CONTEXT_RELEASE_BEHAVIOR window hint.

    The specified window must have an OpenGL or OpenGL ES context. Specifying a window without a context will generate a GLFW_NO_WINDOW_CONTEXT error.这个函数指定OpenGL内容显示的窗口。其中,内容和线程是一对一的关系。

  6. GLFWglproc glfwGetProcAddress(const char * procname); This function returns the address of the specified OpenGL or OpenGL ES core or extension function, if it is supported by the current context.

    A context must be current on the calling thread. Calling this function without a current context will cause a GLFW_NO_CURRENT_CONTEXT error.可以简单的理解为返回OpenGL的内存地址。

  7. glViewport(x, y, width, height); 前两个参数指定窗口左下角的位置,后两个参数控制渲染窗口的宽度和高度。

  8. GLFWframebuffersizefun glfwSetFramebufferSizeCallback (GLFWwindow * window,GLFWframebuffersizefun cbfun );This function sets the framebuffer resize callback of the specified window, which is called when the framebuffer of the specified window is resized.当窗口大小改变时,便会调用这个函数。我们可新建一个函数cbfun实现自己想要的调用效果。

  9. glfwWindowShouldClose(window); This function returns the value of the close flag of the specified window. 这个函数会返回窗口是否应该关闭

  10. glfwSetWindowShouldClose(window, true); 这个函数设置窗口是不是应该关闭。

  11. glfwGetKey(window, GLFW_KEY_ESCAPE);这个函数检查是不是按下了某个按键。

2 代码

#include 
#include 
​
#include
​
​
//编写一个回调函数,每当用户改变窗口大小,视口随之改变
void frambuffer_size_callback(GLFWwindow* window, int width, int height)
{
 glViewport(0, 0, width, height);
 std::cout << "Window has resized" << std::endl;
}
​
//编写一个函数,当用户按下Esc按键时退出
void processInput(GLFWwindow* window)
{
 //检查用户是否按下了Esc按键
 if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
 {
 //如果按下了Esc按键,将这个函数置为true
 glfwSetWindowShouldClose(window, true);
 }
}
​
int main()
{
 glfwInit();
 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
​
 //函数将返回一个glfwWindow的实例对象,这个窗口对象存放了所有和窗口相关的数据,而且会被GLFW其他相关的函数频繁的调用到
 GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
 if (window == NULL)
 {
 std::cout << "Fail to create GLFW window" << std::endl;
 glfwTerminate();
 return -1;
 }
 glfwMakeContextCurrent(window);
​
 if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
 {
 std::cout << "Failed to initialize GLAD" << std::endl;
 return -1;
 }
​
 //设置渲染窗口大小,即视口,前两个参数定义了视口的坐标,后两个参数定义了视口的大小
 glViewport(0, 0, 800, 600);
​
 //注册刚刚编写的回调函数,告诉OpenGL,调整窗口大小的时候调用的函数
 glfwSetFramebufferSizeCallback(window, frambuffer_size_callback);
​
 //循环渲染
 while (!glfwWindowShouldClose(window))//每次检查GLFW是否被要求退出
 {
 processInput(window);
​
 glClearColor(0.2f, 0.3f, 0.3f, 1.0f);//状态设置函数
 glClear(GL_COLOR_BUFFER_BIT);//状态使用函数,设置为glClearColor中设置的颜色
 glfwPollEvents();//检查有没有发生什么事件,更新窗口状态
 glfwSwapBuffers(window);//双缓冲技术
 }
​
 glfwTerminate();
​
 return 0;
}

你可能感兴趣的:(Hello Window)