Learning OpenGL (a) - Simple window painting

A, OpenGL Introduction

OpenGL nature:

The OpenGL (the Open Graphics Library), can generally be considered to be the API , which can comprise a series of function operation pattern image . But get to the bottom down, it was developed by the Khronos organization and maintenance of norms that defines how each function of the execution, and their output value . According to the specification of OpenGL, developers can write all kinds of extensions


OpenGL development:

  • Now rendering modes : Immediate mode, which is fixed rendering pipeline , it is the early use OpenGL rendering mode
    • Pros: Most of the details do abstraction , understanding and easy call
    • Disadvantages: is not conducive to developers complete control, and inefficient
  • Core mode : Core-profile, starting from OpenGL3.2 immediate mode rendering alternative development approach, forcing developers to use modern methods of development
    • Advantages: higher flexibility and efficiency , ease of developers in-depth understanding of graphics programming
    • Disadvantages: Learning more difficult

OpenGL libraries:

The role of the OpenGL library that provides us with all kinds of methods, OpenGL library itself contains:: gl core library, glu utility library, glut utility library (library describes these relations OpenGL library gl, glu, glut, glaux of - geeks niche )

In addition, a major feature of OpenGL is extended , mainly in new features graphics company raised can order driven approach to achieve the developers can support this expansion card, using some of the more advanced and more efficient graphics This extension provides Features

Common to the library there: GLFW library and, GLEW library, GLAD library:

  • GLFW : a specific OpenGL in C language library , which provides some minimum needed to render an object interface . It allows users to create OpenGL context, the definition of window parameters, and a user input processing
  • GLEW : OpenGL extensions (OpenGL Extension Wrangler Library), to help C / C ++ developers initialize OpenGL extensions and write portable applications
  • GLAD : more popular libraries. Use the online service, you can tell OpenGL version GLAD need to define programming, and loaded all OpenGL functions according to this version

Two, OpenGL development environment configuration

Recommended works:

Three, OpenGL draws a simple window

Process Description:

  1. References glfw, glew library
  2. Examples of GLFW window
  3. Create a set GLFWWindow window object
  4. Settings window attributes (coordinates, width and height)
  5. Realization render loop
  6. End rendering
#include<GL/glew.h>
#include<GLFW/glfw3.h>
#include<iostream>
using namespace std;

void processInput(GLFWwindow *window);

int main()
{
    //1. 实例化GLFW窗口:
    //1.1 初始化GLFW库
    glfwInit();

    //1.2 设置GLFW使用的OpenGL版本是3.3 :主版本号(Major)和次版本号(Minor)
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

    //1.3 GLFW使用的是核心模式(Core-profile)
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);



    //2. 创建窗口对象:
    GLFWwindow* window = glfwCreateWindow(800, 600, "SouthBegonia's Window", NULL, NULL);
    //2.1 检验是否创建成功
    if (window == NULL)
    {
        //创建失败则终止GLFW
        cout << "open window failed." << endl;
        glfwTerminate();
        return -1;
    }
    //2.2 设置当前OpenGL上下文
    //    设置参数window中的窗口所关联的OpenGL环境为当前环境,
    //    这个环境在当前线程中会一直保持为当前环境,直到另一个环境被设置为当前环境,或者窗口被删除为止
    glfwMakeContextCurrent(window);


    //3. 设置窗口属性:起始坐标x,y(左下角开始),宽高像素
    glViewport(0, 0, 800, 600);


    //4. 实现渲染循环:
    //   若window窗口未关闭,则进行操作
    while (!glfwWindowShouldClose(window))
    {
        //4.1 设置窗口颜色/清空颜色缓存
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        //4.2 自定义的输入监测
        processInput(window);

        //4.3 交换颜色缓冲函数
        //    实现原理是双缓冲:前buffer存储当前画面信息,后buffer存储下一画面的信息
        glfwSwapBuffers(window);

        //4.4 检查有无触发事件函数
        //    比如键盘输入、鼠标移动等、更新窗口状态,并调用对应的回调函数
        glfwPollEvents();
    }

    //5. 渲染结束
    //   释放/删除之前的分配的所有资源
    glfwTerminate();

    return 0;
}

//输入事件监测函数
void processInput(GLFWwindow *window)
{
    //按下ESC键,变更窗口显示状态
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

Fourth, the reference

Guess you like

Origin www.cnblogs.com/SouthBegonia/p/11938401.html