zoukankan      html  css  js  c++  java
  • OpenGL学习(1)——创建窗口

    教程:LearnOpenGL,系统:Deepin 15.9.3,IDE:Qt Creator。

    添加头文件

    创建窗口用到两个库:GLFW和GLAD,这里GLAD也可以替换成GLEW。添加头文件很简单,直接include就好:

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

    注意添加GLAD和GLFW头文件的次序,如果先添加GLFW的头文件,在编译时会报错。

    初始化GLFW

    调用函数 glfwInit在程序最开始初始化GLFW库:
    glfwInit();

    配置GLFW

    调用函数glfwWindowHint为之后调用创建窗口函数配置Hint,这里配置两个Hint:OpenGL版本和Profile类型。
    因为我使用的是OpengGL 3.0,所以配置版本号为3,另外选择Core Profile作为Profile类型:

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
    

    顺带一提Deepin或Ubuntu下查看OpenGL版本号的方法。两条指令:
    sudo apt-get install mesa-utils
    glxinfo

    创建窗口

    调用glfwCreateWindow函数创建窗口,需要四个参数,分别是:width、height、title、monitor和share,并返回一个GLFWwindow类型的对象。这里忽略最后两个参数,均设置成NULL。
    设置窗口的宽度为800,高度为600,标题为“OpenGL_Demo”:
    GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL_Demo", NULL, NULL);
    调用glfwMakeContextCurrent函数设置已创建窗口的Context为当前线程的主Context:
    glfwMakeContextCurrent(window);

    初始化GLAD

    我也不清楚GLAD库的具体作用,教程上说是用来管理OpenGL的函数指针,所以需要在调用OpenGL的函数前初始化GLAD。概念绕来绕去的,就先放一放。
    将GLFW库的glfwGetProcAddress函数传递给GLAD,glfwGetProcAddress返回OpenGL函数的地址:
    gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);

    配置视口Viewport

    视口是实际用来渲染的窗口,维度一般和窗口window的维度相同,也可以将视口的维度设置得比window的维度小或是大。
    调用glViewport函数进行配置,需要四个参数:x、y、width和height。(x, y)是视口左下角的坐标,这里设置成(0, 0):
    glViewport(0, 0, 800, 600);

    渲染循环

    为了使程序在用户关闭窗口前一直处于渲染的状态,还需要一个while循环:

    while(!glfwWindowShouldClose(window)){
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    

    glfwWindowShouldClose函数检测用户是否要求关闭窗口。glfwPollEvents函数检测是否有事件被触发,比如键盘的输入或者鼠标的移动。glfwSwapBuffers用来交换Front Buffer和Back Buffer,并在窗口显示绘制的内容。

    释放资源

    最后调用glfwTerminate函数释放分配的资源:
    glfwTerminate()

    完整代码如下:

    #include <iostream>
    #include <glad/glad.h>
    #include <GLFW/glfw3.h>
    
    using namespace std;
    
    //void frambuffer_size_callback(GLFWwindow* window, int width, int height);
    void processInput(GLFWwindow* window);
    
    int main()
    {
        //initialize GLFW
        glfwInit();
        //configure GLFW
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
        glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
    
        //creat a window object
        GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL_Demo", NULL, NULL);
        if (window == NULL){
            cout << "Failed to create GLFW window" << endl;
            glfwTerminate();
            return -1;
        }
        glfwMakeContextCurrent(window);
    
        //initialize GLAD to manage function pointers for OpenGL
        if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
            cout << "Failed to initialize GLAD" << endl;
            return -1;
        }
    
        //set width and height of Viewport
        glViewport(0, 0, 800, 600);
        //glfwSetFramebufferSizeCallback(window, frambuffer_size_callback);
    
        //render loop
        while(!glfwWindowShouldClose(window)){
            processInput(window);
            glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
            glClear(GL_COLOR_BUFFER_BIT);
            glfwSwapBuffers(window);
            glfwPollEvents();
        }
    
        glfwTerminate();
    
        return 0;
    }
    /*
    void frambuffer_size_callback(GLFWwindow *window, int width, int height)
    {
        glViewport(0, 0, width, height);
    }
    */
    
    void processInput(GLFWwindow* window)
    {
        //check if ESCAPE is pressed
        if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
            glfwSetWindowShouldClose(window, true);
    }
    
  • 相关阅读:
    爬虫练习
    爬取豆瓣电影top250
    简单爬虫
    正则提取子域名和ip
    用户体验培训总结
    测试经验总结
    项目管理知识总结
    读书笔记——《留住好员工:爱他们,还是失去他们?》
    ISTQB学习笔记
    数据结构和算法with Python
  • 原文地址:https://www.cnblogs.com/yiqian/p/10721191.html
Copyright © 2011-2022 走看看