Learning OpenGL tutorials notes (a) (introductory)

See this article for beginners and discussion, the experts welcomed the criticism, pointing out errors, do not like do not look at ~
recommended with little red book (new version) supporting learning
OpenGL tutorials website link http://www.opengl-tutorial.org/
specific environment to build the question before the blog, some solutions can be put forward in question exchange.

On the basis of the site based on the tutorial, I conducted for the first three courses (not including the built environment) a summary, here combined with color cube this lesson, summarize explain the entire procedure.

Lesson Four
colored cubes

//**第一部分,编译环境的配置**

// Include standard headers
#include <stdio.h>
#include <stdlib.h>

// Include GLEW
#include <GL/glew.h>

// Include GLFW
#include <glfw3.h>

GLFWwindow* window;    //创建窗口句柄

// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace glm;

//将加载着色器的代码放在此文件中
#include <common/shader.hpp>



int main( void )
{

    //**第二部分,配置环境,设置窗口**

    //2.1初始化glfw库
    if( !glfwInit() )
    {
        fprintf( stderr, "Failed to initialize GLFW\n" );
        getchar();//暂停屏幕输出和程序运行,提供作者的浏览时间
        return -1;//通常返回-1表示程序出错
    }

    //2.2设置窗口和显示参数    

    //hint是线索的意思,这里意为选项Hint也有很多种,这里我们只设置了3种,分别是:
    glfwWindowHint(GLFW_SAMPLES, 4); //采用四倍抗锯齿
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); //选择OpenGL主版本
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); //选择OpenGL副版本
     //所以合起来就是OpenGL版本为2.1

    //2.3创建窗口,创建对应的context(上下文)
        //创建窗口,并设置大小和名称
        window = glfwCreateWindow( 1024, 768, "Tutorial 04 - Colored Cube", NULL, NULL);
        //保证窗口创建成功
    if( window == NULL ){
        fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
        getchar();
        glfwTerminate();   //若失败则需要终止glfw库
        return -1;
    } 
    //在使用OpenGL API之前,必须设置好当前的OpenGL上下文
    glfwMakeContextCurrent(window);

    //2.4初始化glew库
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        getchar();
        glfwTerminate();
        return -1;
    }

    //2.5设置相关参数

    //  确保我们可以捕捉下面按下的转义键,函数讲解见下文
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

    // 设置背景颜色为深蓝色(四个参数为red,green,blue,透明度)
    glClearColor(0.0f, 0.0f, 0.4f, 0.0f);

    // 允许深度测试(消隐)
    glEnable(GL_DEPTH_TEST);
    //如果像素比之前的像素更接近相机,则接受当前像素 
    glDepthFunc(GL_LESS); 


      //**第三部分,传入数据,开始绘制**


     //3.1调用已经写好的顶点着色器和片元着色器
    //从着色器创建和编译GLSL程序
    GLuint programID = LoadShaders( "TransformVertexShader.vertexshader", "ColorFragmentShader.fragmentshader" );

    //3.2在C++中创建MVP矩阵
    //创建uniform MVP矩阵的句柄
    GLuint MatrixID = glGetUniformLocation(programID, "MVP");

    // 为缓冲对象创建顶点位置句柄
    GLuint vertexPosition_modelspaceID = glGetAttribLocation(programID, "vertexPosition_modelspace");
    // 为缓冲对象创建顶点颜色句柄
    GLuint vertexColorID = glGetAttribLocation(programID, "vertexColor");

    // P矩阵:创建投影矩阵:45?Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
    glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
    //V矩阵:创建视角矩阵
    glm::mat4 View       = glm::lookAt(
                                glm::vec3(4,3,-3), // Camera is at (4,3,-3), in World Space
                                glm::vec3(0,0,0), // and looks at the origin
                                glm::vec3(0,1,0)  // Head is up (set to 0,-1,0 to look upside-down)
                           );
    // M矩阵:创建模型矩阵 : an identity matrix (model will be at the origin)
    glm::mat4 Model      = glm::mat4(1.0f);
    // 计算得到MVP矩阵 : multiplication of our 3 matrices
    glm::mat4 MVP        = Projection * View * Model; // Remember, matrix multiplication is the other way around

     //3.3 在C++中设置顶点数据的参数
    // 以下是我们的顶点数据,三个浮点数代表着3D的点坐标,每三个点构成一个三角形
    // 一个立方体有六个面,每个面可以由两个三角形组成,所以一共是6*2=12个三角形,12*3=36个点(有重复的点)
        static const GLfloat g_vertex_buffer_data[] = { 
        -1.0f,-1.0f,-1.0f,
        -1.0f,-1.0f, 1.0f,
        -1.0f, 1.0f, 1.0f,
         1.0f, 1.0f,-1.0f,
        -1.0f,-1.0f,-1.0f,
        -1.0f, 1.0f,-1.0f,
         1.0f,-1.0f, 1.0f,
        -1.0f,-1.0f,-1.0f,
         1.0f,-1.0f,-1.0f,
         1.0f, 1.0f,-1.0f,
         1.0f,-1.0f,-1.0f,
        -1.0f,-1.0f,-1.0f,
        -1.0f,-1.0f,-1.0f,
        -1.0f, 1.0f, 1.0f,
        -1.0f, 1.0f,-1.0f,
         1.0f,-1.0f, 1.0f,
        -1.0f,-1.0f, 1.0f,
        -1.0f,-1.0f,-1.0f,
        -1.0f, 1.0f, 1.0f,
        -1.0f,-1.0f, 1.0f,
         1.0f,-1.0f, 1.0f,
         1.0f, 1.0f, 1.0f,
         1.0f,-1.0f,-1.0f,
         1.0f, 1.0f,-1.0f,
         1.0f,-1.0f,-1.0f,
         1.0f, 1.0f, 1.0f,
         1.0f,-1.0f, 1.0f,
         1.0f, 1.0f, 1.0f,
         1.0f, 1.0f,-1.0f,
        -1.0f, 1.0f,-1.0f,
         1.0f, 1.0f, 1.0f,
        -1.0f, 1.0f,-1.0f,
        -1.0f, 1.0f, 1.0f,
         1.0f, 1.0f, 1.0f,
        -1.0f, 1.0f, 1.0f,
         1.0f,-1.0f, 1.0f
    };

    // 为每个点设置颜色,参数是随机生成的
    static const GLfloat g_color_buffer_data[] = { 
        0.583f,  0.771f,  0.014f,
        0.609f,  0.115f,  0.436f,
        0.327f,  0.483f,  0.844f,
        0.822f,  0.569f,  0.201f,
        0.435f,  0.602f,  0.223f,
        0.310f,  0.747f,  0.185f,
        0.597f,  0.770f,  0.761f,
        0.559f,  0.436f,  0.730f,
        0.359f,  0.583f,  0.152f,
        0.483f,  0.596f,  0.789f,
        0.559f,  0.861f,  0.639f,
        0.195f,  0.548f,  0.859f,
        0.014f,  0.184f,  0.576f,
        0.771f,  0.328f,  0.970f,
        0.406f,  0.615f,  0.116f,
        0.676f,  0.977f,  0.133f,
        0.971f,  0.572f,  0.833f,
        0.140f,  0.616f,  0.489f,
        0.997f,  0.513f,  0.064f,
        0.945f,  0.719f,  0.592f,
        0.543f,  0.021f,  0.978f,
        0.279f,  0.317f,  0.505f,
        0.167f,  0.620f,  0.077f,
        0.347f,  0.857f,  0.137f,
        0.055f,  0.953f,  0.042f,
        0.714f,  0.505f,  0.345f,
        0.783f,  0.290f,  0.734f,
        0.722f,  0.645f,  0.174f,
        0.302f,  0.455f,  0.848f,
        0.225f,  0.587f,  0.040f,
        0.517f,  0.713f,  0.338f,
        0.053f,  0.959f,  0.120f,
        0.393f,  0.621f,  0.362f,
        0.673f,  0.211f,  0.457f,
        0.820f,  0.883f,  0.371f,
        0.982f,  0.099f,  0.879f
    };

     //3.4创建VBO,将顶点数组的元素数据传入GLSL(OpenGL shading language),即着色器。

    //创建一个储存其顶点信息的VBO对象
    GLuint vertexbuffer;

    //返回n个当前未使用的缓存名称(此处是1),并保存到后面的数组中
    glGenBuffers(1, &vertexbuffer);

    //绑定缓存对象到OpenGL环境,绑定缓存时需要指明缓存对象的类型,这里是GL_ARRAY_BUFFER(顶点数据)
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

    //创建并初始化VBO对象的数据区,参数为(类型,指针大小,数据的指针,enum(用处))
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

    //创建一个用于存储点的颜色信息的VBO对象
    GLuint colorbuffer;
    glGenBuffers(1, &colorbuffer);
    glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);

   //3.5在着色器中利用MVP矩阵变换顶点,并实现绘制过程

   /*下面是一个无限执行的循环,负责一直处理窗口和操作系统的用户输入等操作,只有用户想退出的时候,才会终止循环*/
    do{

        // 清屏
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        // 使用着色器
        glUseProgram(programID);
        // 将我们的转换发送到当前绑定的着色器,
        // in the "MVP" uniform
        glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);

        // 第一个属性缓冲区:顶点
        glEnableVertexAttribArray(vertexPosition_modelspaceID);//启动顶点属性数组
        glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);//指定当前激活的对象

        glVertexAttribPointer(                    //函数详解见下
            vertexPosition_modelspaceID,          // index为想要设定的属性为顶点位置属性                  3,                   //每个顶点的元素数目                         GL_FLOAT,                    // 元素类型                            GL_FALSE,                    //是否需要归一化处理,限制在[-1,1]之间
                   0,                            //数据紧密封装在一起                           (void*)0                     // array buffer offset
        );

        // 第二个属性缓冲:颜色
        glEnableVertexAttribArray(vertexColorID);
        glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
        glVertexAttribPointer(
            vertexColorID,               // The attribute we want to configure
            3,                           // size
            GL_FLOAT,                    // type
            GL_FALSE,                    // normalized?
            0,                           // stride
            (void*)0                     // array buffer offset
        );

        // 画三角形
        glDrawArrays(GL_TRIANGLES, 0, 12*3); // 12*3 indices starting at 0 -> 12 triangles

        glDisableVertexAttribArray(vertexPosition_modelspaceID);
        glDisableVertexAttribArray(vertexColorID);

        //将画面展现给用户
        glfwSwapBuffers(window);
        //检查操作系统返回的任何信息
        glfwPollEvents();

    } 


 //**第四部分,绘制结束,清理缓存**


    //判断ESC键有没有按下或者有没有关闭窗口的行为
    while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
           glfwWindowShouldClose(window) == 0 );

    // 清理VBO和着色器    
    glDeleteBuffers(1, &vertexbuffer);
    glDeleteBuffers(1, &colorbuffer);
    glDeleteProgram(programID);

    // 关闭OpenGL,终止GLFW库
    glfwTerminate();

    return 0;
}

