WhirlyGlobe  1.1
A 3D interactive globe toolkit for ios
/Users/sjg/iPhone/WhirlyGlobe/WhirlyGlobeSrc/WhirlyGlobeLib/include/Drawable.h
00001 /*
00002  *  Drawable.h
00003  *  WhirlyGlobeLib
00004  *
00005  *  Created by Steve Gifford on 2/1/11.
00006  *  Copyright 2011 mousebird consulting
00007  *
00008  *  Licensed under the Apache License, Version 2.0 (the "License");
00009  *  you may not use this file except in compliance with the License.
00010  *  You may obtain a copy of the License at
00011  *  http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  *  Unless required by applicable law or agreed to in writing, software
00014  *  distributed under the License is distributed on an "AS IS" BASIS,
00015  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  *  See the License for the specific language governing permissions and
00017  *  limitations under the License.
00018  *
00019  */
00020 
00021 #import <OpenGLES/ES1/gl.h>
00022 #import <OpenGLES/ES1/glext.h>
00023 #import <OpenGLES/ES2/gl.h>
00024 #import <OpenGLES/ES2/glext.h>
00025 
00026 #import <vector>
00027 #import <set>
00028 #import "Identifiable.h"
00029 #import "WhirlyVector.h"
00030 #import "GlobeView.h"
00031 
00032 using namespace Eigen;
00033 
00034 namespace WhirlyGlobe
00035 {
00036         
00037 class GlobeScene;
00038         
00045 class ChangeRequest
00046 {
00047 public:
00048         ChangeRequest() { }
00049         virtual ~ChangeRequest() { }
00050                 
00052         virtual void execute(GlobeScene *scene,WhirlyGlobeView *view) = 0;
00053 };      
00054 
00058 class Drawable : public Identifiable
00059 {
00060 public:
00062         Drawable();
00063         virtual ~Drawable();
00064         
00066         virtual GeoMbr getGeoMbr() const = 0;
00067         
00069         virtual unsigned int getDrawPriority() const = 0;
00070         
00072         virtual bool isOn(WhirlyGlobeView *view) const = 0;
00073         
00077         virtual void setupGL(float minZres) { };
00078         
00080         virtual void teardownGL() { };
00081 
00083         virtual void draw(GlobeScene *scene) const = 0; 
00084     
00086     virtual bool hasAlpha() const = 0;
00087 };
00088 
00093 class DrawableChangeRequest : public ChangeRequest
00094 {
00095 public:
00097         DrawableChangeRequest(SimpleIdentity drawId) : drawId(drawId) { }
00098         ~DrawableChangeRequest() { }
00099         
00101         void execute(GlobeScene *scene,WhirlyGlobeView *view);
00102         
00105         virtual void execute2(GlobeScene *scene,Drawable *draw) = 0;
00106         
00107 protected:
00108         SimpleIdentity drawId;
00109 };
00110 
00112 static const float DrawVisibleInvalid = 1e10;
00113     
00115 static const unsigned int MaxDrawablePoints = ((1<<16)-1);
00116 
00121 class BasicDrawable : public Drawable
00122 {
00123 public:
00125         BasicDrawable();
00128         BasicDrawable(unsigned int numVert,unsigned int numTri);
00129         virtual ~BasicDrawable();
00130 
00132         virtual void setupGL(float minZres);
00133         
00135         virtual void teardownGL();      
00136         
00138         virtual void draw(GlobeScene *scene) const;
00139         
00141         virtual unsigned int getDrawPriority() const { return drawPriority; }
00142         
00144         virtual bool isOn(WhirlyGlobeView *view) const;
00146         void setOnOff(bool onOff) { on = onOff; }
00147     
00149     virtual bool hasAlpha() const { return isAlpha; }
00151     void setAlpha(bool onOff) { isAlpha = onOff; }
00152         
00154         virtual GeoMbr getGeoMbr() const { return geoMbr; }
00155         
00157         void setGeoMbr(GeoMbr mbr) { geoMbr = mbr; }
00158         
00160         class Triangle
00161         {
00162         public:
00163                 Triangle() { }
00165                 Triangle(unsigned short v0,unsigned short v1,unsigned short v2) { verts[0] = v0;  verts[1] = v1;  verts[2] = v2; }
00166                 unsigned short verts[3];
00167         };
00168 
00170         void setDrawPriority(unsigned int newPriority) { drawPriority = newPriority; }
00171         unsigned int getDrawPriority() { return drawPriority; }
00172 
00175         void setDrawOffset(unsigned int newOffset) { drawOffset = newOffset; }
00176         unsigned int getDrawOffset() { return drawOffset; }
00177 
00179         void setType(GLenum inType) { type = inType; }
00180         GLenum getType() const { return type; }
00181 
00183         void setTexId(SimpleIdentity inId) { texId = inId; }
00184 
00186         void setColor(RGBAColor inColor) { color = inColor; }
00187 
00189         void setColor(unsigned char inColor[]) { color.r = inColor[0];  color.g = inColor[1];  color.b = inColor[2];  color.a = inColor[3]; }
00190     RGBAColor getColor() const { return color; }
00191 
00195     void setVisibleRange(float minVis,float maxVis) { minVisible = minVis;  maxVisible = maxVis; }
00196     
00198     void getVisibleRange(float &minVis,float &maxVis) { minVis = minVisible;  maxVis = maxVisible; }
00199 
00201         unsigned int addPoint(Point3f pt) { points.push_back(pt); return points.size()-1; }
00202 
00204         void addTexCoord(TexCoord coord) { texCoords.push_back(coord); }
00205 
00207         void addNormal(Point3f norm) { norms.push_back(norm); }
00208 
00210         void addTriangle(Triangle tri) { tris.push_back(tri); }
00211     
00213     unsigned int getNumPoints() const { return points.size(); }
00214     
00216     unsigned int getNumTris() const { return tris.size(); }
00217     
00219     unsigned int getNumNorms() const { return norms.size(); }
00220     
00222     unsigned int getNumTexCoords() const { return texCoords.size(); }
00223         
00224         // Widen a line and turn it into a rectangle of the given width
00225         void addRect(const Point3f &l0, const Vector3f &ln0, const Point3f &l1, const Vector3f &ln1,float width);
00226                 
00227 protected:
00228         void drawReg(GlobeScene *scene) const;
00229         void drawVBO(GlobeScene *scene) const;
00230         
00231         bool on;  // If set, draw.  If not, not
00232         unsigned int drawPriority;  // Used to sort drawables
00233         unsigned int drawOffset;    // Number of units of Z buffer resolution to offset upward (by the normal)
00234     bool isAlpha;  // Set if we want to be drawn last
00235         GeoMbr geoMbr;  // Extents on the globe
00236         GLenum type;  // Primitive(s) type
00237         SimpleIdentity texId;  // ID for Texture (in scene)
00238         RGBAColor color;
00239     float minVisible,maxVisible;
00240     // We'll nuke the data arrays when we hand over the data to GL
00241     unsigned int numPoints, numTris;
00242         std::vector<Vector3f> points;
00243         std::vector<Vector2f> texCoords;
00244         std::vector<Vector3f> norms;
00245         std::vector<Triangle> tris;
00246         
00247         GLuint pointBuffer,texCoordBuffer,normBuffer,triBuffer;
00248 };
00249 
00251 class ColorChangeRequest : public DrawableChangeRequest
00252 {
00253 public:
00254         ColorChangeRequest(SimpleIdentity drawId,RGBAColor color);
00255         
00256         void execute2(GlobeScene *scene,Drawable *draw);
00257         
00258 protected:
00259         unsigned char color[4];
00260 };
00261         
00263 class OnOffChangeRequest : public DrawableChangeRequest
00264 {
00265 public:
00266         OnOffChangeRequest(SimpleIdentity drawId,bool OnOff);
00267         
00268         void execute2(GlobeScene *scene,Drawable *draw);
00269         
00270 protected:
00271         bool newOnOff;
00272 };      
00273 
00275 class VisibilityChangeRequest : public DrawableChangeRequest
00276 {
00277 public:
00278     VisibilityChangeRequest(SimpleIdentity drawId,float minVis,float maxVis);
00279     
00280     void execute2(GlobeScene *scene,Drawable *draw);
00281     
00282 protected:
00283     float minVis,maxVis;
00284 };
00285 
00286 }