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 <glad/glad.h>
#include <GLFW/glfw3.h>
​
#include<iostream>
​
​
//编写一个回调函数,每当用户改变窗口大小,视口随之改变
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;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,162评论 0 13
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 7,647评论 0 3
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,164评论 0 10
  • 幸运的是,在我最迷茫的时候,有两个老师,他们充当了我生命的摆渡人。 班主任檀满仓,北京师范大学心理系毕业。89年的...
    梧声声阅读 3,882评论 0 1
  • 离我们现在不是很远的时候,人们的生活方式。 生活就是一个大染缸,什么都可能会发生。
    辉高阅读 1,755评论 12 7