Knowledge supplement

1.glfw library, glew libraries and library glm

GLEW is a cross-platform OpenGL-based graphics interface of C ++ extensions. GLEW can automatically identify the current platform supports expansion of the number of senior Han all OpenGL. Glew.h long as it contains the header files, you can use all the functions gl, glu, glext, wgl, glx of. GLEW support a variety of popular operating systems.
The official explanation is as follows:. GLEW IS AN Open-Source Cross-Platform Extension loading Library with the Thread-Safe Support for Multiple Rendering contexts and Automatic code Generation Capability GLEW the Provides the Easy-to-use and Efficient Methods for Checking OpenGL Extensions and Core functions on this page.
GLFW OpenGL is a cross-platform application framework, support window creation, accepting input functions and events.
The official explanation is as follows: GLFW IS AN Open Source, Multi-Platform Library for Creating Windows with OpenGL contexts and Receiving the INPUT and Events It IS the Easy to integrate INTO existing Applications and does not Lay the Claim to at The main Loop..
GLM is an OpenGL mathematics storehouse
官方解释如下:GLM is a C++ mathematics library for graphics software based on the OpenGL Shading Language (GLSL) specification.

That sort out:
GLEW Library:
is included glGenbuffer (), OpenGL function library glBindBuffer such, is basic OpenGL libraries .
glfw library:
is an application framework, including the creation of some window function, so the library is first initialized in.
GLM library:
a math library , computing for the matrix.

