-
Notifications
You must be signed in to change notification settings - Fork 4
/
ModelScene.java
352 lines (307 loc) · 8.35 KB
/
ModelScene.java
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
/*******************************************************************************************************
*
* ModelScene.java, in ummisco.gama.opengl, is part of the source code of the GAMA modeling and simulation platform
* (v.1.8.2).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
********************************************************************************************************/
package ummisco.gama.opengl.scene;
import org.locationtech.jts.geom.Geometry;
import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
import msi.gama.common.interfaces.IDisplaySynchronizer;
import msi.gama.common.interfaces.ILayer;
import msi.gama.util.GamaMapFactory;
import msi.gama.util.IMap;
import msi.gama.util.file.GamaGeometryFile;
import msi.gama.util.matrix.IField;
import msi.gaml.statements.draw.DrawingAttributes;
import msi.gaml.statements.draw.MeshDrawingAttributes;
import msi.gaml.statements.draw.TextDrawingAttributes;
import ummisco.gama.dev.utils.DEBUG;
import ummisco.gama.opengl.OpenGL;
import ummisco.gama.opengl.renderer.IOpenGLRenderer;
import ummisco.gama.opengl.scene.layers.AxesLayerObject;
import ummisco.gama.opengl.scene.layers.FrameLayerObject;
import ummisco.gama.opengl.scene.layers.LayerObject;
import ummisco.gama.opengl.scene.layers.OverlayLayerObject;
/**
*
* The class ModelScene. A repository for all the objects that constitute the scene of a model : strings, images,
* shapes... 04/03/14: Now organized by layers to address the issue of z depth
*
* @author drogoul
* @since 3 mai 2013
*
*/
public class ModelScene {
/** The Constant AXES_KEY. */
public static final String AXES_KEY = "__axes__0";
/** The Constant FRAME_KEY. */
public static final String FRAME_KEY = "__frame__0";
/** The layers. */
protected final IMap<String, LayerObject> layers = GamaMapFactory.create();
/** The current layer. */
protected LayerObject currentLayer;
/** The renderer. */
protected final IOpenGLRenderer renderer;
/** The rendered. */
private volatile boolean rendered = false;
/** The object number. */
private volatile int objectNumber;
/** The z increment. */
private volatile double zIncrement;
/** The current layer trace. */
private volatile int currentLayerTrace;
/** The max Z. */
final double maxZ;
/**
* Instantiates a new model scene.
*
* @param renderer
* the renderer
* @param withWorld
* the with world
*/
public ModelScene(final IOpenGLRenderer renderer, final boolean withWorld) {
this.renderer = renderer;
maxZ = renderer.getMaxEnvDim() / 2000d;
if (withWorld) { initWorld(); }
}
/**
* Inits the world.
*/
protected void initWorld() {
if (renderer.getData().isDrawEnv()) {
layers.put(FRAME_KEY, new FrameLayerObject(renderer));
layers.put(AXES_KEY, new AxesLayerObject(renderer));
}
}
/**
* @param context
* Called every new iteration when updateDisplay() is called on the surface
*/
public void wipe(final OpenGL gl) {
layers.forEach((name, obj) -> { if (obj != null && (!obj.isStatic() || obj.isInvalid())) { obj.clear(gl); } });
// Wipe the textures.
gl.deleteVolatileTextures();
}
/**
* Draw.
*
* @param gl
* the gl
*/
public void draw(final OpenGL gl) {
gl.push(GLMatrixFunc.GL_MODELVIEW);
gl.setZIncrement(renderer.getData().isOrtho() ? 0D : zIncrement);
// AD called here so that it is inside the keystone drawing. See #3285
gl.rotateModel();
for (LayerObject layer : layers.values()) {
if (layer != null && !layer.isInvalid()) {
try {
layer.lock();
layer.draw(gl);
} catch (final RuntimeException r) {
DEBUG.ERR("Runtime error " + r.getMessage() + " in OpenGL loop");
r.printStackTrace();
}
}
}
gl.setZIncrement(0);
rendered = true;
IDisplaySynchronizer sync = gl.getRenderer().getSurface().synchronizer;
if (sync != null) { sync.signalRenderingIsFinished(); }
gl.pop(GLMatrixFunc.GL_MODELVIEW);
}
/**
* Compute visual Z increment.
*
* @return the double
*/
private double computeVisualZIncrement() {
if (objectNumber <= 1) return 0d;
// The maximum visual z allowance between the object at the bottom and the one at the top
// The increment is simply
return maxZ / objectNumber;
}
/**
* Cannot add.
*
* @return true, if successful
*/
public boolean cannotAdd() {
return currentLayer == null;
}
/**
* Increment.
*
* @return true, if successful
*/
private boolean increment() {
if (cannotAdd()) return false;
objectNumber += currentLayerTrace;
return true;
}
/**
* Adds the string.
*
* @param string
* the string
* @param attributes
* the attributes
*/
public void addString(final String string, final TextDrawingAttributes attributes) {
if (increment()) { currentLayer.addString(string, attributes); }
}
/**
* Adds the geometry file.
*
* @param file
* the file
* @param attributes
* the attributes
*/
public void addGeometryFile(final GamaGeometryFile file, final DrawingAttributes attributes) {
if (increment()) { currentLayer.addFile(file, attributes); }
}
/**
* Adds the image.
*
* @param img
* the img
* @param attributes
* the attributes
*/
public void addImage(final Object img, final DrawingAttributes attributes) {
if (increment()) { currentLayer.addImage(img, attributes); }
}
/**
* Adds the geometry.
*
* @param geometry
* the geometry
* @param attributes
* the attributes
*/
public void addGeometry(final Geometry geometry, final DrawingAttributes attributes) {
if (increment()) { currentLayer.addGeometry(geometry, attributes); }
}
/**
* Adds the field.
*
* @param fieldValues
* the field values
* @param attributes
* the attributes
*/
public void addField(final IField fieldValues, final MeshDrawingAttributes attributes) {
if (increment()) { currentLayer.addField(fieldValues, attributes); }
}
/**
* Dispose.
*/
public void dispose() {
layers.clear();
currentLayer = null;
}
/**
* Begin drawing layers.
*/
public void beginDrawingLayers() {
currentLayerTrace = 0;
}
/**
* End drawing layers.
*/
public void endDrawingLayers() {
zIncrement = computeVisualZIncrement();
}
/**
* Rendered.
*
* @return true, if successful
*/
public boolean rendered() {
return rendered;
}
/**
* Reload.
*/
public void reload() {
unlock();
dispose();
initWorld();
}
/**
* Unlock.
*/
public void unlock() {
for (final LayerObject l : layers.values()) { l.unlock(); }
}
/**
* Begin drawing layer.
*
* @param layer
* the layer
* @param alpha
* the alpha
*/
public void beginDrawingLayer(final ILayer layer, final Double alpha) {
final String key = layer.getName() + layer.getDefinition().getOrder();
currentLayer = layers.get(key);
if (currentLayer == null) {
currentLayer = createRegularLayer(renderer, layer);
layers.put(key, currentLayer);
}
currentLayer.setAlpha(alpha);
currentLayerTrace = currentLayer.numberOfTraces();
}
/**
* Creates the regular layer.
*
* @param renderer
* the renderer
* @param layer
* the layer
* @return the layer object
*/
protected LayerObject createRegularLayer(final IOpenGLRenderer renderer, final ILayer layer) {
boolean overlay = layer != null && layer.isOverlay();
return overlay ? new OverlayLayerObject(renderer, layer) : new LayerObject(renderer, layer);
}
/**
* @return
*/
public ModelScene copyStatic() {
final ModelScene newScene = new ModelScene(renderer, false);
layers.forEach((name, layer) -> {
if ((layer.isStatic() || layer.hasTrace()) && !layer.isInvalid()) { newScene.layers.put(name, layer); }
});
return newScene;
}
/**
*
*/
public void invalidateLayers() {
layers.forEach((name, layer) -> { layer.invalidate(); });
}
/**
* Layer offset changed.
*/
public void layerOffsetChanged() {
layers.forEach((name, layer) -> { if (layer.canSplit()) { layer.recomputeOffset(); } });
}
/**
* Recompute layout dimensions.
*
* @param gl
* the gl
*/
// public void recomputeLayoutDimensions(final OpenGL gl) {
// layers.forEach((name, layer) -> { if (layer.isOverlay() || layer.isStatic()) { layer.forceRedraw(gl); } });
//
// }
}