OpenGL learning (2) - create the first window

create

GLFW

GLFW specifically for OpenGL is a C library, it provides some minimum required to render an object interface. It allows users to create OpenGL context, the definition of window parameters and process user input, which is what we need.

#include <GLFW/glfw3.h>

int main() {
    //——————————初始化——————————
    glfwInit();
    /*glfwWindowHint()可以设置一些关于窗口的选项*/
    /*由于GFLW创建的OpenGL上下文可能是任何版本的,所以你可以设置OpenGL的最低版本
    下面的代码设置了OpenGL最低版本为4.0*/
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); // OpenGL主版本号
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); // OpenGL副版本号
    /*OpenGL模式(GLFW_OPENGL_PROFILE),下列为设置为核心模式*/
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    //——————————创建窗口——————————
    /*glfwCreateWindow()接受5个参数。
    第一个、第二个是窗口的宽和高,以像素为单位;第三个是窗口标题*/
    GLFWwindow* window = glfwCreateWindow(800, 600, "OPENGL", NULL, NULL);

    //——————————渲染引擎——————————
    while (!glfwWindowShouldClose(window))
    {
        /*glfwPollEvents()用来检查是否有事件被触发,例如点击关闭按钮、点击鼠标、按下键盘,等等。*/
        glfwPollEvents();
    }
    //——————————退出——————————
    /*循环执行完毕后,我们需要释放前面所申请的资源*/
    glfwTerminate();

    return 0;
}

GLAD

Because only a OpenGL standards / specifications, the specific implementation is achieved by a developer for a particular driver card. As many OpenGL driver version, where most of its functions are not finalized at compile time, need to check at runtime. So the task falls on developers who need to get developers to address the function at run-time and save it for later use in a function pointer. The method of obtaining the address varies by platform, on Windows will be like this:

// 定义函数原型
typedef void (*GL_GENBUFFERS) (GLsizei, GLuint*);
// 找到正确的函数并赋值给函数指针
GL_GENBUFFERS glGenBuffers  = (GL_GENBUFFERS)wglGetProcAddress("glGenBuffers");
// 现在函数可以被正常调用了
GLuint buffer;
glGenBuffers(1, &buffer);

You can see the code is very complex and very complicated, we need to have to repeat this process for each function may be used. Fortunately, some libraries to simplify this process, in which GLAD is the latest and most popular library.

#include <glad\glad.h>  //必须写在GLFW头文件之前
#include <GLFW/glfw3.h>
#include <stdio.h>

int main() {
    
    //——————————glfw初始化——————————
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); 
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    
    GLFWwindow* window = glfwCreateWindow(800, 600, "OPENGL", NULL, NULL);
    // 创建失败时给出反馈
    if (window == NULL) {
        printf("err:windows is NULL");
        glfwTerminate();
        return -1;
    }

    //设置当前的窗口上下文,即让当前窗口的环境在当前线程上成为当前环境
    glfwMakeContextCurrent(window);

    //——————————glad初始化——————————
    /*一定要在设置完当前的窗口上下文之后*/
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        return -1;
    }

    //——————————渲染引擎——————————
    while (!glfwWindowShouldClose(window))
    {
        // gl开头的函数,是OpenGL的函数
        /* glClearColor窗口被清除时的颜色,也就是背景颜色,前三个参数分别为R,G,B,第四个参数是透明度*/
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        /* 这将会清除当前窗口,把所有像素的颜色都设置为前面所设置的清除颜色。*/
        glClear(GL_COLOR_BUFFER_BIT);
        glfwPollEvents();
        // 交换缓冲区,即在window上更新内容 
        glfwSwapBuffers(window);
    }

    //——————————退出——————————
    glfwTerminate();

    return 0;
}

Guess you like

Origin www.cnblogs.com/maeryouyou/p/11938740.html