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 com
.sun
.opengl
.util
.texture
.*;
10 import gov
.nasa
.worldwind
.geom
.*;
12 import javax
.media
.opengl
.*;
17 * @version $Id: DrawContextImpl.java 1994 2007-06-11 16:33:33Z dcollins $
19 public class DrawContextImpl
extends WWObjectImpl
implements DrawContext
21 private javax
.media
.opengl
.GLContext glContext
;
22 private javax
.media
.opengl
.glu
.GLU glu
= new javax
.media
.opengl
.glu
.GLU();
26 private double verticalExaggeration
= 1d
;
27 private gov
.nasa
.worldwind
.geom
.Sector visibleSector
;
28 private SectorGeometryList surfaceGeometry
;// = new SectorGeometryList();
29 private gov
.nasa
.worldwind
.PickedObjectList pickedObjects
= new gov
.nasa
.worldwind
.PickedObjectList();
30 private int uniquePickNumber
= 0;
31 private java
.awt
.Color clearColor
= new java
.awt
.Color(0, 0, 0, 0);
32 private boolean isPickingMode
= false;
33 private int numTextureUnits
= -1;
34 private SurfaceTileRenderer surfaceTileRenderer
= new SurfaceTileRenderer();
36 PriorityQueue
<OrderedRenderable
> orderedRenderables
= new PriorityQueue
<OrderedRenderable
>(100,
37 new Comparator
<OrderedRenderable
>()
39 public int compare(OrderedRenderable orA
, OrderedRenderable orB
)
41 double eA
= orA
.getDistanceFromEye();
42 double eB
= orB
.getDistanceFromEye();
44 return eA
> eB ?
-1 : eA
== eB ?
0 : 1;
48 public final javax
.media
.opengl
.GL
getGL()
50 return this.getGLContext().getGL();
53 public final javax
.media
.opengl
.glu
.GLU
getGLU()
58 public final javax
.media
.opengl
.GLContext
getGLContext()
60 return this.glContext
;
63 public final int getDrawableHeight()
65 return this.getGLDrawable().getHeight();
68 public final int getDrawableWidth()
70 return this.getGLDrawable().getWidth();
73 public final javax
.media
.opengl
.GLDrawable
getGLDrawable()
75 return this.getGLContext().getGLDrawable();
78 public final void initialize(javax
.media
.opengl
.GLContext glContext
)
80 if (glContext
== null)
82 String message
= WorldWind
.retrieveErrMsg("nullValue.GLContextIsNull");
83 gov
.nasa
.worldwind
.WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
84 throw new IllegalArgumentException(message
);
87 this.glContext
= glContext
;
88 this.visibleSector
= null;
89 if (this.surfaceGeometry
!= null)
90 this.surfaceGeometry
.clear();
91 this.surfaceGeometry
= null;
93 this.pickedObjects
.clear();
94 this.orderedRenderables
.clear();
95 this.uniquePickNumber
= 0;
97 if (this.numTextureUnits
< 1)
98 this.numTextureUnits
= queryMaxTextureUnits(glContext
);
101 private static int queryMaxTextureUnits(GLContext glContext
)
103 int[] mtu
= new int[1];
104 glContext
.getGL().glGetIntegerv(GL
.GL_MAX_TEXTURE_UNITS
, mtu
, 0);
108 public final void setModel(gov
.nasa
.worldwind
.Model model
)
111 if (this.model
== null)
114 Globe g
= this.model
.getGlobe();
119 public final Model
getModel()
124 public final LayerList
getLayers()
126 return this.model
.getLayers();
129 public final Sector
getVisibleSector()
131 return this.visibleSector
;
134 public final void setVisibleSector(Sector s
)
136 // don't check for null - it is possible that no globe is active, no view is active, no sectors visible, etc.
137 this.visibleSector
= s
;
140 public void setSurfaceGeometry(SectorGeometryList surfaceGeometry
)
142 this.surfaceGeometry
= surfaceGeometry
;
145 public SectorGeometryList
getSurfaceGeometry()
147 return surfaceGeometry
;
150 public final Globe
getGlobe()
152 return this.globe
!= null ?
this.globe
: this.model
.getGlobe();
155 public final void setView(gov
.nasa
.worldwind
.View view
)
160 public final View
getView()
165 public final void setGLContext(javax
.media
.opengl
.GLContext glContext
)
167 if (glContext
== null)
169 String message
= WorldWind
.retrieveErrMsg("nullValue.GLContextIsNull");
170 gov
.nasa
.worldwind
.WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
171 throw new IllegalArgumentException(message
);
174 this.glContext
= glContext
;
177 public final double getVerticalExaggeration()
179 return verticalExaggeration
;
182 public final void setVerticalExaggeration(double verticalExaggeration
)
184 this.verticalExaggeration
= verticalExaggeration
;
188 * Add picked objects to the current list of picked objects.
190 * @param pickedObjects the list of picked objects to add
191 * @throws IllegalArgumentException if <code>pickedObjects is null</code>
193 public void addPickedObjects(gov
.nasa
.worldwind
.PickedObjectList pickedObjects
)
195 if (pickedObjects
== null)
197 String msg
= WorldWind
.retrieveErrMsg("nullValue.PickedObjectList");
198 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, msg
);
199 throw new IllegalArgumentException(msg
);
202 if (this.pickedObjects
== null)
204 this.pickedObjects
= pickedObjects
;
208 for (gov
.nasa
.worldwind
.PickedObject po
: pickedObjects
)
210 this.pickedObjects
.add(po
);
215 * Adds a single insatnce of the picked object to the current picked-object list
217 * @param pickedObject the object to add
218 * @throws IllegalArgumentException if <code>picked Object is null</code>
220 public void addPickedObject(PickedObject pickedObject
)
222 if (null == pickedObject
)
224 String msg
= WorldWind
.retrieveErrMsg("nullValue.PickedObject");
225 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, msg
);
226 throw new IllegalArgumentException(msg
);
229 if (null == this.pickedObjects
)
230 this.pickedObjects
= new gov
.nasa
.worldwind
.PickedObjectList();
232 this.pickedObjects
.add(pickedObject
);
235 public gov
.nasa
.worldwind
.PickedObjectList
getPickedObjects()
237 return this.pickedObjects
;
240 public java
.awt
.Color
getUniquePickColor()
242 this.uniquePickNumber
++;
243 int clearColorCode
= this.getClearColor().getRGB();
245 if (clearColorCode
== this.uniquePickNumber
)
246 this.uniquePickNumber
++;
248 if (this.uniquePickNumber
>= 0x00FFFFFF)
250 this.uniquePickNumber
= 1; // no black, no white
251 if (clearColorCode
== this.uniquePickNumber
)
252 this.uniquePickNumber
++;
255 return new java
.awt
.Color(this.uniquePickNumber
, true); // has alpha
258 public java
.awt
.Color
getClearColor()
260 return this.clearColor
;
264 * Returns true if the Picking mode is active, otherwise return false
266 * @return true for Picking mode, otherwise false
268 public boolean isPickingMode()
270 return this.isPickingMode
;
274 * Enables color picking mode
276 public void enablePickingMode()
278 this.isPickingMode
= true;
282 * Disables color picking mode
284 public void disablePickingMode()
286 this.isPickingMode
= false;
289 public void addOrderedRenderable(OrderedRenderable orderedRenderable
)
291 if (null == orderedRenderable
)
293 String msg
= WorldWind
.retrieveErrMsg("nullValue.OrderedRenderable");
294 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, msg
);
295 return; // benign event
298 this.orderedRenderables
.add(orderedRenderable
);
301 public java
.util
.Queue
<OrderedRenderable
> getOrderedRenderables()
303 return this.orderedRenderables
;
306 public void drawUnitQuad()
308 GL gl
= this.getGL();
310 gl
.glBegin(GL
.GL_QUADS
); // TODO: use a vertex array or vertex buffer
311 gl
.glVertex2d(0d
, 0d
);
312 gl
.glVertex2d(1, 0d
);
314 gl
.glVertex2d(0d
, 1);
318 public void drawUnitQuad(TextureCoords texCoords
)
320 GL gl
= this.getGL();
322 gl
.glBegin(GL
.GL_QUADS
); // TODO: use a vertex array or vertex buffer
323 gl
.glTexCoord2d(texCoords
.left(), texCoords
.bottom());
324 gl
.glVertex2d(0d
, 0d
);
325 gl
.glTexCoord2d(texCoords
.right(), texCoords
.bottom());
326 gl
.glVertex2d(1, 0d
);
327 gl
.glTexCoord2d(texCoords
.right(), texCoords
.top());
329 gl
.glTexCoord2d(texCoords
.left(), texCoords
.top());
330 gl
.glVertex2d(0d
, 1);
334 public int getNumTextureUnits()
336 return numTextureUnits
;
339 public void setNumTextureUnits(int numTextureUnits
)
341 // TODO: validate arg for >= 1
342 this.numTextureUnits
= numTextureUnits
;
345 public Vec4
getPointOnGlobe(Angle latitude
, Angle longitude
)
347 if (latitude
== null || longitude
== null)
349 String message
= WorldWind
.retrieveErrMsg("nullValue.LatitudeOrLongitudeIsNull");
350 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
351 throw new IllegalArgumentException(message
);
354 if (!this.getVisibleSector().contains(latitude
, longitude
))
357 SectorGeometryList sectorGeometry
= this.getSurfaceGeometry();
358 if (sectorGeometry
!= null)
360 Vec4 p
= sectorGeometry
.getSurfacePoint(latitude
, longitude
);
367 // Globe globe = this.getGlobe();
368 // if (globe == null)
371 // double elevation = this.getVerticalExaggeration() * globe.getElevation(latitude, longitude);
372 // return this.getGlobe().computeSurfacePoint(latitude, longitude, elevation);
375 public SurfaceTileRenderer
getSurfaceTileRenderer()
377 return this.surfaceTileRenderer
;