平移、旋转、缩放的实现
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205#include<iostream> #include <math.h> #include<Windows.h> #include <GL/glut.h> using namespace std; 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() { //窗口背景为白色 glClearColor(1.0, 1.0, 1.0, 0.0); } void matrix3x3SetIdentity(Matrix3x3 matIdent3x3) { GLint row, col; for (row = 0; row < 3; row++) { for (col = 0; col < 3; col++) { matIdent3x3[row][col] = (row == col); } } } void matrix3x3PreMultiply(Matrix3x3 m1, Matrix3x3 m2) { 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[1][2] = pivotPt.y * (1 - cos(theta)) - pivotPt.x * 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) { glBegin(GL_TRIANGLES); for (GLint k = 0; k < 3; k++) { glVertex2f(verts[k].x, verts[k].y); } glEnd(); } void displayFcn() { GLint nVerts = 3; wcPt2D verts[3] = { {50.0,25.0},{150.0,25.0},{100.0,100.0} }; wcPt2D centroidPt; GLint xSum = 0, ySum = 0; for (GLint 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; glClear(GL_COLOR_BUFFER_BIT); glColor3f(200.0 / 255.0, 200.0 / 255.0, 169.0 / 255.0); triangle(verts); matrix3x3SetIdentity(matComposite); scale2D(sx, sy, fixedPt); transformVerts2D(nVerts, verts); glColor3f(137.0 / 255.0, 190.0 / 255.0, 178.0 / 255.0); triangle(verts); matrix3x3SetIdentity(matComposite); rotate2D(pivPt, theta); transformVerts2D(nVerts, verts); glColor3f(69.0 / 255.0, 137.0 / 255.0, 148.0 / 255.0); triangle(verts); matrix3x3SetIdentity(matComposite); translate2D(tx, ty); transformVerts2D(nVerts, verts); glColor3f(178.0 / 255.0, 200.0 / 255.0, 187.0 / 255.0); triangle(verts); glFlush(); } void winReshapeFcn(GLint newWidth, GLint newHeight) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(xwcMin, xwcMax, ywcMin, ywcMax); glClear(GL_COLOR_BUFFER_BIT); } int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(50, 50); glutInitWindowSize(winWidth, winHeight); glutCreateWindow("二维几何变换"); init(); glutDisplayFunc(displayFcn); glutReshapeFunc(winReshapeFcn); glutMainLoop(); system("pause"); return 0; }
运行结果
最后
以上就是沉静西牛最近收集整理的关于OpenGL——二维几何变换的全部内容,更多相关OpenGL——二维几何变换内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复