Tulip  4.7.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
GlComplexPolygon.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 #ifndef GLCOMPLEXPOLYGON_H
20 #define GLCOMPLEXPOLYGON_H
21 
22 #ifdef WIN32
23 #include <windows.h>
24 #endif
25 
26 #if defined(__APPLE__)
27 #include <OpenGL/gl.h>
28 #include <OpenGL/glu.h>
29 #else
30 #include <GL/gl.h>
31 #include <GL/glu.h>
32 #endif
33 
34 #ifndef CALLBACK
35 #define CALLBACK
36 #endif
37 
38 #include <vector>
39 #include <map>
40 #include <set>
41 
42 #include <tulip/Color.h>
43 #include <tulip/Coord.h>
44 #include <tulip/tulipconf.h>
45 #include <tulip/GlSimpleEntity.h>
46 
47 namespace tlp {
48 
49 typedef struct {
50  GLdouble x, y, z, r, g, b, a;
51 } VERTEX;
52 
53 void CALLBACK beginCallback(GLenum which, GLvoid *polygonData);
54 void CALLBACK errorCallback(GLenum errorCode);
55 void CALLBACK endCallback(GLvoid *polygonData);
56 void CALLBACK vertexCallback(GLvoid *vertex, GLvoid *polygonData);
57 void CALLBACK combineCallback(GLdouble coords[3], VERTEX *d[4], GLfloat w[4], VERTEX** dataOut, GLvoid *polygonData);
58 
59 /**
60  * @ingroup OpenGL
61  * @brief Class to create a complex polygon (concave polygon or polygon with hole)
62  * If you want to create a complex polygon you have 4 constructors :
63  * Constructors with vector of coords : to create a complex polygon without hole
64  * - In this case you have two constructor : with and without outline color
65  * - You can create a polygon like this :
66  * \code
67  * vector <Coord> coords;
68  * coords.push_back(Coord(0,0,0));
69  * coords.push_back(Coord(10,0,0));
70  * coords.push_back(Coord(10,10,0));
71  * coords.push_back(Coord(0,10,0));
72  * GlComplexPolygon *complexPolygon=new GlComplexPolygon(coords,Color(255,0,0,255));
73  * layer->addGlEntity(complexPolygon,"complexPolygon");
74  * \endcode
75  *
76  * Constructors with vector of vector of Coords : to create a complex polygon with hole
77  * - In this case you have two constructor : with and without outline color
78  * - The first vector of coords is the polygon and others vector are holes
79  * - You can create a polygon with hole like this :
80  * \code
81  * vector <vector <Coord> > coords;
82  * vector <Coord> polygon;
83  * vector <Coord> hole;
84  * polygon.push_back(Coord(0,0,0));
85  * polygon.push_back(Coord(10,0,0));
86  * polygon.push_back(Coord(10,10,0));
87  * polygon.push_back(Coord(0,10,0));
88  * hole.push_back(Coord(4,4,0));
89  * hole.push_back(Coord(6,4,0));
90  * hole.push_back(Coord(6,6,0));
91  * hole.push_back(Coord(4,6,0));
92  * coords.push_back(polygon);
93  * coords.push_back(hole);
94  * GlComplexPolygon *complexPolygon=new GlComplexPolygon(coords,Color(255,0,0,255));
95  * layer->addGlEntity(complexPolygon,"complexPolygon");
96  * \endcode
97  *
98  * In constructors you can specify the polygon border style : polygonEdgesType parameter (0 -> straight lines, 1 -> catmull rom curves, 2 -> bezier curves)
99  * You can also specify the texture name if you want to create a textured complex polygon
100  *
101  * In complex polygon you can add a smooth border : see activateQuadBorder(..) function
102  * And you can specify the texture zoom : see setTextureZoom(...) function
103  */
104 class TLP_GL_SCOPE GlComplexPolygon : public GlSimpleEntity {
105 
106  friend void CALLBACK beginCallback(GLenum which, GLvoid *polygonData);
107  friend void CALLBACK errorCallback(GLenum errorCode);
108  friend void CALLBACK endCallback(GLvoid *polygonData);
109  friend void CALLBACK vertexCallback(GLvoid *vertex, GLvoid *polygonData);
110  friend void CALLBACK combineCallback(GLdouble coords[3], VERTEX *d[4], GLfloat w[4], VERTEX** dataOut, GLvoid *polygonData);
111 
112 public:
113  /**
114  * @brief Default constructor
115  * @warning don't use this constructor if you want to create a complex polygon, see others constructors
116  */
118  /**
119  * @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
120  */
121  GlComplexPolygon(const std::vector<Coord> &coords,Color fcolor,int polygonEdgesType=0,const std::string &textureName = "");
122  /**
123  * @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
124  */
125  GlComplexPolygon(const std::vector<Coord> &coords,Color fcolor,Color ocolor,int polygonEdgesType=0,const std::string &textureName = "");
126  /**
127  * @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
128  */
129  GlComplexPolygon(const std::vector<std::vector<Coord> >&coords,Color fcolor,int polygonEdgesType=0,const std::string &textureName = "");
130  /**
131  * @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
132  */
133  GlComplexPolygon(const std::vector<std::vector<Coord> >&coords,Color fcolor,Color ocolor,int polygonEdgesType=0,const std::string &textureName = "");
134 
135  virtual ~GlComplexPolygon() {}
136 
137  /**
138  * @brief Draw the complex polygon
139  */
140  virtual void draw(float lod,Camera *camera);
141 
142  /**
143  * @brief Set if the polygon is outlined or not
144  */
145  void setOutlineMode(const bool);
146 
147  /**
148  * @brief Set size of outline
149  */
150  void setOutlineSize(double size);
151 
152  /**
153  * @brief Get fill color of GlComplexPolygon
154  */
155  Color getFillColor() const {
156  return fillColor;
157  }
158 
159  /**
160  * @brief Set fill color of GlComplexPolygon
161  */
162  void setFillColor(const Color &color) {
163  fillColor=color;
164  }
165 
166  /**
167  * @brief Get outline color of GlComplexPolygon
168  */
169  Color getOutlineColor() const {
170  return outlineColor;
171  }
172 
173  /**
174  * @brief Set outline color of GlComplexPolygon
175  */
176  void setOutlineColor(const Color &color) {
177  outlineColor=color;
178  }
179 
180  /**
181  * @brief Get the texture zoom factor
182  */
183  float getTextureZoom() {
184  return textureZoom;
185  }
186 
187  /**
188  * @brief Set the texture zoom factor
189  *
190  * By default if you have a polygon with a size bigger than (1,1,0) the texture will be repeated
191  * If you want to don't have this texture repeat you have to modify texture zoom
192  * 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
193  */
194  void setTextureZoom(float zoom) {
195  textureZoom=zoom;
196  runTesselation();
197  }
198 
199  /**
200  * @brief Get the textureName
201  */
202  std::string getTextureName();
203 
204  /**
205  * @brief Set the textureName
206  */
207  void setTextureName(const std::string &name);
208 
209  /**
210  * @brief Draw a thick (textured) border around the polygon.
211  *
212  * The graphic card must support geometry shader to make this feature to work.
213  * The position parameter determines the way the border is drawn (depending on the polygon points ordering):
214  * - 0 : the border is drawn outside (or inside) the polygon
215  * - 1 : the border is centered on the polygon outline
216  * - 2 : the border is drawn inside (or outside) the polygon
217  *
218  * The texCoordFactor parameter determines the way the texture is applied : if < 1, the texture will be expanded and > 1, the texture will be compressed
219  * The polygonId parameter determines on which contour of the polygon, the border will be applied
220  */
221  void activateQuadBorder(const float borderWidth, const Color &color, const std::string &texture = "", const int position = 1,
222  const float texCoordFactor = 1.f, const int polygonId = 0);
223 
224  /**
225  * @brief Desactivate the textured quad border
226  */
227  void desactivateQuadBorder(const int polygonId = 0);
228 
229  /**
230  * @brief Translate entity
231  */
232  virtual void translate(const Coord& mouvement);
233 
234  /**
235  * @brief Function to export data and type outString (in XML format)
236  */
237  virtual void getXML(std::string &outString);
238 
239  /**
240  * @brief Function to export data in outString (in XML format)
241  */
242  virtual void getXMLOnlyData(std::string &outString);
243 
244  /**
245  * @brief Function to set data with inString (in XML format)
246  */
247  virtual void setWithXML(const std::string &inString, unsigned int &currentPosition);
248 
249  const std::vector<std::vector<Coord> > &getPolygonSides() const {
250  return points;
251  }
252 
253 
254 protected:
255 
256  /**
257  * @brief Add a new point in polygon
258  */
259  virtual void addPoint(const Coord& point);
260  /**
261  * @brief Begin a new hole in the polygon
262  */
263  virtual void beginNewHole();
264 
265  void runTesselation();
266  void createPolygon(const std::vector<Coord> &coords,int polygonEdgesType);
267  void startPrimitive(GLenum primitive);
268  void endPrimitive();
269  void addVertex(const Coord &vertexCoord, const Vec2f &vertexTexCoord);
270  VERTEX *allocateNewVertex();
271 
272  std::vector<std::vector<Coord> > points;
273  std::vector<std::vector<GLfloat> > pointsIdx;
274  std::set<GLenum> primitivesSet;
275  std::map<GLenum, std::vector<Coord> > verticesMap;
276  std::map<GLenum, std::vector<Vec2f> > texCoordsMap;
277  std::map<GLenum, std::vector<int> >startIndicesMap;
278  std::map<GLenum, std::vector<int> >verticesCountMap;
279  std::vector<VERTEX *> allocatedVertices;
280  GLenum currentPrimitive;
281  int nbPrimitiveVertices;
282  int currentVector;
283  bool outlined;
284  Color fillColor;
285  Color outlineColor;
286  double outlineSize;
287  std::string textureName;
288  float textureZoom;
289  std::vector<bool> quadBorderActivated;
290  std::vector<float> quadBorderWidth;
291  std::vector<Color> quadBorderColor;
292  std::vector<std::string> quadBorderTexture;
293  std::vector<int> quadBorderPosition;
294  std::vector<float> quadBorderTexFactor;
295 };
296 
297 }
298 #endif
Color getOutlineColor() const
Get outline color of GlComplexPolygon.
Base class for all Tulip OpenGL entities.
void setTextureZoom(float zoom)
Set the texture zoom factor.
Class to create a complex polygon (concave polygon or polygon with hole) If you want to create a comp...
Color getFillColor() const
Get fill color of GlComplexPolygon.
float getTextureZoom()
Get the texture zoom factor.
void setFillColor(const Color &color)
Set fill color of GlComplexPolygon.
void setOutlineColor(const Color &color)
Set outline color of GlComplexPolygon.
GlComplexPolygon()
Default constructor.