OpenGL | Use OpenGL to draw different triangles

1. Build an OpenGL drawing framework

1.3D scene initialization

(1) Code

void Init()
{
    glMatrixMode(GL_PROJECTION);     //将当前矩阵指定为投影矩阵,对投影矩阵操作
    gluPerspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);//创建一个对称的透视投影矩阵,并且用这个矩阵乘以当前矩阵
    glMatrixMode(GL_MODELVIEW);      //将当前矩阵指定为模型视图矩阵,对模型视图矩阵操作
    glLoadIdentity();                //把矩阵设为单位矩阵
}

(2) Introduction of related functions

a. glMatrixMode specifies the matrix mode

The glMatrixMode() command sets the current matrix to the mode specified by the parameters to meet the matrix transformation required for different drawings. Generally speaking:

  • When you need to draw an object or perform geometric transformation on the drawn object, you need to set the transformation matrix to the model view mode;

  • And when you need to set a certain projection method for the drawn object, you need to set the transformation matrix to the projection mode;

  • The transformation matrix needs to be set to texture mode only when doing texture mapping.

Used together with glLoadIdentity(), the function of glLoadIdentity() is to reset the currently specified matrix to the identity matrix.

void glMatrixMode(GLenum mode)

mode specifies which matrix stack is the target of the next matrix operation, optional values: 

  • GL_MODELVIEW: Applies subsequent matrix operations to the modelview matrix stack. After executing this command, you can output your own object graphics.

  • GL_PROJECTION: Applies subsequent matrix operations to the projection matrix stack. After executing this command, we can add perspective to our scene.

  • GL_TEXTURE: Applies subsequent matrix operations to the texture matrix stack. After executing this command, we can add texture maps to our graphics.

b. gluPerspective creates a perspective projection matrix

OpenGL supports two types of projection transformations, perspective projection and orthographic projection.

void gluPerspective(GLdouble fovy,GLdouble aspect,GLdouble zNear, GLdouble zFar);

Creates a symmetric perspective projection matrix and multiplies the current matrix by this matrix.

  • The parameter fovy defines the angle of the field of view on the YZ plane, and the range is [0.0, 180.0];

  • The parameter aspect is the ratio of the projection plane width to height;

  • The parameters Near and Far are respectively the distances from the near and far clipping planes to the viewpoint (along the negative Z axis), and they are always positive.

2. Draw an independent triangle

Treat each ternary vertex as an independent triangle. Vertices 3n - 2, 3n - 1, and 3n define triangle n. Draw N/3 triangles.

(1) Code

void Draw()
{
    glClearColor(1, 1, 1, 1.0f);  //白色背景
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_TRIANGLES);        //注意是逆时针绘制(CCW),可以通过   glFrontFace(GL_CW); 改成顺时针绘制
    glColor4ub(0, 0, 255, 255);   //蓝
    glVertex3f(-0.2f, -0.2f, -1.5f);
    
    glColor4ub(255, 0, 0, 255);   //红
    glVertex3f(0.2f, -0.2f, -1.5f);

    glColor4ub(0, 255, 0, 255);   //绿
    glVertex3f(0.0f, 0.2f, -1.5f);
    glEnd();
}

Note: After the triangle is drawn, SwapBuffers is required to exchange buffers , otherwise the triangle cannot be drawn on the screen.

//... 
//防止窗口一闪而过
    MSG msg;
    while (true)
    {
        if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
            {
                break;
            }
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        //实时绘制
        Draw();
        SwapBuffers(dc);    //交换缓冲区
    }
//...

(2) Drawing effect

By default OpenGL will draw both front and back sides. The front direction of the point is counterclockwise, so when drawing, the default is to draw counterclockwise, that is, the first point connects to the second point, the second point connects to the third point, and the third point connects to the first Point, drawn like this.

The front side is the side that the camera is facing, that is: the side drawn counterclockwise when our eyes see it.

When drawing a triangle surface, pass in the GL_TRIANGLES parameter in the glBegin method, and then set multiple points between glBegin and glEnd, OpenGL will automatically form three points into a triangle surface and draw it. Each point can set a color value, and the color on the surface is obtained through the color difference of the three points.

(3) Introduction of related functions

a. glClearColor

Specifies the color to use when flushing the color buffer. Note: glClearColor only plays the role of Set, not Clear. So it should be used in conjunction with glClear.

b. glClear

Clear buffer color and set to default. There are four parameters:

GL_COLOR_BUFFER_BIT

Buffers currently enabled for color writing.

GL_DEPTH_BUFFER_BIT

depth buffer.

GL_ACCUM_BUFFER_BIT

Cumulative buffer.

GL_STENCIL_BUFFER_BIT

Die buffer.

3. Draw a connected triangle (strip)

Draws a set of connected triangles. Defines a triangle for each vertex rendered after the first two. For odd n, vertices n, n + 1, and n + 2 define triangle n. For an even number n, vertices n + 1, n, and n + 2 define triangle n. Draws N - 2 triangles.

(1) Code

void Draw()
{
    glClearColor(1, 1, 1, 1.0f);  //白色背景
    glClear(GL_COLOR_BUFFER_BIT);

    //绘制连接的三角形
    glBegin(GL_TRIANGLE_STRIP);
    glColor4ub(0, 0, 255, 255);   //蓝
    glVertex3f(-0.2f, -0.2f, -1.5f);

    glColor4ub(255, 0, 0, 255);   //红
    glVertex3f(0.2f, -0.2f, -1.5f);

    glColor4ub(0, 255, 0, 255);   //绿
    glVertex3f(0.0f, 0.2f, -1.5f);

    glColor4ub(0, 255, 255, 255); //青
    glVertex3f(0.4f, 0.2f, -1.5f);

    glEnd();
}

(2) Drawing effect

4. Draw a connected triangle (fan shape)

(1) Code

void Draw()
{
    glClearColor(1, 1, 1, 1.0f);  //白色背景
    glClear(GL_COLOR_BUFFER_BIT);

    //绘制连接的三角形
    glBegin(GL_TRIANGLE_FAN);
    glColor4ub(255, 255, 0, 255);     //黄
    glVertex3f(-0.2f, 0, -1.5f);

    glColor4ub(0, 255, 255, 255);     //青
    glVertex3f(0.4f, 0, -1.5f);

    glColor4ub(255, 0, 0, 255);       //红
    glVertex3f(0.4f, 0.2f, -1.5f);

    glColor4ub(0, 255, 0, 255);       //绿
    glVertex3f(0.3f, 0.34f, -1.5f);

    glColor4ub(255, 0, 255, 255);     //粉
    glVertex3f(0.13f, 0.4f, -1.5f);

    glEnd();
}

(2) Drawing effect

5. Reference documents

Guess you like

Origin blog.csdn.net/weixin_39766005/article/details/128814152