LearnOpenGL代码注释-你好,窗口

原文地址

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <iostream>

//函数声明
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);

// settings
//定义窗口宽度与高度
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

int main()
{
    // glfw: initialize and configure
    // ------------------------------
    glfwInit();//初始化GLFW
    //              名称                    版本号
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);//配置GLFW
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // glfw window creation
    // --------------------
    //创建窗口
    GLFWwindow* 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: load all OpenGL function pointers
    // ---------------------------------------
    //GLAD管理OpenGL的函数指针
    //初始化GLAD
    //glfwGetProcAddress  函数指针
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }    

    // render loop
    // -----------
    //循环渲染:主动关闭前不断绘制图像
    //glfwWindowShouldClose(window):检查是否要求退出
    while (!glfwWindowShouldClose(window))
    {
        // input
        // -----
        //调用检测用户输入函数
        processInput(window);

        // render
        // ------
        //以一个自定义颜色清空屏幕
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);//设置清空屏幕的颜色
        glClear(GL_COLOR_BUFFER_BIT);//清空屏幕,有三个可选参数值

        // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
        // -------------------------------------------------------------------------------
        //存储窗口中每一个像素的颜色值的大缓冲
        //交换缓冲:交换前缓冲和后缓冲,双缓冲技术
        glfwSwapBuffers(window);
        //检查用户事件,并调用相应函数;此处无调用。
        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);
}

猜你喜欢

转载自blog.csdn.net/qq_33446100/article/details/82778953