Tulip  4.6.0
Better Visualization Through Research
library/tulip-ogl/include/tulip/GlComplexPolygon.h
00001 /*
00002  *
00003  * This file is part of Tulip (www.tulip-software.org)
00004  *
00005  * Authors: David Auber and the Tulip development Team
00006  * from LaBRI, University of Bordeaux
00007  *
00008  * Tulip is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation, either version 3
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Tulip is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  *
00018  */
00019 #ifndef GLCOMPLEXPOLYGON_H
00020 #define GLCOMPLEXPOLYGON_H
00021 
00022 #ifdef WIN32
00023 #include <windows.h>
00024 #endif
00025 
00026 #if defined(__APPLE__)
00027 #include <OpenGL/gl.h>
00028 #include <OpenGL/glu.h>
00029 #else
00030 #include <GL/gl.h>
00031 #include <GL/glu.h>
00032 #endif
00033 
00034 #ifndef CALLBACK
00035 #define CALLBACK
00036 #endif
00037 
00038 #include <vector>
00039 #include <map>
00040 #include <set>
00041 
00042 #include <tulip/Color.h>
00043 #include <tulip/Coord.h>
00044 #include <tulip/tulipconf.h>
00045 #include <tulip/GlSimpleEntity.h>
00046 
00047 namespace tlp {
00048 
00049 typedef struct {
00050   GLdouble x, y, z, r, g, b, a;
00051 } VERTEX;
00052 
00053 void CALLBACK beginCallback(GLenum which, GLvoid *polygonData);
00054 void CALLBACK errorCallback(GLenum errorCode);
00055 void CALLBACK endCallback(GLvoid *polygonData);
00056 void CALLBACK vertexCallback(GLvoid *vertex, GLvoid *polygonData);
00057 void CALLBACK combineCallback(GLdouble coords[3], VERTEX *d[4], GLfloat w[4], VERTEX** dataOut, GLvoid *polygonData);
00058 
00059 /**
00060  * @ingroup OpenGL
00061  * @brief Class to create a complex polygon (concave polygon or polygon with hole)
00062  * If you want to create a complex polygon you have 4 constructors :
00063  * Constructors with vector of coords : to create a complex polygon without hole
00064  *   - In this case you have two constructor : with and without outline color
00065  *   - You can create a polygon like this :
00066  * \code
00067  *     vector <Coord> coords;
00068  *     coords.push_back(Coord(0,0,0));
00069  *     coords.push_back(Coord(10,0,0));
00070  *     coords.push_back(Coord(10,10,0));
00071  *     coords.push_back(Coord(0,10,0));
00072  *     GlComplexPolygon *complexPolygon=new GlComplexPolygon(coords,Color(255,0,0,255));
00073  *     layer->addGlEntity(complexPolygon,"complexPolygon");
00074  * \endcode
00075  *
00076  * Constructors with vector of vector of Coords : to create a complex polygon with hole
00077  *   - In this case you have two constructor : with and without outline color
00078  *   - The first vector of coords is the polygon and others vector are holes
00079  *   - You can create a polygon with hole like this :
00080  * \code
00081  *     vector <vector <Coord> > coords;
00082  *     vector <Coord> polygon;
00083  *     vector <Coord> hole;
00084  *     polygon.push_back(Coord(0,0,0));
00085  *     polygon.push_back(Coord(10,0,0));
00086  *     polygon.push_back(Coord(10,10,0));
00087  *     polygon.push_back(Coord(0,10,0));
00088  *     hole.push_back(Coord(4,4,0));
00089  *     hole.push_back(Coord(6,4,0));
00090  *     hole.push_back(Coord(6,6,0));
00091  *     hole.push_back(Coord(4,6,0));
00092  *     coords.push_back(polygon);
00093  *     coords.push_back(hole);
00094  *     GlComplexPolygon *complexPolygon=new GlComplexPolygon(coords,Color(255,0,0,255));
00095  *     layer->addGlEntity(complexPolygon,"complexPolygon");
00096  * \endcode
00097  *
00098  * In constructors you can specify the polygon border style : polygonEdgesType parameter (0 -> straight lines, 1 -> catmull rom curves, 2 -> bezier curves)
00099  * You can also specify the texture name if you want to create a textured complex polygon
00100  *
00101  * In complex polygon you can add a smooth border : see activateQuadBorder(..) function
00102  * And you can specify the texture zoom : see setTextureZoom(...) function
00103  */
00104 class TLP_GL_SCOPE GlComplexPolygon : public GlSimpleEntity {
00105 
00106   friend void CALLBACK beginCallback(GLenum which, GLvoid *polygonData);
00107   friend void CALLBACK errorCallback(GLenum errorCode);
00108   friend void CALLBACK endCallback(GLvoid *polygonData);
00109   friend void CALLBACK vertexCallback(GLvoid *vertex, GLvoid *polygonData);
00110   friend void CALLBACK combineCallback(GLdouble coords[3], VERTEX *d[4], GLfloat w[4], VERTEX** dataOut, GLvoid *polygonData);
00111 
00112 public:
00113   /**
00114    * @brief Default constructor
00115    * @warning don't use this constructor if you want to create a complex polygon, see others constructors
00116    */
00117   GlComplexPolygon() {}
00118   /**
00119    * @brief Constructor with a vector of coords, a fill color, a polygon edges type(0 -> straight lines, 1 -> catmull rom curves, 2 -> bezier curves) and a textureName if you want
00120    */
00121   GlComplexPolygon(const std::vector<Coord> &coords,Color fcolor,int polygonEdgesType=0,const std::string &textureName = "");
00122   /**
00123    * @brief Constructor with a vector of coords, a fill color, an outline color, a polygon edges type(0 -> straight lines, 1 -> catmull rom curves, 2 -> bezier curves) and a textureName if you want
00124    */
00125   GlComplexPolygon(const std::vector<Coord> &coords,Color fcolor,Color ocolor,int polygonEdgesType=0,const std::string &textureName = "");
00126   /**
00127    * @brief Constructor with a vector of vector of coords (the first vector of coord is the polygon and others vectors are holes in polygon), a fill color, a polygon edges type(0 -> straight lines, 1 -> catmull rom curves, 2 -> bezier curves) and a textureName if you want
00128    */
00129   GlComplexPolygon(const std::vector<std::vector<Coord> >&coords,Color fcolor,int polygonEdgesType=0,const std::string &textureName = "");
00130   /**
00131    * @brief Constructor with a vector of vector of coords (the first vector of coord is the polygon and others vectors are holes in polygon), a fill color, an outline color a polygon edges type(0 -> straight lines, 1 -> catmull rom curves, 2 -> bezier curves) and a textureName if you want
00132    */
00133   GlComplexPolygon(const std::vector<std::vector<Coord> >&coords,Color fcolor,Color ocolor,int polygonEdgesType=0,const std::string &textureName = "");
00134 
00135   virtual ~GlComplexPolygon() {}
00136 
00137   /**
00138    * @brief Draw the complex polygon
00139    */
00140   virtual void draw(float lod,Camera *camera);
00141 
00142   /**
00143    * @brief Set if the polygon is outlined or not
00144    */
00145   void setOutlineMode(const bool);
00146 
00147   /**
00148    * @brief Set size of outline
00149    */
00150   void setOutlineSize(double size);
00151 
00152   /**
00153    * @brief Get fill color of GlComplexPolygon
00154    */
00155   Color getFillColor() const {
00156     return fillColor;
00157   }
00158 
00159   /**
00160    * @brief Set fill color of GlComplexPolygon
00161    */
00162   void setFillColor(const Color &color) {
00163     fillColor=color;
00164   }
00165 
00166   /**
00167    * @brief Get outline color of GlComplexPolygon
00168    */
00169   Color getOutlineColor() const {
00170     return outlineColor;
00171   }
00172 
00173   /**
00174    * @brief Set outline color of GlComplexPolygon
00175    */
00176   void setOutlineColor(const Color &color) {
00177     outlineColor=color;
00178   }
00179 
00180   /**
00181    * @brief Get the texture zoom factor
00182    */
00183   float getTextureZoom() {
00184     return textureZoom;
00185   }
00186 
00187   /**
00188    * @brief Set the texture zoom factor
00189    *
00190    * By default if you have a polygon with a size bigger than (1,1,0) the texture will be repeated
00191    * If you want to don't have this texture repeat you have to modify texture zoom
00192    * For example if you have a polygon with coords ((0,0,0),(5,0,0),(5,5,0),(0,5,0)) you can set texture zoom to 5. to don't have texture repeat
00193    */
00194   void setTextureZoom(float zoom) {
00195     textureZoom=zoom;
00196     runTesselation();
00197   }
00198 
00199   /**
00200    * @brief Get the textureName
00201    */
00202   std::string getTextureName();
00203 
00204   /**
00205    * @brief Set the textureName
00206    */
00207   void setTextureName(const std::string &name);
00208 
00209   /**
00210    * @brief Draw a thick (textured) border around the polygon.
00211    *
00212    * The graphic card must support geometry shader to make this feature to work.
00213    * The position parameter determines the way the border is drawn (depending on the polygon points ordering):
00214    *     - 0 : the border is drawn outside (or inside) the polygon
00215    *     - 1 : the border is centered on the polygon outline
00216    *     - 2 : the border is drawn inside (or outside) the polygon
00217    *
00218    * The texCoordFactor parameter determines the way the texture is applied : if < 1, the texture will be expanded and > 1, the texture will be compressed
00219    * The polygonId parameter determines on which contour of the polygon, the border will be applied
00220    */
00221   void activateQuadBorder(const float borderWidth, const Color &color, const std::string &texture = "", const int position = 1,
00222                           const float texCoordFactor = 1.f, const int polygonId = 0);
00223 
00224   /**
00225    * @brief Desactivate the textured quad border
00226    */
00227   void desactivateQuadBorder(const int polygonId = 0);
00228 
00229   /**
00230    * @brief Translate entity
00231    */
00232   virtual void translate(const Coord& mouvement);
00233 
00234   /**
00235    * @brief Function to export data and type outString (in XML format)
00236    */
00237   virtual void getXML(std::string &outString);
00238 
00239   /**
00240    * @brief Function to export data in outString (in XML format)
00241    */
00242   virtual void getXMLOnlyData(std::string &outString);
00243 
00244   /**
00245    * @brief Function to set data with inString (in XML format)
00246    */
00247   virtual void setWithXML(const std::string &inString, unsigned int &currentPosition);
00248 
00249   const std::vector<std::vector<Coord> > &getPolygonSides() const {
00250     return points;
00251   }
00252 
00253 
00254 protected:
00255 
00256   /**
00257    * @brief Add a new point in polygon
00258    */
00259   virtual void addPoint(const Coord& point);
00260   /**
00261    * @brief Begin a new hole in the polygon
00262    */
00263   virtual void beginNewHole();
00264 
00265   void runTesselation();
00266   void createPolygon(const std::vector<Coord> &coords,int polygonEdgesType);
00267   void startPrimitive(GLenum primitive);
00268   void endPrimitive();
00269   void addVertex(const Coord &vertexCoord, const Vec2f &vertexTexCoord);
00270   VERTEX *allocateNewVertex();
00271 
00272   std::vector<std::vector<Coord> > points;
00273   std::vector<std::vector<GLfloat> > pointsIdx;
00274   std::set<GLenum> primitivesSet;
00275   std::map<GLenum, std::vector<Coord> > verticesMap;
00276   std::map<GLenum, std::vector<Vec2f> > texCoordsMap;
00277   std::map<GLenum, std::vector<int> >startIndicesMap;
00278   std::map<GLenum, std::vector<int> >verticesCountMap;
00279   std::vector<VERTEX *> allocatedVertices;
00280   GLenum currentPrimitive;
00281   int nbPrimitiveVertices;
00282   int currentVector;
00283   bool outlined;
00284   Color fillColor;
00285   Color outlineColor;
00286   double outlineSize;
00287   std::string textureName;
00288   float textureZoom;
00289   std::vector<bool> quadBorderActivated;
00290   std::vector<float> quadBorderWidth;
00291   std::vector<Color> quadBorderColor;
00292   std::vector<std::string> quadBorderTexture;
00293   std::vector<int> quadBorderPosition;
00294   std::vector<float> quadBorderTexFactor;
00295 };
00296 
00297 }
00298 #endif
 All Classes Files Functions Variables Enumerations Enumerator Properties