1、计算机图形学程序课程设计题目:分别在四个视区内显示空间四周体三视图、透视投影图。学院:信息科学与技术学院专业:计算机科学与技术姓名:oc学号:oc电话:oc邮箱:oc目录一、设计概述(1)设计题目。2(2)设计规定。2(3)设计原理。2(4)算法设计。5(5)程序运营成果。9二、核心算法流程图。10三、程序源代码。12四、程序运营成果分析。24五、设计总结分析。25六、参照文献。26一设计概述 设计题目计算机图形学基本(第二版)陆枫 何云峰 编著电子工业出版社P228-7.16:运用OpenGL中多视区,分别在四个视区内显示图7-41所示空间四周体主视图、俯视图、侧视图、透视投影图。 设计规定
2、 设计内容: 1. 掌握主视图、俯视图、侧视图和透视投影变换矩阵; 2. 掌握透视投影图、三视图生成原理;功能规定:分别在四个视区内显示P228-图7-41所示空间四周体主视图、俯视图、侧视图、透视投影图。 设计原理正投影正投影依照投影面与坐标轴夹角可分为三视图和正轴测图。当投影面与某一坐标轴垂直时,得到投影为三视图,这时投影方向与这个坐标轴方向一致,否则,得到投影为正轴测图。1.主视图(V面投影)将三维物体向XOZ平面作垂直投影,得到主视图。由投影变换先后三维物体上点到主视图上点关系,其变换矩阵为:Tv=Txoz= 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1Tv为主视图投
3、影变换矩阵。简称主视图投影变换矩阵。2.侧视图(W面投影)将三维物体向YOZ平面作垂直投影,得到侧视图。为使侧视图与主视图在一种平面内,就要使W面绕Z轴正向旋转90。同步为了保证侧视图与主视图有一段距离,还要使W面再沿X方向平移一段距离x0,这样即得到侧视图。变换矩阵为:Tv=Tyoz= 0 0 0 0 -1 0 0 0 0 0 1 0 -x0 0 0 1Tv为主视图投影变换矩阵。简称主视图投影变换矩阵。3.俯视图(H面投影)将三维物体向XOY平面作垂直投影,得到俯视图。为使俯视图与主视图在一种平面内,就要使H面绕X轴负向旋转90。同步为了保证侧视图与主视图有一段距离,还要使H面再沿Z方向平移
4、一段距离-z0,这样即得到侧视图。变换矩阵为:Tv=Txoy= 1 0 0 0 0 0 -1 0 0 0 0 0 0 0z0 1Tv为主视图投影变换矩阵。简称主视图投影变换矩阵。三视图常作为重要工程施工图纸,由于在三视图上可以测量距离和角度。但一种三视图只有物体在一面投影,因此单独从某一种方面三视图很难想象出物体三维形状,只有将主视图、侧视图和俯视图放在一起,才有也许综合处物体空间形状。总来说三视图中主视图、俯视图和侧视图都是通过变换矩阵得来。透视投影-一点透视一点透视只有一种主灭点。灭点可以看做是无限远处一种点在投影面上点。一点透视普通环节: 将三维物体平移到恰当位置l,m,n. 令视点在z
5、轴,进行透视变换。 最后,向xoy面做正投影变换,将成果变换到xoy面上。如此一点透视变换矩阵为:Tv=Txoy= 1 0 0 0 0 1 0 0 0 0 0 1/d l m 0 1+n/d 算法设计核心算法1.构造类表达三维坐标系下点struct DefPointdouble x,y,z,tag;2.为顶点建立顶点表:PointMaxNum,TPointMaxNum,XOZPointMaxNum,XOYPointMaxNum,YOZPointMaxNum,YOYPointMaxNum3.定义各个视图变换矩阵以及变换函数变换矩阵:double Matrix44 = 1,0,0,0 , 0,1,
6、0,0 , 0,0,1,0 , 500,300,300,1 ;/初始化为单位矩阵double XOZ44 = 1,0,0,0 , 0,0,0,0 , 0,0,1,0 , 0,0,0,1 ;/主视图变换矩阵double XOY44 = 1,0,0,0 , 0,0,-1,0 , 0,0,0,0 , 0,0,-50,1 ;/俯视图变换矩阵double YOZ44 = 0,0,0,0 , -1,0,0,0 , 0,0,1,0 , -150,0,0,1 ;/侧视图变换矩阵double DD= -400,NN = -200,MM = -360,LL = -500;/DD视点,平移到恰当距离NN.MM.LL
7、double YOY44 = 1,0,0,0 , 0,1,0,0 , 0,0,0,1 / DD , LL,MM,0,1 + NN / DD ;/一点透视矩阵变换函数:void TransForm(DefPoint NewPoint,DefPoint OldPoint,double Tran44)变换函数重要功能是将三维图形顶点与变换矩阵相乘得到变换后矩阵。4.四个视图显示算法。void Display()glClear(GL_COLOR_BUFFER_BIT);OnCoordinate();glColor3f(0.0f,0.0f,0.0f);glBegin(GL_LINES);glVertex
8、2d(winWidth / 2,0); glVertex2d(winWidth / 2,winHeight);glVertex2d(0,winHeight / 2); glVertex2d(winWidth,winHeight / 2);glEnd();glColor3f(1.0f,0.0f,0.0f);OnDraw(XOZPoint);glColor3f(0.0f,1.0f,0.0f);OnDraw(XOYPoint);glColor3f(0.0f,0.0f,1.0f);OnDraw(YOZPoint);glColor3f(1.0f,0.0f,0.0f);OnDraw_O(YOYPoint)
9、;glutSwapBuffers();5.三视图划线算法/绘制指定图形void OnDraw(DefPoint TempPoint)glBegin(GL_LINES);for (int i = 0;iFaceNum;i+)int size = Facei.size();for (int j = 0;jsize;j+)glVertex2d(TempPointFaceij.x,TempPointFaceij.z); glVertex2d(TempPointFacei(j + 1) % size.x,TempPointFacei(j + 1) % size.z);glEnd();6一点透视划线算法/
10、绘制指定图形void OnDraw_O(DefPoint TempPoint)glColor3f(1.0f,0.0f,0.0f);glBegin(GL_LINES);glVertex2d(TempPoint0.x,TempPoint0.y); glVertex2d(TempPoint1.x,TempPoint1.y);glVertex2d(TempPoint0.x,TempPoint0.y);glVertex2d(TempPoint2.x,TempPoint2.y);glVertex2d(TempPoint0.x,TempPoint0.y); glVertex2d(TempPoint3.x,T
11、empPoint3.y);glVertex2d(TempPoint1.x,TempPoint1.y); glVertex2d(TempPoint2.x,TempPoint2.y);glVertex2d(TempPoint1.x,TempPoint1.y); glVertex2d(TempPoint3.x,TempPoint3.y);glVertex2d(TempPoint2.x,TempPoint2.y); glVertex2d(TempPoint3.x,TempPoint3.y);glEnd();glColor3f(0.0f,1.0f,0.0f);glBegin(GL_LINES);glVe
12、rtex2d(TempPoint0.x,TempPoint0.y); glVertex2d(0,0);glVertex2d(TempPoint1.x,TempPoint1.y); glVertex2d(0,0);glVertex2d(TempPoint2.x,TempPoint2.y); glVertex2d(0,0);glVertex2d(TempPoint3.x,TempPoint3.y); glVertex2d(0,0);glEnd();(5)程序运营成果二核心算法流程图(1)矩阵变换函数流程图(2)三视图绘制算法流程图三程序源代码#include stdafx.h#include#in
13、clude#includeusing namespace std;const int MaxNum = 200;vectorFace10;/最大数,面数vectorint winWidth = 1000,winHeight = 600;/窗口宽高int PointNum = 4,FaceNum = 4;/顾客图形顶点,面数double Matrix44 = 1,0,0,0 , 0,1,0,0 , 0,0,1,0 , 500,300,300,1 ;/初始化为单位矩阵double XOZ44 = 1,0,0,0 , 0,0,0,0 , 0,0,1,0 , 0,0,0,1 ;/主视图变换矩阵doub
14、le XOY44 = 1,0,0,0 , 0,0,-1,0 , 0,0,0,0 , 0,0,-50,1 ;/俯视图变换矩阵double YOZ44 = 0,0,0,0 , -1,0,0,0 , 0,0,1,0 , -150,0,0,1 ;/侧视图变换矩阵double DD= -400,NN = -200,MM = -360,LL = -500;/DD视点,平移到恰当距离NN.MM.LLdouble YOY44 = 1,0,0,0 , 0,1,0,0 , 0,0,0,1 / DD , LL,MM,0,1 + NN / DD ;/一点透视矩阵/定义图形顶点struct DefPointdouble
15、 x,y,z,tag;PointMaxNum,TPointMaxNum,XOZPointMaxNum,XOYPointMaxNum,YOZPointMaxNum,YOYPointMaxNum;/初始化自定义三维图形void ThPmidInit()PointNum = 4;Point0.x = 400,Point0.y = 0,Point0.z = 0,Point0.tag = 1;Point1.x = 400,Point1.y = 200,Point1.z = 0,Point1.tag = 1;Point2.x = 0,Point2.y = 200,Point2.z = 0,Point2.t
16、ag = 1;Point3.x = 200,Point3.y = 200,Point3.z = 200,Point3.tag = 1;FaceNum = 4;Face0.push_back(0);Face0.push_back(1);Face0.push_back(2);Face1.push_back(0);Face1.push_back(1);Face1.push_back(3);Face2.push_back(0);Face2.push_back(2);Face2.push_back(3);Face3.push_back(1);Face3.push_back(2);Face3.push_b
17、ack(3);/获得变换后顾客图形顶点void TransForm(DefPoint NewPoint,DefPoint OldPoint,double Tran44)for (int i = 0;iPointNum;i+)double tx = OldPointi.x,ty = OldPointi.y,tz = OldPointi.z,ttag = OldPointi.tag;NewPointi.x = tx*Tran00 + ty*Tran10 +tz*Tran20 + ttag*Tran30;NewPointi.y = tx*Tran01 + ty*Tran11 + tz*Tran21
18、+ ttag*Tran31;NewPointi.z = tx*Tran02 + ty*Tran12 + tz*Tran22 + ttag*Tran32;NewPointi.tag = tx*Tran03 + ty*Tran13 +tz*Tran23 + ttag*Tran33;if (NewPointi.tag != 0 & NewPointi.tag != 1)NewPointi.x /= NewPointi.tag,NewPointi.y /= NewPointi.tag,NewPointi.z /= NewPointi.tag,NewPointi.tag = 1;/重新指定窗口宽高voi
19、d ReShape(int w,int h)winWidth = w;winHeight = h;glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,winWidth,0.0,winHeight);/绘制指定图形void OnDraw(DefPoint TempPoint)glBegin(GL_LINES);for (int i = 0;iFaceNum;i+)int size = Facei.size();for (int j = 0;jsize;j+)glVertex2d(TempP
20、ointFaceij.x,TempPointFaceij.z); glVertex2d(TempPointFacei(j + 1) % size.x,TempPointFacei(j + 1) % size.z);glEnd();/绘制指定图形void OnDraw_O(DefPoint TempPoint)glColor3f(1.0f,0.0f,0.0f);glBegin(GL_LINES);glVertex2d(TempPoint0.x,TempPoint0.y); glVertex2d(TempPoint1.x,TempPoint1.y);glVertex2d(TempPoint0.x,
21、TempPoint0.y);glVertex2d(TempPoint2.x,TempPoint2.y);glVertex2d(TempPoint0.x,TempPoint0.y); glVertex2d(TempPoint3.x,TempPoint3.y);glVertex2d(TempPoint1.x,TempPoint1.y); glVertex2d(TempPoint2.x,TempPoint2.y);glVertex2d(TempPoint1.x,TempPoint1.y); glVertex2d(TempPoint3.x,TempPoint3.y);glVertex2d(TempPo
22、int2.x,TempPoint2.y); glVertex2d(TempPoint3.x,TempPoint3.y);glEnd();glColor3f(0.0f,1.0f,0.0f);glBegin(GL_LINES);glVertex2d(TempPoint0.x,TempPoint0.y); glVertex2d(0,0);glVertex2d(TempPoint1.x,TempPoint1.y); glVertex2d(0,0);glVertex2d(TempPoint2.x,TempPoint2.y); glVertex2d(0,0);glVertex2d(TempPoint3.x
23、,TempPoint3.y); glVertex2d(0,0);glEnd();/绘制坐标系void OnCoordinate()glColor3f(0.0f,0.0f,0.0f);glBegin(GL_LINES);glVertex2d(winWidth / 2,0); glVertex2d(winWidth / 2,winHeight);glVertex2d(0,winHeight / 2); glVertex2d(winWidth,winHeight / 2);/标记Z轴 glVertex2d(winWidth / 2 + 5,winHeight - 15); glVertex2d(wi
24、nWidth / 2 + 15,winHeight - 15);glVertex2d(winWidth / 2 + 5,winHeight - 25);glVertex2d(winWidth / 2 + 15,winHeight - 15);glVertex2d(winWidth / 2 + 5,winHeight - 25);glVertex2d(winWidth / 2 + 15,winHeight - 25);glVertex2d(winWidth / 2 - 5,winHeight - 5); glVertex2d(winWidth / 2,winHeight);glVertex2d(
25、winWidth / 2 + 5,winHeight - 5); glVertex2d(winWidth / 2,winHeight);/标记Y轴glVertex2d(winWidth / 2 + 25,0 + 15); glVertex2d(winWidth / 2 + 20,0 + 10);glVertex2d(winWidth / 2 + 15,0 + 15); glVertex2d(winWidth / 2 + 20,0 + 10);glVertex2d(winWidth / 2 + 20,0 + 10); glVertex2d(winWidth / 2 + 20,0 + 5);glV
26、ertex2d(winWidth / 2 - 5,0 + 5);glVertex2d(winWidth / 2,0);glVertex2d(winWidth / 2 + 5,0 + 5);glVertex2d(winWidth / 2,0);/标记Y轴glVertex2d(0 + 25,winHeight / 2 + 15); glVertex2d(0 + 20,winHeight / 2 + 10);glVertex2d(0 + 15,winHeight / 2 + 15); glVertex2d(0 + 20,winHeight / 2 + 10);glVertex2d(0 + 20,wi
27、nHeight / 2 + 10);glVertex2d(0 + 20,winHeight / 2 + 5);glVertex2d(0 + 5,winHeight / 2 + 5);glVertex2d(0,winHeight / 2);glVertex2d(0 + 5,winHeight / 2 - 5); glVertex2d(0,winHeight / 2);/标记X轴glVertex2d(winWidth - 25,winHeight / 2 + 15);glVertex2d(winWidth - 15,winHeight / 2 + 5);glVertex2d(winWidth -
28、25,winHeight / 2 + 5);glVertex2d(winWidth - 15,winHeight / 2 + 15);glVertex2d(winWidth - 5,winHeight / 2 - 5); glVertex2d(winWidth,winHeight / 2);glVertex2d(winWidth - 5,winHeight / 2 + 5); glVertex2d(winWidth,winHeight / 2);glEnd();/绘制图形void Display()glClear(GL_COLOR_BUFFER_BIT);OnCoordinate();glCo
29、lor3f(0.0f,0.0f,0.0f);glBegin(GL_LINES);glVertex2d(winWidth / 2,0); glVertex2d(winWidth / 2,winHeight);glVertex2d(0,winHeight / 2); glVertex2d(winWidth,winHeight / 2);glEnd();glColor3f(1.0f,0.0f,0.0f);OnDraw(XOZPoint);glColor3f(0.0f,1.0f,0.0f);OnDraw(XOYPoint);glColor3f(0.0f,0.0f,1.0f);OnDraw(YOZPoi
30、nt);glColor3f(1.0f,0.0f,0.0f);OnDraw_O(YOYPoint);glutSwapBuffers();/三维图形变换主函数void GetThPmidView()TransForm(XOZPoint,Point,XOZ);TransForm(XOYPoint,Point,XOY);TransForm(YOZPoint,Point,YOZ);TransForm(YOYPoint,Point,YOY);TransForm(XOZPoint,XOZPoint,Matrix);TransForm(XOYPoint,XOYPoint,Matrix);TransForm(Y
31、OZPoint,YOZPoint,Matrix);TransForm(YOYPoint,YOYPoint,Matrix);/初始化void Initial()for (int i = 0;i10;i+)Facei.clear();glClearColor(1.0f,1.0f,1.0f,1.0f);ThPmidInit();GetThPmidView();int main(int argc,char* argv)glutInit(&argc,argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);glutInitWindowSize(1000,600)
32、;glutInitWindowPosition(150,100);glutCreateWindow(三维图形 透视投影图&三视图 演示程序);glutDisplayFunc(Display);glutReshapeFunc(ReShape);Initial();glutMainLoop();return 0;四程序运营成果分析在程序中预先输入要实现三维物体顶点和面信息,然后通过矩阵变换函数,得到变换后函数。然后由划线函数绘制出图形。系统局限性及改进方案在完毕计算机图形学课程设计后,我发现尚有许多局限性,所学到知识还远远不够,以至于尚有某些功能不能较好完毕。其实我这个设计只是一种很简朴东西,仅仅
33、实现了最简朴透视投影图,三视图算法罢了,受限于知识缺少影响,不能实现较抱负设计。我以为较抱负设计是,最重要一点是增长设计灵动性,最大便利于顾客和观众,让她们觉得这个设计是不错东西。可以再程序中实现让顾客输入三维物体顶点和面信息,并且建立一种三维坐标系将图形放在原点处,使顾客一目了然,同步也将三视图置于二维坐标系中,并标出哪个是哪个图形,即各个图形代表意思。我以为解决以上问题只有通过在后来学习,对图形学和OpenGL有更深理解才有也许解决该问题。同步要彻底学好C+这门语言,没有精通语言,就无法实现更完美功能和设计。这次实践增强了我动手能力,提高和巩固了图形学方面知识,特别是软件方面。让我结识到把
34、理论应用到实践中去是多么重要。这个过程中,我耗费了大量时间和精力,更重要是,我在学会实践基本上,同步还懂得合伙精神重要性,学会了互相学习。这次课程设计终于顺利完毕了,在设计中遇到了诸多编程问题,最后在各位同窗和教师地协助下,终于游逆而解。同步,在教师那里我学到了诸多实用知识。我表达再次衷心感谢! 五设计总结分析紧张而高兴计算机图形学课程设计终于顺利完毕了,历时一周时间,其中包括着高兴,也有辛酸。我选设计题目是关于“透视投影和三视图设计”,开始时候觉得这个题目是比较简朴,不就是几种矩阵作运算罢了。其实否则,做了之后,发现设计思路虽然简朴,但我以为它真正困难地方是程序设计,是要如何设计实现矩阵算法
35、程序代码,如何让自己程序代码看起来简洁实现功能又强。但是在我在认真学习和在网上查找资料后最后完毕了设计,最后实现了透视投影和三视图算法。通过一周努力,我对图形学有了更深结识,突然发现图形学是一门很故意思课程,世界可以被你玩于股掌之中,想让它实现什么就可以调用函数实现功能,想要什么色彩都可以图形学在当前社会中是有诸多用处,任何物体模型都离不开使用到它,因此我会在将来学习生活中多注意这方面有关知识,掌握同样技能不是多余,而是为自己将来工作又增长了一份资本。六参照文献(1)计算机图形学基本(第二版)陆枫 何云峰 编著 电子工业出版社.(2)CSDN博客:作者:晓风残月。计算机图形学三维变换、三维观测与消隐算法实现。博客地址: