OpenGL编程基础基本图元的绘制上章回顾绘制三维物体的函数glutWireCube;glutSolidCube;//绘制正方体glutWireTorus;glutSolidTorus;//绘制圆环…………输入处理函数glutKeyboardFunc;//处理键盘事件glutMouseFunc;//处理鼠标按键事件glutMotionFunc;//处理鼠标移动事件空闲处理函数glutIdleFunc;双缓存glutInitDisplayMode(GLUT_DOUBLE)glutSwapBuffers()2/38本章内容OpenGL绘图操作清空窗口基本图元的绘制使用多边形构造曲面3/38内容进度OpenGL绘图操作清空窗口基本图元的绘制使用多边形构造曲面4/38OpenGL绘图操作三种基本的绘图操作清空窗口设置清屏颜色设置清除缓存绘制几何体点直线多边形绘制光栅物体二维图像位图字符内容进度OpenGL绘图操作清空窗口基本图元的绘制使用多边形构造曲面6/38清空窗口清空窗口设置清屏颜色(glClearColor)设置清除缓存(glClear)缓存种类颜色缓存深度缓存累积缓存(可以实现阴影、跟随等特效)模板缓存7/38清空窗口voidglClearColor(GLfloatred,GLfloatgreen,GLfloatblue,GLfloatalpha);功能:设置清屏颜色参数:red:红色分量的值green:绿色分量的值blue:蓝色分量的值alpha:透明度返回值:无备注:各个分量及alpha的值均应控制在[0,1]区间内8/38清空窗口voidglClear(GLbitfieldmask);功能:指定被清空的缓存参数:mask:被清空的缓存标志返回值:无备注:缓存标志可以为:GL_COLOR_BUFFER_BIT(颜色缓存)GL_DEPTH_BUFFER_BIT(深度缓存)GL_ACCUM_BUFFER_BIT(累积缓存)GL_STENCIL_BUFFER_BIT(模板缓存)9/38例:清空窗口清除颜色缓存glClearColor(0.0,0.0,0.0,0.0);glClear(GL_COLOR_BUFFER_BIT);清除深度缓存glEnable(GL_DEPTH_TEST);glClear(GL_DEPTH_BUFFER_BIT);可以一次设置清除多个缓存glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);10/38内容进度OpenGL绘图操作清空窗口基本图元的绘制使用多边形构造曲面11/38基本图元的绘制任何复杂的3D图形都是由几种基本图元构成的如点、直线、三角形、四边形、多边形等OpenGL允许程序绘制一些基本图元点线段三角形四边形多边形绘制图元需要用到3个函数glBegin()glEnd()glVertex3f()12/38基本图元的绘制voidglBegin(GLenummode);功能:开始绘制图元参数:mode:图元类型,可取下列值GL_POINTS绘制点GL_LINES绘制线段GL_LINE_STRIP绘制相连的线段GL_LINE_LOOP绘制相连的线段,并将第一个和最后一个顶点相连GL_TRIANGLES绘制三角形GL_TRIANGLE_STRIP绘制三角形带GL_TRIANGLE_FAN绘制三角形扇GL_QUADS绘制四边形GL_QUAD_STRIP绘制四边形带GL_POLYGON绘制凸多边形13/38基本图元的绘制voidglEnd(void);功能:结束绘制图元参数:无返回值:无备注:无14/38基本图元的绘制voidglVertex3f(GLfloatx,GLfloaty,GLfloatz);功能:设置顶点坐标参数:x:顶点的X轴坐标y:顶点的Y轴坐标z:顶点的Z轴坐标返回值:无备注:glVertex3f函数只能出现在glBegin和glEnd之间15/38点的绘制点的绘制glBegin(GL_POINTS);glVertex3f(-0.5,0.0,0.0);glVertex3f(0.0,0.5,0.0);glVertex3f(0.5,0.0,0.0);glEnd();16/38点的绘制voidglPointSize(GLfloatsize);功能设置点的大小参数:size:指定点的大小,以像素为单位返回值:无备注:size的大小不应小于1.0即点的大小不应小于1个像素17/38例:点的绘制glPointSize(10.0);glBegin(GL_POINTS);glVertex3f(-0.5,0.0,0.0);glVertex3f(0.0,0.5,0.0);glVertex3f(0.5,0.0,0.0);glEnd();线段的绘制线段图元的绘制glBegin(GL_LINES);glVertex3f(-0.3,0.0,0.0);glVertex3f(-0.2,0.5,0.0);glVertex3f(-0.1,0.0,0.0);glVertex3f(0.0,0.5,0.0);glEnd();19/38线段的绘制voidglLineWidth(GLfloatwidth);功能设置线段的宽度参数:width:指定线段的宽度,以像素为单位返回值:无备注:width的大小不应小于1.0即线段的宽度不应小于1个像素20/38例:线段的绘制glLineWidth(5.0);glBegin(GL_LINES);glVertex3f(-0.3,0.0,0.0);glVertex3f(-0.2,0.5,0.0);glVertex3f(-0.1,0.0,0.0);glVertex3f(0.0,0.5,0.0);glEnd();相连线段的绘制相连线段的绘制1glBegin(GL_LINE_STRIP);glVertex3f(-0.3,0.0,0.0);glVertex3f(-0.2,0.5,0.0);glVertex3f(-0.1,0.0,0.0);glVertex3f(0.0,0.5,0.0);glEnd();22/38相连线段的绘制相连线段的绘制2glBegin(GL_LINE_LOOP);glVertex3f(-0.3,0.0,0.0);glVertex3f(-0.2,0.5,0.0);glVertex3f(-0.1,0.0,0.0);glVertex3f(0.0,0.5,0.0);glEnd();23/38三角形的绘制三角形的绘制glBegin(GL_TRIANGLES);glVertex3f(-0.3,0.0,0.0);glVertex3f(-0.2,0.5,0.0);glVertex3f(-0.1,0.0,0.0);glVertex3f(0.0,0.5,0.0);glVertex3f(0.1,0.0,0.0);glVertex3f(0.2,0.5,0.0);glEnd();24/38三角形带的绘制三角形带的绘制glBegin(GL_TRIANGLE_STRIP);glVertex3f(-0.3,0.0,0.0);glVertex3f(-0.2,0.5,0.0);glVertex3f(-0.1,0.0,0.0);glVertex3f(0.0,0.5,0.0);glVertex3f(0.1,0.0,0.0);glVertex3f(0.2,0.5,0.0);glEnd();25/38三角形扇的绘制三角形扇的绘制glBegin(GL_TRIANGLE_FAN);glVertex3f(-0.3,0.0,0.0);glVertex3f(-0.2,0.5,0.0);glVertex3f(-0.1,0.0,0.0);glVertex3f(0.0,0.5,0.0);glVertex3f(0.1,0.0,0.0);glVertex3f(0.2,0.5,0.0);glEnd();26/38四边形的绘制四边形的绘制glBegin(GL_QUADS);glVertex3f(0.25,0.25,0.0);glVertex3f(0.75,0.25,0.0);glVertex3f(0.75,0.75,0.0);glVertex3f(0.25,0.75,0.0);glEnd();27/38多边形的绘制多边形的绘制glBegin(GL_POLYGON);glVertex3f(0.25,0.25,0.0);glVertex3f(0.75,0.25,0.0);glVertex3f(0.95,0.85,0.0);glVertex3f(0.15,0.75,0.0);glEnd();注意:OpenGL中只能绘制凸多边形28/38多边形的绘制voidglPolygonMode(GLenumface,GLenummode);功能设置多边形的绘制模式参数:face:指定正面或背面mode:指定绘制模式返回值:无备注:face的取值为:GL_FRONT//正面GL_BACK//背面GL_FRONT_AND_BACK//正面和背面29/38多边形的绘制mode的取值为:GL_POINT//顶点方式绘制GL_LINE//轮廓方式绘制GL_FILL//填充方式绘制30/38多边形的绘制多边形的绘制glPolygonMode(GL_FRONT,GL_LINE);glBegin(GL_POLYGON);glVertex3f(0.25,0.25,0.0);glVertex3f(0.75,0.25,0.0);glVertex3f(0.95,0.85,0.0);glVertex3f(0.15,0.75,0.0);glEnd();31/38练习不使用glutWireCube()函数,绘制边长为0.5的线框正方体不使用glutSolidCube()函数,绘制边长为0.5的填充正方体彩色金字塔Shapes工具(可以看到修改绘图参数的效果)32/38编写绘图函数应注意的问题编写绘图函数应遵循以下格式voiddisplay(void){//窗口清屏////绘图////交换缓存//}33/38编写绘图函数应注意的问题例如:voiddisplay(void){//窗口清屏glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//绘图glBegin(GL_TRIANGLES);glVertex3f(-0.5,0.0,0.0);glVertex3f(0.0,0.5,0.0);glVertex3f(0.5,0.0,0.0);glEnd();//交换缓存glutSwapBuffers();}34/38编写绘图函数应注意的问题若使用单缓存绘图模式,需要在绘图函数的最后调用glFlush();若使用单缓存绘图模式,则不能在绘图函数中交换缓存(glutSwapBuffers)若使用双缓存绘图模式,必须且只能在绘图函数中交换一次缓存(glutSwapBuffers)glBegin,glEnd不能嵌套使用35/38编写绘图函数应注意的问题例:该程序使用单缓存绘图模式voiddisplay(void){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glBegin(GL_POINTS);glVertex3f(-0.5,0.0,0.0);glVertex3f(0.0,0.5,0.0);glEnd();glutSwapBuffers();}36/38交换缓存出错编写绘图函数应注意的问题例:该程序使用双缓存绘图模式voiddisplay(void){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_