-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ2_0_2DTransform.java
More file actions
283 lines (208 loc) · 6.57 KB
/
J2_0_2DTransform.java
File metadata and controls
283 lines (208 loc) · 6.57 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Created on 2004-2-29
* @author Jim X. Chen: 2D transformation OpenGL style implementatoin
*/
//import javax.media.opengl.*;
//import net.java.games.jogl.*;
import com.jogamp.opengl.GL4;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.awt.GLCanvas;
public class J2_0_2DTransform extends JOGL1_4_5_Circle {
private static float my3dMatStack[][][] =
new float[24][4][4];
static GLCanvas canvas; // drawable in a frame
static GL4 gl; // handle to OpenGL functions
static int WIDTH = 800, HEIGHT = 800; // used to set the window size
private static int stackPtr = 0;
static float vdata[][] = { {1.0f, 0.0f, 0.0f, 0.0f}
, {0.0f, 1.0f, 0.0f, 0.0f}
, {-1.0f, 0.0f, 0.0f, 0.0f}
, {0.0f, -1.0f, 0.0f, 0.0f}
};
static int cnt = 1;
int flip;
// called for OpenGL rendering every reshape
public void display(GLAutoDrawable drawable) {
if (cnt<1||cnt>200) {
flip = -flip;
}
cnt = cnt+flip;
gl.glClear(gl.GL_COLOR_BUFFER_BIT|gl.GL_DEPTH_BUFFER_BIT);
// white triangle is scaled
my3dScalef(cnt, cnt);
transDrawTriangle(vdata[0], vdata[1], vdata[2]);
// red triangle is rotated and scaled
my3dRotatef((float)cnt/15);
transDrawTriangle(vdata[0], vdata[1], vdata[2]);
// green triangle is translated, rotated, and scaled
my3dTranslatef((float)cnt/100, 0.0f);
transDrawTriangle(vdata[0], vdata[1], vdata[2]);
}
// the vertices are transformed first then drawn
public void transDrawTriangle(float[] v1,
float[] v2, float[] v3) {
float v[][] = new float[3][3];
my3dTransformf(v1, v[0]);
my3dTransformf(v2, v[1]);
my3dTransformf(v3, v[2]);
//gl.glVertex3fv(v[0],0);
int x0 = (int)v[0][0];
int xn = (int)v[1][0];
int y0 = (int)v[0][1];
int yn = (int)v[1][1];
JOGL1_4_2_Line temp = new JOGL1_4_2_Line();
temp.line(x0, y0, xn, yn);
/*
//gl.glVertex3fv(v[1],0);
x0 = (int)v[1][0];
xn = (int)v[2][0];
y0 = (int)v[1][1];
yn = (int)v[2][1];
temp.line(x0, y0, xn, yn);
x0 = (int)v[2][0];
xn = (int)v[0][0];
y0 = (int)v[2][1];
yn = (int)v[0][1];
temp.line(x0, y0, xn, yn);
*/
}
// initialize a 3*3 matrix to all zeros
private static void my3dClearMatrix(float mat[][]) {
for (int i = 0; i<4; i++) {
for (int j = 0; j<4; j++) {
mat[i][j] = 0.0f;
}
}
}
// initialize a matrix to Identity matrix
private static void my3dIdentity(float mat[][]) {
my3dClearMatrix(mat);
for (int i = 0; i<4; i++) {
mat[i][i] = 1.0f;
}
}
// initialize the current matrix to Identity matrix
public static void my3dLoadIdentity() {
my3dIdentity(my3dMatStack[stackPtr]);
}
// multiply the current matrix with mat
public void my3dMultMatrix(float mat[][]) {
float matTmp[][] = new float[3][3];
my3dClearMatrix(matTmp);
for (int i = 0; i<4; i++) {
for (int j = 0; j<4; j++) {
for (int k = 0; k<4; k++) {
matTmp[i][j] +=
my3dMatStack[stackPtr][i][k]*mat[k][j];
}
}
}
// save the result on the current matrix
for (int i = 0; i<3; i++) {
for (int j = 0; j<3; j++) {
my3dMatStack[stackPtr][i][j] = matTmp[i][j];
}
}
}
// multiply the current matrix with a translation matrix
public void my3dTranslatef(float x, float y) {
float T[][] = new float[4][4];
my3dIdentity(T);
T[0][2] = x;
T[1][2] = y;
my3dMultMatrix(T);
}
// multiply the current matrix with a rotation matrix
public void my3dRotatef(float angle) {
float R[][] = new float[4][4];
my3dIdentity(R);
R[0][0] = (float)Math.cos(angle);
R[0][1] = (float)-Math.sin(angle);
R[1][0] = (float)Math.sin(angle);
R[1][1] = (float)Math.cos(angle);
my3dMultMatrix(R);
}
// multiply the current matrix with a scale matrix
public void my3dScalef(float x, float y) {
float S[][] = new float[3][3];
my3dIdentity(S);
S[0][0] = x;
S[1][1] = y;
my3dMultMatrix(S);
}
// v1 = (the current matrix) * v
// here v and v1 are vertices in homogeneous coord.
public void my3dTransHomoVertex(float v[], float v1[]) {
int i, j;
for (i = 0; i<4; i++) {
v1[i] = 0.0f;
}
for (i = 0; i<3; i++) {
for (j = 0; j<3; j++) {
v1[i] +=
my3dMatStack[stackPtr][i][j]*v[j];
}
}
}
// vertex = (the current matrix) * vertex
// here vertex is in homogeneous coord.
public void my3dTransHomoVertex(float vertex[]) {
float vertex1[] = new float[3];
my3dTransHomoVertex(vertex, vertex1);
for (int i = 0; i<3; i++) {
vertex[i] = vertex1[i];
}
}
// transform v to v1 by the current matrix
// here v and v1 are not in homogeneous coordinates
public void my3dTransformf(float v[], float v1[]) {
float vertex[] = new float[3];
// extend to homogenius coord
vertex[0] = v[0];
vertex[1] = v[1];
vertex[2] = 1;
// multiply the vertex by the current matrix
my3dTransHomoVertex(vertex);
// return to 3D coord
v1[0] = vertex[0]/vertex[2];
v1[1] = vertex[1]/vertex[2];
}
// transform v by the current matrix
// here v is not in homogeneous coordinates
public void my3dTransformf(float[] v) {
float vertex[] = new float[4];
// extend to homogenius coord
vertex[0] = v[0];
vertex[1] = v[1];
vertex[2] = (float)Math.random();
vertex[3] = 1;
// multiply the vertex by the current matrix
my3dTransHomoVertex(vertex);
// return to 3D coord
v[0] = vertex[0]/vertex[2];
v[1] = vertex[1]/vertex[2];
}
// move the stack pointer up, and copy the previous
// matrix to the current matrix
public void my3dPushMatrix() {
int tmp = stackPtr+1;
for (int i = 0; i<3; i++) {
for (int j = 0; j<3; j++) {
my3dMatStack[tmp][i][j] =
my3dMatStack[stackPtr][i][j];
}
}
stackPtr++;
}
// move the stack pointer down
public void my2dPopMatrix() {
stackPtr--;
}
public static void main(String[] args) {
J2_0_2DTransform f = new J2_0_2DTransform();
f.setTitle("JOGL J2_0_2DTransform");
f.setSize(WIDTH, HEIGHT);
f.setVisible(true);
my3dLoadIdentity();
}
}