2. About glfwMakeContextCurrent (window) function
as a novice, I think the context (ie context) is not easy to understand one thing,
access to information: that context is a very abstract concept, it can be tentatively put his understanding into an object this object contains all the objects of OpenGL.

Also keep in mind about this thing,
OpenGL is a state machine model is used in client-server mode, where I still do not understand this, continue to learn it!
3. function glfwSetInputMode (GLFWwindow * window, int mode
, ubt value) SEE glfw INPUT Reference available
Write pictures described here
other words, this function sets the input mode options, only one of the following three GLFW_CURSOR, GLFW_STICKY_KEYS GLFW_STICKY_MOUSE_BUTTONS
Sticky Keys: after blocking key such as adhesion start button, press alt + f4 can just press a button once
Write pictures described here
did by the students, we move on, which is available in English according to the above, this function allows blocking key exists, and, when a keyboard is pressed If, after the bond is called, glfwGetKey function returns GLFW_PRESS, only applies only to the situation you just want to know if this key is pressed, but does not apply to the following situation: you want to know when or in what order press press.

4.glVertexAttribPointer function
can take a look at the function name, function gl representatives glew library, vertex vertex, Attrib is attribution acronym, meaning that property, pointer pointer, this function is: specify when rendering index value index (index attribute is a position in the shader) of vertex attribute array of data format and location . (Transfer from Baidu Encyclopedia)
void glVertexAttribPointer (GLuint index, GLint size, GLenum of the type, GLboolean on Normalized, a stride of GLsizei, GLvoid const * pointer);
Parameters:

index
you want to modify the value of the vertex attribute index
size
specifies the number of components per vertex attributes. It must be 2, 3 or 4. The initial value is 4. (E.g., by three position (x, y, z) composition, and color 4 (R & lt, G, B, A))
type
specifies the type of data in the array for each component. Symbolic constants have available GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_FIXED, and GL_FLOAT, the initial value GL_FLOAT.
normalized
specified when accessed, whether the value of the fixed point data should be normalized (GL_TRUE) or direct conversion to a fixed point value (GL_FALSE).

stride
specified offset between consecutive vertex attributes. If it is 0, the vertex attributes as will be understood: that they are closely spaced together. The initial value of zero.
pointer
specifies a first component a first offset in the array vertex attribute. The array with bound GL_ARRAY_BUFFER stored in the buffer. The initial value of 0;

Guess you like

Origin blog.csdn.net/alexhu2010q/article/details/80927681