WhirlyGlobe  1.2
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 <map>
00029 #import "Identifiable.h"
00030 #import "WhirlyVector.h"
00031 #import "GlobeView.h"
00032 
00033 using namespace Eigen;
00034 
00035 @class RendererFrameInfo;
00036 
00037 namespace WhirlyGlobe
00038 {
00039         
00040 class GlobeScene;
00041 
00044 typedef std::map<SimpleIdentity,SimpleIdentity> TextureIDMap;
00045         
00052 class ChangeRequest
00053 {
00054 public:
00055         ChangeRequest() { }
00056         virtual ~ChangeRequest() { }
00057                 
00059         virtual void execute(GlobeScene *scene,WhirlyGlobeView *view) = 0;
00060 };      
00061 
00065 class Drawable : public Identifiable
00066 {
00067 public:
00069         Drawable();
00070         virtual ~Drawable();
00071         
00073         virtual GeoMbr getGeoMbr() const = 0;
00074         
00076         virtual unsigned int getDrawPriority() const = 0;
00077         
00079         virtual bool isOn(RendererFrameInfo *frameInfo) const = 0;
00080         
00084         virtual void setupGL(float minZres) { };
00085         
00087         virtual void teardownGL() { };
00088 
00090         virtual void draw(RendererFrameInfo *frameInfo,GlobeScene *scene) const = 0;    
00091     
00093     virtual bool hasAlpha(RendererFrameInfo *frameInfo) const = 0;
00094     
00096     virtual bool canCache() const = 0;
00097 
00100     virtual bool readFromFile(FILE *fp,const TextureIDMap &texIdMap, bool doTextures=true) { return false; }
00101     
00103     virtual bool writeToFile(FILE *fp,const TextureIDMap &texIdMap, bool doTextures=true) const { return false; }
00104 };
00105 
00110 class DrawableChangeRequest : public ChangeRequest
00111 {
00112 public:
00114         DrawableChangeRequest(SimpleIdentity drawId) : drawId(drawId) { }
00115         ~DrawableChangeRequest() { }
00116         
00118         void execute(GlobeScene *scene,WhirlyGlobeView *view);
00119         
00122         virtual void execute2(GlobeScene *scene,Drawable *draw) = 0;
00123         
00124 protected:
00125         SimpleIdentity drawId;
00126 };
00127 
00129 static const float DrawVisibleInvalid = 1e10;
00130     
00132 static const unsigned int MaxDrawablePoints = ((1<<16)-1);
00133     
00138 class BasicDrawable : public Drawable
00139 {
00140 public:
00142         BasicDrawable();
00145         BasicDrawable(unsigned int numVert,unsigned int numTri);
00146         virtual ~BasicDrawable();
00147 
00149         virtual void setupGL(float minZres);
00150         
00152         virtual void teardownGL();      
00153         
00155         virtual void draw(RendererFrameInfo *frameInfo,GlobeScene *scene) const;
00156         
00158         virtual unsigned int getDrawPriority() const { return drawPriority; }
00159         
00161         virtual bool isOn(RendererFrameInfo *frameInfo) const;
00163         void setOnOff(bool onOff) { on = onOff; }
00164     
00166     virtual bool hasAlpha(RendererFrameInfo *frameInfo) const;
00168     void setAlpha(bool onOff) { isAlpha = onOff; }
00169         
00171         virtual GeoMbr getGeoMbr() const { return geoMbr; }
00172         
00174         void setGeoMbr(GeoMbr mbr) { geoMbr = mbr; }
00175         
00177         class Triangle
00178         {
00179         public:
00180                 Triangle() { }
00182                 Triangle(unsigned short v0,unsigned short v1,unsigned short v2) { verts[0] = v0;  verts[1] = v1;  verts[2] = v2; }
00183                 unsigned short verts[3];
00184         };
00185 
00187         void setDrawPriority(unsigned int newPriority) { drawPriority = newPriority; }
00188         unsigned int getDrawPriority() { return drawPriority; }
00189 
00192         void setDrawOffset(unsigned int newOffset) { drawOffset = newOffset; }
00193         unsigned int getDrawOffset() { return drawOffset; }
00194 
00196         void setType(GLenum inType) { type = inType; }
00197         GLenum getType() const { return type; }
00198 
00200         void setTexId(SimpleIdentity inId) { texId = inId; }
00201 
00203         void setColor(RGBAColor inColor) { color = inColor; }
00204 
00206         void setColor(unsigned char inColor[]) { color.r = inColor[0];  color.g = inColor[1];  color.b = inColor[2];  color.a = inColor[3]; }
00207     RGBAColor getColor() const { return color; }
00208 
00212     void setVisibleRange(float minVis,float maxVis) { minVisible = minVis;  maxVisible = maxVis; }
00213     
00215     void getVisibleRange(float &minVis,float &maxVis) { minVis = minVisible;  maxVis = maxVisible; }
00216     
00218     void setFade(NSTimeInterval inFadeDown,NSTimeInterval inFadeUp) { fadeUp = inFadeUp;  fadeDown = inFadeDown; }
00219 
00221         unsigned int addPoint(Point3f pt) { points.push_back(pt); return points.size()-1; }
00222 
00224         void addTexCoord(TexCoord coord) { texCoords.push_back(coord); }
00225     
00227     void addColor(RGBAColor color) { colors.push_back(color); }
00228 
00230         void addNormal(Point3f norm) { norms.push_back(norm); }
00231 
00233         void addTriangle(Triangle tri) { tris.push_back(tri); }
00234     
00236     unsigned int getNumPoints() const { return points.size(); }
00237     
00239     unsigned int getNumTris() const { return tris.size(); }
00240     
00242     unsigned int getNumNorms() const { return norms.size(); }
00243     
00245     unsigned int getNumTexCoords() const { return texCoords.size(); }
00246         
00247         // Widen a line and turn it into a rectangle of the given width
00248         void addRect(const Point3f &l0, const Vector3f &ln0, const Point3f &l1, const Vector3f &ln1,float width);
00249 
00251     virtual bool canCache() const { return true; }
00252     
00254     virtual bool readFromFile(FILE *fp, const TextureIDMap &texIdMap,bool doTextures=true);
00255     
00257     virtual bool writeToFile(FILE *fp, const TextureIDMap &texIdMap,bool doTextures=true) const;
00258 
00259 protected:
00260         void drawReg(RendererFrameInfo *frameInfo,GlobeScene *scene) const;
00261         void drawVBO(RendererFrameInfo *frameInfo,GlobeScene *scene) const;
00262         
00263         bool on;  // If set, draw.  If not, not
00264     bool usingBuffers;  // If set, we've downloaded the buffers already
00265     NSTimeInterval fadeUp,fadeDown;  // Controls fade in and fade out
00266         unsigned int drawPriority;  // Used to sort drawables
00267         unsigned int drawOffset;    // Number of units of Z buffer resolution to offset upward (by the normal)
00268     bool isAlpha;  // Set if we want to be drawn last
00269         GeoMbr geoMbr;  // Extents on the globe
00270         GLenum type;  // Primitive(s) type
00271         SimpleIdentity texId;  // ID for Texture (in scene)
00272         RGBAColor color;
00273     float minVisible,maxVisible;
00274     // We'll nuke the data arrays when we hand over the data to GL
00275     unsigned int numPoints, numTris;
00276         std::vector<Vector3f> points;
00277     std::vector<RGBAColor> colors;
00278         std::vector<Vector2f> texCoords;
00279         std::vector<Vector3f> norms;
00280         std::vector<Triangle> tris;
00281         
00282         GLuint pointBuffer,colorBuffer,texCoordBuffer,normBuffer,triBuffer;
00283 };
00284 
00286 class ColorChangeRequest : public DrawableChangeRequest
00287 {
00288 public:
00289         ColorChangeRequest(SimpleIdentity drawId,RGBAColor color);
00290         
00291         void execute2(GlobeScene *scene,Drawable *draw);
00292         
00293 protected:
00294         unsigned char color[4];
00295 };
00296         
00298 class OnOffChangeRequest : public DrawableChangeRequest
00299 {
00300 public:
00301         OnOffChangeRequest(SimpleIdentity drawId,bool OnOff);
00302         
00303         void execute2(GlobeScene *scene,Drawable *draw);
00304         
00305 protected:
00306         bool newOnOff;
00307 };      
00308 
00310 class VisibilityChangeRequest : public DrawableChangeRequest
00311 {
00312 public:
00313     VisibilityChangeRequest(SimpleIdentity drawId,float minVis,float maxVis);
00314     
00315     void execute2(GlobeScene *scene,Drawable *draw);
00316     
00317 protected:
00318     float minVis,maxVis;
00319 };
00320     
00322 class FadeChangeRequest : public DrawableChangeRequest
00323 {
00324 public:
00325     FadeChangeRequest(SimpleIdentity drawId,NSTimeInterval fadeUp,NSTimeInterval fadeDown);
00326     
00327     void execute2(GlobeScene *scene,Drawable *draw);
00328     
00329 protected:
00330     NSTimeInterval fadeUp,fadeDown;
00331 };
00332     
00333 }