计算机图形学 第五讲 图形变换
- 格式:ppt
- 大小:789.50 KB
- 文档页数:45
计算机图形学图形变换实验五:图形变换⼀、实验⽬的:1、掌握图形变换的基本⽅法。
2、初步掌握映射菜单消息和捕获键盘消息的⽅法。
⼆、实验内容及要求:1、以三⾓形为例,使⽤Visual C++实现⼆维图形的平移、旋转和缩放功能。
2、每⼈单独完成实验。
3、按要求撰写实验报告,写出实验⼼得,并在实验报告中附上程序的核⼼算法代码。
三、实验设备:微机,Visual C++6.0四、实验内容和步骤:1、打开VC,新建⼀个MFC Appwizard项⽬,选择创建单⽂档⼯程(SDI⼯程)。
假设⼯程名为Transform。
如图1和图2所⽰。
图1图22、在图2的界⾯上点击Finish,完成⼯程的创建。
3、在TransformView.h⽂件中,加⼊如下代码:public:CPoint Pt[3]; //存储三⾓形的三个顶点float dAngle; //存储三⾓形旋转的⾓度4、在类CTransformView的构造函数中定义三⾓形的三个顶点的初始坐标和dAngle的初值,代码如下;CTransformView::CTransformView(){// TODO: add construction code herePt[0].x = 200; Pt[0].y = 220;Pt[1].x = 260; Pt[1].y = 300;Pt[2].x = 360; Pt[2].y = 180;dAngle = 0.0;}5、在类CTransformView中添加成员函数void DrawTriangle(CDC *pDC),并实现该函数。
(该部分代码请同学们⾃⼰实现,为了简便编程,可以使⽤MoveTo和LineTo函数,也可以调⽤⾃⼰在实验2中编写的DDA或者Bresenham画线函数);6、在类CTransformView的OnDraw()函数中添加绘制三⾓形的代码;void CTransformView::OnDraw(CDC* pDC){CTransformDoc* pDoc = GetDocument();ASSERT_V ALID(pDoc);// TODO: add draw code for native data here}7、映射菜单消息,⽅法是打开ResourceView菜单,依次展开MENU \ IDR_MAINFRAME,添加“图形变换”主菜单项,在其下添加“平移”,如图3所⽰。
第五章图形变换重点:掌握二维几何变换、二维观察变换、三维几何变换以及三维观察变换。
难点:理解常用的平移、比例、旋转变换,特别是复合变换。
课时安排:授课4学时。
图形变换包括二维几何变换,二维观察变换,三维几何变换和三维观察变换。
为了能使各种几何变换(平移、旋转、比例等)以相同的矩阵形式表示,从而统一使用矩阵乘法运算来实现变换的组合,现都采用齐次坐标系来表示各种变换。
齐次坐标系齐次坐标系:n维空间中的物体可用n+1维齐次坐标空间来表示。
例如二维空间直线ax+by+c=0,在齐次空间成为aX+bY+cW=0,以X、Y和W为三维变量,构成没有常数项的三维平面(因此得名齐次空间)。
点P(x、y)在齐次坐标系中用P(wx,wy,w)表示,其中W是不为零的比例系数。
所以从n维的通常空间到n+1维的齐次空间变换是一到多的变换,而其反变换是多到一的变换。
例如齐次空间点P(X、Y、W) 对应的笛卡尔坐标是x=X/W和y=Y/W。
将通常笛卡尔坐标用齐次坐标表示时,W的值取1。
采用齐次坐标系可以将平移、比例、旋转这三种基本变换都以相同的矩阵形式来表示,并统一地用矩阵乘法来实现变换的组合。
齐次坐标系在三维透视变换中有更重要的作用,它使非线形变换也能采用线形变换的矩阵表示形式。
5.1 二维几何变换二维几何变换就是在平面上对二维点的坐标进行变换,从而形成新的坐标。
二维几何变换主要包括:平移、比例、旋转、对称、错切、仿射和复合变换。
5.1.1 二维平移变换如图所示,它使图形移动位置。
新图p'的每一图元点是原图形p中每个图元点在x和y方向分别移动Tx和Ty产生,所以对应点之间的坐标值满足关系式x'=x+Txy'=y+Ty可利用矩阵形式表示成:[x' y']=[x y]+[Tx Ty]简记为:P'=P+T,T=[Tx Ty]是平移变换矩阵(行向量)。
从矩阵形式来看,平移变换是矩阵加法,而比例和旋转变换则是矩阵乘法。
贵州大学实验报告学院:计算机科学与技术专业:计算机科学与技术班级:计科131实验内容#include"stdafx.h"#include<glut.h>#include<stdlib.h>#include<math.h>GLsizei winWidth = 600, winHeight = 600;GLfloat xwcMin = 0.0, xwcMax = 225.0;GLfloat ywcMin = 0.0, ywcMax = 225.0;class wcPt2D{public:GLfloat x, y;};typedef GLfloat Matrix3x3[3][3];Matrix3x3 matComposite;const GLdouble pi = 3.14159;void init(void){glClearColor(1.0, 1.0, 1.0, 0.0);}void matrix3x3SetIdentity(Matrix3x3matIdent3x3){GLint row, col;for (row = 0; row<3; row++)for (col = 0; col<3; col++)matIdent3x3[row][col] = (row == col);//生成{{1,0,0}{0,1,0}{0,0,1}}}void matrix3x3PreMultiply(Matrix3x3m1, Matrix3x3m2){GLint row, col;Matrix3x3 matTemp;for (row = 0; row<3; row++)for (col = 0; col<3; col++)matTemp[row][col] = m1[row][0] * m2[0][col] + m1[row][1] * m2[1][col] + m1[row][2] * m2[2][col];for (row = 0; row<3; row++)for (col = 0; col<3; col++)m2[row][col] = matTemp[row][col];}void translate2D(GLfloat tx, GLfloat ty)//平移{Matrix3x3 matTransl;matrix3x3SetIdentity(matTransl);matTransl[0][2] = tx;matTransl[1][2] = ty;matrix3x3PreMultiply(matTransl, matComposite);}void rotate2D(wcPt2D pivotPt, GLfloat theta)//旋转{Matrix3x3 matRot;matrix3x3SetIdentity(matRot);matRot[0][0] = cos(theta);matRot[0][1] = -sin(theta);matRot[0][2] = pivotPt.x*(1 - cos(theta)) + pivotPt.y*sin(theta);matRot[1][0] = sin(theta);matRot[1][1] = cos(theta);matRot[31][2] = pivotPt.x*(1 - cos(theta)) - pivotPt.y*sin(theta);matrix3x3PreMultiply(matRot, matComposite);}void scale2D(GLfloat sx, GLfloat sy, wcPt2D fixedPt)//缩放{Matrix3x3 matScale;matrix3x3SetIdentity(matScale);matScale[0][0] = sx;matScale[0][2] = (1 - sx)*fixedPt.x;matScale[1][1] = sy;matScale[1][2] = (1 - sy)*fixedPt.y;matrix3x3PreMultiply(matScale, matComposite);}void transformVerts2D(GLint nVerts, wcPt2D * verts)//组合变化后的矩阵{GLint k;GLfloat temp;for (k = 0; k<nVerts; k++){temp = matComposite[0][0] * verts[k].x + matComposite[0][1] * verts[k].y + matComposite[0][2];verts[k].y = matComposite[1][0] * verts[k].x + matComposite[1][1] *verts[k].y + matComposite[1][2];verts[k].x = temp;}}void triangle(wcPt2D*verts){GLint k;glBegin(GL_TRIANGLES);for (k = 0; k<3; k++)glVertex2f(verts[k].x, verts[k].y);glEnd();}void displayFcn(void){GLint nVerts = 3;wcPt2D verts[3] = { { 50.0, 25.0 }, { 150.0, 25.0 }, { 100.0, 100.0 } };wcPt2D centroidPt;GLint k, xSum = 0, ySum = 0;for (k = 0; k<nVerts; k++){xSum += verts[k].x;ySum += verts[k].y;}centroidPt.x = GLfloat(xSum) / GLfloat(nVerts);centroidPt.y = GLfloat(ySum) / GLfloat(nVerts);wcPt2D pivPt, fixedPt;pivPt = centroidPt;fixedPt = centroidPt;GLfloat tx = 0.0, ty = 100.0;GLfloat sx = 0.5, sy = 0.5;GLdouble theta = pi / 2.0;glClear(GL_COLOR_BUFFER_BIT);glColor3f(0.0, 0.0, 1.0);triangle(verts);//三角形matrix3x3SetIdentity(matComposite);scale2D(sx, sy, fixedPt);//缩小transformVerts2D(nVerts, verts);glColor3f(1.0, 0.0, 0.0);triangle(verts);matrix3x3SetIdentity(matComposite);rotate2D(pivPt, theta);//旋转90transformVerts2D(nVerts, verts);glColor3f(1.0, 0.0, 0.0);triangle(verts);matrix3x3SetIdentity(matComposite);translate2D(tx, ty);//平移transformVerts2D(nVerts, verts);glColor3f(1.0, 0.0, 0.0);实验结果。