OpenGL基础3

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

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_

1 / 46
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功