计算机图形学课程设计 透视投影图 三视图(15页).doc
-计算机图形学课程设计 透视投影图 三视图-第 15 页计算机图形学程序课程设计题目:分别在四个视区内显示空间四面体的三视图、透视投影图。学院:信息科学与技术学院专业:计算机科学与技术姓名:oc学号:oc电话:oc邮箱:oc目录一、设计概述(1)设计题目。2(2)设计要求。2(3)设计原理。2(4)算法设计。5(5)程序运行结果。9二、核心算法流程图。10三、程序源代码。12四、程序运行结果分析。24五、设计总结分析。25六、参考文献。26一设计概述 设计题目计算机图形学基础(第二版)陆枫 何云峰 编著电子工业出版社:利用OpenGL中的多视区,分别在四个视区内显示图7-41所示空间四面体的主视图、俯视图、侧视图、透视投影图。 设计要求 设计内容: 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为主视图的投影变换矩阵。简称主视图投影变换矩阵。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方向平移一段距离-z0,这样即得到侧视图。变换矩阵为:Tv=Txoy= 1 0 0 0 0 0 -1 0 0 0 0 0 0 0z0 1Tv为主视图的投影变换矩阵。简称主视图投影变换矩阵。三视图常作为主要的工程施工图纸,因为在三视图上可以测量距离和角度。但一种三视图只有物体在一面的投影,所以单独从某一个方面的三视图很难想象出物体的三维形状,只有将主视图、侧视图和俯视图放在一起,才有可能综合处物体的空间形状。总的来说三视图中主视图、俯视图和侧视图都是通过变换矩阵得来的。透视投影-一点透视一点透视只有一个主灭点。灭点可以看做是无限远处的一个点在投影面上的点。一点透视的一般步骤: 将三维物体平移到适当位置l,m,n. 令视点在z轴,进行透视变换。 最后,向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, 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视点,平移到适当距离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);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(YOZPoint);glColor3f(1.0f, 0.0f, 0.0f);OnDraw_O(YOYPoint);glutSwapBuffers();5.三视图的划线算法/绘制指定的图形void OnDraw(DefPoint TempPoint)glBegin(GL_LINES);for (int i = 0; i<FaceNum; i+)int size = Facei.size();for (int j = 0; j<size; j+)glVertex2d(TempPointFaceij.x, TempPointFaceij.z); glVertex2d(TempPointFacei(j + 1) % size.x, TempPointFacei(j + 1) % size.z);glEnd();6一点透视的划线算法/绘制指定的图形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, TempPoint3.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);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,TempPoint3.y); glVertex2d(0, 0);glEnd();(5)程序运行结果二核心算法流程图(1)矩阵变换函数流程图(2)三视图绘制算法流程图三程序源代码#include "stdafx.h"#include<glut.h>#include<vector>#include<iostream>using namespace std;const int MaxNum = 200;vector<int>Face10;/最大数,面数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 ;/主视图变换矩阵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;double YOY44 = 1, 0, 0, 0 , 0, 1, 0, 0 , 0, 0, 0, 1 / DD , LL, MM, 0, 1 + NN / DD ;/一点透视矩阵/定义图形的顶点struct DefPointdouble 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.tag = 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_back(3);/获得变换后的用户图形的顶点void TransForm(DefPoint NewPoint, DefPoint OldPoint, double Tran44)for (int i = 0; i<PointNum; 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 + 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;/重新指定窗口的宽高void 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; i<FaceNum; i+)int size = Facei.size();for (int j = 0; j<size; j+)glVertex2d(TempPointFaceij.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, 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(TempPoint2.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, 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(winWidth / 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(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);glVertex2d(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, winHeight / 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 - 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();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);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);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(YOZPoint, YOZPoint, Matrix);TransForm(YOYPoint, YOYPoint, Matrix);/初始化void Initial()for (int i = 0; i<10; 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);glutInitWindowPosition(150, 100);glutCreateWindow("三维图形 透视投影图&三视图 演示程序");glutDisplayFunc(Display);glutReshapeFunc(ReShape);Initial();glutMainLoop();return 0;四程序运行结果分析在程序中预先输入要实现的三维物体的顶点和面的信息,然后经过矩阵变换函数,得到变换后的函数。然后由划线函数绘制出图形。系统不足及改进方案在完成计算机图形学课程设计后,我发现还有许多不足,所学到的知识还远远不够,以至于还有一些功能不能很好完成。其实我的这个设计只是一个很简单的东西,仅仅实现了最简单的透视投影图,三视图的算法罢了,受限于知识缺乏的影响,不能实现较理想的设计。我认为较理想的设计是,最重要的一点是增加设计的灵动性,最大便利于用户和观众,让他们觉得这个设计是不错的东西。可以再程序中实现让用户输入三维物体的顶点和面的信息,并且建立一个三维坐标系将图形放在原点处,使用户一目了然,同时也将三视图置于二维坐标系中,并标出哪个是哪个图形,即各个图形代表的意思。我认为解决以上问题只有通过在以后的学习,对图形学和OpenGL有更深的了解才有可能解决该问题。同时要彻底学好C+这门语言,没有精通的语言,就无法实现更完美的功能和设计。这次实践增强了我的动手能力,提高和巩固了图形学方面的知识,特别是软件方面。让我认识到把理论应用到实践中去是多么重要。这个过程中,我花费了大量的时间和精力,更重要的是,我在学会实践的基础上,同时还懂得合作精神的重要性,学会了互相学习。这次课程设计终于顺利完成了,在设计中遇到了很多编程问题,最后在各位同学和老师地帮助下,终于游逆而解。同时,在老师那里我学到了很多实用的知识。我表示再次衷心的感谢! 五设计总结分析紧张而愉快的计算机图形学课程设计终于顺利完成了,历时一周时间,其中包含着快乐,也有辛酸。我选的设计题目是关于“透视投影和三视图的设计”,开始的时候觉得这个题目是比较简单的,不就是几个矩阵作运算罢了。其实不然,做了之后,发现设计思路虽然简单,但我认为它真正困难的地方是程序设计,是要怎样设计实现矩阵算法的程序代码,怎样让自己的程序代码看起来简练实现的功能又强。不过在我在认真学习和在网上查找资料后最终完成了设计,最终实现了透视投影和三视图的算法。通过一周的努力,我对图形学有了更深的认识,突然发现图形学是一门很有意思的课程,世界可以被你玩于股掌之中,想让它实现什么就可以调用函数实现功能,想要什么色彩都可以的图形学在现在的社会中是有很多用处的,任何物体模型都离不开使用到它,所以我会在将来的学习生活中多注意这方面的相关知识的,掌握一样技能不是多余的,而是为自己将来工作又增加了一份资本。六参考文献(1)计算机图形学基础(第二版)陆枫 何云峰 编著 电子工业出版社.(2)CSDN博客:作者:晓风残月。计算机图形学三维变换、三维观察与消隐算法的实现。博客地址: