Update to Worldwind release 0.4.1
[worldwind-tracker.git] / gov / nasa / worldwind / render / LocationRenderer.java
blob491cdacecd3dfb30b7fd8f4a39543ba859a29a45
1 /*
2 Copyright (C) 2001, 2006 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.geom.*;
10 import gov.nasa.worldwind.tracks.TrackPoint;
11 import gov.nasa.worldwind.layers.Layer;
12 import gov.nasa.worldwind.Disposable;
14 import javax.media.opengl.glu.*;
15 import javax.media.opengl.*;
16 import java.util.Iterator;
18 /**
19 * @author tag
20 * @version $Id: LocationRenderer.java 2984 2007-09-22 02:20:10Z tgaskins $
22 public abstract class LocationRenderer implements Disposable
24 protected int lowerLimit = 0;
25 protected int upperLimit = Integer.MAX_VALUE;
26 protected final Shape SPHERE = new Sphere();
27 protected final Shape CONE = new Cone();
28 protected final Shape CYLINDER = new Cylinder();
29 private double elevation = 10d;
30 private boolean overrideMarkerElevation = false;
32 public void dispose()
34 this.CONE.dispose();
35 this.CYLINDER.dispose();
36 this.SPHERE.dispose();
39 public int getLowerLimit()
41 return this.lowerLimit;
44 public void setLowerLimit(int lowerLimit)
46 this.lowerLimit = lowerLimit;
49 public int getUpperLimit()
51 return this.upperLimit;
54 public void setUpperLimit(int upperLimit)
56 this.upperLimit = upperLimit;
59 public double getElevation()
61 return elevation;
64 public void setElevation(double elevation)
66 this.elevation = elevation;
69 public boolean isOverrideElevation()
71 return overrideMarkerElevation;
74 public void setOverrideElevation(boolean overrideMarkerElevation)
76 this.overrideMarkerElevation = overrideMarkerElevation;
79 protected Vec4 computeSurfacePoint(DrawContext dc, TrackPoint tp)
81 Position pos = tp.getPosition();
83 if (!this.overrideMarkerElevation)
84 return dc.getGlobe().computePointFromPosition(pos);
86 // Compute points that are at the track-specified elevation
87 Vec4 point = dc.getSurfaceGeometry().getSurfacePoint(pos.getLatitude(), pos.getLongitude(), this.elevation);
88 if (point != null)
89 return point;
91 // Point is outside the current sector geometry, so compute it from the globe.
92 return dc.getGlobe().computePointFromPosition(pos.getLatitude(), pos.getLongitude(), this.elevation);
95 protected void begin(DrawContext dc)
97 GL gl = dc.getGL();
98 Vec4 cameraPosition = dc.getView().getEyePoint();
100 gl.glPushAttrib(
101 GL.GL_TEXTURE_BIT | GL.GL_ENABLE_BIT | GL.GL_CURRENT_BIT | GL.GL_LIGHTING_BIT | GL.GL_TRANSFORM_BIT);
102 gl.glDisable(GL.GL_TEXTURE_2D);
104 float[] lightPosition =
105 {(float) (cameraPosition.x * 2), (float) (cameraPosition.y / 2), (float) (cameraPosition.z), 0.0f};
106 float[] lightDiffuse = {1.0f, 1.0f, 1.0f, 1.0f};
107 float[] lightAmbient = {1.0f, 1.0f, 1.0f, 1.0f};
108 float[] lightSpecular = {1.0f, 1.0f, 1.0f, 1.0f};
110 gl.glDisable(GL.GL_COLOR_MATERIAL);
112 gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, lightPosition, 0);
113 gl.glLightfv(GL.GL_LIGHT1, GL.GL_DIFFUSE, lightDiffuse, 0);
114 gl.glLightfv(GL.GL_LIGHT1, GL.GL_AMBIENT, lightAmbient, 0);
115 gl.glLightfv(GL.GL_LIGHT1, GL.GL_SPECULAR, lightSpecular, 0);
117 gl.glDisable(GL.GL_LIGHT0);
118 gl.glEnable(GL.GL_LIGHT1);
119 gl.glEnable(GL.GL_LIGHTING);
120 gl.glEnable(GL.GL_NORMALIZE);
122 gl.glMatrixMode(GL.GL_MODELVIEW);
123 gl.glPushMatrix();
126 protected void end(DrawContext dc)
128 GL gl = dc.getGL();
130 gl.glMatrixMode(GL.GL_MODELVIEW);
131 gl.glPopMatrix();
133 gl.glDisable(GL.GL_LIGHT1);
134 gl.glEnable(GL.GL_LIGHT0);
135 gl.glDisable(GL.GL_LIGHTING);
136 gl.glDisable(GL.GL_NORMALIZE);
137 gl.glPopAttrib();
140 public void pick(DrawContext dc, Iterator<TrackPoint> trackPositions, java.awt.Point pickPoint, Layer layer)
142 // TODO: picking
145 public Vec4 render(DrawContext dc, Iterator<TrackPoint> trackPositions)
147 return this.draw(dc, trackPositions);
150 protected abstract Vec4 draw(DrawContext dc, Iterator<TrackPoint> trackPositions);
152 protected static abstract class Shape
154 protected String name;
155 protected int glListId;
156 protected GLUquadric quadric;
157 protected boolean isInitialized = false;
159 abstract protected void doRender(DrawContext dc, Vec4 point, double radius);
161 protected void initialize(DrawContext dc)
163 this.glListId = dc.getGL().glGenLists(1);
164 this.quadric = dc.getGLU().gluNewQuadric();
165 dc.getGLU().gluQuadricDrawStyle(quadric, GLU.GLU_FILL);
166 dc.getGLU().gluQuadricNormals(quadric, GLU.GLU_SMOOTH);
167 dc.getGLU().gluQuadricOrientation(quadric, GLU.GLU_OUTSIDE);
168 dc.getGLU().gluQuadricTexture(quadric, false);
171 private void dispose()
173 if (this.isInitialized)
175 GLU glu = new GLU();
176 glu.gluDeleteQuadric(this.quadric);
177 this.isInitialized = false;
179 GLContext glc = GLContext.getCurrent();
180 if (glc == null)
181 return;
183 glc.getGL().glDeleteLists(this.glListId, 1);
185 this.glListId = -1;
189 protected void render(DrawContext dc, Vec4 point, double radius)
191 dc.getView().pushReferenceCenter(dc, point);
192 this.doRender(dc, point, radius);
193 dc.getView().popReferenceCenter(dc);
197 private static class Sphere extends Shape
199 protected void initialize(DrawContext dc)
201 super.initialize(dc);
203 this.name = "Sphere";
204 double radius = 1;
205 int slices = 36;
206 int stacks = 18;
208 dc.getGL().glNewList(this.glListId, GL.GL_COMPILE);
209 dc.getGLU().gluSphere(this.quadric, radius, slices, stacks);
210 dc.getGL().glEndList();
212 this.isInitialized = true;
215 protected void doRender(DrawContext dc, Vec4 point, double radius)
217 dc.getGL().glScaled(radius, radius, radius);
218 dc.getGL().glCallList(this.glListId);
222 private static class Cone extends Shape
224 protected void initialize(DrawContext dc)
226 super.initialize(dc);
228 this.name = "Cone";
229 int slices = 30;
230 int stacks = 30;
231 int loops = 2;
233 dc.getGL().glNewList(this.glListId, GL.GL_COMPILE);
234 dc.getGLU().gluQuadricOrientation(quadric, GLU.GLU_OUTSIDE);
235 dc.getGLU().gluCylinder(quadric, 1d, 0d, 2d, slices, (int) (2 * (Math.sqrt(stacks)) + 1));
236 dc.getGLU().gluDisk(quadric, 0d, 1d, slices, loops);
237 dc.getGL().glEndList();
239 this.isInitialized = true;
242 protected void doRender(DrawContext dc, Vec4 point, double size)
244 PolarPoint p = PolarPoint.fromCartesian(point);
246 dc.getGL().glScaled(size, size, size);
247 dc.getGL().glRotated(p.getLongitude().getDegrees(), 0, 1, 0);
248 dc.getGL().glRotated(Math.abs(p.getLatitude().getDegrees()), Math.signum(p.getLatitude().getDegrees()) * -1,
249 0, 0);
250 dc.getGL().glCallList(this.glListId);
254 protected static class Cylinder extends Shape
256 protected void initialize(DrawContext dc)
258 super.initialize(dc);
260 this.name = "Cylinder";
261 int slices = 30;
262 int stacks = 1;
263 int loops = 1;
265 dc.getGL().glNewList(this.glListId, GL.GL_COMPILE);
266 dc.getGLU().gluCylinder(quadric, 1d, 1d, 2d, slices, (int) (2 * (Math.sqrt(stacks)) + 1));
267 dc.getGLU().gluDisk(quadric, 0d, 1d, slices, loops);
268 dc.getGL().glTranslated(0, 0, 2);
269 dc.getGLU().gluDisk(quadric, 0d, 1d, slices, loops);
270 dc.getGL().glTranslated(0, 0, -2);
271 dc.getGL().glEndList();
273 this.isInitialized = true;
276 protected void doRender(DrawContext dc, Vec4 point, double size)
278 PolarPoint p = PolarPoint.fromCartesian(point);
280 dc.getGL().glScaled(size, size, size);
281 dc.getGL().glRotated(p.getLongitude().getDegrees(), 0, 1, 0);
282 dc.getGL().glRotated(Math.abs(p.getLatitude().getDegrees()), Math.signum(p.getLatitude().getDegrees()) * -1,
283 0, 0);
284 dc.getGL().glCallList(this.glListId);