Reference resources Click Open Link
Program code:
void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); glBegin( /* Fill in the pattern you want here. */ ); /* Use glVertex* series functions here */ /* Specify the desired vertex position */ glEnd(); glFlush(); }
1. Callable functions between glBegin() and glEnd()
Functional Meaning
glVertex*() Sets vertex coordinates
glColor*() Sets the current color
glIndex*() Sets the current color table
glNormal*() Sets Normal Coordinates
glCoord*() generates coordinates
glCallList(),glCallLists() executes the display list
glTexCoord*() Sets texture coordinates
glEdgeFlag*() controls boundary rendering
glMaterial*() Setting Material
Table 7-2 Callable functions between glBegin() and glEnd()
glVertex3f() indicates that the function belongs to gl library and the parameters are three float parameter pointers. We use glVertex*() to represent this kind of function.
2. Geometric primitive types and descriptions
Type description
GL_POINTS Single Vertex Set
GL_LINES Multi-group Double Vertex Line Segments
GL_POLYGON Single Simple Filled Convex Polygon
GL_TRAINGLES Multi-group Independent Filling Triangle
GL_QUADS Multiple Groups of Independently Filled Quadrilateral
GL_LINE_STRIP Unclosed Fold Line
GL_LINE_LOOP Closed Fold
GL_TRAINGLE_STRIP Linear Continuously Filled Triangular Strings
GL_TRAINGLE_FAN Sector Continuously Filled Triangular Strings
GL_QUAD_STRIP Filling Quadrilateral Strings Continuously
Example 2
/* Let the five vertices of a Pentagon be located as follows: A E B D C Firstly, according to the sequence equation of cosine theorem, the distance a from the center to the vertex of a pentagonal star is calculated. (Suppose that the edge length of a Pentagon corresponding to a regular pentagon is.0) a = 1 / (2-2*cos(72*Pi/180)); Then, according to the definition of sine and cosine, the x coordinates bx and y coordinates by of B and y coordinates of C are calculated. (Suppose that the center of the Pentagon is at the origin of the coordinates. bx = a * cos(18 * Pi/180); by = a * sin(18 * Pi/180); cy = -a * cos(18 * Pi/180); The coordinates of five points can be expressed simply by the above four quantities and some constants. */ #include <math.h> const GLfloat Pi = 3.1415926536f; void myDisplay(void) { GLfloat a = 1 / (2-2*cos(72*Pi/180)); GLfloat bx = a * cos(18 * Pi/180); GLfloat by = a * sin(18 * Pi/180); GLfloat cy = -a * cos(18 * Pi/180); GLfloat PointA[2] = { 0, a }, PointB[2] = { bx, by }, PointC[2] = { 0.5, cy }, PointD[2] = { -0.5, cy }, PointE[2] = { -bx, by }; glClear(GL_COLOR_BUFFER_BIT); // In order of A->C->E->B->D->A, the pentagram can be drawn in one stroke. glBegin(GL_LINE_LOOP); glVertex2fv(PointA); glVertex2fv(PointC); glVertex2fv(PointE); glVertex2fv(PointB); glVertex2fv(PointD); glEnd(); glFlush(); }
Example 3
/*
Since the default coordinate values of OpenGL can only range from - 1 to 1, (you can modify them, but leave the method for later.)
So we set a factor factor to reduce all coordinate values in equal proportion.
So you can draw more sinusoidal cycles.
Try to modify factor values and observe changes
*/
#include <math.h> const GLfloat factor = 0.1f; void myDisplay(void) { GLfloat x; glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_LINES); glVertex2f(-1.0f, 0.0f); glVertex2f(1.0f, 0.0f); // The above two points can draw the x-axis. glVertex2f(0.0f, -1.0f); glVertex2f(0.0f, 1.0f); // The above two points can draw the y axis. glEnd(); glBegin(GL_LINE_STRIP); for(x=-1.0f/factor; x<1.0f/factor; x+=0.01f) { glVertex2f(x*factor, sin(x)*factor); } glEnd(); glFlush(); }
Note (0,0 points in the center of the image)
Point size defaults to 1 pixel, but it can also be changed. The command to change is glPointSize, whose function prototype is as follows:
void glPointSize(GLfloat size);
The size must be greater than 0.0f, with a default value of 1.0f in pixels.
Note: For specific OpenGL implementations, there is a limit to the size of the point. If the size of the setting exceeds the maximum value, the setting may be problematic.
void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); glPointSize(5.0f); glBegin(GL_POINTS); glVertex2f(0.0f, 0.0f); glVertex2f(0.5f, 0.5f); glEnd(); glFlush(); }
Draw the dotted line.
First, glEnable(GL_LINE_STIPPLE) is used to start the dotted line mode (which can be turned off using glDisable(GL_LINE_STIPPLE).
Then, use glLineStipple to set the style of dashed lines.
void glLineStipple(GLint factor, GLushort pattern);
Patterns are 16-length sequences of 1 and 0. From the lowest point of view, if 1, the factor points that should be drawn next on the line will be drawn as real; if 0, the factor points that should be drawn next on the line will be drawn as virtual.
void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_LINE_STIPPLE); glLineStipple(4, 0x0F0F); glLineWidth(10.0f); glBegin(GL_LINES); glVertex2f(0.0f, 0.0f); glVertex2f(0.5f, 0.5f); glEnd(); glFlush(); }