-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathCachedRenderableLayer.java
More file actions
346 lines (312 loc) · 11 KB
/
CachedRenderableLayer.java
File metadata and controls
346 lines (312 loc) · 11 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
/*
* 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.geom.*;
import gov.nasa.worldwind.pick.PickSupport;
import gov.nasa.worldwind.render.*;
import gov.nasa.worldwind.util.*;
import com.jogamp.opengl.*;
import java.util.Collection;
/**
* Holds a collection of Renderables and manages local caching of them. Provides searching for Renderables by sector,
* location or name.
* <p/>
* NOTE: This class is experimental and not fully implemented. You should not use it now.
*
* @author tag
* @version $Id: CachedRenderableLayer.java 1171 2013-02-11 21:45:02Z dcollins $
*/
public class CachedRenderableLayer extends AbstractLayer
{
protected static final int DEFAULT_DEPTH = 4;
protected BasicQuadTree<Renderable> extentTree; // this is used until we work out the caching and retrieval scheme
protected PickSupport pickSupport = new PickSupport();
/**
* Constructs a layer instance.
*
* @param coverage the geographic area covered by the layer's Renderables.
*
* @throws IllegalArgumentException if the coverage sector is null.
*/
public CachedRenderableLayer(Sector coverage)
{
// Extent tree checks args
this.extentTree = new BasicQuadTree<Renderable>(DEFAULT_DEPTH, coverage, null);
}
/**
* Constructs a layer instance.
*
* @param coverage the geographic area covered by the layer's Renderables.
* @param numLevels the depth of the tree used to sort the Renderables.
*
* @throws IllegalArgumentException if the coverage sector is null or the number of levels is less than 1;
*/
public CachedRenderableLayer(Sector coverage, int numLevels)
{
// Extent tree checks args
this.extentTree = new BasicQuadTree<Renderable>(numLevels, coverage, null);
}
/**
* Indictes whether the layer contains Renderables.
*
* @return true if the layer contains Renderables, otherwise false.
*/
public boolean hasItems()
{
return this.extentTree.hasItems();
}
/**
* Add a Renderable to the layer.
*
* @param item the Renderable to add.
*
* @throws IllegalArgumentException if the item is null or does not implement {@link gov.nasa.worldwind.render.GeographicExtent}.
* @see #add(gov.nasa.worldwind.render.Renderable, String)
*/
public void add(Renderable item)
{
this.add(item, null); // extent tree checks args
}
/**
* Adds a named Renderable to the layer. The Renderable can subsequently participate in a name search of the layer.
*
* @param item the Renderable to add.
* @param name a name for the Renderable. May be null, in which case the item has no name.
*
* @throws IllegalArgumentException if the item is null or does not implement {@link gov.nasa.worldwind.render.GeographicExtent}.
* @see #add(gov.nasa.worldwind.render.Renderable)
*/
public void add(Renderable item, String name)
{
if (!(item instanceof GeographicExtent))
{
String message = Logging.getMessage("GeographicTree.NotGeometricExtent");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
// extent tree checks args
this.extentTree.add(item, ((GeographicExtent) item).getSector().asDegreesArray(), name);
}
/**
* Remove a Renderable from the layer if the Renderable is in the layer.
*
* @param item the Renderable to remove
*
* @see #removeByName(String)
*/
public void remove(Renderable item)
{
// extent tree checks args
this.extentTree.remove(item);
}
/**
* Remove a named Renderable from the layer if it is in the layer.
*
* @param name the name of the Renderable to remove. If null, no Renderable is removed.
*
* @see #remove(gov.nasa.worldwind.render.Renderable)
*/
public void removeByName(String name)
{
this.extentTree.removeByName(name);
}
/**
* Returns all Renderables at a specfied location.
*
* @param location the location of interest.
*
* @return the Collection of Renderables at the specified location.
*
* @see #getRenderables(gov.nasa.worldwind.geom.Sector)
* @see #getAllRenderables()
*/
public Collection<? extends Renderable> getRenderables(LatLon location)
{
// extent tree checks args
return this.extentTree.getItemsAtLocation(location, null);
}
/**
* Returns all Renderables within or intersecting a specified sector.
*
* @param extent the location of interest.
*
* @return the Collection of Renderables within or intersecting the boundary of the sector.
*
* @see #getRenderables(gov.nasa.worldwind.geom.LatLon)
* @see #getAllRenderables()
*/
public Collection<? extends Renderable> getRenderables(Sector extent)
{
// extent tree checks args
return this.extentTree.getItemsInRegion(extent, null);
}
/**
* Returns all Renderables in the layer.
*
* @return an Iterable over all the Renderables in the layer.
*/
public Iterable<? extends Renderable> getAllRenderables()
{
return this.extentTree; // the tree is an Iterable
}
/**
* Searches the layer for a named Renderable.
*
* @param name the name to search for. If null, no search is performed and null is returned.
*
* @return the Renderable of the given name, or null if no Renderable with the name is in the layer.
*/
public Renderable getByName(String name)
{
return this.extentTree.getByName(name);
}
/**
* 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 any Renderables implementing @{link Dispose} and removes all Renderables from the layer. */
public void dispose()
{
this.disposeRenderables();
}
protected void disposeRenderables()
{
for (Renderable renderable : this.getAllRenderables())
{
try
{
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.extentTree.clear();
}
protected void doPreRender(DrawContext dc)
{
this.doPreRender(dc, this.getAllRenderables());
}
protected void doPick(DrawContext dc, java.awt.Point pickPoint)
{
this.doPick(dc, this.getAllRenderables(), pickPoint);
}
protected void doRender(DrawContext dc)
{
this.doRender(dc, this.getAllRenderables());
}
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(GL2.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);
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.CachedRenderableLayer.Name");
}
}