forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGLScene.java
More file actions
executable file
·340 lines (307 loc) · 9.56 KB
/
Copy pathGLScene.java
File metadata and controls
executable file
·340 lines (307 loc) · 9.56 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
package javaforce.gl;
import java.util.*;
import javaforce.*;
import static javaforce.gl.GL.*;
/** GLScene is a primitive 3D framework.
* Holds all loaded 3D meshes and related resources.
*/
public class GLScene {
private static final boolean DEBUG = false;
private boolean needinittex = true;
ArrayList<GLModel> ml;
HashMap<String, GLTexture> tl; //texture list
HashMap<String, GLModel> mtl; //model templates list
private ArrayList<Integer> freeglidlist;
GLTexture blankTexture;
public GLScene() {
freeglidlist = new ArrayList<Integer>();
reset();
texturePath = "";
blankTexture = new GLTexture(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)
//code
public void init(String vertex, String fragment) { //must give size of render window
glFrontFace(GL.GL_CCW); //3DS uses GL_CCW
glEnable(GL.GL_CULL_FACE); //don't draw back sides
glEnable(GL.GL_DEPTH_TEST);
glDepthFunc(GL.GL_LEQUAL);
glEnable(GL.GL_BLEND);
glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
glEnable(GL.GL_TEXTURE_2D);
glActiveTexture(GL.GL_TEXTURE0);
vertexShader = glCreateShader(GL.GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, new String[] {vertex}, null);
glCompileShader(vertexShader);
JFLog.log("vertex log=" + glGetShaderInfoLog(vertexShader));
fragShader = glCreateShader(GL.GL_FRAGMENT_SHADER);
glShaderSource(fragShader, 1, new String[] {fragment}, null);
glCompileShader(fragShader);
JFLog.log("fragment log=" + glGetShaderInfoLog(fragShader));
program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragShader);
glLinkProgram(program);
JFLog.log("program log=" + glGetProgramInfoLog(program));
glUseProgram(program);
vpa = glGetAttribLocation(program, "aVertexPosition");
glEnableVertexAttribArray(vpa);
tca[0] = glGetAttribLocation(program, "aTextureCoord1");
glEnableVertexAttribArray(tca[0]);
int uSampler1 = glGetUniformLocation(program, "uSampler1");
glUniform1i(uSampler1, 0);
tca[1] = glGetAttribLocation(program, "aTextureCoord2");
glEnableVertexAttribArray(tca[1]);
int uSampler2 = glGetUniformLocation(program, "uSampler2");
glUniform1i(uSampler2, 1);
mpu = glGetUniformLocation(program, "uPMatrix");
mmu = glGetUniformLocation(program, "uMMatrix");
mvu = glGetUniformLocation(program, "uVMatrix");
uUVMaps = glGetUniformLocation(program, "uUVMaps");
// 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<GLModel>();
tl = new HashMap<String, GLTexture>();
mtl = new HashMap<String, GLModel>();
}
private void releaseTextures() {
Set keyset = tl.keySet();
Iterator iter = keyset.iterator();
String texidx;
GLTexture tex;
while (iter.hasNext()) {
texidx = (String)iter.next();
tex = tl.get(texidx);
if (tex.glid != -1) {
releaseTexture(tex.glid);
tex.glid = -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;
GLObject obj;
GLModel 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++) {
GLUVMap 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;
GLTexture tex;
tex = tl.get(fn);
if (tex != null) {
tex.refcnt++;
return true;
}
needinittex = true;
tex = new GLTexture(idx);
tex.name = fn;
if (!tex.load(fn)) {
JFLog.log("Error:Failed to load texture:" + fn);
return false;
}
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) {
GLTexture tex = tl.get(fn);
if (tex == null) {
tex = new GLTexture(idx);
tl.put(fn, tex);
} else {
tex.loaded = false;
}
tex.set(px, w, h);
needinittex = true;
return false;
}
//load textures into video memory (texture objects)
boolean initTextures() {
if (!needinittex) {
return true;
}
//setup blankTexture
if (blankTexture.glid == -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(GLTexture 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) {
int id[] = new int[1];
id[0] = glid;
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).glid);
}
}
/** Release a cloned model @ index.
*/
public void releaseModel(int idx) {
GLModel mod;
GLObject 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++) {
GLUVMap map = obj.maps.get(m);
tl.get(mod.getTexture(map.textureIndex)).refcnt--;
}
}
ml.remove(idx);
}
public int modelCount() { return ml.size(); }
public boolean addModel(GLModel mod) { return addModel(mod, 0); } //places Model at start of list
public boolean addModel(GLModel mod, int idx) { if (mod == null) return false; ml.add(idx, mod); return true;} //place Models with transparent textures last
public int indexOfModel(GLModel mod) { return ml.indexOf(mod); }
public void removeModel(int idx) { ml.remove(idx); }
public void removeModel(GLModel 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 GLModel load3DS(String fn) {
GLModel mod;
mod = mtl.get(fn);
if (mod != null) {
mod.refcnt++;
return mod;
}
GL_3DS loader = new GL_3DS();
mod = loader.load(fn);
if (mod == null) return null;
mtl.put(fn, mod);
mod.refcnt = 1;
mod = (GLModel)mod.clone();
return mod;
}
/** Loads a .blend file into the template array.
* Use addModel() to add a clone into the render scene.
*/
public GLModel loadBlend(String fn) {
GLModel mod;
mod = mtl.get(fn);
if (mod != null) {
mod.refcnt++;
return mod;
}
GL_BLEND loader = new GL_BLEND();
mod = loader.load(fn);
if (mod == null) return null;
mtl.put(fn, mod);
mod.refcnt = 1;
mod = (GLModel)mod.clone();
return mod;
}
/** Loads a .JF3D file into the template array.
* Use addModel() to add a clone into the render scene.
*/
public GLModel loadJF3D(String fn) {
GLModel mod;
mod = mtl.get(fn);
if (mod != null) {
mod.refcnt++;
return mod;
}
GL_JF3D loader = new GL_JF3D();
mod = loader.load(fn);
if (mod == null) return null;
mtl.put(fn, mod);
mod.refcnt = 1;
mod = (GLModel)mod.clone();
return mod;
}
/** Clones a pre-loaded model.
* Use addModel() to add into the render scene.
*/
public GLModel cloneModel(String fn) {
GLModel mod = mtl.get(fn);
if (mod == null) return null;
mod.refcnt++;
return (GLModel)mod.clone();
}
public void unloadModel(GLModel mod) {
mod.refcnt--;
}
//this will release all unused models
public void releaseModel() {
Set keyset = mtl.keySet();
Iterator iter = keyset.iterator();
String idx;
GLModel mod;
while (iter.hasNext()) {
idx = (String)iter.next();
mod = mtl.get(idx);
if (mod.refcnt == 0) {
mtl.remove(idx);
}
}
}
}