(a)变换前的三角形 (b)变换后的三角形 (c)程序显示结果
1 #include2 3 #include 4 5 #include 6 7 /* 初始化显示窗口大小 */ 8 9 GLsizei winWidth=600,winHeight=600; 10 11 /* 设置世界坐标系的显示范围 */ 12 13 GLfloat xwcMin=0.0,xwcMax=225.0; 14 15 GLfloat ywcMin=0.0,ywcMax=225.0; 16 17 /* 定义二维点数据结构 */ 18 19 class wcPt2D 20 21 { 22 23 public: 24 25 GLfloat x, y; 26 27 }; 28 29 typedef GLfloat Matrix3x3 [3][3]; 30 31 Matrix3x3 matComposite; //定义复合矩阵 32 33 const GLdouble pi=3.14159; 34 35 void init (void) 36 37 { 38 39 /* 设置显示窗口的背景颜色为白色 */ 40 41 glClearColor(1.0,1.0,1.0,0.0); 42 43 } 44 45 /* 构建3*3的单位矩阵 */ 46 47 void matrix3x3SetIdentity(Matrix3x3 matIdent3x3) 48 49 { 50 51 GLint row,col; 52 53 for (row=0;row<3;row++) 54 55 for (col=0;col<3;col++) 56 57 matIdent3x3[row][col]=(row==col); 58 59 } 60 61 /* 变换矩阵m1前乘矩阵m2,储存结果到m2中 */ 62 63 void matrix3x3PreMultiply(Matrix3x3 m1, Matrix3x3 m2) 64 65 { 66 67 GLint row, col; 68 69 Matrix3x3 matTemp; 70 71 for(row=0; row<3;row++) 72 73 for(col=0;col<3;col++) 74 75 matTemp[row][col]=m1[row][0]*m2[0][col]+m1[row][1]*m2[1][col]+m1[row][2]*m2[2][col]; 76 77 for(row=0;row<3;row++) 78 79 for(col=0;col<3;col++) 80 81 m2[row][col]=matTemp[row][col]; 82 83 } 84 85 /* 平移变换函数,平移量tx,ty */ 86 87 void translate2D(GLfloat tx,GLfloat ty) 88 89 { 90 91 Matrix3x3 matTransl; 92 93 /* 初始化平移矩阵为单位矩阵 */ 94 95 matrix3x3SetIdentity(matTransl); 96 97 matTransl[0][2]=tx; 98 99 matTransl[1][2]=ty;100 101 /* 将平移矩阵前乘到复合矩阵matComposite中 */102 103 matrix3x3PreMultiply(matTransl,matComposite);104 105 }106 107 /* 旋转变换函数,参数为中心点pivotPt和旋转角度theta */108 109 void rotate2D(wcPt2D pivotPt, GLfloat theta)110 111 {112 113 Matrix3x3 matRot;114 115 /* 初始化旋转矩阵为单位矩阵 */116 117 matrix3x3SetIdentity(matRot);118 119 matRot[0][0]=cos(theta);120 121 matRot[0][1]=-sin(theta);122 123 matRot[0][2]=pivotPt.x*(1-cos(theta))+pivotPt.y*sin(theta);124 125 matRot[1][0]=sin(theta);126 127 matRot[1][1]=cos(theta);128 129 matRot[1][2]=pivotPt.y*(1-cos(theta))-pivotPt.x*sin(theta);130 131 /* 将旋转矩阵前乘到复合矩阵matComposite中 */132 133 matrix3x3PreMultiply(matRot,matComposite);134 135 }136 137 /* 比例变换函数,参数为基准点fixedPt和缩放比例sx、sy */138 139 void scale2D(GLfloat sx,GLfloat sy,wcPt2D fixedPt)140 141 {142 143 Matrix3x3 matScale;144 145 /* 初始化缩放矩阵为单位矩阵 */146 147 matrix3x3SetIdentity(matScale);148 149 matScale[0][0]=sx;150 151 matScale[0][2]=(1-sx)*fixedPt.x;152 153 matScale[1][1]=sy;154 155 matScale[1][2]=(1-sy)*fixedPt.y;156 157 /* 将缩放矩阵前乘到复合矩阵matComposite中 */158 159 matrix3x3PreMultiply(matScale,matComposite);160 161 }162 163 /* 利用复合矩阵计算变换后坐标 */164 165 void transformVerts2D(GLint nVerts,wcPt2D * verts)166 167 {168 169 GLint k;170 171 GLfloat temp;172 173 for(k=0;k
附上本实验的VC++(VC++2008)