-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathScene.java
More file actions
367 lines (334 loc) · 10.2 KB
/
Copy pathScene.java
File metadata and controls
367 lines (334 loc) · 10.2 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package javaforce.gl;
import java.util.*;
import javaforce.*;
import javaforce.gl.model.*;
import static javaforce.gl.GL.*;
/** Scene is a primitive 3D framework.
*
* Stores all 3D models and related resources.
*/
public class Scene {
private static final boolean DEBUG = false;
private boolean needinittex = true;
ArrayList<Model> ml;
HashMap<String, Texture> tl; //texture list
HashMap<String, Model> mtl; //model templates list
private ArrayList<Integer> freeglidlist;
Texture blankTexture;
public Scene() {
freeglidlist = new ArrayList<Integer>();
reset();
texturePath = "";
blankTexture = new Texture(0);
blankTexture.set(new int[] {-1},1,1); //white pixel
}
public boolean inited = false;
public String texturePath;
public int fragShader, vertexShader, program;
public int vpa; //attribs
public int[] tca = new int[2];
public int uUVMaps;
public int mpu, mmu, mvu; //uniform matrix'es (perspective, model, view)
public void init(String vertex, String fragment) { //must give size of render window
GL gl = GL.getInstance();
gl.glFrontFace(GL_CCW); //3DS uses GL_CCW
gl.glEnable(GL_CULL_FACE); //don't draw back sides
gl.glEnable(GL_DEPTH_TEST);
gl.glDepthFunc(GL_LEQUAL);
gl.glEnable(GL_BLEND);
gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gl.glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
gl.glEnable(GL_TEXTURE_2D);
gl.glActiveTexture(GL_TEXTURE0);
vertexShader = gl.glCreateShader(GL_VERTEX_SHADER);
gl.glShaderSource(vertexShader, 1, new String[] {vertex}, null);
gl.glCompileShader(vertexShader);
if (debug) JFLog.log("vertex log=" + gl.glGetShaderInfoLog(vertexShader));
fragShader = gl.glCreateShader(GL_FRAGMENT_SHADER);
gl.glShaderSource(fragShader, 1, new String[] {fragment}, null);
gl.glCompileShader(fragShader);
if (debug) JFLog.log("fragment log=" + gl.glGetShaderInfoLog(fragShader));
program = gl.glCreateProgram();
gl.glAttachShader(program, vertexShader);
gl.glAttachShader(program, fragShader);
gl.glLinkProgram(program);
if (debug) JFLog.log("program log=" + gl.glGetProgramInfoLog(program));
gl.glUseProgram(program);
vpa = gl.glGetAttribLocation(program, "aVertexPosition");
gl.glEnableVertexAttribArray(vpa);
tca[0] = gl.glGetAttribLocation(program, "aTextureCoord1");
gl.glEnableVertexAttribArray(tca[0]);
int uSampler1 = gl.glGetUniformLocation(program, "uSampler1");
gl.glUniform1i(uSampler1, 0);
tca[1] = gl.glGetAttribLocation(program, "aTextureCoord2");
gl.glEnableVertexAttribArray(tca[1]);
int uSampler2 = gl.glGetUniformLocation(program, "uSampler2");
gl.glUniform1i(uSampler2, 1);
mpu = gl.glGetUniformLocation(program, "uPMatrix");
mmu = gl.glGetUniformLocation(program, "uMMatrix");
mvu = gl.glGetUniformLocation(program, "uVMatrix");
uUVMaps = gl.glGetUniformLocation(program, "uUVMaps");
if (debug) {
JFLog.log("attribs=" + vpa + "," + tca[0] + "," + tca[1]);
JFLog.log("uniforms=" + mpu + "," + mmu + "," + mvu + "," + uUVMaps + "," + uSampler1 + "," + uSampler2);
}
initTextures();
inited = true;
}
public void reset() {
if (tl != null) releaseTextures();
ml = new ArrayList<Model>();
tl = new HashMap<String, Texture>();
mtl = new HashMap<String, Model>();
}
private void releaseTextures() {
Set keyset = tl.keySet();
Iterator iter = keyset.iterator();
String texidx;
Texture tex;
while (iter.hasNext()) {
texidx = (String)iter.next();
tex = tl.get(texidx);
if (tex.tid != -1) {
releaseTexture(tex.tid);
tex.tid = -1;
}
}
}
private void releaseTexture(int glid) {
freeglidlist.add(glid);
}
//load textures from disk to general-purpose memory
public boolean loadTextures() {
//scan thru object list and load them all
boolean ret = true;
Object3 obj;
Model mod;
int modCnt = ml.size();
for(int a=0;a<modCnt;a++) {
mod = ml.get(a);
int objCnt = mod.ol.size();
for(int b=0;b<objCnt;b++) {
obj = mod.ol.get(b);
int mapCnt = obj.maps.size();
for(int m=0;m<mapCnt;m++) {
UVMap map = obj.maps.get(m);
if (map.texloaded) continue;
if (loadTexture(mod.getTexture(map.textureIndex), map.idx)) {
map.texloaded = true;
} else {
ret = false;
}
}
}
}
return ret;
}
private boolean loadTexture(String fn, int idx) {
if (fn == null) return false;
Texture tex;
tex = tl.get(fn);
if (tex != null) {
tex.refcnt++;
return true;
}
needinittex = true;
tex = new Texture(idx);
tex.name = fn;
int extidx = fn.lastIndexOf('.');
if (extidx == -1) {
JFLog.log("Unsupported texture:" + fn);
return false;
}
String ext = fn.substring(extidx);
switch (ext.toLowerCase()) {
case ".png":
if (!tex.loadPNG(fn)) {
JFLog.log("Error:Failed to load texture:" + fn);
return false;
}
break;
case ".jpg":
if (!tex.loadJPG(fn)) {
JFLog.log("Error:Failed to load texture:" + fn);
return false;
}
break;
}
tex.refcnt = 1;
tl.put(fn, tex);
return true;
}
//directly load a texture
public boolean setTexture(String fn, int[] px, int w, int h, int idx) {
Texture tex = tl.get(fn);
if (tex == null) {
tex = new Texture(idx);
tl.put(fn, tex);
} else {
tex.loaded = false;
}
tex.set(px, w, h);
needinittex = true;
return false;
}
//directly load a texture
public boolean setTexture(String fn, Texture tex) {
tl.put(fn, tex);
needinittex = true;
return false;
}
//load textures into video memory (texture objects)
boolean initTextures() {
if (!needinittex) {
return true;
}
//setup blankTexture
if (blankTexture.tid == -1) initTexture(blankTexture);
//first uninit any that have been deleted
if (freeglidlist.size() > 0) uninitTextures();
//scan thru object list and load them all
Set keyset = tl.keySet();
Iterator iter = keyset.iterator();
String texidx;
while (iter.hasNext()) {
texidx = (String)iter.next();
if (!initTexture(tl.get(texidx))) return false;
}
needinittex = false;
return true;
}
private boolean initTexture(Texture tex) {
return tex.load();
}
private boolean uninitTextures() {
while (freeglidlist.size() > 0) {
uninitTexture(freeglidlist.get(0));
freeglidlist.remove(0);
}
return true;
}
private boolean uninitTexture(int glid) {
GL gl = GL.getInstance();
int[] id = new int[1];
id[0] = glid;
gl.glDeleteTextures(1, id);
return true;
}
public void releaseUnusedTextures() {
Set keyset = tl.keySet();
Iterator iter = keyset.iterator();
String texidx;
while (iter.hasNext()) {
texidx = (String)iter.next();
if (tl.get(texidx).refcnt == 0) releaseTexture(tl.get(texidx).tid);
}
}
/** Release a cloned model @ index.
*/
public void releaseModel(int idx) {
Model mod;
Object3 obj;
mod = ml.get(idx);
int size = mod.ol.size();
for(int a=0;a<size;a++) {
obj = mod.ol.get(a);
for(int m=0;m<obj.maps.size();m++) {
UVMap map = obj.maps.get(m);
tl.get(mod.getTexture(map.textureIndex)).refcnt--;
}
}
ml.remove(idx);
}
public int modelCount() { return ml.size(); }
public boolean addModel(Model mod) { return addModel(mod, 0); } //places Model at start of list
public boolean addModel(Model mod, int idx) { if (mod == null) return false; ml.add(idx, mod); return true;} //place Models with transparent textures last
public int indexOfModel(Model mod) { return ml.indexOf(mod); }
public void removeModel(int idx) { ml.remove(idx); }
public void removeModel(Model mod) { ml.remove(mod); }
public void nextFrame(int objidx) { ml.get(objidx).nextFrame(); }
public void setFrame(int objidx, int frame) { ml.get(objidx).setFrame(frame); }
public void modelTranslate(int idx, float x, float y, float z) { ml.get(idx).translate(x,y,z); }
public void modelRotate(int idx, float angle, float x, float y, float z) { ml.get(idx).rotate(angle,x,y,z); }
public void modelScale(int idx, float x, float y, float z) { ml.get(idx).scale(x,y,z); }
/** Loads a .3DS file into the template array.
* Use addModel() to add a clone into the render scene.
*/
public Model load3DS(String fn) {
Model mod;
mod = mtl.get(fn);
if (mod != null) {
mod.refcnt++;
return mod;
}
Model3DS loader = new Model3DS();
mod = loader.load(fn);
if (mod == null) return null;
mtl.put(fn, mod);
mod.refcnt = 1;
mod = (Model)mod.clone();
return mod;
}
/** Loads a .blend file into the template array.
* Use addModel() to add a clone into the render scene.
*/
public Model loadBlend(String fn) {
Model mod;
mod = mtl.get(fn);
if (mod != null) {
mod.refcnt++;
return mod;
}
ModelBLEND loader = new ModelBLEND();
mod = loader.load(fn);
if (mod == null) return null;
mtl.put(fn, mod);
mod.refcnt = 1;
mod = (Model)mod.clone();
return mod;
}
/** Loads a .JF3D file into the template array.
* Use addModel() to add a clone into the render scene.
*/
public Model loadJF3D(String fn) {
Model mod;
mod = mtl.get(fn);
if (mod != null) {
mod.refcnt++;
return mod;
}
ModelJF3D loader = new ModelJF3D();
mod = loader.load(fn);
if (mod == null) return null;
mtl.put(fn, mod);
mod.refcnt = 1;
mod = (Model)mod.clone();
return mod;
}
/** Clones a pre-loaded model.
* Use addModel() to add into the render scene.
*/
public Model cloneModel(String fn) {
Model mod = mtl.get(fn);
if (mod == null) return null;
mod.refcnt++;
return (Model)mod.clone();
}
public void unloadModel(Model mod) {
mod.refcnt--;
}
//this will release all unused models
public void releaseModel() {
Set keyset = mtl.keySet();
Iterator iter = keyset.iterator();
String idx;
Model mod;
while (iter.hasNext()) {
idx = (String)iter.next();
mod = mtl.get(idx);
if (mod.refcnt == 0) {
mtl.remove(idx);
}
}
}
}