比如我们可以同时清除颜色缓冲区和深度缓冲区
glClearColor (0.0, 0.0, 0.0, 0.0);//指定颜色缓冲区清除为黑色 glClearDepth(1.0);//指定深度缓冲区的清除值为1.0 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//指定要清除的缓冲区并清除
glColor3f(1.0,1.0,1.0);描述几何物体的顶点
glVertex2s(2,3); glVertex3d(0.0,0.0,3.14); glVertex4f(2.3,1.0,-2.2,112.3);绘制结束后最好强制绘制完成
void glFlush();//强制以前的opengl命令开始执行,保证它们在有限时间内完成。 void glFinish();//强制以前的opengl命令完成执行。在命令执行完成前这个函数不会返回。点和线宽度
void glPointSize(GLfloat size);//设置被渲染点的宽度,以pixel为单位,默认为1.0 void glLineWidth(GLfloat width);//以像素为单位设置宽度,用于直线的渲染设置直线的点画模式:
factor为重复因子,pattern为16为二进制数(1画0不画) 使用前必须调用glEnable(GL_LINE_STIPPLE)
void glLineStipple(GLint factor,GLushort pattern);
#include <GL/glut.h> #include <stdlib.h> #define drawOneLine(x1,y1,x2,y2) glBegin(GL_LINES); \ glVertex2f ((x1),(y1)); glVertex2f ((x2),(y2)); glEnd(); void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT);//采用恒定着色模式,使用图元中某个顶点的颜色来渲染整个图元。 } void display(void) { int i; glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); //白色 glEnable (GL_LINE_STIPPLE); //开启直线点画功能 //效果1 glLineStipple (1, 0x0101); //设置当前直线的点画模式 drawOneLine (50.0, 125.0, 150.0, 125.0); glLineStipple (1, 0x00FF); //设置当前直线的点画模式 drawOneLine (150.0, 125.0, 250.0, 125.0); glLineStipple (1, 0x1C47); //设置当前直线的点画模式 drawOneLine (250.0, 125.0, 350.0, 125.0); //效果2 glLineWidth (5.0); //设置线宽 glLineStipple (1, 0x0101); drawOneLine (50.0, 100.0, 150.0, 100.0); glLineStipple (1, 0x00FF); drawOneLine (150.0, 100.0, 250.0, 100.0); glLineStipple (1, 0x1C47); drawOneLine (250.0, 100.0, 350.0, 100.0); glLineWidth (1.0); //效果3 glLineStipple (1, 0x1C47); glBegin (GL_LINE_STRIP); for (i = 0; i < 7; i++) glVertex2f (50.0 + ((GLfloat) i * 50.0), 75.0); glEnd (); //效果4 for (i = 0; i < 6; i++) { drawOneLine (50.0 + ((GLfloat) i * 50.0), 50.0, 50.0 + ((GLfloat)(i+1) * 50.0), 50.0); } //效果5 glLineStipple (5, 0x1C47); //重复因子为5 drawOneLine (50.0, 25.0, 350.0, 25.0); glDisable (GL_LINE_STIPPLE); glFlush (); } void reshape (int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h);//调整绘图的像素矩阵大小 glMatrixMode (GL_PROJECTION); //将当前矩阵指定为投影矩阵 glLoadIdentity (); //把当前矩阵设为单位矩阵 gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; } } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (400, 150); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); //移动窗口或者改变窗口大小的时候调用 glutKeyboardFunc(keyboard); glutMainLoop(); return 0; }