2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
7 package gov
.nasa
.worldwind
;
9 import gov
.nasa
.worldwind
.geom
.*;
11 import javax
.media
.opengl
.*;
14 import com
.sun
.opengl
.util
.texture
.*;
18 * @version $Id: DrawContextImpl.java 1465 2007-04-14 01:05:37Z tgaskins $
20 public class DrawContextImpl
extends WWObjectImpl
implements DrawContext
22 private javax
.media
.opengl
.GLContext glContext
;
23 private javax
.media
.opengl
.glu
.GLU glu
= new javax
.media
.opengl
.glu
.GLU();
27 private double verticalExaggeration
= 1d
;
28 private gov
.nasa
.worldwind
.geom
.Sector visibleSector
;
29 private SectorGeometryList surfaceGeometry
;// = new SectorGeometryList();
30 private gov
.nasa
.worldwind
.PickedObjectList pickedObjects
= new gov
.nasa
.worldwind
.PickedObjectList();
31 private int uniquePickNumber
= 0;
32 private java
.awt
.Color clearColor
= new java
.awt
.Color(0, 0, 0, 0);
33 private boolean isPickingMode
= false;
34 private int numTextureUnits
= -1;
35 private SurfaceTileRenderer surfaceTileRenderer
= new SurfaceTileRenderer();
37 PriorityQueue
<OrderedRenderable
> orderedRenderables
= new PriorityQueue
<OrderedRenderable
>(100,
38 new Comparator
<OrderedRenderable
>()
40 public int compare(OrderedRenderable orA
, OrderedRenderable orB
)
42 double eA
= orA
.getDistanceFromEye();
43 double eB
= orB
.getDistanceFromEye();
45 return eA
> eB ?
-1 : eA
== eB ?
0 : 1;
49 public final javax
.media
.opengl
.GL
getGL()
51 return this.getGLContext().getGL();
54 public final javax
.media
.opengl
.glu
.GLU
getGLU()
59 public final javax
.media
.opengl
.GLContext
getGLContext()
61 return this.glContext
;
64 public final int getDrawableHeight()
66 return this.getGLDrawable().getHeight();
69 public final int getDrawableWidth()
71 return this.getGLDrawable().getWidth();
74 public final javax
.media
.opengl
.GLDrawable
getGLDrawable()
76 return this.getGLContext().getGLDrawable();
79 public final void initialize(javax
.media
.opengl
.GLContext glContext
)
81 if (glContext
== null)
83 String message
= WorldWind
.retrieveErrMsg("nullValue.GLContextIsNull");
84 gov
.nasa
.worldwind
.WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
85 throw new IllegalArgumentException(message
);
88 this.glContext
= glContext
;
90 if(this.isPickingMode())
92 // do not clean rendered sectors for picking
96 this.visibleSector
= null;
97 if (this.surfaceGeometry
!= null)
98 this.surfaceGeometry
.clear();
99 this.surfaceGeometry
= null;
101 this.pickedObjects
.clear();
102 this.orderedRenderables
.clear();
103 this.uniquePickNumber
= 0;
105 if (this.numTextureUnits
< 1)
106 this.numTextureUnits
= queryMaxTextureUnits(glContext
);
109 private static int queryMaxTextureUnits(GLContext glContext
)
111 int[] mtu
= new int[1];
112 glContext
.getGL().glGetIntegerv(GL
.GL_MAX_TEXTURE_UNITS
, mtu
, 0);
116 public final void setModel(gov
.nasa
.worldwind
.Model model
)
119 if (this.model
== null)
122 Globe g
= this.model
.getGlobe();
127 public final Model
getModel()
132 public final LayerList
getLayers()
134 return this.model
.getLayers();
137 public final Sector
getVisibleSector()
139 return this.visibleSector
;
142 public final void setVisibleSector(Sector s
)
144 // don't check for null - it is possible that no globe is active, no view is active, no sectors visible, etc.
145 this.visibleSector
= s
;
148 public void setSurfaceGeometry(SectorGeometryList surfaceGeometry
)
150 this.surfaceGeometry
= surfaceGeometry
;
153 public SectorGeometryList
getSurfaceGeometry()
155 return surfaceGeometry
;
158 public final Globe
getGlobe()
160 return this.globe
!= null ?
this.globe
: this.model
.getGlobe();
163 public final void setView(gov
.nasa
.worldwind
.View view
)
168 public final View
getView()
173 public final void setGLContext(javax
.media
.opengl
.GLContext glContext
)
175 if (glContext
== null)
177 String message
= WorldWind
.retrieveErrMsg("nullValue.GLContextIsNull");
178 gov
.nasa
.worldwind
.WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
179 throw new IllegalArgumentException(message
);
182 this.glContext
= glContext
;
185 public final double getVerticalExaggeration()
187 return verticalExaggeration
;
190 public final void setVerticalExaggeration(double verticalExaggeration
)
192 this.verticalExaggeration
= verticalExaggeration
;
196 * Add picked objects to the current list of picked objects.
197 * @param pickedObjects the list of picked objects to add
198 * @throws IllegalArgumentException if <code>pickedObjects is null</code>
200 public void addPickedObjects(gov
.nasa
.worldwind
.PickedObjectList pickedObjects
)
202 if (pickedObjects
== null)
204 String msg
= WorldWind
.retrieveErrMsg("nullValue.PickedObjectList");
205 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, msg
);
206 throw new IllegalArgumentException(msg
);
209 if (this.pickedObjects
== null)
211 this.pickedObjects
= pickedObjects
;
215 for (gov
.nasa
.worldwind
.PickedObject po
: pickedObjects
)
217 this.pickedObjects
.add(po
);
222 * Adds a single insatnce of the picked object to the current picked-object list
223 * @param pickedObject the object to add
224 * @throws IllegalArgumentException if <code>picked Object is null</code>
226 public void addPickedObject(PickedObject pickedObject
)
228 if(null == pickedObject
)
230 String msg
= WorldWind
.retrieveErrMsg("nullValue.PickedObject");
231 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, msg
);
232 throw new IllegalArgumentException(msg
);
235 if(null == this.pickedObjects
)
236 this.pickedObjects
= new gov
.nasa
.worldwind
.PickedObjectList();
238 this.pickedObjects
.add(pickedObject
);
241 public gov
.nasa
.worldwind
.PickedObjectList
getPickedObjects()
243 return this.pickedObjects
;
246 public java
.awt
.Color
getUniquePickColor()
248 this.uniquePickNumber
++;
249 int clearColorCode
= this.getClearColor().getRGB();
251 if(clearColorCode
== this.uniquePickNumber
)
252 this.uniquePickNumber
++;
254 if(this.uniquePickNumber
>= 0x00FFFFFF)
256 this.uniquePickNumber
= 1; // no black, no white
257 if(clearColorCode
== this.uniquePickNumber
)
258 this.uniquePickNumber
++;
261 return new java
.awt
.Color(this.uniquePickNumber
, true); // has alpha
264 public java
.awt
.Color
getClearColor()
266 return this.clearColor
;
270 * Returns true if the Picking mode is active, otherwise return false
271 * @return true for Picking mode, otherwise false
273 public boolean isPickingMode()
275 return this.isPickingMode
;
279 * Enables color picking mode
281 public void enablePickingMode()
283 this.isPickingMode
= true;
287 * Disables color picking mode
289 public void disablePickingMode()
291 this.isPickingMode
= false;
294 public void addOrderedRenderable(OrderedRenderable orderedRenderable
)
296 if(null == orderedRenderable
)
298 String msg
= WorldWind
.retrieveErrMsg("nullValue.OrderedRenderable");
299 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, msg
);
300 return; // benign event
303 this.orderedRenderables
.add(orderedRenderable
);
306 public java
.util
.Queue
<OrderedRenderable
> getOrderedRenderables()
308 return this.orderedRenderables
;
311 public void drawUnitQuad()
313 GL gl
= this.getGL();
315 gl
.glBegin(GL
.GL_QUADS
); // TODO: use a vertex array or vertex buffer
316 gl
.glVertex2d(0d
, 0d
);
317 gl
.glVertex2d(1, 0d
);
319 gl
.glVertex2d(0d
, 1);
323 public void drawUnitQuad(TextureCoords texCoords
)
325 GL gl
= this.getGL();
327 gl
.glBegin(GL
.GL_QUADS
); // TODO: use a vertex array or vertex buffer
328 gl
.glTexCoord2d(texCoords
.left(), texCoords
.bottom());
329 gl
.glVertex2d(0d
, 0d
);
330 gl
.glTexCoord2d(texCoords
.right(), texCoords
.bottom());
331 gl
.glVertex2d(1, 0d
);
332 gl
.glTexCoord2d(texCoords
.right(), texCoords
.top());
334 gl
.glTexCoord2d(texCoords
.left(), texCoords
.top());
335 gl
.glVertex2d(0d
, 1);
339 public int getNumTextureUnits()
341 return numTextureUnits
;
344 public void setNumTextureUnits(int numTextureUnits
)
346 // TODO: validate arg for >= 1
347 this.numTextureUnits
= numTextureUnits
;
350 public Point
getPointOnGlobe(Angle latitude
, Angle longitude
)
352 if (latitude
== null || longitude
== null)
354 String message
= WorldWind
.retrieveErrMsg("nullValue.LatitudeOrLongitudeIsNull");
355 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
356 throw new IllegalArgumentException(message
);
359 if (!this.getVisibleSector().contains(latitude
, longitude
))
362 SectorGeometryList sectorGeometry
= this.getSurfaceGeometry();
363 if (sectorGeometry
!= null)
365 Point p
= sectorGeometry
.getSurfacePoint(latitude
, longitude
);
372 // Globe globe = this.getGlobe();
373 // if (globe == null)
376 // double elevation = this.getVerticalExaggeration() * globe.getElevation(latitude, longitude);
377 // return this.getGlobe().computeSurfacePoint(latitude, longitude, elevation);
380 public SurfaceTileRenderer
getSurfaceTileRenderer()
382 return this.surfaceTileRenderer
;