Qt opengl draws points, lines, triangles, polygons (2)

Article directory

  • 1. Related macro definition parameters
  • 2. Code and example diagrams
    • 1. Point LG_POINTS 0x0000
    • 2. Line GL_LINES 0x0001
    • 3. Line GL_LINE_LOOP 0x0002
    • 4. GL_LINE_STRIP 0x0003
    • 5. GL_TRIANGLES 0x0004
    • 6. GL_TRIANGLE_STRIP 0x0005
    • 7. GL_TRIANGLE_FAN 0x0006
    • 8. GL_QUADS 0x0007
    • 9. GL_QUAD_STRIP 0x0008
    • 10. GL_POLYGON 0x0009
  • Summarize

1. Related macro definition parameters

#define GL_POINTS 0x0000 // Points
#define GL_LINES 0x0001 // Straight line segment, in the order of adding points, every 2 points form a line segment
#define GL_LINE_LOOP 0x0002 // Straight line segment, in the order of adding points, two adjacent points are connected, and the first point and the last point are also connected (polygon)
#define GL_LINE_STRIP 0x0003 // Similar to GL_LINE_LOOP, the difference is that the first point and the last point are not connected
#define GL_TRIANGLES 0x0004 // In the order of adding points, every 3 points form a triangle (surface)
#define GL_TRIANGLE_STRIP 0x0005// Triangle (face), see the code demonstration below for the difference
#define GL_TRIANGLE_FAN 0x0006 //Triangle (face), see the code demonstration below for the difference
#define GL_QUADS 0x0007 //
#define GL_QUAD_STRIP 0x0008 //
#define GL_POLYGON 0x0009 //

2. Code and sample pictures

1. Point LG_POINTS 0x0000

void MOpenGLWidget::paintGL()
{<!-- -->
    glClear(GL_COLOR_BUFFER_BIT);

    //point size
    glPointSize(6.0f);
    //Start drawing (point)
    glBegin(GL_POINTS);
    {<!-- -->
        //
        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(-1.0f, 0.0f, 0.0f);

        glVertex3f(-0.5f, 0.0f, 0.0f);

        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(0.0f, 0.0f, 0.0f);

        glColor3f(0.0f, 0.0f, 1.0f);
        glVertex3f(0.5f, 0.0f, 0.0f);

        glVertex3f(1.0f, 0.0f, 0.0f);
    }
    //end drawing
    glEnd();
}

2. Line GL_LINES 0x0001

