Worldwind public release 0.2
[worldwind-tracker.git] / gov / nasa / worldwind / geom / SurfaceShape.java
blob5470a7da027ae5d83a5ebebd22f47e426a47b129
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 gov.nasa.worldwind.*;
10 import gov.nasa.worldwind.layers.*;
12 import javax.media.opengl.*;
13 import java.awt.*;
14 import java.awt.image.*;
15 import java.util.*;
17 import com.sun.opengl.util.texture.*;
19 /**
20 * @author tag
21 * @version $Id: SurfaceShape.java 1767 2007-05-07 21:36:12Z tgaskins $
23 public abstract class SurfaceShape implements Renderable, Disposable
25 private static final Color DEFAULT_COLOR = new Color(1f, 1f, 0f, 0.4f);
26 private static final Color DEFAULT_BORDER_COLOR = new Color(1f, 1f, 0f, 0.7f);
27 private static final int DEFAULT_TEXTURE_SIZE = 512;
28 private static final int DEFAULT_NUM_EDGE_INTERVALS = 10;
30 private TextureTile tile;
31 private int textureSize = DEFAULT_TEXTURE_SIZE;
32 private Paint paint;
33 private Color borderColor;
34 private Stroke stroke = new BasicStroke();
35 private boolean drawBorder = true;
36 private boolean drawInterior = true;
37 private boolean antiAlias = true;
38 private int numEdgeIntervals = DEFAULT_NUM_EDGE_INTERVALS;
39 private ArrayList<LatLon> positions = new ArrayList<LatLon>();
41 protected abstract BufferedImage drawShape(BufferedImage image);
43 public SurfaceShape(Iterable<LatLon> positions, Color color, Color borderColor)
45 if (positions == null)
47 String message = WorldWind.retrieveErrMsg("nullValue.PositionsListIsNull");
48 WorldWind.logger().log(java.util.logging.Level.FINE, message);
49 throw new IllegalArgumentException(message);
52 createTextureTiles(Sector.boundingSector(positions));
53 this.paint = color != null ? color : DEFAULT_COLOR;
54 this.borderColor = borderColor != null ? borderColor : DEFAULT_BORDER_COLOR;
56 for (LatLon p : positions)
58 this.positions.add(p);
62 private void createTextureTiles(Sector sector)
64 this.tile = new TextureTile(sector);
67 public void dispose()
69 if (this.tile != null)
70 this.tile.dispose();
73 public Sector getSector()
75 return this.tile.getSector();
78 public ArrayList<LatLon> getPositions()
80 return positions;
83 private TextureTile getTextureTile()
85 return this.tile;
88 public Paint getPaint()
90 return paint;
93 public void setPaint(Paint paint)
95 this.paint = paint;
96 this.getTextureTile().setTextureData(null);
99 public Color getBorderColor()
101 return borderColor;
104 public void setBorderColor(Color borderColor)
106 this.borderColor = borderColor;
107 this.getTextureTile().setTextureData(null);
110 public int getTextureSize()
112 return textureSize;
115 public void setTextureSize(int textureSize)
117 this.textureSize = textureSize;
118 this.getTextureTile().setTextureData(null);
121 public Stroke getStroke()
123 return stroke;
126 public void setStroke(Stroke stroke)
128 this.stroke = stroke;
129 this.getTextureTile().setTextureData(null);
132 public boolean isDrawBorder()
134 return drawBorder;
137 public void setDrawBorder(boolean drawBorder)
139 this.drawBorder = drawBorder;
142 public boolean isDrawInterior()
144 return drawInterior;
147 public void setDrawInterior(boolean drawInterior)
149 this.drawInterior = drawInterior;
152 public boolean isAntiAlias()
154 return antiAlias;
157 public void setAntiAlias(boolean antiAlias)
159 this.antiAlias = antiAlias;
162 public int getNumEdgeIntervals()
164 return numEdgeIntervals;
167 public void setNumEdgeIntervals(int numEdgeIntervals)
169 this.numEdgeIntervals = numEdgeIntervals;
172 private boolean intersects(Sector sector)
174 return this.tile.getSector().intersects(sector);
177 public void render(DrawContext dc)
179 if (!this.intersects(dc.getVisibleSector()))
180 return;
182 if (this.getTextureTile().getTextureData() == null)
183 this.tile.setTextureData(this.makeTextureData(this.textureSize));
185 GL gl = dc.getGL();
187 gl.glPushAttrib(GL.GL_COLOR_BUFFER_BIT | GL.GL_POLYGON_BIT);
191 if (!dc.isPickingMode())
193 gl.glEnable(GL.GL_BLEND);
194 gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
197 gl.glPolygonMode(GL.GL_FRONT, GL.GL_FILL);
198 gl.glEnable(GL.GL_CULL_FACE);
199 gl.glCullFace(GL.GL_BACK);
201 dc.getSurfaceTileRenderer().renderTile(dc, this.tile);
203 finally
205 gl.glPopAttrib();
209 private TextureData makeTextureData(int size)
211 BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_4BYTE_ABGR);
213 TextureData td = new TextureData(GL.GL_RGBA, GL.GL_RGBA, false, this.drawShape(image));
214 td.setMustFlipVertically(false);
216 return td;