Read computer graphics programming (using OpenGL and C + +). 2

OpenGL can draw points, lines and triangles. These simple things are called primitives. Most 3D models are usually composed of many triangular primitives. An entity consists of vertices. Vertices can be read from files and loaded into buffers by C++/OpenGL applications, or hard coded strings directly in C + + files or directly in GLSL code. Before loading vertices, the C++/OpenGL application must compile and link the appropriate GLSL vertex shader and fragment shader programs, and then load them into the pipeline.

The C++/OpenGL application is also responsible for notifying OpenGL to build triangles and starting the execution of GLSL code in the pipeline by calling the function glDrawArrays(). All vertices are passed into the vertex shader. The shader executes once for each vertex, which is usually performed in parallel.

glDrawArrays (GLenum mode, GLint first, GLsizei count);

The mode parameter is the type of entity - GL for triangles_ TRIANGLES. The First parameter indicates which vertex to paint from (usually vertex 0, i.e. the First vertex), and count is the total number of vertices to paint.

Load GLSL program into shader
1. Use C + + to obtain GLSL shader code and read it from file or string
2. Create an OpenGL shader object and load GLSL shader code into the shader object
3. Compile and connect shader objects with OpenGL command and install them into GPU

The default size of OpenGL midpoint is 1 pixel.

There is a blue dot in the center of the window. The code is as follows.

main.cpp

(#include list (same as before)

#define numVAOs 1

GLuint renderingProgram;
GLuint vao[numVAOs];

GLuint createShaderProgram()
{
    const char* vshaderSource = 
        "#version 460 \n"
        "void main(void) \n"
        "{ gl_Position = vec4(0.0, 0.0, 0.0, 1.0); }";

    const char* fshaderSource =
        "#version 460 \n"
        "out vec4 color; \n"
        "void main(void) \n"
        "{ color = vec4(0.0, 0.0, 1.0, 1.0); }";

    GLuint vShader = glCreateShader(GL_VERTEX_SHADER); // build type GL_VERTEX_SHADER Shader that returns the sequence number that references it
    GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER); // build type GL_FRAGMENT_SHADER Shader that returns the sequence number that references it

    glShaderSource(vShader, 1, &vshaderSource, NULL); // take GLSL The code is loaded from a string into an empty shader object 
    glShaderSource(fShader, 1, &fshaderSource, NULL);
    glCompileShader(vShader); // Compile shaders
    glCompileShader(fShader);

    GLuint vfProgram = glCreateProgram(); // Create a program object and store an integer pointing to it ID
    glAttachShader(vfProgram, vShader); // Adding shaders to procedural objects
    glAttachShader(vfProgram, fShader);
    glLinkProgram(vfProgram); // Give Way GLSL The compiler ensures their compatibility

    return vfProgram;
}

void init(GLFWwindow* window){
    renderingProgram = createShaderProgram();
    glGenVertexArrays(numVAOs, vao);
    glBindVertexArray(vao[0]);
}


void display(GLFWwindow* window, double currentTime)
{
    glUseProgram(renderingProgram); // Load a program that contains two compiled shaders OpenGL Pipeline stage (at GPU On), no shader is running, just load the shader into the hardware
    glDrawArrays(GL_POINTS, 0, 1); // Start pipeline treatment process
}
(main The function is the same as before)

The string vshaderSource is a vertex hard coded in the vertex shader. The main goal of vertex shaders is to send vertices to pipelines. Built in variable gl_Position is used to set the coordinate position of the vertex in 3D space and send it to the next pipeline stage. GLSL data type vec4 is used to store 4 tuples.

The vertices will then move along the pipeline to the raster shader, where they are converted to pixel positions. Finally, these pixels reach the fragment shader fshaderSource. The purpose of the clip shader is to give RGB colors to the pixels to be displayed. In this case, it is blue. The "out" tag indicates that the color variable is an output variable. In vertex shaders, {gl_Position is a predefined output variable, so it does not have to be labeled "out".

When preparing to send the data set to the pipeline, it is sent in the form of buffer. These buffers will finally be stored in vertex array object (VAO). Even if the application does not use any buffer, OpenGL still needs to have at least one created VAO when using the shader, so the last two lines of init() are used to create the VAO required by OpenGL.

glShaderSource() has four parameters:

1. Shader object; 2. Number of strings in shader source code; 3. String pointer containing source code; 4,

glShaderSource(GLuint shader, GLsizei count, const GLchar *const* string, const GLint* length);

The results are as follows:

 

Add the following code to display() and put it in front of glDrawArrays()

glPointSize(30.0f);

When the rasterization phase receives vertices from the vertex shader, it sets the pixel color value for a point with a size of 30 pixels.

The results are as follows:

 

Keywords: C++ OpenGL

Added by daleks on Fri, 28 Jan 2022 19:41:46 +0200