//Start drawing (line segment)
glBegin(GL_LINES);
{<!-- -->
    glColor3f(0.0f, 0.0f, 0.0f);
    glVertex3f(-1.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 0.0f, 0.0f);
    glVertex3f(0.0f, -1.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
}
//end drawing
glEnd();

3. Line GL_LINE_LOOP 0x0002

//Start drawing (polygonal line)
glBegin(GL_LINE_LOOP);
{<!-- -->
    glColor3f(1,0,0);
    glVertex2f(-1.0f, 0.0f);

    glColor3f(0,1,0);
    glVertex2f(0.0f, -1.0f);

    glColor3f(0,0,1);
    glVertex2f(1.0f, 0.0f);

    glColor3f(1,1,1);
    glVertex2f(0.0f, 1.0f);
}
//end drawing
glEnd();

4. GL_LINE_STRIP 0x0003

//Start drawing (continuous line segments)
glBegin(GL_LINE_STRIP);
{<!-- -->
    glColor3f(1,0,0);
    glVertex2f(-0.5f, 0.0f);

    glColor3f(0,1,0);
    glVertex2f(0.0f, -0.5f);

    glColor3f(0,0,1);
    glVertex2f(0.5f, 0.0f);

    glColor3f(1,1,1);
    glVertex2f(0.0f, 0.5f);
}
//end drawing
glEnd();

5. GL_TRIANGLES 0x0004

First draw the points used

 //Point size
    glPointSize(6.0f);
    //
    glBegin(GL_POINTS);
    {<!-- -->
        glColor3f(0, 0, 0);
        
        glVertex3f(-0.9f, 0.2f, 0.0f);
        glVertex3f(-0.7f, -0.2f, 0.0f);
        glVertex3f(-0.5f, 0.2f, 0.0f);
        
        glVertex3f(-0.3f, -0.2f, 0.0f);
        glVertex3f(-0.1f, 0.2f, 0.0f);
        glVertex3f(0.1f, -0.2f, 0.0f);
        
        glVertex3f(0.3f, 0.2f, 0.0f);
        glVertex3f(0.5f, -0.2f, 0.0f);
        glVertex3f(0.7f, 0.2f, 0.0f);
    }
    glEnd();

GL_TRIANGLS rules for drawing triangles are as follows:
If the order of the added N coordinate points is { P0, P1, P2, … , P n-2< /sub>, P n-1, P ~n ~}
The triangle formed is: { P0, P1, P2 }, { P3, P< sub>4
, P5 } ,… , { Pn-5, Pn-4, P n-3 }, { Pn-2, Pn-1, Pn }

//Start drawing (triangle)
glBegin(GL_TRIANGLES);
{<!-- -->
    glColor3f(0, 0, 0);
    glVertex3f(-0.9f, 0.2f, 0.0f);
    glColor3f(1, 0, 0);
    glVertex3f(-0.7f, -0.2f, 0.0f);
    glColor3f(0, 1, 0);
    glVertex3f(-0.5f, 0.2f, 0.0f);
    
    glColor3f(1, 1, 0);
    glVertex3f(-0.3f, -0.2f, 0.0f);
    glColor3f(0, 0, 1);
    glVertex3f(-0.1f, 0.2f, 0.0f);
    glColor3f(1, 0, 1);
    glVertex3f(0.1f, -0.2f, 0.0f);

    glColor3f(0, 1, 1);
    glVertex3f(0.3f, 0.2f, 0.0f);
    glColor3f(1, 1, 1);
    glVertex3f(0.5f, -0.2f, 0.0f);
    glColor3f(0, 0, 0);
    glVertex3f(0.7f, 0.2f, 0.0f);
}
glEnd();

6. GL_TRIANGLE_STRIP 0x0005

The code used in section 5 remains unchanged, only glBegin(GL_TRIANGLE); is changed to glBegin(GL_TRIANGL_STRIP); and the result is as shown below.

GL_TRIANGL_STRIP The rules for drawing triangles are as follows:
If the order of the added N coordinate points is { P0, P1, P2, … , P n-2< /sub>, P n-1, P n }
The triangle formed is: { P0, P1, P2 }, { P1, P< sub>2
, P3 }, { P2, P3, P4} ,… , { Pn-3, Pn-2, Pn-1} , { Pn-2< /sub>, Pn-1, Pn}

7. GL_TRIANGLE_FAN 0x0006

The code used in section 5 remains unchanged, only glBegin(GL_TRIANGLE); is changed to glBegin(GL_TRIANGL_FAN); and the result is as shown below.

GL_TRIANGL_STRIP The rules for drawing triangles are as follows:
If the order of the added N coordinate points is { P0, P1, P2, … , P n-2< /sub>, P n-1, P n }
The triangle formed is: { P0, P1, P2 }, { P0, P< sub>2
, P3 } ,… , { P0, Pn-2, Pn- 1 },, { P0, Pn-1, Pn }

8.GL_QUADS 0x0007

Mark the points used

//Point size
glPointSize(6.0f);
//
glBegin(GL_POINTS);
{<!-- -->
//The order of adding points here is inconsistent with the order of drawing quadrilaterals. This is just to draw the points used for easy understanding and has nothing to do with drawing quadrilaterals.
    glColor3f(0, 0, 0);
    glVertex3f(-1.0f, -0.55f, 0.0f);
    glVertex3f(-1.0f, -0.95f, 0.0f);
    glVertex3f(-0.6f, -0.55f, 0.0f);
    glVertex3f(-0.6f, -0.95f, 0.0f);
    glVertex3f(-0.2f, -0.55f, 0.0f);
    glVertex3f(-0.2f, -0.95f, 0.0f);
    glVertex3f(0.2f, -0.95f, 0.0f);
    glVertex3f(0.2f, -0.55f, 0.0f);
    glVertex3f(0.6f, -0.55f, 0.0f);
    glVertex3f(0.6f, -0.95f, 0.0f);
    glVertex3f(1.0f, -0.95f, 0.0f);
    glVertex3f(1.0f, -0.55f, 0.0f);
}
glEnd();
//!GL_QUADS Quadrilaterals
glBegin(GL_QUADS);
{<!-- -->
    glColor3f(0, 0, 0);
    glVertex3f(-1.0f, -0.55f, 0.0f);
    glColor3f(1, 0, 0);
    glVertex3f(-1.0f, -0.95f, 0.0f);

    glColor3f(1, 1, 0);
    glVertex3f(-0.6f, -0.55f, 0.0f);
    glColor3f(0, 1, 0);
    glVertex3f(-0.6f, -0.95f, 0.0f);


    glColor3f(0, 0, 1);
    glVertex3f(-0.2f, -0.55f, 0.0f);
    glColor3f(1, 0, 1);
    glVertex3f(-0.2f, -0.95f, 0.0f);

    glColor3f(0, 1, 1);
    glVertex3f(0.2f, -0.95f, 0.0f);
    glColor3f(1, 1, 1);
    glVertex3f(0.2f, -0.55f, 0.0f);


    glColor3f(0, 0, 0);
    glVertex3f(0.6f, -0.55f, 0.0f);
    glColor3f(0, 0, 1);
    glVertex3f(1.0f, -0.55f, 0.0f);

    glColor3f(0, 1, 0);
    glVertex3f(0.6f, -0.95f, 0.0f);
    glColor3f(0, 1, 0);
    glVertex3f(1.0f, -0.95f, 0.0f);

}
glEnd();

As shown in the picture and sample code above, 12 points and 3 quadrilaterals composed of these 12 points are drawn. The order of adding point coordinates is marked in the picture.
From the above figure, the following characteristics can be analyzed:

  1. Add N vertices, each 4 forming a quadrilateral. (If N%4 is not 0, only N/4 quadrilaterals can be drawn, and the extra N%4 vertices are useless)
  2. Quadrilaterals are drawn in the order in which vertices are added
    (This part is my personal understanding, not sure if it is wrong)

9. GL_QUAD_STRIP 0x0008

The code used in Section 8 is continued, only glBegin(GL_QUADS); is changed to glBegin(GL_QUAD_STRIP); and the result is as shown below.

It can be found from the picture that the vertices can be shared, just like the adjacent sides of two buildings sharing a wall.
In the figure, there are { P0, P1, P2, P3, P4< /sub>,P5,P6,P7,P8,P9< /sub>,P10,P11} and other 12 vertices.
The quadrilateral composed of: { P0, P1, P2, P3 }, { P2, P3, P4, P5 }, { P4, P 5, P6, P7 }, { P6, P7 , P8, P9 }, { P8, P9, P10 sub>, P11 }
Draw order:
… 1. Define each quadrilateral, the first point is P0, the second point is P1, and the third point is P2< /sub>, the fourth point is P3
… 2. Draw the route: P0 –》 P1 –》P3, P0 – -》P2 –》P3, the quadrilateral formed is the final result
(This part is my personal understanding, not sure if it is wrong)

10. GL_POLYGON 0x0009

 //Polygon
    glBegin(GL_POLYGON);
    {<!-- -->
        //1
        glColor3f(0, 0, 0);
        glVertex3f(-0.8f, 0.6f, 0.0f);
        //2
        glColor3f(1, 0, 0);
        glVertex3f(-1.0f, 0.75f, 0.0f);
        //3
        glColor3f(0, 1, 0);
        glVertex3f(-0.8f, 0.90f, 0.0f);
        //3
        glColor3f(1, 1, 0);
        glVertex3f(-0.3f, 0.90f, 0.0f);
        //2
        glColor3f(0, 0, 1);
        glVertex3f(-0.1f, 0.75f, 0.0f);
        //1
        glColor3f(1, 0, 1);
        glVertex3f(-0.3f, 0.60f, 0.0f);
        //2
        glColor3f(1, 1, 1);
        glVertex3f(-0.4f, 0.75f, 0.0f);
        //1
        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex3f(-0.5f, 0.6f, 0.0f);
        //1
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(-0.6f, 0.6f, 0.0f);
        //2
        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(-0.7f, 0.75f, 0.0f);

    }
    glEnd();

    glBegin(GL_POINTS);
    {<!-- -->
        //1
        glColor3f(0, 0, 0);
        glVertex3f(-0.8f, 0.6f, 0.0f);
        //2
        glColor3f(1, 0, 0);
        glVertex3f(-1.0f, 0.75f, 0.0f);
        //3
        glColor3f(0, 1, 0);
        glVertex3f(-0.8f, 0.90f, 0.0f);
        //3
        glColor3f(1, 1, 0);
        glVertex3f(-0.3f, 0.90f, 0.0f);
        //2
        glColor3f(0, 0, 1);
        glVertex3f(-0.1f, 0.75f, 0.0f);
        //1
        glColor3f(1, 0, 1);
        glVertex3f(-0.3f, 0.60f, 0.0f);
        //2
        glColor3f(1, 1, 1);
        glVertex3f(-0.4f, 0.75f, 0.0f);
        //1
        glColor3f(1.0f, 1.0f, 1.0f);
        glVertex3f(-0.5f, 0.6f, 0.0f);
        //1
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(-0.6f, 0.6f, 0.0f);
        //2
        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(-0.7f, 0.75f, 0.0f);
    }
    glEnd();

    //polygon
    glBegin(GL_POLYGON);
    {<!-- -->
        //1
        glColor3f(0, 0, 0);
        glVertex3f(0.8f, 0.6f, 0.0f);
        //2
        glColor3f(1, 0, 0);
        glVertex3f(1.0f, 0.75f, 0.0f);
        //3
        glColor3f(0, 1, 0);
        glVertex3f(0.8f, 0.90f, 0.0f);
        //3
        glColor3f(1, 1, 0);
        glVertex3f(0.3f, 0.90f, 0.0f);
        //2
        glColor3f(0, 0, 1);
        glVertex3f(0.1f, 0.75f, 0.0f);
        //1
        glColor3f(1, 0, 1);
        glVertex3f(0.3f, 0.60f, 0.0f);
        //2
        glColor3f(1, 1, 1);
        glVertex3f(0.4f, 0.45f, 0.0f);
        //1
        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex3f(0.5f, 0.6f, 0.0f);
        //1
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(0.6f, 0.6f, 0.0f);
        //2
        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(0.7f, 0.45f, 0.0f);

    }
    glEnd();

    glBegin(GL_POINTS);
    {<!-- -->
        //1
        glColor3f(0, 0, 0);
        glVertex3f(0.8f, 0.6f, 0.0f);
        //2
        glColor3f(1, 0, 0);
        glVertex3f(1.0f, 0.75f, 0.0f);
        //3
        glColor3f(0, 1, 0);
        glVertex3f(0.8f, 0.90f, 0.0f);
        //3
        glColor3f(1, 1, 0);
        glVertex3f(0.3f, 0.90f, 0.0f);
        //2
        glColor3f(0, 0, 1);
        glVertex3f(0.1f, 0.75f, 0.0f);
        //1
        glColor3f(1, 0, 1);
        glVertex3f(0.3f, 0.60f, 0.0f);
        //2
        glColor3f(0.5f, 0.5f, 0.5f);
        glVertex3f(0.4f, 0.45f, 0.0f);
        //1
        glColor3f(1.0f, 0.0f, 1.0f);
        glVertex3f(0.5f, 0.6f, 0.0f);
        //1
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(0.6f, 0.6f, 0.0f);
        //2
        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(0.7f, 0.45f, 0.0f);
    }
    glEnd();

Drawing in order can only draw convex polygons, cannot draw concave polygons

Summary

The above content is based on personal study and understanding, and there may be errors.