1. Foreword:
The method of drawing lines in Delphi is basically the same as that of drawing points. The difference lies in the parameters of glBegin(). The drawing framework code can refer to the code in OpenGL2d drawing initialization under Delphi, the address is //www.VeVB.COm/article/52141.htm. The modified part is the content of the Draw function.
2. Line drawing steps:
GL_LINES: Treat each vertex as an independent line segment. A total of n line segments are defined between vertices 2n-1 and 2n, and a total of N/2 line segments are drawn.
GL_LINE_STRIP: Draw a set of line segments connected in sequence from the first vertex to the last vertex. The n and n+1 vertices define line segment n. A total of n-1 line segments are drawn.
GL_LINE_LOOP: Draw a set of line segments connected in sequence from the first vertex to the last vertex, and then the last vertex is connected to the first vertex. The nth and n+1 vertices define line segment n, and a total of n line segments are drawn.
The following functions may be used to draw lines:
glLineWidth() sets the line width
glShadeModel() sets the color transition mode
glColor3f() sets the line color
procedure TForm1.Draw;begin
// Clear the buffer
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
// ----------- GL_LINES -----------
//Set line width
glLineWidth(5);
// Gradient color between two points
glShadeModel(GL_SMOOTH);
glBegin(GL_LINES); // draw lines
//Set the color of the starting line
glColor3f(1, 0, 0);
// draw starting point
glVertex2f(50, 50);
//Set the color of the end point line
glColor3f(0, 0, 1);
//End point of painting
glVertex2f(200, 200);
glEnd;
// Same color between two points
glShadeModel(GL_FLAT);
glBegin(GL_LINES);
glColor3f(0, 1, 0);
glVertex2f(200, 50);
glColor3f(1, 1, 0);
glVertex2f(50, 200);
glEnd;
// ----------- GL_LINE_LOOP -----------
// Three points close the loop to form a triangle glShadeModel(GL_SMOOTH); does not work
glLineWidth(1);
glBegin(GL_LINE_LOOP);
glColor3f(0, 0, 1);
glVertex2f(250, 50);
glVertex2f(250, 200);
glVertex2f(280, 150);
glEnd;
// ----------- GL_LINE_STRIP -----------
// Multi-point polyline glShadeModel(GL_SMOOTH); does not work
glLineWidth(1);
glBegin(GL_LINE_STRIP);
glColor3f(1, 0, 255);
glVertex2f(350, 50);
glVertex2f(400, 200);
glVertex2f(480, 150);
glVertex2f(490, 60);
glEnd;
SwapBuffers(FDC); //Swap the contents of the double buffer, which will copy the graphics just drawn to the screen.
end;
You can see the color gradient of the first X line. OpenGL has this style by default. OpenGL provides a function glShadeModel to decide how to handle this different color situation.
glShadeModel(GL_SMOOTH); // Smooth mode, which is also the default processing method of opengl, achieves the effect of color gradient glShadeModel(GL_FLAT); // Monochrome mode, the color of the line segment is based on the color of a certain vertex, which is determined by the specific implementation . To avoid uncertainty, try to use the same color across polygons
After testing, it was found that the gradient effect is invalid under the GL_LINE_LOOP and GL_LINE_STRIP styles! The specific reasons need to be further ascertained.
In addition, OpenGL provides anti-aliasing function, the function is
glEnable(GL_LINE_SMOOTH); //Enable glHint(GL_LINE_SMOOTH,GL_NICEST); glDisable(GL_LINE_SMOOTH); //Close
After using it, I found that the anti-aliasing effect was not obvious. It remains to be further studied.
Click here to download the complete code