-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathRenderableLayer.java
More file actions
550 lines (501 loc) · 22.3 KB
/
RenderableLayer.java
File metadata and controls
550 lines (501 loc) · 22.3 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
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
/*
* Copyright (C) 2012 United States Government as represented by the Administrator of the
* National Aeronautics and Space Administration.
* All Rights Reserved.
*/
package gov.nasa.worldwind.layers;
import gov.nasa.worldwind.*;
import gov.nasa.worldwind.avlist.AVList;
import gov.nasa.worldwind.event.*;
import gov.nasa.worldwind.pick.PickSupport;
import gov.nasa.worldwind.render.*;
import gov.nasa.worldwind.util.Logging;
import com.jogamp.opengl.GL2;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* The <code>RenderableLayer</code> class manages a collection of {@link gov.nasa.worldwind.render.Renderable} objects
* for rendering, picking, and disposal.
*
* @author tag
* @version $Id: RenderableLayer.java 3435 2015-10-13 10:32:43Z dcollins $
* @see gov.nasa.worldwind.render.Renderable
*/
public class RenderableLayer extends AbstractLayer
{
protected Collection<Renderable> renderables = new ConcurrentLinkedQueue<Renderable>();
protected Iterable<Renderable> renderablesOverride;
protected PickSupport pickSupport = new PickSupport();
/** Creates a new <code>RenderableLayer</code> with a null <code>delegateOwner</code> */
public RenderableLayer()
{
}
/**
* Adds the specified <code>renderable</code> to the end of this layer's internal collection. If this layer's
* internal collection has been overridden with a call to {@link #setRenderables(Iterable)}, this will throw an
* exception.
* <p/>
* If the <code>renderable</code> implements {@link gov.nasa.worldwind.avlist.AVList}, the layer forwards its
* property change events to the layer's property change listeners. Any property change listeners the layer attaches
* to the <code>renderable</code> are removed in {@link #removeRenderable(gov.nasa.worldwind.render.Renderable)},
* {@link #removeAllRenderables()}, or {@link #dispose()}.
*
* @param renderable Renderable to add.
*
* @throws IllegalArgumentException If <code>renderable</code> is null.
* @throws IllegalStateException If a custom Iterable has been specified by a call to <code>setRenderables</code>.
*/
public void addRenderable(Renderable renderable)
{
if (renderable == null)
{
String msg = Logging.getMessage("nullValue.RenderableIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (this.renderablesOverride != null)
{
String msg = Logging.getMessage("generic.LayerIsUsingCustomIterable");
Logging.logger().severe(msg);
throw new IllegalStateException(msg);
}
this.renderables.add(renderable);
// Attach the layer as a property change listener of the renderable. This forwards property change events from
// the renderable to the SceneController.
if (renderable instanceof AVList)
((AVList) renderable).addPropertyChangeListener(this);
}
/**
* Inserts the specified <code>renderable</code> at the specified <code>index</code> in this layer's internal
* collection. If this layer's internal collection has been overridden with a call to {@link
* #setRenderables(Iterable)}, this will throw an exception.
* <p/>
* If the <code>renderable</code> implements {@link gov.nasa.worldwind.avlist.AVList}, the layer forwards its
* property change events to the layer's property change listeners. Any property change listeners the layer attaches
* to the <code>renderable</code> are removed in {@link #removeRenderable(gov.nasa.worldwind.render.Renderable)},
* {@link #removeAllRenderables()}, or {@link #dispose()}.
*
* @param index the index at which to insert the specified renderable.
* @param renderable Renderable to insert.
*
* @throws IllegalArgumentException If <code>renderable</code> is null, if the <code>index</code> is less than zero,
* or if the <code>index</code> is greater than the number of renderables in this
* layer.
* @throws IllegalStateException If a custom Iterable has been specified by a call to <code>setRenderables</code>.
*/
public void addRenderable(int index, Renderable renderable)
{
if (renderable == null)
{
String msg = Logging.getMessage("nullValue.RenderableIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (this.renderablesOverride != null)
{
String msg = Logging.getMessage("generic.LayerIsUsingCustomIterable");
Logging.logger().severe(msg);
throw new IllegalStateException(msg);
}
if (index < 0 || index > this.renderables.size())
{
String msg = Logging.getMessage("generic.indexOutOfRange", index);
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
// The renderables are contained in a ConcurrentLinkedQueue, which does not support element insertion. Make a
// shallow copy of the queue, insert into the copy, then replace the queue contents with the copy. This process
// maintains the element order, with the new renderabable inserted in the specified index.
ArrayList<Renderable> copy = new ArrayList<Renderable>(this.renderables);
copy.add(index, renderable);
this.renderables.clear();
this.renderables.addAll(copy);
// Attach the layer as a property change listener of the renderable. This forwards property change events from
// the renderable to the SceneController.
if (renderable instanceof AVList)
((AVList) renderable).addPropertyChangeListener(this);
}
/**
* Adds the contents of the specified <code>renderables</code> to this layer's internal collection. If this layer's
* internal collection has been overriden with a call to {@link #setRenderables(Iterable)}, this will throw an
* exception.
* <p/>
* If any of the <code>renderables</code> implement {@link gov.nasa.worldwind.avlist.AVList}, the layer forwards
* their property change events to the layer's property change listeners. Any property change listeners the layer
* attaches to the <code>renderable</code> are removed in {@link #removeRenderable(gov.nasa.worldwind.render.Renderable)},
* {@link #removeAllRenderables()}, or {@link #dispose()}.
*
* @param renderables Renderables to add.
*
* @throws IllegalArgumentException If <code>renderables</code> is null.
* @throws IllegalStateException If a custom Iterable has been specified by a call to <code>setRenderables</code>.
*/
public void addRenderables(Iterable<? extends Renderable> renderables)
{
if (renderables == null)
{
String msg = Logging.getMessage("nullValue.IterableIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (this.renderablesOverride != null)
{
String msg = Logging.getMessage("generic.LayerIsUsingCustomIterable");
Logging.logger().severe(msg);
throw new IllegalStateException(msg);
}
for (Renderable renderable : renderables)
{
// Internal list of renderables does not accept null values.
if (renderable != null)
this.renderables.add(renderable);
// Attach the layer as a property change listener of the renderable. This forwards property change events
// from the renderable to the SceneController.
if (renderable instanceof AVList)
((AVList) renderable).addPropertyChangeListener(this);
}
}
/**
* Removes the specified <code>renderable</code> from this layer's internal collection, if it exists. If this
* layer's internal collection has been overridden with a call to {@link #setRenderables(Iterable)}, this will throw
* an exception.
* <p/>
* If the <code>renderable</code> implements {@link gov.nasa.worldwind.avlist.AVList}, this stops forwarding the its
* property change events to the layer's property change listeners. Any property change listeners the layer attached
* to the <code>renderable</code> in {@link #addRenderable(gov.nasa.worldwind.render.Renderable)} or {@link
* #addRenderables(Iterable)} are removed.
*
* @param renderable Renderable to remove.
*
* @throws IllegalArgumentException If <code>renderable</code> is null.
* @throws IllegalStateException If a custom Iterable has been specified by a call to <code>setRenderables</code>.
*/
public void removeRenderable(Renderable renderable)
{
if (renderable == null)
{
String msg = Logging.getMessage("nullValue.RenderableIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (this.renderablesOverride != null)
{
String msg = Logging.getMessage("generic.LayerIsUsingCustomIterable");
Logging.logger().severe(msg);
throw new IllegalStateException(msg);
}
this.renderables.remove(renderable);
// Remove the layer as a property change listener of the renderable. This prevents the renderable from keeping a
// dangling reference to the layer.
if (renderable instanceof AVList)
((AVList) renderable).removePropertyChangeListener(this);
}
/**
* Clears the contents of this layer's internal Renderable collection. If this layer's internal collection has been
* overriden with a call to {@link #setRenderables(Iterable)}, this will throw an exception.
* <p/>
* If any of the <code>renderables</code> implement {@link gov.nasa.worldwind.avlist.AVList}, this stops forwarding
* their property change events to the layer's property change listeners. Any property change listeners the layer
* attached to the <code>renderables</code> in {@link #addRenderable(gov.nasa.worldwind.render.Renderable)} or
* {@link #addRenderables(Iterable)} are removed.
*
* @throws IllegalStateException If a custom Iterable has been specified by a call to <code>setRenderables</code>.
*/
public void removeAllRenderables()
{
if (this.renderablesOverride != null)
{
String msg = Logging.getMessage("generic.LayerIsUsingCustomIterable");
Logging.logger().severe(msg);
throw new IllegalStateException(msg);
}
this.clearRenderables();
}
protected void clearRenderables()
{
if (this.renderables != null && this.renderables.size() > 0)
{
// Remove the layer as property change listener of any renderables. This prevents the renderables from
// keeping a dangling references to the layer.
for (Renderable renderable : this.renderables)
{
if (renderable instanceof AVList)
((AVList) renderable).removePropertyChangeListener(this);
}
this.renderables.clear();
}
}
public int getNumRenderables()
{
if (this.renderablesOverride != null)
{
int size = 0;
//noinspection UnusedDeclaration
for (Renderable r : this.renderablesOverride)
{
++size;
}
return size;
}
else
{
return this.renderables.size();
}
}
/**
* Returns the Iterable of Renderables currently in use by this layer. If the caller has specified a custom Iterable
* via {@link #setRenderables(Iterable)}, this will returns a reference to that Iterable. If the caller passed
* <code>setRenderables</code> a null parameter, or if <code>setRenderables</code> has not been called, this returns
* a view of this layer's internal collection of Renderables.
*
* @return Iterable of currently active Renderables.
*/
public Iterable<Renderable> getRenderables()
{
return this.getActiveRenderables();
}
/**
* Returns the Iterable of currently active Renderables. If the caller has specified a custom Iterable via {@link
* #setRenderables(Iterable)}, this will returns a reference to that Iterable. If the caller passed
* <code>setRenderables</code> a null parameter, or if <code>setRenderables</code> has not been called, this returns
* a view of this layer's internal collection of Renderables.
*
* @return Iterable of currently active Renderables.
*/
protected Iterable<Renderable> getActiveRenderables()
{
if (this.renderablesOverride != null)
{
return this.renderablesOverride;
}
else
{
// Return an unmodifiable reference to the internal list of renderables.
// This prevents callers from changing this list and invalidating any invariants we have established.
return java.util.Collections.unmodifiableCollection(this.renderables);
}
}
/**
* Overrides the collection of currently active Renderables with the specified <code>renderableIterable</code>. This
* layer will maintain a reference to <code>renderableIterable</code> strictly for picking and rendering. This layer
* will not modify the reference, or dispose of its contents. This will also clear and dispose of the internal
* collection of Renderables, and will prevent any modification to its contents via <code>addRenderable,
* addRenderables, removeRenderables, or dispose</code>.
* <p/>
* Unlike {@link #addRenderable(gov.nasa.worldwind.render.Renderable)} or {@link #addRenderables(Iterable)}, this
* does not forward any of the renderable's property change events to the layer's property change listeners. Since
* the layer is not in control of the iIterable's contents, attaching property change listeners to the renderables
* could cause the them to hold dangling references to the layer. If any of the renderables in the Iterable rely on
* forwarding property change events for proper operation - such as {@link gov.nasa.worldwind.render.AbstractBrowserBalloon}
* - use {@link #addRenderables(Iterable)} instead.
* <p/>
* If the specified <code>renderableIterable</code> is null, this layer reverts to maintaining its internal
* collection.
*
* @param renderableIterable Iterable to use instead of this layer's internal collection, or null to use this
* layer's internal collection.
*/
public void setRenderables(Iterable<Renderable> renderableIterable)
{
this.renderablesOverride = renderableIterable;
// Dispose of the internal collection of Renderables.
this.disposeRenderables();
// Clear the internal collection of Renderables.
this.clearRenderables();
}
/**
* Opacity is not applied to layers of this type because each renderable typically has its own opacity control.
*
* @param opacity the current opacity value, which is ignored by this layer.
*/
@Override
public void setOpacity(double opacity)
{
super.setOpacity(opacity);
}
/**
* Returns the layer's opacity value, which is ignored by this layer because each of its renderables typiically has
* its own opacity control.
*
* @return The layer opacity, a value between 0 and 1.
*/
@Override
public double getOpacity()
{
return super.getOpacity();
}
/**
* Disposes the contents of this layer's internal Renderable collection, but does not remove any elements from that
* collection.
* <p/>
* If any of layer's internal Renderables implement {@link gov.nasa.worldwind.avlist.AVList}, this stops forwarding
* their property change events to the layer's property change listeners. Any property change listeners the layer
* attached to the <code>renderables</code> in {@link #addRenderable(gov.nasa.worldwind.render.Renderable)} or
* {@link #addRenderables(Iterable)} are removed.
*
* @throws IllegalStateException If a custom Iterable has been specified by a call to <code>setRenderables</code>.
*/
public void dispose()
{
if (this.renderablesOverride != null)
{
String msg = Logging.getMessage("generic.LayerIsUsingCustomIterable");
Logging.logger().severe(msg);
throw new IllegalStateException(msg);
}
this.disposeRenderables();
}
protected void disposeRenderables()
{
if (this.renderables != null && this.renderables.size() > 0)
{
for (Renderable renderable : this.renderables)
{
try
{
// Remove the layer as a property change listener of the renderable. This prevents the renderable
// from keeping a dangling reference to the layer.
if (renderable instanceof AVList)
((AVList) renderable).removePropertyChangeListener(this);
if (renderable instanceof Disposable)
((Disposable) renderable).dispose();
}
catch (Exception e)
{
String msg = Logging.getMessage("generic.ExceptionAttemptingToDisposeRenderable");
Logging.logger().severe(msg);
// continue to next renderable
}
}
}
this.renderables.clear();
}
protected void doPreRender(DrawContext dc)
{
this.doPreRender(dc, this.getActiveRenderables());
}
protected void doPick(DrawContext dc, java.awt.Point pickPoint)
{
this.doPick(dc, this.getActiveRenderables(), pickPoint);
}
protected void doRender(DrawContext dc)
{
this.doRender(dc, this.getActiveRenderables());
}
protected void doPreRender(DrawContext dc, Iterable<? extends Renderable> renderables)
{
for (Renderable renderable : renderables)
{
try
{
// If the caller has specified their own Iterable,
// then we cannot make any guarantees about its contents.
if (renderable != null && renderable instanceof PreRenderable)
((PreRenderable) renderable).preRender(dc);
}
catch (Exception e)
{
String msg = Logging.getMessage("generic.ExceptionWhilePrerenderingRenderable");
Logging.logger().severe(msg);
// continue to next renderable
}
}
}
protected void doPick(DrawContext dc, Iterable<? extends Renderable> renderables, java.awt.Point pickPoint)
{
GL2 gl = dc.getGL().getGL2(); // GL initialization checks for GL2 compatibility.
this.pickSupport.clearPickList();
this.pickSupport.beginPicking(dc);
try
{
for (Renderable renderable : renderables)
{
// If the caller has specified their own Iterable,
// then we cannot make any guarantees about its contents.
if (renderable != null)
{
// float[] inColor = new float[4];
// gl.glGetFloatv(GL.GL_CURRENT_COLOR, inColor, 0);
java.awt.Color color = dc.getUniquePickColor();
gl.glColor3ub((byte) color.getRed(), (byte) color.getGreen(), (byte) color.getBlue());
try
{
renderable.render(dc);
}
catch (Exception e)
{
String msg = Logging.getMessage("generic.ExceptionWhilePickingRenderable");
Logging.logger().severe(msg);
Logging.logger().log(java.util.logging.Level.FINER, msg, e); // show exception for this level
continue; // go on to next renderable
}
//
// gl.glColor4fv(inColor, 0);
if (renderable instanceof Locatable)
{
this.pickSupport.addPickableObject(color.getRGB(), renderable,
((Locatable) renderable).getPosition(), false);
}
else
{
this.pickSupport.addPickableObject(color.getRGB(), renderable);
}
}
}
this.pickSupport.resolvePick(dc, pickPoint, this);
}
finally
{
this.pickSupport.endPicking(dc);
}
}
protected void doRender(DrawContext dc, Iterable<? extends Renderable> renderables)
{
for (Renderable renderable : renderables)
{
try
{
// If the caller has specified their own Iterable,
// then we cannot make any guarantees about its contents.
if (renderable != null)
renderable.render(dc);
}
catch (Exception e)
{
String msg = Logging.getMessage("generic.ExceptionWhileRenderingRenderable");
Logging.logger().log(java.util.logging.Level.SEVERE, msg, e);
// continue to next renderable
}
}
}
@Override
public String toString()
{
return Logging.getMessage("layers.RenderableLayer.Name");
}
/**
* {@inheritDoc}
* <p/>
* This implementation forwards the message to each Renderable that implements {@link MessageListener}.
*
* @param message The message that was received.
*/
@Override
public void onMessage(Message message)
{
for (Renderable renderable : this.renderables)
{
try
{
if (renderable instanceof MessageListener)
((MessageListener) renderable).onMessage(message);
}
catch (Exception e)
{
String msg = Logging.getMessage("generic.ExceptionInvokingMessageListener");
Logging.logger().log(java.util.logging.Level.SEVERE, msg, e);
// continue to next renderable
}
}
}
}