OpenGL One (Triangle)

Preface:

While C ++ is a language syntax and foolish batch (food is original sin), but it is the best entry OpenGL this technology of choice.

Tips

Vertex array objects: Vertex Array Object, VAO

Vertex buffer objects: Vertex Buffer Object, VBO

Index buffer object: Element Buffer Object, EBO or Index Buffer Object, IBO

A little bit of basic concepts

3D coordinates into 2D coordinates through the OpenGL rendering pipeline graphics management.

Graphics rendering pipeline can be divided into two main parts:

  • The first part is the 3D coordinates into 2D coordinates

  • The second part is the actual coordinates into 2D color pixels

Note: 2D pixel coordinates and different, accurate 2D coordinates represent a point in 2D space kind of position, but this is an approximation of 2D pixel point, 2D pixel receive limit your screen resolution.

To accept a particular line of 3D coordinates, and then put them into colored 2D pixel output on the screen. Graphics rendering There are many stages, each stage is highly specialized, there is a specific function processing.

Today there are many graphics core, each applet, fast data processing for the extreme operating each line on the GPU, these small programs called shaders (Shader) . Shader OpenGL shading language using the GLSL .

 

 

 

(Portions are portions can write your own shaders blue) Vertex Shader && Fragment Shader must write

Vertex Shader: as a single point as input.

Fragment Shader: calculating the final color of a pixel.

Vertex input

Normalized device coordinates (Normaliezd Device Coordiantes, NDC)

Been to vertex shader is treated vertex coordinates NDC, xyz are between -1.0 and 1.0. And usually different screen coordinates, (0,0) is the center of the image coordinates.

 

 

Normalized device coordinates would then transformed into screen space coordinates (Screen-space Coordinates), which is provided by using the data you glViewport function performs viewport transformation (Viewport Transform) completed. The resulting screen space coordinates will be converted into a fragment shader input to the fragment.

float vertices[] = {
    -0.5f, -0.5f, 0.0f,
     0.5f, -0.5f, 0.0f,
     0.0f,  0.5f, 0.0f
};

 

Vertex array from the CPU to the GPU and shake, the vertex information stored intact in the VBO.

In order to determine the relationship between each vertex, we created VAO, VBO what all the metadata inside the specific representative.

In general it is a model with a VAO cache.

 

VAO can have a Array Buffer a Element Buffer .

VBO cached Array Buffer VAO's. EBO cache to ELement Buffer.

unsigned int VAO;
glGenVertexArrays ( . 1 , & VAO);
 / * generate a return to & VAO VAO, this case may generate a plurality, then the above will VAO defined as an array * / 
/ * 
    The above is equivalent to 
    unsigned int VAO[1];
    glGenVertexArrays (1, VAO);
    Draw a triangle with this feeling a bit out of your pants off fart
*/
glBindVertexArray (VAO);
/ * All of the above code is equivalent to VAO made a Array Buffer, then went to tie * / 
unsigned int VBO;
glGenBuffers ( 1 , & VBO);
 / * The above code produces a VBO buffer * / 
glBindBuffer (GL_ARRAY_BUFFER, VBO)        // two parameters: 1 to give him where tied to a buffer 2. The transmission ID. 
/ * above just one line of code that is generated by the VBO buffer bind to the target GL_ARRAY_BUFFER * /

 

Here glBufferData () function. To copy data to the user-defined function of the current binding buffer .

Pass four parameters

  • The first parameter is the type of Buffer: VAO There are two types of Buffer, at this time we are using Array Buffer.

  • The second argument specifies a specific number of data to be buffered, with a length of bytes Expression

  • The third parameter is an array that we want to send a specific

  • The fourth parameter is described in detail how we should deal with the value of the cards we just sent, there are about three forms

    • GL_STATIC_DRAW: data with little or no change.

    • GL_DYNAMIC_DRAW: Data will be changed a lot.

    • GL_STREAM_DRAW: Data change every time you draw.

  

glBufferData (GL_ARRAY_BUFFER, the sizeof (the vertices), the vertices, GL_STATIC_DRAW);
 / * code above action is to copy the previously defined vertex data buffer to the husband * /

 

The above code to establish a link between the vertex data and VBO. As it has been defined VAO buffer and bound Array Buffer, it establishes a relationship between the vertex data with VAO.

Vertex shader

#version 330 core
layout (location = 0) in vec3 aPos;
void main()
{
    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}

 

Next is the shader, this time directly ok. This is an ordinary vertex shader.

The hard-coded directly on ok. (Write the code inside the code)

Fragment shader

#version 330 core
out vec4 FragColor;
void main()
{
    FragColor = Vec4 ( 1.0f , 0.5f , 0.2f , 1.0f );
} 

 

And the same vertex shader, direct hard-coded. This is the GLSL shader language.

