Updated to worldwind release 20070817
[worldwind-tracker.git] / gov / nasa / worldwind / geom / Quadrilateral.java
blob38e990f1422fc8757efcb52c5f71eb1831861b84
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.geom;
9 import com.sun.opengl.util.*;
10 import gov.nasa.worldwind.*;
12 import javax.media.opengl.*;
13 import java.awt.*;
14 import java.nio.*;
16 /**
17 * @author tag
18 * @version $Id: Quadrilateral.java 2233 2007-07-06 23:56:34Z tgaskins $
20 public class Quadrilateral implements Renderable, Movable
22 private LatLon southwestCorner;
23 private LatLon northeastCorner;
24 private double elevation;
25 private Vec4 referenceCenter;
26 private DoubleBuffer vertices;
27 private int antiAliasHint = GL.GL_FASTEST;
28 private Color color = Color.WHITE;
30 public Quadrilateral(LatLon southwestCorner, LatLon northeastCorner, double elevation)
32 if (southwestCorner == null || northeastCorner == null)
34 String msg = WorldWind.retrieveErrMsg("nullValue.PositionIsNull");
35 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
36 throw new IllegalArgumentException(msg);
39 this.southwestCorner = southwestCorner;
40 this.northeastCorner = northeastCorner;
41 this.elevation = elevation;
44 public Quadrilateral(Sector sector, double elevation)
46 if (sector == null)
48 String msg = WorldWind.retrieveErrMsg("nullValue.SectorIsNull");
49 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
50 throw new IllegalArgumentException(msg);
53 this.southwestCorner = new LatLon(sector.getMinLatitude(), sector.getMinLongitude());
54 this.northeastCorner = new LatLon(sector.getMaxLatitude(), sector.getMaxLongitude());
55 this.elevation = elevation;
58 public Color getColor()
60 return color;
63 public void setColor(Color color)
65 if (color == null)
67 String msg = WorldWind.retrieveErrMsg("nullValue.ColorIsNull");
68 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
69 throw new IllegalArgumentException(msg);
72 this.color = color;
75 public int getAntiAliasHint()
77 return antiAliasHint;
80 public void setAntiAliasHint(int hint)
82 if (!(hint == GL.GL_DONT_CARE || hint == GL.GL_FASTEST || hint == GL.GL_NICEST))
84 String msg = WorldWind.retrieveErrMsg("generic.InvalidHint");
85 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
86 throw new IllegalArgumentException(msg);
89 this.antiAliasHint = hint;
92 public void setCorners(LatLon southWest, LatLon northEast)
94 this.southwestCorner = southWest;
95 this.northeastCorner = northEast;
96 this.vertices = null;
99 public LatLon[] getCorners()
101 LatLon[] retVal = new LatLon[2];
103 retVal[0] = this.southwestCorner;
104 retVal[1] = this.northeastCorner;
106 return retVal;
109 public double getElevation()
111 return elevation;
114 public void setElevation(double elevation)
116 this.elevation = elevation;
117 this.vertices = null;
120 private void intializeGeometry(DrawContext dc)
122 DoubleBuffer verts = BufferUtil.newDoubleBuffer(12);
124 Vec4[] p = new Vec4[4];
126 p[0] = dc.getGlobe().computePointFromPosition(this.southwestCorner.getLatitude(),
127 this.southwestCorner.getLongitude(), this.elevation);
128 p[1] = dc.getGlobe().computePointFromPosition(this.southwestCorner.getLatitude(),
129 this.northeastCorner.getLongitude(), this.elevation);
130 p[2] = dc.getGlobe().computePointFromPosition(this.northeastCorner.getLatitude(),
131 this.northeastCorner.getLongitude(), this.elevation);
132 p[3] = dc.getGlobe().computePointFromPosition(this.northeastCorner.getLatitude(),
133 this.southwestCorner.getLongitude(), this.elevation);
135 Vec4 refcenter = new Vec4(
136 (p[0].x + p[2].x) / 2.0,
137 (p[0].y + p[2].y) / 2.0,
138 (p[0].z + p[2].z) / 2.0);
140 for (int i = 0; i < 4; i++)
142 verts.put(p[i].x - refcenter.x);
143 verts.put(p[i].y - refcenter.y);
144 verts.put(p[i].z - refcenter.z);
147 this.referenceCenter = refcenter;
148 this.vertices = verts;
151 public void render(DrawContext dc)
153 if (dc == null)
155 String message = WorldWind.retrieveErrMsg("nullValue.DrawContextIsNull");
156 WorldWind.logger().log(java.util.logging.Level.FINE, message);
157 throw new IllegalStateException(message);
160 if (this.vertices == null)
162 this.intializeGeometry(dc);
164 if (this.vertices == null)
165 return; // TODO: log a warning
168 GL gl = dc.getGL();
170 int attrBits = GL.GL_HINT_BIT | GL.GL_CURRENT_BIT;
171 if (!dc.isPickingMode())
173 if (this.color.getAlpha() != 255)
174 attrBits |= GL.GL_COLOR_BUFFER_BIT;
177 gl.glPushAttrib(attrBits);
178 gl.glPushClientAttrib(GL.GL_CLIENT_VERTEX_ARRAY_BIT);
179 dc.getView().pushReferenceCenter(dc, this.referenceCenter);
183 if (!dc.isPickingMode())
185 if (this.color.getAlpha() != 255)
187 gl.glEnable(GL.GL_BLEND);
188 gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
190 dc.getGL().glColor4ub((byte) this.color.getRed(), (byte) this.color.getGreen(),
191 (byte) this.color.getBlue(), (byte) this.color.getAlpha());
194 gl.glHint(GL.GL_POLYGON_SMOOTH_HINT, this.antiAliasHint);
195 gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
196 gl.glVertexPointer(3, GL.GL_DOUBLE, 0, this.vertices.rewind());
197 gl.glDrawArrays(GL.GL_QUADS, 0, 4);
199 finally
201 gl.glPopClientAttrib();
202 gl.glPopAttrib();
203 dc.getView().popReferenceCenter(dc);
207 public Position getReferencePosition()
209 return new Position(this.southwestCorner, this.elevation);
212 public void move(Position delta)
214 if (delta == null)
216 String msg = WorldWind.retrieveErrMsg("nullValue.PositionIsNull");
217 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
218 throw new IllegalArgumentException(msg);
221 this.northeastCorner = this.northeastCorner.add(delta);
222 this.southwestCorner = this.southwestCorner.add(delta);
223 this.elevation = this.elevation + delta.getElevation();
224 this.vertices = null;
227 public void moveTo(Position position)
229 if (position == null)
231 String msg = WorldWind.retrieveErrMsg("nullValue.PositionIsNull");
232 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
233 throw new IllegalArgumentException(msg);
236 Position delta = position.subtract(this.getReferencePosition());
237 this.move(delta);