-
Notifications
You must be signed in to change notification settings - Fork 4
/
LayerObject.java
631 lines (559 loc) · 14.4 KB
/
LayerObject.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*******************************************************************************************************
*
* LayerObject.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.layers;
import java.util.ArrayList;
import java.util.LinkedList;
import org.locationtech.jts.geom.Geometry;
import com.jogamp.opengl.GL;
import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
import msi.gama.common.geometry.AxisAngle;
import msi.gama.common.geometry.Rotation3D;
import msi.gama.common.geometry.Scaling3D;
import msi.gama.common.interfaces.IKeyword;
import msi.gama.common.interfaces.ILayer;
import msi.gama.metamodel.shape.GamaPoint;
import msi.gama.runtime.IScope;
import msi.gama.util.file.GamaGeometryFile;
import msi.gama.util.matrix.IField;
import msi.gaml.expressions.IExpression;
import msi.gaml.expressions.units.PixelUnitExpression;
import msi.gaml.operators.Cast;
import msi.gaml.statements.draw.DrawingAttributes;
import msi.gaml.statements.draw.MeshDrawingAttributes;
import msi.gaml.statements.draw.TextDrawingAttributes;
import msi.gaml.types.GamaGeometryType;
import ummisco.gama.opengl.OpenGL;
import ummisco.gama.opengl.renderer.IOpenGLRenderer;
import ummisco.gama.opengl.scene.AbstractObject;
import ummisco.gama.opengl.scene.geometry.GeometryObject;
import ummisco.gama.opengl.scene.mesh.MeshObject;
import ummisco.gama.opengl.scene.resources.ResourceObject;
import ummisco.gama.opengl.scene.text.StringObject;
/**
* Class LayerObject.
*
* @author drogoul
* @since 3 mars 2014
*
*/
@SuppressWarnings ({ "rawtypes", "unchecked" })
public class LayerObject {
/** The Constant NULL_OFFSET. */
final static GamaPoint NULL_OFFSET = new GamaPoint();
/** The Constant NULL_SCALE. */
final static GamaPoint NULL_SCALE = new GamaPoint(1, 1, 1);
/** The Constant NULL_ROTATION. */
final static AxisAngle NULL_ROTATION = new AxisAngle(0d);
/**
* The Class Trace.
*/
class Trace extends ArrayList<AbstractObject<?, ?>> {
/** The offset. */
final GamaPoint offset = new GamaPoint(NULL_OFFSET);
/** The scale. */
final GamaPoint scale = new GamaPoint(NULL_SCALE);
/** The rotation. */
AxisAngle rotation = NULL_ROTATION;
/**
* Instantiates a new trace.
*/
Trace() {
computeOffset();
computeScale();
computeRotation();
}
/**
* Compute scale.
*
* @return the gama point
*/
public void computeScale() {
LayerObject.this.computeScale(this);
}
/**
* Compute offset.
*
* @return the gama point
*/
public void computeOffset() {
LayerObject.this.computeOffset(this);
}
/**
* Compute rotation.
*/
public void computeRotation() {
LayerObject.this.computeRotation(this);
}
/**
* Gets the offset.
*
* @return the offset
*/
public GamaPoint getOffset() { return offset; }
/**
* Gets the scale.
*
* @return the scale
*/
public GamaPoint getScale() { return scale; }
/**
* Gets the rotation.
*
* @return the rotation
*/
public AxisAngle getRotation() { return rotation; }
/**
* As array.
*
* @return the abstract object[]
*/
public AbstractObject[] asArray() {
return toArray(new AbstractObject[size()]);
}
}
/** The alpha. */
protected volatile Double alpha = 1d;
/** The layer. */
public final ILayer layer;
/** The is invalid. */
volatile boolean isInvalid;
/** The locked. */
private volatile boolean locked;
/** The is animated. */
volatile boolean isAnimated;
/** The renderer. */
protected final IOpenGLRenderer renderer;
/** The traces. */
protected final LinkedList<Trace> traces;
/** The current list. */
protected Trace currentList;
/** The open GL list index. */
protected volatile Integer openGLListIndex;
/** The is fading. */
protected volatile boolean isFading;
/**
* Instantiates a new layer object.
*
* @param renderer2
* the renderer 2
* @param layer
* the layer
*/
public LayerObject(final IOpenGLRenderer renderer2, final ILayer layer) {
this.renderer = renderer2;
this.layer = layer;
currentList = new Trace();
if (layer == null) {
traces = null;
} else {
Integer number = layer.getData().getTrace();
if (number == null || number == 0) {
traces = null;
} else {
traces = new LinkedList();
traces.add(currentList);
}
}
}
/**
* Compute rotation.
*
* @param trace
* the trace
*/
public void computeRotation(final Trace trace) {
AxisAngle oldRotation = trace.rotation;
// if vectors are different... (not computed for the moment)
// if angles are different
Double newAngleInDeg = layer.getData().getRotation();
if (newAngleInDeg.equals(oldRotation.getAngle())) return;
trace.rotation = new AxisAngle(Rotation3D.PLUS_K, newAngleInDeg);
}
/**
* Compute scale.
*/
public void computeScale(final Trace list) {
double zScale = layer.getData().getSize().getZ();
if (zScale <= 0) { zScale = 1; }
list.scale.setLocation(renderer.getLayerWidth() / renderer.getWidth(),
renderer.getLayerHeight() / renderer.getHeight(), zScale);
}
/**
* Compute offset.
*/
public void computeOffset(final Trace list) {
final IScope scope = renderer.getSurface().getScope();
final IExpression expr = layer.getDefinition().getFacet(IKeyword.POSITION);
if (expr != null) {
final boolean containsPixels = expr.findAny(e -> e instanceof PixelUnitExpression);
GamaPoint offset = list.offset;
offset.setLocation(Cast.asPoint(scope, expr.value(scope)));
if (Math.abs(offset.x) <= 1 && !containsPixels) { offset.x *= renderer.getEnvWidth(); }
if (Math.abs(offset.y) <= 1 && !containsPixels) { offset.y *= renderer.getEnvHeight(); }
//REMOVE TO FIX #3342
//if (offset.x < 0) { offset.x = renderer.getEnvWidth() - offset.x; }
//if (offset.y < 0) { offset.y = renderer.getEnvHeight() - offset.y; }
}
increaseZ(list);
}
/**
* Increase Z.
*/
protected void increaseZ(final Trace list) {
double currentZLayer = renderer.getMaxEnvDim() * layer.getData().getPosition().getZ();
currentZLayer += layer.getData().getAddedElevation() * renderer.getMaxEnvDim();
list.offset.z = currentZLayer;
}
/**
* Checks if is light interaction.
*
* @return true, if is light interaction
*/
public boolean isLightInteraction() { return true; }
/**
* Checks if is pickable.
*
* @return true, if is pickable
*/
protected boolean isPickable() { return layer != null && layer.getData().isSelectable(); }
/**
* Draw.
*
* @param gl
* the gl
*/
public void draw(final OpenGL gl) {
if (isInvalid()) return;
if (hasDepth()) {
gl.getGL().glEnable(GL.GL_DEPTH_TEST);
} else {
// Addition to fix #2228 and #2222
gl.suspendZTranslation();
gl.getGL().glDisable(GL.GL_DEPTH_TEST);
}
gl.push(GLMatrixFunc.GL_MODELVIEW);
try {
doDrawing(gl);
} finally {
stopDrawing(gl);
}
}
/**
* Do drawing.
*
* @param gl
* the gl
* @param picking
* the picking
*/
protected void doDrawing(final OpenGL gl) {
if (renderer.getPickingHelper().isPicking()) {
if (isPickable()) { gl.runWithNames(() -> drawAllObjects(gl, true)); }
} else if (isAnimated) {
drawAllObjects(gl, false);
} else {
if (openGLListIndex == null) { openGLListIndex = gl.compileAsList(() -> drawAllObjects(gl, false)); }
gl.drawList(openGLListIndex);
}
}
/**
* Prepare drawing.
*
* @param gl
* the gl
*/
protected void prepareDrawing(final OpenGL gl, final Trace list) {
final GamaPoint nonNullOffset = list.getOffset();
gl.translateBy(nonNullOffset.x, -nonNullOffset.y, hasDepth() ? nonNullOffset.z : 0);
final GamaPoint nonNullScale = list.getScale();
gl.scaleBy(nonNullScale.x, nonNullScale.y, nonNullScale.z);
final AxisAngle nonNullRotation = list.getRotation();
// Rotation
double x = nonNullOffset.x + renderer.getEnvWidth() * nonNullScale.x / 2;
double y = nonNullOffset.y + renderer.getEnvHeight() * nonNullScale.y / 2;
gl.translateBy(x, -y, 0d);
GamaPoint p = nonNullRotation.getAxis();
gl.rotateBy(nonNullRotation.getAngle(), p.x, p.y, p.z);
gl.translateBy(-x, y, 0d);
}
/**
* Enable depth test.
*
* @return true, if successful
*/
protected boolean hasDepth() {
return true;
}
/**
* Stop drawing.
*
* @param gl
* the gl
*/
protected void stopDrawing(final OpenGL gl) {
gl.pop(GLMatrixFunc.GL_MODELVIEW);
}
/**
* Draw all objects.
*
* @param gl
* the gl
* @param picking
* the picking
*/
protected void drawAllObjects(final OpenGL gl, final boolean picking) {
if (traces != null) {
double delta = 0;
if (isFading) {
final int size = traces.size();
delta = size == 0 ? 0 : 1d / size;
}
double alpha = 0d;
for (final Trace list : traces) {
alpha = delta == 0d ? this.alpha : this.alpha * (alpha + delta);
drawObjects(gl, list, alpha, picking);
}
} else {
drawObjects(gl, currentList, alpha, picking);
}
}
/**
* Draw objects.
*
* @param gl
* the gl
* @param list
* the list
* @param alpha
* the alpha
* @param picking
* the picking
*/
protected final void drawObjects(final OpenGL gl, final Trace list, final double alpha, final boolean picking) {
prepareDrawing(gl, list);
gl.setCurrentObjectAlpha(alpha);
for (final AbstractObject object : list.asArray()) { gl.getDrawerFor(object.type).draw(object, picking); }
}
/**
* Checks if is static.
*
* @return true, if is static
*/
public boolean isStatic() { return layer == null || !layer.getData().isDynamic(); }
/**
* Sets the alpha.
*
* @param a
* the new alpha
*/
public void setAlpha(final Double a) { alpha = a; }
/**
* Sets the offset.
*
* @param offset
* the new offset
*/
public void setOffset(final GamaPoint offset) {
if (offset != null) {
currentList.offset.setLocation(offset);
} else {
currentList.offset.setLocation(NULL_OFFSET);
}
}
/**
* Sets the scale.
*
* @param scale
* the new scale
*/
public void setScale(final GamaPoint scale) {
currentList.scale.setLocation(scale);
}
/**
* Adds the string.
*
* @param string
* the string
* @param attributes
* the attributes
*/
public void addString(final String string, final TextDrawingAttributes attributes) {
currentList.add(new StringObject(string, attributes));
}
/**
* Adds the file.
*
* @param file
* the file
* @param attributes
* the attributes
*/
public void addFile(final GamaGeometryFile file, final DrawingAttributes attributes) {
currentList.add(new ResourceObject(file, attributes));
}
/**
* Adds the image.
*
* @param o
* the o
* @param attributes
* the attributes
*/
public void addImage(final Object o, final DrawingAttributes attributes) {
// If no dimensions have been defined, then the image is considered as wide and tall as the environment
Scaling3D size = attributes.getSize();
if (size == null) {
size = Scaling3D.of(renderer.getEnvWidth(), renderer.getEnvHeight(), 0);
attributes.setSize(size);
}
final GamaPoint loc = attributes.getLocation();
final GamaPoint newLoc = loc == null ? size.toGamaPoint().dividedBy(2) : loc;
// We build a rectangle that will serve as a "support" for the image (which will become its texture)
final Geometry geometry =
GamaGeometryType.buildRectangle(size.getX(), size.getY(), new GamaPoint()).getInnerGeometry();
attributes.setLocation(newLoc);
attributes.setTexture(o);
attributes.setSynthetic(true);
addGeometry(geometry, attributes);
}
/**
* Adds the field.
*
* @param fieldValues
* the field values
* @param attributes
* the attributes
*/
public void addField(final IField fieldValues, final MeshDrawingAttributes attributes) {
currentList.add(new MeshObject(fieldValues, attributes));
}
/**
* Adds the geometry.
*
* @param geometry
* the geometry
* @param attributes
* the attributes
*/
public void addGeometry(final Geometry geometry, final DrawingAttributes attributes) {
isAnimated = /* isAnimated || ?? */attributes.isAnimated();
currentList.add(new GeometryObject(geometry, attributes));
}
/**
* Gets the trace.
*
* @return the trace
*/
protected int getTrace() {
if (layer == null) return 0;
final Integer trace = layer.getData().getTrace();
return trace == null ? 0 : trace;
}
/**
* Gets the fading.
*
* @return the fading
*/
protected boolean getFading() {
if (layer == null) return false;
final Boolean fading = layer.getData().getFading();
return fading == null ? false : fading;
}
/**
* Clear.
*
* @param gl
* the gl
*/
public void clear(final OpenGL gl) {
if (traces != null) {
final int sizeLimit = getTrace();
isFading = getFading();
final int size = traces.size();
for (int i = 0, n = size - sizeLimit; i < n; i++) { traces.poll(); }
currentList = new Trace();
traces.offer(currentList);
} else {
currentList.clear();
}
final Integer index = openGLListIndex;
if (index != null) {
gl.deleteList(index);
openGLListIndex = null;
}
}
/**
* Checks if is invalid.
*
* @return true, if is invalid
*/
public boolean isInvalid() { return isInvalid; }
/**
* Invalidate.
*/
public void invalidate() {
isInvalid = true;
}
/**
* Checks for trace.
*
* @return true, if successful
*/
public boolean hasTrace() {
return getTrace() > 0;
}
/**
* Checks if is locked.
*
* @return true, if is locked
*/
public boolean isLocked() { return locked; }
/**
* Lock.
*/
public void lock() {
locked = true;
}
/**
* Unlock.
*/
public void unlock() {
locked = false;
}
/**
* Checks if is overlay.
*
* @return true, if is overlay
*/
public boolean isOverlay() { return false; }
/**
* Number of traces.
*
* @return the int
*/
public int numberOfTraces() {
return traces == null ? 1 : traces.size();
}
/**
* Can split.
*
* @return true, if successful
*/
public boolean canSplit() {
return true;
}
/**
* Recompute offset.
*/
public void recomputeOffset() {
computeOffset(currentList);
}
}