Use the GL_LINES function:
This primitive takes two vertices and draws a line segment between them. If the vertices are more than two, OpenGL draws a vertex for each pair of vertices of two vertices. If the application specifies n vertices, OpenGL renders n/2 line segments. If n is odd, OpenGL ignores the final vertex.
Example:
Main function:
the following code are write in main function
int main(int argc, char **argv)
{
glutInit(&argc, argv); // initializes the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //set the display mode
glutInitWindowSize(640,480); //set the window size
glutInitWindowPosition(100,100); //set the window position on screen
glutCreateWindow(“draw the line”); //open the screen window
glutDisplayFunc(Displayline);
myInit(); // additional initializations, if necessary
glutMainLoop(); //go into a perpetual loop
}
Define the the myInit(); function:
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}
void Displayline() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glEnd();
glFlush();
}
Note:
Both myInit(); and Displayline() must be define above main()
glbegin():
Marks the
beginning of a vertex-data list that describes a geometric primitive
glEnd():
Marks the
end of a vertex-data list.
0 comments:
Post a Comment