Worldwind public release 0.2
[worldwind-tracker.git] / gov / nasa / worldwind / geom / Quadrilateral.java
blobe7bc7ae6628a8b7673282dba1ad25cc124a16ffb
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 1787 2007-05-08 17:11:30Z dcollins $
20 public class Quadrilateral implements Renderable
22 private LatLon southwestCorner;
23 private LatLon northeastCorner;
24 private double elevation;
25 private Point 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 Color getColor()
46 return color;
49 public void setColor(Color color)
51 if (color == null)
53 String msg = WorldWind.retrieveErrMsg("nullValue.ColorIsNull");
54 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
55 throw new IllegalArgumentException(msg);
58 this.color = color;
61 public int getAntiAliasHint()
63 return antiAliasHint;
66 public void setAntiAliasHint(int hint)
68 if (!(hint == GL.GL_DONT_CARE || hint == GL.GL_FASTEST || hint == GL.GL_NICEST))
70 String msg = WorldWind.retrieveErrMsg("generic.InvalidHint");
71 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
72 throw new IllegalArgumentException(msg);
75 this.antiAliasHint = hint;
78 public void setCorners(LatLon southWest, LatLon northEast)
80 this.southwestCorner = southWest;
81 this.northeastCorner = northEast;
82 this.vertices = null;
85 public LatLon[] getCorners()
87 LatLon[] retVal = new LatLon[2];
89 retVal[0] = this.southwestCorner;
90 retVal[1] = this.northeastCorner;
92 return retVal;
95 public double getElevation()
97 return elevation;
100 public void setElevation(double elevation)
102 this.elevation = elevation;
103 this.vertices = null;
106 private void intializeGeometry(DrawContext dc)
108 DoubleBuffer verts = BufferUtil.newDoubleBuffer(12);
110 Point[] p = new Point[4];
112 p[0] = dc.getGlobe().computePointFromPosition(this.southwestCorner.getLatitude(),
113 this.southwestCorner.getLongitude(), this.elevation);
114 p[1] = dc.getGlobe().computePointFromPosition(this.southwestCorner.getLatitude(),
115 this.northeastCorner.getLongitude(), this.elevation);
116 p[2] = dc.getGlobe().computePointFromPosition(this.northeastCorner.getLatitude(),
117 this.northeastCorner.getLongitude(), this.elevation);
118 p[3] = dc.getGlobe().computePointFromPosition(this.northeastCorner.getLatitude(),
119 this.southwestCorner.getLongitude(), this.elevation);
121 Point refcenter = Point.midPoint(p[0], p[2]);
123 for (int i = 0; i < 4; i++)
125 verts.put(p[i].x() - refcenter.x());
126 verts.put(p[i].y() - refcenter.y());
127 verts.put(p[i].z() - refcenter.z());
130 this.referenceCenter = refcenter;
131 this.vertices = verts;
134 public void render(DrawContext dc)
136 if (dc == null)
138 String message = WorldWind.retrieveErrMsg("nullValue.DrawContextIsNull");
139 WorldWind.logger().log(java.util.logging.Level.FINE, message);
140 throw new IllegalStateException(message);
143 if (this.vertices == null)
145 this.intializeGeometry(dc);
147 if (this.vertices == null)
148 return; // TODO: log a warning
151 GL gl = dc.getGL();
153 int attrBits = GL.GL_HINT_BIT | GL.GL_CURRENT_BIT;
154 if (!dc.isPickingMode())
156 attrBits += GL.GL_CURRENT_BIT;
157 if (this.color.getAlpha() != 255)
158 attrBits += GL.GL_COLOR_BUFFER_BIT;
161 gl.glPushAttrib(attrBits);
162 gl.glPushClientAttrib(GL.GL_CLIENT_VERTEX_ARRAY_BIT);
163 dc.getView().pushReferenceCenter(dc, this.referenceCenter);
167 if (!dc.isPickingMode())
169 if (this.color.getAlpha() != 255)
171 gl.glEnable(GL.GL_BLEND);
172 gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
174 dc.getGL().glColor4ub((byte) this.color.getRed(), (byte) this.color.getGreen(),
175 (byte) this.color.getBlue(), (byte) this.color.getAlpha());
178 gl.glHint(GL.GL_POLYGON_SMOOTH_HINT, this.antiAliasHint);
179 gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
180 gl.glVertexPointer(3, GL.GL_DOUBLE, 0, this.vertices.rewind());
181 gl.glDrawArrays(GL.GL_QUADS, 0, 4);
183 finally
185 gl.glPopClientAttrib();
186 gl.glPopAttrib();
187 dc.getView().popReferenceCenter(dc);