4.4.1 二维复合矩阵编程实例

clip_image002clip_image004

(a)变换前的三角形                 (b)变换后的三角形          (c)程序显示结果

  1 #include <GL/glut.h>
  2 
  3 #include <stdlib.h>
  4 
  5 #include <math.h>
  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<nVerts;k++)
174 
175 {
176 
177 temp=matComposite[0][0]*verts[k].x+matComposite[0][1]*verts[k].y+matComposite[0][2];
178 
179 verts[k].y=matComposite[1][0]*verts[k].x+matComposite[1][1]*verts[k].y+matComposite[1][2];
180 
181 verts[k].x=temp;
182 
183 }
184 
185 }
186 
187 /* 三角形绘制函数 */
188 
189 void triangle(wcPt2D * verts)
190 
191 {
192 
193 GLint k;
194 
195 glBegin(GL_TRIANGLES);
196 
197 for(k=0;k<3;k++)
198 
199 glVertex2f(verts[k].x,verts[k].y);
200 
201 glEnd();
202 
203 }
204 
205 void displayFcn(void)
206 
207 {
208 
209 /* 定义三角形的初始位置 */
210 
211 GLint nVerts=3;
212 
213 wcPt2D verts[3]={{50.0,25.0},{150.0,25.0},{100.0,100.0}};
214 
215 /* 计算三角形中心位置 */
216 
217 wcPt2D centroidPt;
218 
219 GLint k,xSum=0,ySum=0;
220 
221 for(k=0;k<nVerts;k++)
222 
223 {
224 
225 xSum+=verts[k].x;
226 
227 ySum+=verts[k].y;
228 
229 }
230 
231 centroidPt.x=GLfloat(xSum)/GLfloat(nVerts);
232 
233 centroidPt.y=GLfloat(ySum)/GLfloat(nVerts);
234 
235 /* 设置几何变换参数*/
236 
237 wcPt2D pivPt,fixedPt;
238 
239 pivPt=centroidPt;
240 
241 fixedPt=centroidPt;
242 
243 GLfloat tx=0.0,ty=100.0;
244 
245 GLfloat sx=0.5,sy=0.5;
246 
247 GLdouble theta=pi/2.0;
248 
249 glClear(GL_COLOR_BUFFER_BIT); // 清空显示窗口
250 
251 glColor3f(0.0,0.0,1.0); // 设置前景色为蓝色
252 
253 triangle(verts); //显示蓝色三角形(变换前)
254 
255 /* 初始化复合矩阵为单位矩阵 */
256 
257 matrix3x3SetIdentity(matComposite);
258 
259 /* 根据变换序列重建复合矩阵 */
260 
261 scale2D(sx,sy,fixedPt); //变换序列1:缩放变换
262 
263 rotate2D(pivPt,theta); //变换序列2:旋转变换
264 
265 translate2D(tx,ty); //变换序列3:平移变换
266 
267 /* 应用复合矩阵到三角形 */
268 
269 transformVerts2D(nVerts,verts);
270 
271 glColor3f(1.0,0.0,0.0); //重新设置前景色为红色
272 
273 triangle(verts); //显示红色三角形(变换后)
274 
275 glFlush();
276 
277 }
278 
279 void winReshapeFcn(GLint newWidth,GLint newHeight)
280 
281 {
282 
283 glMatrixMode(GL_PROJECTION);
284 
285 glLoadIdentity();
286 
287 gluOrtho2D(xwcMin,xwcMax,ywcMin,ywcMax);
288 
289 glClear(GL_COLOR_BUFFER_BIT);
290 
291 }
292 
293 void main(int argc, char ** argv)
294 
295 {
296 
297 glutInit(&argc,argv);
298 
299 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
300 
301 glutInitWindowPosition(50,50);
302 
303 glutInitWindowSize(winWidth,winHeight);
304 
305 glutCreateWindow("二维几何变换实例-复合变换");
306 
307 init();
308 
309 glutDisplayFunc(displayFcn);
310 
311 glutReshapeFunc(winReshapeFcn);
312 
313 glutMainLoop();
314 
315 }

附上本实验的VC++工程代码(VC++2008)

posted on 2014-06-16 09:57  慢步前行  阅读(1647)  评论(0编辑  收藏  举报

导航