Update to Worldwind release 0.4.1
[worldwind-tracker.git] / gov / nasa / worldwind / render / GlobeAnnotation.java
blob9f0ed97f12859b8298d8e6a44db2210ddfdd9625
1 /*
2 Copyright (C) 2001, 2006, 2007 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
5 All Rights Reserved.
6 */
7 package gov.nasa.worldwind.render;
9 import gov.nasa.worldwind.Locatable;
10 import gov.nasa.worldwind.Movable;
11 import gov.nasa.worldwind.pick.PickSupport;
12 import gov.nasa.worldwind.util.Logging;
13 import gov.nasa.worldwind.geom.Position;
14 import gov.nasa.worldwind.geom.Vec4;
16 import javax.media.opengl.GL;
17 import java.awt.*;
19 /**
20 * Represent a text label attached to a Position on the globe and its rendering attributes.
21 * @author Patrick Murris
22 * @version $Id$
23 * @see AbstractAnnotation
24 * @see AnnotationAttributes
26 public class GlobeAnnotation extends AbstractAnnotation implements Locatable, Movable
28 private Position position;
30 /**
31 * Creates a <code>GlobeAnnotation</code> with the given text, at the given globe <code>Position</code>.
32 * @param text the annotation text.
33 * @param position the annotation <code>Position</code>.
35 public GlobeAnnotation(String text, Position position)
37 this.init(text, position, null, null);
40 /**
41 * Creates a <code>GlobeAnnotation</code> with the given text, at the given globe <code>Position</code>.
42 * Specifiy the <code>Font</code> to be used.
43 * @param text the annotation text.
44 * @param position the annotation <code>Position</code>.
45 * @param font the <code>Font</code> to use.
47 public GlobeAnnotation(String text, Position position, Font font)
49 this.init(text, position, font, null);
52 /**
53 * Creates a <code>GlobeAnnotation</code> with the given text, at the given globe <code>Position</code>.
54 * Specifiy the <code>Font</code> and text <code>Color</code> to be used.
55 * @param text the annotation text.
56 * @param position the annotation <code>Position</code>.
57 * @param font the <code>Font</code> to use.
58 * @param textColor the text <code>Color</code>.
60 public GlobeAnnotation(String text, Position position, Font font, Color textColor)
62 this.init(text, position, font, textColor);
65 /**
66 * Creates a <code>GlobeAnnotation</code> with the given text, at the given globe <code>Position</code>.
67 * Specify the default {@link AnnotationAttributes} set.
68 * @param text the annotation text.
69 * @param position the annotation <code>Position</code>.
70 * @param defaults the default {@link AnnotationAttributes} set.
72 public GlobeAnnotation(String text, Position position, AnnotationAttributes defaults)
74 if (text == null)
76 String message = Logging.getMessage("nullValue.StringIsNull");
77 Logging.logger().severe(message);
78 throw new IllegalArgumentException(message);
81 if (position == null)
83 String message = Logging.getMessage("nullValue.PositionIsNull");
84 Logging.logger().severe(message);
85 throw new IllegalArgumentException(message);
88 if (defaults == null)
90 String message = Logging.getMessage("nullValue.AnnotationAttributesIsNull");
91 Logging.logger().severe(message);
92 throw new IllegalArgumentException(message);
95 this.setText(text);
96 this.position = position;
97 this.getAttributes().setDefaults(defaults);
100 private void init(String text, Position position, Font font, Color textColor)
102 if (text == null)
104 String message = Logging.getMessage("nullValue.StringIsNull");
105 Logging.logger().severe(message);
106 throw new IllegalArgumentException(message);
109 if (position == null)
111 String message = Logging.getMessage("nullValue.PositionIsNull");
112 Logging.logger().severe(message);
113 throw new IllegalArgumentException(message);
116 this.setText(text);
117 this.position = position;
118 this.getAttributes().setFont(font);
119 this.getAttributes().setTextColor(textColor);
123 //-- Rendering ----------------------------------------------------------------
125 protected void doDraw(DrawContext dc)
127 if (dc.isPickingMode() && this.getPickSupport() == null)
128 return;
130 Vec4 point = dc.getAnnotationRenderer().getAnnotationDrawPoint(dc, this);
131 if (point == null)
132 return;
134 double eyeDistance = dc.getView().getEyePoint().distanceTo3(point);
135 final Vec4 screenPoint = dc.getView().project(point);
136 if (screenPoint == null)
137 return;
139 Position pos = dc.getGlobe().computePositionFromPoint(point);
141 // Determine scaling and transparency factors based on distance from eye vs the distance to the look at point
142 double drawScale = computeLookAtDistance(dc) / eyeDistance; // TODO: cache lookAtDistance for one frame cycle
143 double drawAlpha = Math.min(1d, Math.max(attributes.getDistanceMinOpacity(), Math.sqrt(drawScale)));
144 drawScale = Math.min(attributes.getDistanceMaxScale(),
145 Math.max(attributes.getDistanceMinScale(), drawScale)); // Clamp to factor range
147 // Prepare to draw
148 this.setDepthFunc(dc, screenPoint);
149 GL gl = dc.getGL();
150 gl.glMatrixMode(GL.GL_MODELVIEW);
151 gl.glLoadIdentity();
152 // Translate to screenpoint
153 gl.glTranslated(screenPoint.x, screenPoint.y, 0d);
155 // Draw
156 drawAnnotation(dc, new Point((int)screenPoint.x, (int)screenPoint.y), drawScale, drawAlpha, pos);
159 //-- Locatable ----------------------------------------------------------------
160 public Position getPosition() // Locatable
162 return this.position;
165 public void setPosition(Position position)
167 this.position = position;
170 //-- Movable ------------------------------------------------------------------
171 public void move(Position position)
173 if (position == null)
175 String msg = Logging.getMessage("nullValue.PositionIsNull");
176 Logging.logger().severe(msg);
177 throw new IllegalArgumentException(msg);
180 this.position = this.position.add(position);
183 public void moveTo(Position position)
185 if (position == null)
187 String msg = Logging.getMessage("nullValue.PositionIsNull");
188 Logging.logger().severe(msg);
189 throw new IllegalArgumentException(msg);
192 this.position = position;
195 public Position getReferencePosition()
197 return this.position;