-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathTestGL.java
More file actions
executable file
·291 lines (258 loc) · 8.86 KB
/
Copy pathTestGL.java
File metadata and controls
executable file
·291 lines (258 loc) · 8.86 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
284
285
286
287
288
289
290
291
package javaforce.tests;
import java.util.*;
import javaforce.*;
import javaforce.awt.*;
import javaforce.ui.*;
import javaforce.gl.*;
/** OpenGL Test
*
* @author pquiring
*/
public class TestGL implements WindowEvents {
public static Window window;
public static Cube cube;
public static void main(String args[]) {
window = new Window();
window.create(Window.STYLE_VISIBLE | Window.STYLE_TITLEBAR | Window.STYLE_RESIZABLE, "Test", 512, 512, null);
window.show();
window.setWindowListener(new TestGL());
GL.getInstance(); //must call AFTER window is created to load func ptrs
cube = new Cube(true);
cube.init();
while (true) {
cube.render();
window.pollEvents();
}
}
public static void swap() {
window.swap();
}
public void windowResize(int x, int y) {
cube.resize(x, y);
}
public void windowClosing() {
System.exit(0);
}
public static class Cube {
java.util.Timer glTimer, fpsTimer;
final Object fpsLock = new Object();
int fpsCounter;
final int FPS = 65;
Scene scene = new Scene();
Render render = new Render();
Model mod;
Object3 box;
boolean keys[] = new boolean[1024];
final float speedMove = 2.0f;
final float speedRotate = 5.0f;
float alpha = 0.5f, alphadir = -0.01f;
boolean doSwap = false;
public Cube(boolean doSwap) {
this.doSwap = doSwap;
}
public void init() {
scene.texturePath = "./";
System.out.println("GL Version=" + GL.getInstance().glGetString(GL.GL_VERSION));
int glver[] = GL.getVersion();
if (glver[0] < 2) {
JFAWT.showError("Error", "OpenGL Version < 2.0 : Detected : " + glver[0] + "." + glver[1]);
System.exit(0);
}
createWorld();
//setup timers
glTimer = new java.util.Timer();
int delay = 1000 / FPS;
glTimer.scheduleAtFixedRate(new TimerTask() {
public void run() {
frame();
}
}, delay, delay);
fpsTimer = new java.util.Timer();
fpsTimer.scheduleAtFixedRate(new TimerTask() {
public void run() {
int cnt;
synchronized(fpsLock) {
cnt = fpsCounter;
fpsCounter = 0;
}
JFLog.log("fps=" + cnt);
// JFLog.log("camera=" + scene.m_camera.toString());
// JFLog.log("model=" + scene.m_model.toString());
}
}, 1000, 1000);
}
public void render() {
synchronized(fpsLock) {
fpsCounter++;
}
processMovement();
render.render();
if (doSwap) TestGL.swap(); //only swap with GLCanvas, not GLJPanel
}
public void resize(int width, int height) {
render.resize(width, height);
}
public void frame() {
box.m.addRotate(3.0f, 1.0f, 0.0f, 0.0f);
box.m.addRotate(2.0f, 0.0f, 1.0f, 0.0f);
box.m.addRotate(1.0f, 0.0f, 0.0f, 1.0f);
}
public void createWorld() {
float x, y, z;
int width = 512;
int height = 512;
System.out.println("size=" + width + "," + height);
scene.init(VertexShader.source, FragmentShader.source);
render.init(scene, width, height);
resetPosition();
x = -0.5f;
y = -0.5f;
z = -0.5f;
box = new Object3();
UVMap map = box.createUVMap();
map.textureIndex = 0;
mod = new Model();
mod.addObject(box);
mod.addTexture("javaforce.png");
scene.addModel(mod);
//create a box
makeWall(x,y,z,1, box);
makeWall(x,y,z,2, box);
makeWall(x,y,z,3, box);
makeWall(x,y,z,4, box);
makeWall(x,y,z,5, box);
makeWall(x,y,z,6, box);
if (!scene.loadTextures()) {
JFLog.log("Failed to load all textures");
System.exit(0);
}
box.copyBuffers();
}
public Object3 makeWall(float x,float y,float z,int side,Object3 obj) {
//use counter clock wise triangles
float vp[]; //vertex coords (positions)
vp = new float[] {
(x ) * 10.0f, (y ) * 10.0f, (z ) * 10.0f,
(x+1.0f) * 10.0f, (y ) * 10.0f, (z ) * 10.0f,
(x ) * 10.0f, (y+1.0f) * 10.0f, (z ) * 10.0f,
(x+1.0f) * 10.0f, (y+1.0f) * 10.0f, (z ) * 10.0f,
(x ) * 10.0f, (y ) * 10.0f, (z+1.0f) * 10.0f,
(x+1.0f) * 10.0f, (y ) * 10.0f, (z+1.0f) * 10.0f,
(x ) * 10.0f, (y+1.0f) * 10.0f, (z+1.0f) * 10.0f,
(x+1.0f) * 10.0f, (y+1.0f) * 10.0f, (z+1.0f) * 10.0f
};
int off = obj.vpl.size() / 3; //current vertex count
int pts[] = null;
float uv[] = new float[8 * 2];
switch (side) {
case 1: //top
pts = new int[] {2,6,7,3};
break;
case 2: //bottom
pts = new int[] {5,4,0,1};
break;
case 3: //left
pts = new int[] {2,0,4,6};
break;
case 4: //right
pts = new int[] {7,5,1,3};
break;
case 5: //front
pts = new int[] {6,4,5,7};
break;
case 6: //back
pts = new int[] {3,1,0,2};
break;
}
float u = 0.0f;
float v = 0.0f;
for(int a=0;a<4;a++) {
uv[pts[a] * 2 + 0] = u;
uv[pts[a] * 2 + 1] = v;
if (u == 0.0f && v == 0.0f) v = 1.0f;
else if (u == 0.0f && v == 1.0f) u = 1.0f;
else if (u == 1.0f && v == 1.0f) v = 0.0f;
// else if (tx == 0.0f && ty == 0.0f) tx = 0.0f; //not needed - end of loop
}
obj.addVertex(vp, uv);
obj.addPoly(new int[] {off + pts[0], off + pts[1], off + pts[2]});
obj.addPoly(new int[] {off + pts[0], off + pts[2], off + pts[3]});
return obj;
}
Vector3 viewpoint = new Vector3();
Vector3 uppoint = new Vector3();
Vector3 leftpoint = new Vector3();
Vector3 boxCenter = new Vector3();
public void processMovement() {
viewpoint.set(0.0f, 0.0f, -1.0f); //normally looking into monitor
render.m_camera.mult(viewpoint);
uppoint.set(0.0f, 1.0f, 0.0f); //normally up is along y axis
render.m_camera.mult(uppoint);
leftpoint.set(-1.0f, 0.0f, 0.0f); //normally left is along x axis
render.m_camera.mult(leftpoint);
if (keys[KeyCode.VK_LEFT]) { rotateLR(-1.0f); }
if (keys[KeyCode.VK_RIGHT]) { rotateLR(1.0f); }
if (keys[KeyCode.VK_UP]) { rotateUD(1.0f); }
if (keys[KeyCode.VK_DOWN]) { rotateUD(-1.0f); }
if (keys[KeyCode.VK_Z]) { spin(1.0f); }
if (keys[KeyCode.VK_X]) { spin(-1.0f); }
if (keys[KeyCode.VK_Q]) { resetPosition(); }
if (keys[KeyCode.VK_F1]) { render.cameraRotate(10.0f, 1.0f, 0.0f, 0.0f); }
if (keys[KeyCode.VK_F2]) { render.cameraRotate(10.0f, 0.0f, 1.0f, 0.0f); }
if (keys[KeyCode.VK_F3]) { render.cameraRotate(10.0f, 0.0f, 0.0f, 1.0f); }
if (keys[KeyCode.VK_F5]) { render.cameraRotate(-10.0f, 1.0f, 0.0f, 0.0f); }
if (keys[KeyCode.VK_F6]) { render.cameraRotate(-10.0f, 0.0f, 1.0f, 0.0f); }
if (keys[KeyCode.VK_F7]) { render.cameraRotate(-10.0f, 0.0f, 0.0f, 1.0f); }
if (keys[KeyCode.VK_9]) { render.fovy -= 10.0f; System.out.println("fovy = " + render.fovy); }
if (keys[KeyCode.VK_0]) { render.fovy += 10.0f; System.out.println("fovy = " + render.fovy); }
if (keys[KeyCode.VK_S]) { move(1); }
if (keys[KeyCode.VK_W]) { move(-1); }
if (keys[KeyCode.VK_D]) { stride(1); }
if (keys[KeyCode.VK_A]) { stride(-1); }
//fade box in/out
alpha += alphadir;
if (alpha < 0.0f) {
alpha = 0.0f;
alphadir *= -1.0f;
} else if (alpha > 0.5f) {
alpha = 0.5f;
alphadir *= -1.0f;
}
box.color[3] = alpha;
}
public void rotateLR(float dir) {
render.cameraRotate(speedRotate * uppoint.v[0], dir, 0.0f, 0.0f);
render.cameraRotate(speedRotate * uppoint.v[1], 0.0f, dir, 0.0f);
render.cameraRotate(speedRotate * uppoint.v[2], 0.0f, 0.0f, dir);
}
public void rotateUD(float dir) {
render.cameraRotate(speedRotate * leftpoint.v[0], dir, 0.0f, 0.0f);
render.cameraRotate(speedRotate * leftpoint.v[1], 0.0f, dir, 0.0f);
render.cameraRotate(speedRotate * leftpoint.v[2], 0.0f, 0.0f, dir);
}
public void spin(float dir) {
render.cameraRotate(speedRotate * viewpoint.v[0], dir, 0.0f, 0.0f);
render.cameraRotate(speedRotate * viewpoint.v[1], 0.0f, dir, 0.0f);
render.cameraRotate(speedRotate * viewpoint.v[2], 0.0f, 0.0f, dir);
}
public void move(float dir) {
render.modelTranslate(
viewpoint.v[0] * speedMove * dir,
viewpoint.v[1] * speedMove * dir,
viewpoint.v[2] * speedMove * dir
);
}
public void stride(float dir) {
render.modelTranslate(
leftpoint.v[0] * speedMove * dir,
leftpoint.v[1] * speedMove * dir,
leftpoint.v[2] * speedMove * dir
);
}
public void resetPosition() {
render.cameraReset();
render.modelReset();
render.modelTranslate(0.0f, 0.0f, -20.0f);
}
}
}