Shader compiler

unsigned int vertexShader;
vertexshader = glCreateShader (GL_VERTEX_SHADER);             // Create a shader 
glShaderSource (vertexshader, . 1 , & vertexShaderSource, NULL);     // the shader source attached to the shader object 
glCompilesShader (vertexshader);                              // compiler shader

 

Similarly, a fragment shader is such a flow

unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader,1,&fragmentShaderSource,NULL);
glCompiledsShader(fragmentShader);

 

Tips: After compiling shaders can be used to see if the code compiles successfully

int success;
string infoLog;
glGetShaderiv(vertexShader,GL_COMPILE_STATUS,&success);
if(! success){
    glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
    std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}

 

Shader programs

Only shader alone is not enough, take two shader assembled into a shader program to come up with.

unsigned int shaderProgram glCreateProgram = ();          // Create a shader program 
glAttachShader (shaderProgram, vertexshader);              // the compiled shader program before the object attached to 
glAttachShader (shaderProgram, fragmentShader);
glLinkProgram (shaderProgram);                            // Finally glLinkProgram link them

 

Just like shader compiler, we can also detect whether the shader link failure.

glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if(!success) {
    glGetProgramInfoLog (shaderProgram, 512 , NULL, infolog);
     // failed operation to be performed 
}

 

So far, the results we get is a program object, you can call glUseProgram function, with the object just created as his parameters to activate the object

glUseProgram(shaderProgram);

 

() Function call after each call shader and render call will use this program objects (that is, before writing shaders) in glUseProgram

If not, then you can delete off.

glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);

 

So far, we have to send the data to the GPU vertex, and instructs the shader processing.

Link vertex attributes

The aforementioned has established a link between the vertex data with VAO, but this time OpenGL do not know how to explain it to the vertex data in memory, and how it will link the vertex data to the shader property.

Firstly Code

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray ( 0 );                        // Open No. Attribute of 0 
the while (! glWindowShouldClose (window)) {
    processInput(window);
    
    GlClearColor ( 0.2f , 0.3f , 0.3f , 1.0f );
    glClear(GL_COLOR_BUFFER_BIT);
    
    glBindVertexArray (VAO);
    glUseProgram(shaderProgram);
    glDrawArrays(GL_TRIANGLES,0,3);
    
    glfwSwapBuffers(window);
    glfwPollevents();
}

 

OK, no accident, then you can now see a triangle. (Unexpected to see it again on the above operation / funny) Let's explain the code.

glVertexAttribPointer ( 0 , 3 , GL_FLOAT, GL_FALSE, 3 * sizeof ( float ), ( void *) 0 );
 // link vertex attributes (vertex analytical data): tells the computer how to read the data in each cache 
glEnableVertexAttribArray ( 0 ) ;

 

VAO there above the Array Buffer , the above points has n columns, (vertex feature values) called Vertex Attribute

 

 

We are resolved to VBO

  • Preparing position data stored as 32-bit (4-byte) floating point values

  • Each vertex contains three such values

  • In between these three values ​​is no gap, closely spaced

  • The first data value in the beginning of the red flower position

So our glVertexAttribPointer () function will tell how to resolve the OpenGL vertex data

  • Parameters 1: development of vertex attributes configured in the shader, the position values ​​may be set to 0 vertex attributes.

  • 2 parameters: the size of the development of vertex attributes, consists of three values, the three.

  • Parameter 3: Develop Data Types

  • Parameter 4: Do we want to define the data to be standardized (Normalize). If we set GL_TRUE, all data will be mapped to 0 (for a signed data type is signed -1) between 1. We set it GL_FALSE.

  • Parameter 5: called the step size , tell us the interval between successive vertex attributes.

  • Parameter 6: It indicates the position of the offset data in the buffer start position (Offset). Since the position data at the beginning of the array, so here is 0.

This time has been resolved well VAO data inside. To use VAO, have to do is use glBindVertexArray binding VAO. So have the following code:

glBindVertexArray (VAO);
glUseProgram(shaderProgram);
glDrawArrays(GL_TRIANGLES,0,3);

 

About glDrawArrays function:

  • The first parameter: intended to be drawn primitive type

    /*
    GL_POINTS  GL_LINES  GL_LINE_LOOP  GL_LINE_STRIP
    GL_TRIANGLES  GL_TRIANGLE_STRIP  GL_TRIANGLE_FAN
    */

     

  • The second parameter: the starting index array of vertices

  • The third parameter: the number of vertices intend to draw

  • Action: extracting data from a data rendered substantially FIG array element

Finally resorted to the full version of the code and sand sculpture renderings

https://paste.ubuntu.com/p/wBM9XctMny/

 

 

 

Guess you like

Origin www.cnblogs.com/LittleBanana/p/11870384.html