-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathAbstractSceneController.java
More file actions
1130 lines (978 loc) · 39.3 KB
/
AbstractSceneController.java
File metadata and controls
1130 lines (978 loc) · 39.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
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.cache.GpuResourceCache;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.layers.Layer;
import gov.nasa.worldwind.pick.*;
import gov.nasa.worldwind.render.*;
import gov.nasa.worldwind.terrain.*;
import gov.nasa.worldwind.util.*;
import com.jogamp.opengl.*;
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.logging.Level;
/**
* @author tag
* @version $Id: AbstractSceneController.java 2442 2014-11-19 22:50:42Z tgaskins $
*/
public abstract class AbstractSceneController extends WWObjectImpl implements SceneController
{
protected Model model;
protected View view;
protected double verticalExaggeration = 1d;
protected DrawContext dc = new DrawContextImpl();
/**
* The list of picked objects at the current pick point. This list is computed during each call to repaint.
* Initially <code>null</code>.
*/
protected PickedObjectList lastPickedObjects;
/**
* The list of picked objects that intersect the current pick rectangle. This list is computed during each call to
* repaint. Initially <code>null</code>.
*/
protected PickedObjectList lastObjectsInPickRect;
/**
* Map of integer color codes to picked objects used to quickly resolve the top picked objects in {@link
* #doResolveTopPick(gov.nasa.worldwind.render.DrawContext, java.awt.Rectangle)}. This map is used only when a pick
* rectangle is specified. Initialized to a new HashMap.
*/
protected Map<Integer, PickedObject> pickableObjects = new HashMap<Integer, PickedObject>();
protected long frame = 0;
protected long timebase = System.currentTimeMillis();
protected double framesPerSecond;
protected double frameTime;
protected double pickTime;
/**
* The pick point in AWT screen coordinates, or <code>null</code> if the pick point is disabled. Initially
* <code>null</code>.
*/
protected Point pickPoint = null;
/**
* The pick rectangle in AWT screen coordinates, or <code>null</code> if the pick rectangle is disabled. Initially
* <code>null</code>.
*/
protected Rectangle pickRect = null;
protected boolean deepPick = false;
protected GpuResourceCache gpuResourceCache;
protected TextRendererCache textRendererCache = new TextRendererCache();
protected Set<String> perFrameStatisticsKeys = new HashSet<String>();
protected Collection<PerformanceStatistic> perFrameStatistics = new ArrayList<PerformanceStatistic>();
protected Collection<Throwable> renderingExceptions = new ArrayList<Throwable>();
protected ScreenCreditController screenCreditController;
protected GLRuntimeCapabilities glRuntimeCaps = new GLRuntimeCapabilities();
protected ArrayList<Point> pickPoints = new ArrayList<Point>();
/**
* Support class used to build the composite representation of surface objects as a list of SurfaceTiles. We keep a
* reference to the tile builder instance used to build tiles because it acts as a cache key to the tiles and
* determines when the tiles must be updated. The tile builder does not retain any references the SurfaceObjects, so
* keeping a reference to it does not leak memory.
*/
protected SurfaceObjectTileBuilder surfaceObjectTileBuilder = new SurfaceObjectTileBuilder();
/** The display name for the surface object tile count performance statistic. */
protected static final String SURFACE_OBJECT_TILE_COUNT_NAME = "Surface Object Tiles";
protected ClutterFilter clutterFilter = new BasicClutterFilter();
//protected Map<String, GroupingFilter> groupingFilters = new HashMap<String, GroupingFilter>();
protected boolean deferOrderedRendering;
public AbstractSceneController()
{
this.setVerticalExaggeration(Configuration.getDoubleValue(AVKey.VERTICAL_EXAGGERATION, 1d));
}
public void reinitialize()
{
if (this.textRendererCache != null)
this.textRendererCache.dispose();
this.textRendererCache = new TextRendererCache();
}
/** Releases resources associated with this scene controller. */
public void dispose()
{
if (this.lastPickedObjects != null)
this.lastPickedObjects.clear();
this.lastPickedObjects = null;
if (this.lastObjectsInPickRect != null)
this.lastObjectsInPickRect.clear();
this.lastObjectsInPickRect = null;
if (this.dc != null)
this.dc.dispose();
if (this.textRendererCache != null)
this.textRendererCache.dispose();
}
public GpuResourceCache getGpuResourceCache()
{
return this.gpuResourceCache;
}
public void setGpuResourceCache(GpuResourceCache gpuResourceCache)
{
this.gpuResourceCache = gpuResourceCache;
}
public TextRendererCache getTextRendererCache()
{
return textRendererCache;
}
public Model getModel()
{
return this.model;
}
public View getView()
{
return this.view;
}
public void setModel(Model model)
{
if (this.model != null)
this.model.removePropertyChangeListener(this);
if (model != null)
model.addPropertyChangeListener(this);
Model oldModel = this.model;
this.model = model;
this.firePropertyChange(AVKey.MODEL, oldModel, model);
}
public void setView(View view)
{
if (this.view != null)
this.view.removePropertyChangeListener(this);
if (view != null)
view.addPropertyChangeListener(this);
View oldView = this.view;
this.view = view;
this.firePropertyChange(AVKey.VIEW, oldView, view);
}
public void setVerticalExaggeration(double verticalExaggeration)
{
Double oldVE = this.verticalExaggeration;
this.verticalExaggeration = verticalExaggeration;
this.firePropertyChange(AVKey.VERTICAL_EXAGGERATION, oldVE, verticalExaggeration);
}
public double getVerticalExaggeration()
{
return this.verticalExaggeration;
}
/** {@inheritDoc} */
public void setPickPoint(Point pickPoint)
{
this.pickPoint = pickPoint;
}
/** {@inheritDoc} */
public Point getPickPoint()
{
return this.pickPoint;
}
/** {@inheritDoc} */
public void setPickRectangle(Rectangle pickRect)
{
this.pickRect = pickRect;
}
/** {@inheritDoc} */
public Rectangle getPickRectangle()
{
return this.pickRect;
}
/** {@inheritDoc} */
public PickedObjectList getPickedObjectList()
{
return this.lastPickedObjects;
}
protected void setPickedObjectList(PickedObjectList pol)
{
this.lastPickedObjects = pol;
}
/** {@inheritDoc} */
public PickedObjectList getObjectsInPickRectangle()
{
return this.lastObjectsInPickRect;
}
public void setDeepPickEnabled(boolean tf)
{
this.deepPick = tf;
}
public boolean isDeepPickEnabled()
{
return this.deepPick;
}
public SectorGeometryList getTerrain()
{
return this.dc.getSurfaceGeometry();
}
public DrawContext getDrawContext()
{
return this.dc;
}
public double getFramesPerSecond()
{
return this.framesPerSecond;
}
public double getFrameTime()
{
return this.frameTime;
}
public void setPerFrameStatisticsKeys(Set<String> keys)
{
this.perFrameStatisticsKeys.clear();
if (keys == null)
return;
for (String key : keys)
{
if (key != null)
this.perFrameStatisticsKeys.add(key);
}
}
public Collection<PerformanceStatistic> getPerFrameStatistics()
{
return perFrameStatistics;
}
public Collection<Throwable> getRenderingExceptions()
{
return this.renderingExceptions;
}
public ScreenCreditController getScreenCreditController()
{
return screenCreditController;
}
public void setScreenCreditController(ScreenCreditController screenCreditController)
{
this.screenCreditController = screenCreditController;
}
/** {@inheritDoc} */
public GLRuntimeCapabilities getGLRuntimeCapabilities()
{
return this.glRuntimeCaps;
}
/** {@inheritDoc} */
public void setGLRuntimeCapabilities(GLRuntimeCapabilities capabilities)
{
if (capabilities == null)
{
String message = Logging.getMessage("nullValue.GLRuntimeCapabilitiesIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
this.glRuntimeCaps = capabilities;
}
@Override
public ClutterFilter getClutterFilter()
{
return clutterFilter;
}
@Override
public void setClutterFilter(ClutterFilter clutterFilter)
{
this.clutterFilter = clutterFilter;
}
//
// @Override
// public GroupingFilter getGroupingFilter(String filterName)
// {
// if (WWUtil.isEmpty(filterName))
// {
// String msg = Logging.getMessage("nullValue.OrderedRenderable"); // TODO: proper log message
// Logging.logger().warning(msg);
// return null;
// }
//
// return this.dc.getGroupingFilter(filterName);
// }
//
// @Override
// public void addGroupingFilter(String filterName, GroupingFilter filter)
// {
// if (WWUtil.isEmpty(filterName))
// {
// String msg = Logging.getMessage("nullValue.OrderedRenderable"); // TODO: proper log message
// Logging.logger().warning(msg);
// return;
// }
//
// if (filter == null)
// {
// String msg = Logging.getMessage("nullValue.OrderedRenderable"); // TODO: proper log message
// Logging.logger().warning(msg);
// return; // benign event
// }
//
// this.groupingFilters.put(filterName, filter);
// }
//
// @Override
// public void removeGroupingFilter(String filterName)
// {
// if (WWUtil.isEmpty(filterName))
// {
// String msg = Logging.getMessage("nullValue.OrderedRenderable"); // TODO: proper log message
// Logging.logger().warning(msg);
// return;
// }
//
// this.groupingFilters.remove(filterName);
// }
//
// @Override
// public void removeAllGroupingFilters()
// {
// this.groupingFilters.clear();
// }
//
// protected void resetGroupingFilters()
// {
// if (this.getView() == null)
// return;
//
// Rectangle2D vp = this.getView().getViewport();
// if (vp == null)
// return;
//
// for (GroupingFilter filter : this.groupingFilters.values())
// {
// filter.clear();
// filter.setDimensions((int) vp.getWidth(), (int) vp.getHeight());
// }
// }
public boolean isDeferOrderedRendering()
{
return deferOrderedRendering;
}
public void setDeferOrderedRendering(boolean deferOrderedRendering)
{
this.deferOrderedRendering = deferOrderedRendering;
}
public int repaint()
{
this.frameTime = System.currentTimeMillis();
this.perFrameStatistics.clear();
this.renderingExceptions.clear(); // Clear the rendering exceptions accumulated during the last frame.
this.glRuntimeCaps.initialize(GLContext.getCurrent());
this.initializeDrawContext(this.dc);
this.doRepaint(this.dc);
++this.frame;
long time = System.currentTimeMillis();
this.frameTime = System.currentTimeMillis() - this.frameTime;
if (time - this.timebase > 2000) // recalculate every two seconds
{
this.framesPerSecond = frame * 1000d / (time - timebase);
this.timebase = time;
this.frame = 0;
}
this.dc.setPerFrameStatistic(PerformanceStatistic.FRAME_TIME, "Frame Time (ms)", (int) this.frameTime);
this.dc.setPerFrameStatistic(PerformanceStatistic.FRAME_RATE, "Frame Rate (fps)", (int) this.framesPerSecond);
this.dc.setPerFrameStatistic(PerformanceStatistic.PICK_TIME, "Pick Time (ms)", (int) this.pickTime);
Set<String> perfKeys = dc.getPerFrameStatisticsKeys();
if (perfKeys == null)
return dc.getRedrawRequested();
if (perfKeys.contains(PerformanceStatistic.MEMORY_CACHE) || perfKeys.contains(PerformanceStatistic.ALL))
{
this.dc.setPerFrameStatistics(WorldWind.getMemoryCacheSet().getPerformanceStatistics());
}
if (perfKeys.contains(PerformanceStatistic.TEXTURE_CACHE) || perfKeys.contains(PerformanceStatistic.ALL))
{
if (dc.getTextureCache() != null)
this.dc.setPerFrameStatistic(PerformanceStatistic.TEXTURE_CACHE,
"Texture Cache size (Kb)", this.dc.getTextureCache().getUsedCapacity() / 1000);
}
if (perfKeys.contains(PerformanceStatistic.JVM_HEAP) || perfKeys.contains(PerformanceStatistic.ALL))
{
long totalMemory = Runtime.getRuntime().totalMemory();
this.dc.setPerFrameStatistic(PerformanceStatistic.JVM_HEAP,
"JVM total memory (Kb)", totalMemory / 1000);
this.dc.setPerFrameStatistic(PerformanceStatistic.JVM_HEAP_USED,
"JVM used memory (Kb)", (totalMemory - Runtime.getRuntime().freeMemory()) / 1000);
}
return dc.getRedrawRequested();
}
abstract protected void doRepaint(DrawContext dc);
protected void initializeDrawContext(DrawContext dc)
{
dc.initialize(GLContext.getCurrent());
dc.setGLRuntimeCapabilities(this.glRuntimeCaps);
dc.setPerFrameStatisticsKeys(this.perFrameStatisticsKeys, this.perFrameStatistics);
dc.setRenderingExceptions(this.renderingExceptions);
dc.setGpuResourceCache(this.gpuResourceCache);
dc.setTextRendererCache(this.textRendererCache);
dc.setModel(this.model);
dc.setView(this.view);
dc.setVerticalExaggeration(this.verticalExaggeration);
dc.setPickPoint(this.pickPoint);
dc.setPickRectangle(this.pickRect);
dc.setViewportCenterScreenPoint(this.getViewportCenter(dc));
dc.setViewportCenterPosition(null);
dc.setClutterFilter(this.getClutterFilter());
// dc.setGroupingFilters(this.groupingFilters);
long frameTimeStamp = System.currentTimeMillis();
// Ensure that the frame time stamps differ between frames. This is necessary on machines with low-resolution
// JVM clocks or that are so fast that they render under 1 millisecond.
if (frameTimeStamp == dc.getFrameTimeStamp())
{
++frameTimeStamp;
}
dc.setFrameTimeStamp(frameTimeStamp);
// Indicate the frame time stamp to apps.
this.setValue(AVKey.FRAME_TIMESTAMP, frameTimeStamp);
}
protected Point getViewportCenter(DrawContext dc)
{
View view = dc.getView();
if (view == null)
return null;
Rectangle viewport = view.getViewport();
if (viewport == null)
return null;
return new Point((int) (viewport.getCenterX() + 0.5), (int) (viewport.getCenterY() + 0.5));
}
protected void initializeFrame(DrawContext dc)
{
if (dc.getGLContext() == null)
{
String message = Logging.getMessage("BasicSceneController.GLContextNullStartRedisplay");
Logging.logger().severe(message);
throw new IllegalStateException(message);
}
GL2 gl = dc.getGL().getGL2(); // GL initialization checks for GL2 compatibility.
gl.glPushAttrib(GL2.GL_VIEWPORT_BIT | GL2.GL_ENABLE_BIT | GL2.GL_TRANSFORM_BIT);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glEnable(GL.GL_DEPTH_TEST);
}
protected void clearFrame(DrawContext dc)
{
Color cc = dc.getClearColor();
dc.getGL().glClearColor(cc.getRed(), cc.getGreen(), cc.getBlue(), cc.getAlpha());
dc.getGL().glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
}
protected void finalizeFrame(DrawContext dc)
{
GL2 gl = dc.getGL().getGL2(); // GL initialization checks for GL2 compatibility.
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glPopMatrix();
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glPopMatrix();
gl.glPopAttrib();
// checkGLErrors(dc);
}
protected void applyView(DrawContext dc)
{
if (dc.getView() != null)
dc.getView().apply(dc);
//
// this.resetGroupingFilters();
}
protected void createPickFrustum(DrawContext dc)
{
dc.addPickPointFrustum();
dc.addPickRectangleFrustum();
}
protected void createTerrain(DrawContext dc)
{
if (dc.getSurfaceGeometry() == null)
{
if (dc.getModel() != null && dc.getModel().getGlobe() != null)
{
SectorGeometryList sgl = dc.getModel().getGlobe().tessellate(dc);
dc.setSurfaceGeometry(sgl);
dc.setVisibleSector(sgl.getSector());
}
if (dc.getSurfaceGeometry() == null)
{
Logging.logger().warning("generic.NoSurfaceGeometry");
dc.setPerFrameStatistic(PerformanceStatistic.TERRAIN_TILE_COUNT, "Terrain Tiles", 0);
// keep going because some layers, etc. may have meaning w/o surface geometry
}
dc.setPerFrameStatistic(PerformanceStatistic.TERRAIN_TILE_COUNT, "Terrain Tiles",
dc.getSurfaceGeometry().size());
}
}
protected void preRender(DrawContext dc)
{
try
{
dc.setPreRenderMode(true);
// Pre-render the layers.
if (dc.getLayers() != null)
{
for (Layer layer : dc.getLayers())
{
try
{
dc.setCurrentLayer(layer);
layer.preRender(dc);
}
catch (Exception e)
{
String message = Logging.getMessage("SceneController.ExceptionWhilePreRenderingLayer",
(layer != null ? layer.getClass().getName() : Logging.getMessage("term.unknown")));
Logging.logger().log(Level.SEVERE, message, e);
// Don't abort; continue on to the next layer.
}
}
dc.setCurrentLayer(null);
}
// Pre-render the deferred/ordered surface renderables.
this.preRenderOrderedSurfaceRenderables(dc);
}
catch (Exception e)
{
Logging.logger().log(Level.SEVERE, Logging.getMessage("BasicSceneController.ExceptionDuringPreRendering"),
e);
}
finally
{
dc.setPreRenderMode(false);
}
}
protected void pickTerrain(DrawContext dc)
{
if (dc.isPickingMode() && dc.getVisibleSector() != null && dc.getSurfaceGeometry() != null &&
dc.getSurfaceGeometry().size() > 0)
{
this.pickPoints.clear();
if (dc.getPickPoint() != null)
this.pickPoints.add(dc.getPickPoint());
Point vpc = dc.getViewportCenterScreenPoint();
if (vpc != null && dc.getViewportCenterPosition() == null)
this.pickPoints.add(vpc);
if (this.pickPoints.size() == 0)
return;
List<PickedObject> pickedObjects = dc.getSurfaceGeometry().pick(dc, this.pickPoints);
if (pickedObjects == null || pickedObjects.size() == 0)
return;
for (PickedObject po : pickedObjects)
{
if (po == null)
continue;
if (po.getPickPoint().equals(dc.getPickPoint()))
dc.addPickedObject(po);
else if (po.getPickPoint().equals(vpc))
dc.setViewportCenterPosition((Position) po.getObject());
}
}
}
protected void pickLayers(DrawContext dc)
{
if (dc.getLayers() != null)
{
for (Layer layer : dc.getLayers())
{
try
{
if (layer != null && layer.isPickEnabled())
{
dc.setCurrentLayer(layer);
layer.pick(dc, dc.getPickPoint());
}
}
catch (Exception e)
{
String message = Logging.getMessage("SceneController.ExceptionWhilePickingInLayer",
(layer != null ? layer.getClass().getName() : Logging.getMessage("term.unknown")));
Logging.logger().log(Level.SEVERE, message, e);
// Don't abort; continue on to the next layer.
}
}
dc.setCurrentLayer(null);
}
}
protected void resolveTopPick(DrawContext dc)
{
// Resolve the top object at the pick point, if the pick point is enabled.
if (dc.getPickPoint() != null)
this.doResolveTopPick(dc, dc.getPickPoint());
// Resolve the top objects in the pick rectangle, if the pick rectangle is enabled.
if (dc.getPickRectangle() != null && !dc.getPickRectangle().isEmpty())
this.doResolveTopPick(dc, dc.getPickRectangle());
}
protected void doResolveTopPick(DrawContext dc, Point pickPoint)
{
PickedObjectList pol = dc.getPickedObjects();
if (pol != null && pol.size() == 1)
{
// If there is only one picked object, then it must be the top object so we're done.
pol.get(0).setOnTop();
}
else if (pol != null && pol.size() > 1)
{
// If there is more than one picked object, then find the picked object corresponding to the top color at
// the pick point, and mark it as on top
int colorCode = dc.getPickColorAtPoint(pickPoint);
if (colorCode != 0)
{
for (PickedObject po : pol)
{
if (po != null && po.getColorCode() == colorCode)
{
po.setOnTop();
break; // No need to check the remaining picked objects.
}
}
}
}
}
protected void doResolveTopPick(DrawContext dc, Rectangle pickRect)
{
PickedObjectList pol = dc.getObjectsInPickRectangle();
if (pol != null && pol.size() == 1)
{
// If there is only one picked object, then it must be the top object so we're done.
pol.get(0).setOnTop();
}
else if (pol != null && pol.size() > 1)
{
int[] minAndMaxColorCodes = null;
for (PickedObject po : pol)
{
int colorCode = po.getColorCode();
// Put all of the eligible picked objects in a map to provide constant time access to a picked object
// by its color code. Since the number of unique color codes and picked objects may both be large, using
// a hash map reduces the complexity of the next loop from O(n*m) to O(n*c), where n and m are the
// lengths of the unique color list and picked object list, respectively, and c is the constant time
// associated with a hash map access.
this.pickableObjects.put(colorCode, po);
// Keep track of the minimum and maximum color codes of the scene's picked objects. These values are
// used to cull the number of colors that the draw context must consider with identifying the unique
// pick colors in the specified screen rectangle.
if (minAndMaxColorCodes == null)
minAndMaxColorCodes = new int[] {colorCode, colorCode};
else
{
if (minAndMaxColorCodes[0] > colorCode)
minAndMaxColorCodes[0] = colorCode;
if (minAndMaxColorCodes[1] < colorCode)
minAndMaxColorCodes[1] = colorCode;
}
}
// If there is more than one picked object, then find the picked objects corresponding to each of the top
// colors in the pick rectangle, and mark them all as on top.
int[] colorCodes = dc.getPickColorsInRectangle(pickRect, minAndMaxColorCodes);
if (colorCodes != null && colorCodes.length > 0)
{
// Find the top picked object for each unique color code, if any, and mark it as on top.
for (int colorCode : colorCodes)
{
if (colorCode != 0) // This should never happen, but we check anyway.
{
PickedObject po = this.pickableObjects.get(colorCode);
if (po != null)
po.setOnTop();
}
}
}
// Clear the map of eligible picked objects so that the picked objects from this frame do not affect the
// next frame. This also ensures that we do not leak memory by retaining references to picked objects.
this.pickableObjects.clear();
}
}
protected void pick(DrawContext dc)
{
this.pickTime = System.currentTimeMillis();
this.lastPickedObjects = null;
this.lastObjectsInPickRect = null;
try
{
dc.enablePickingMode();
this.pickTerrain(dc);
this.doNonTerrainPick(dc);
if (this.isDeferOrderedRendering())
return;
this.resolveTopPick(dc);
this.lastPickedObjects = new PickedObjectList(dc.getPickedObjects());
this.lastObjectsInPickRect = new PickedObjectList(dc.getObjectsInPickRectangle());
if (this.isDeepPickEnabled() &&
(this.lastPickedObjects.hasNonTerrainObjects() || this.lastObjectsInPickRect.hasNonTerrainObjects()))
{
this.doDeepPick(dc);
}
}
catch (Throwable e)
{
Logging.logger().log(Level.SEVERE, Logging.getMessage("BasicSceneController.ExceptionDuringPick"), e);
}
finally
{
dc.disablePickingMode();
this.pickTime = System.currentTimeMillis() - this.pickTime;
}
}
protected void doNonTerrainPick(DrawContext dc)
{
// Don't do the pick if there's no current pick point and no current pick rectangle.
if (dc.getPickPoint() == null && (dc.getPickRectangle() == null || dc.getPickRectangle().isEmpty()))
return;
// Pick against the layers.
this.pickLayers(dc);
// Pick against the deferred/ordered surface renderables.
this.pickOrderedSurfaceRenderables(dc);
if (this.isDeferOrderedRendering())
return;
// Pick against the screen credits.
if (this.screenCreditController != null)
this.screenCreditController.pick(dc, dc.getPickPoint());
// Pick against the deferred/ordered renderables.
dc.setOrderedRenderingMode(true);
// dc.applyGroupingFilters();
dc.applyClutterFilter();
while (dc.peekOrderedRenderables() != null)
{
dc.pollOrderedRenderables().pick(dc, dc.getPickPoint());
}
dc.setOrderedRenderingMode(false);
}
protected void doDeepPick(DrawContext dc)
{
PickedObjectList currentPickedObjects = this.lastPickedObjects;
PickedObjectList currentObjectsInPickRect = this.lastObjectsInPickRect;
dc.setDeepPickingEnabled(true);
this.doNonTerrainPick(dc);
dc.setDeepPickingEnabled(false);
this.lastPickedObjects = this.mergePickedObjectLists(currentPickedObjects, dc.getPickedObjects());
this.lastObjectsInPickRect = this.mergePickedObjectLists(currentObjectsInPickRect,
dc.getObjectsInPickRectangle());
}
protected PickedObjectList mergePickedObjectLists(PickedObjectList listA, PickedObjectList listB)
{
if (listA == null || listB == null || !listA.hasNonTerrainObjects() || !listB.hasNonTerrainObjects())
return listA;
for (PickedObject pb : listB)
{
if (pb.isTerrain())
continue;
boolean common = false; // cannot modify listA within its iterator, so use a flag to indicate commonality
for (PickedObject pa : listA)
{
if (pa.isTerrain())
continue;
if (pa.getObject() == pb.getObject())
{
common = true;
break;
}
}
if (!common)
listA.add(pb);
}
return listA;
}
protected void draw(DrawContext dc)
{
try
{
// Draw the layers.
if (dc.getLayers() != null)
{
for (Layer layer : dc.getLayers())
{
try
{
if (layer != null)
{
dc.setCurrentLayer(layer);
layer.render(dc);
}
}
catch (Exception e)
{
String message = Logging.getMessage("SceneController.ExceptionWhileRenderingLayer",
(layer != null ? layer.getClass().getName() : Logging.getMessage("term.unknown")));
Logging.logger().log(Level.SEVERE, message, e);
// Don't abort; continue on to the next layer.
}
}
dc.setCurrentLayer(null);
}
// Draw the deferred/ordered surface renderables.
this.drawOrderedSurfaceRenderables(dc);
if (this.isDeferOrderedRendering())
return;
if (this.screenCreditController != null)
this.screenCreditController.render(dc);
// Draw the deferred/ordered renderables.
dc.setOrderedRenderingMode(true);
// dc.applyGroupingFilters();
dc.applyClutterFilter();
while (dc.peekOrderedRenderables() != null)
{
try
{
dc.pollOrderedRenderables().render(dc);
}
catch (Exception e)
{
Logging.logger().log(Level.WARNING,
Logging.getMessage("BasicSceneController.ExceptionDuringRendering"), e);
}
}
dc.setOrderedRenderingMode(false);
// Draw the diagnostic displays.
if (dc.getSurfaceGeometry() != null && dc.getModel() != null && (dc.getModel().isShowWireframeExterior() ||
dc.getModel().isShowWireframeInterior() || dc.getModel().isShowTessellationBoundingVolumes()))
{
Model model = dc.getModel();
float[] previousColor = new float[4];
GL2 gl = dc.getGL().getGL2(); // GL initialization checks for GL2 compatibility.
gl.glGetFloatv(GL2.GL_CURRENT_COLOR, previousColor, 0);
for (SectorGeometry sg : dc.getSurfaceGeometry())
{
if (model.isShowWireframeInterior() || model.isShowWireframeExterior())
sg.renderWireframe(dc, model.isShowWireframeInterior(), model.isShowWireframeExterior());
if (model.isShowTessellationBoundingVolumes())
{
gl.glColor3d(1, 0, 0);
sg.renderBoundingVolume(dc);
}
}
gl.glColor4fv(previousColor, 0);
}
}
catch (Throwable e)
{
Logging.logger().log(Level.SEVERE, Logging.getMessage("BasicSceneController.ExceptionDuringRendering"), e);
}
}
/**
* Called to check for openGL errors. This method includes a "round-trip" between the application and renderer,
* which is slow. Therefore, this method is excluded from the "normal" render pass. It is here as a matter of
* convenience to developers, and is not part of the API.
*
* @param dc the relevant <code>DrawContext</code>
*/
@SuppressWarnings({"UNUSED_SYMBOL", "UnusedDeclaration"})
protected void checkGLErrors(DrawContext dc)
{
GL gl = dc.getGL();
for (int err = gl.glGetError(); err != GL.GL_NO_ERROR; err = gl.glGetError())
{
String msg = dc.getGLU().gluErrorString(err);
msg += err;
Logging.logger().severe(msg);
}
}
//**************************************************************//
//******************** Ordered Surface Renderable ************//
//**************************************************************//
protected void preRenderOrderedSurfaceRenderables(DrawContext dc)
{
if (dc.getOrderedSurfaceRenderables().isEmpty())
return;
dc.setOrderedRenderingMode(true);
// Build a composite representation of the SurfaceObjects. This operation potentially modifies the framebuffer
// contents to update surface tile textures, therefore it must be executed during the preRender phase.