Moka controlers
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
precompile-face.cc
Go to the documentation of this file.
1 /*
2  * lib-controler-gmap : Le contrôleur de 3-G-cartes, surcouche de lib-controler.
3  * Copyright (C) 2004, Moka Team, Université de Poitiers, Laboratoire SIC
4  * http://www.sic.sp2mi.univ-poitiers.fr/
5  * Copyright (C) 2009, Guillaume Damiand, CNRS, LIRIS,
6  * guillaume.damiand@liris.cnrs.fr, http://liris.cnrs.fr/
7  *
8  * This file is part of lib-controler-gmap
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 //******************************************************************************
25 #include "controler-gmap-types.hh"
26 #include "precompile-face.hh"
27 #include "parameter-gmap-vertex.hh"
28 #include "parameter-face.hh"
29 #include <cassert>
30 #include <cstdlib>
31 using namespace GMap3d;
32 //******************************************************************************
34  CParameterFace * AParameterFace ) :
35  FParameterGMapV(AParameterGMapV),
36  FParameterFace (AParameterFace),
37  FTess(NULL)
38 {
39  assert(FParameterGMapV!=NULL && FParameterFace!=NULL);
40 
41  FParameterGMapV->addPrecompileToUpdate(this);
42  FParameterFace->addPrecompileToUpdate(this);
43 }
44 //******************************************************************************
46  CPrecompile (APrecompile),
47  FParameterGMapV(APrecompile.FParameterGMapV),
48  FParameterFace (static_cast<CParameterFace*>
49  (APrecompile.FParameterFace->copy())),
50  FTess(NULL)
51 {
52  assert(FParameterGMapV!=NULL && FParameterFace!=NULL);
53 
54  FParameterGMapV->addPrecompileToUpdate(this);
55  FParameterFace->addPrecompileToUpdate(this);
56 }
57 //******************************************************************************
59 {
60  FParameterGMapV->removePrecompileToUpdate(this);
61  FParameterFace->removePrecompileToUpdate(this);
62 }
63 //******************************************************************************
65 { return new CPrecompileFace(*this); }
66 //******************************************************************************
68 {
69  switch (AParameter->getType())
70  {
72  setGMapVertex(static_cast<CParameterGMapVertex *>(AParameter));
73  break;
74  case PARAMETER_FACE:
75  setFace(static_cast<CParameterFace *>(AParameter));
76  break;
77  }
78 }
79 //******************************************************************************
81 { return FParameterFace; }
82 //******************************************************************************
84 {
85  assert(AParameterGMapV != NULL);
86  AParameterGMapV->addPrecompileToUpdate(this);
87  FParameterGMapV->removePrecompileToUpdate(this);
88  FParameterGMapV = AParameterGMapV;
89  setToUpdate();
90 }
91 //******************************************************************************
93 {
94  assert(AFace != NULL);
95  AFace->addPrecompileToUpdate(this);
96  FParameterFace->removePrecompileToUpdate(this);
97  FParameterFace = AFace;
98  setToUpdate();
99 }
100 //******************************************************************************
102 { return PRECOMPILE_FACE; }
103 //******************************************************************************
104 // monCombine :
105 // Fonction nécessaire si on veut afficher les faces auto-intersectantes.
106 // Sans cette fonction, ces faces sont affichées en fil de fer.
107 static void monCombine(GLdouble c[3], void *d[4], GLfloat w[4], void **out)
108 {
109  GLdouble *nv = (GLdouble *) malloc(sizeof(GLdouble)*3);
110 
111  nv[0] = c[0];
112 
113  nv[1] = c[1];
114 
115  nv[2] = c[2];
116 
117  *out = nv;
118 }
119 
120 /* TODO use openGL to draw the darts ?
121 static void edgeFlagDataCB(GLboolean flag, void* lpContext)
122 { glEdgeFlag(flag); }
123 
124 gluTessCallback(FTess, GLU_TESS_EDGE_FLAG_DATA,
125 (GLvoid (WINAPI *) ( )) & edgeFlagDataCB);
126 */
127 
128 //******************************************************************************
130 {
131  if (FParameterFace->getBLFace()<1)
132  {
133  glEnable(GL_BLEND);
134  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
135  }
136 
137  bool bursted1 = ! areEqual(FParameterGMapV->getDrawingMap()->getBurstCoef(1), 1.0);
138  int treated = FParameterGMapV->getDrawingMap()->getNewMark();
139 
140 #ifdef _WINDOWS
141 #define WINAPI __stdcall
142 #else
143 #define WINAPI
144 #endif
145 
146  FTess = gluNewTess();
147  gluTessCallback(FTess, GLU_TESS_BEGIN,
148  (GLvoid (WINAPI *) ( )) & glBegin );
149  gluTessCallback(FTess, GLU_TESS_END,
150  (GLvoid (WINAPI *) ( )) & glEnd );
151  gluTessCallback(FTess, GLU_TESS_VERTEX,
152  (GLvoid (WINAPI *) ( )) & glVertex3dv);
153  gluTessCallback(FTess, GLU_TESS_COMBINE,
154  (GLvoid (WINAPI *) ( )) & monCombine );
155 
156  // Couleur globale :
157  if ( !FParameterFace->getRandomCLFace () &&
158  !FParameterFace->getRandomCLVolume() )
159  glColor4f(FParameterFace->getCLFace(0),
160  FParameterFace->getCLFace(1),
161  FParameterFace->getCLFace(2),
162  FParameterFace->getBLFace());
163 
164  for (CDynamicCoverageAll it(FParameterGMapV->getDrawingMap()); it.cont(); ++it)
165  if (! FParameterGMapV->getDrawingMap()->isMarked(*it, treated))
166  {
167  if ( FParameterFace->getRandomCLVolume() )
168  setRandomColor(*it, 3);
169 
170  for (CDynamicCoverage012 it2(FParameterGMapV->getDrawingMap(), *it);
171  it2.cont(); ++it2)
172  if (! FParameterGMapV->getDrawingMap()->isMarked(*it2, treated))
173  {
174  if (FParameterGMapV->getDrawingMap()->isClosedPolyline(*it2))
175  // && FParameterGMapV->getMap()->isPlanarPolyline(*it)
176  {
177  int nbSommetsFace = FParameterGMapV->getDrawingMap()->
178  getNbPolylineVertices(*it2);
179  if (nbSommetsFace > 2)
180  {
181  if ( FParameterFace->getRandomCLFace() )
182  setRandomColor(*it2 , 2);
183 
184  drawFilledFace(*it2, nbSommetsFace, bursted1);
185  }
186  }
187  FParameterGMapV->getDrawingMap()->markOrbit(*it2, ORBIT_01, treated);
188  }
189  }
190 
191  FParameterGMapV->getDrawingMap()->negateMaskMark(treated);
192  FParameterGMapV->getDrawingMap()->freeMark(treated);
193  gluDeleteTess(FTess);
194 
195  glDisable(GL_BLEND);
196 }
197 //------------------------------------------------------------------------------
198 void CPrecompileFace::setRandomColor(CDart* ADart, int ADimension)
199 {
200  assert( ADimension!=0 );
201 
202  float r,v,b;
203 
204  if ( FParameterFace->getRandomColorGeometry() )
205  {
206  CVertex bary = FParameterGMapV->getMap()->
207  barycenter(ADart, ORBIT_CELL[ADimension]);
208  //fabs(v)-floor(fabs(v))
209  r = bary.getX();
210  v = bary.getY();
211  b = bary.getZ();
212 #undef FLT
213  }
214  else
215  {
216  long int c = 0;
217 
218  CCoverage* cov = FParameterGMapV->getMap()->
219  getDynamicCoverage(ADart, ORBIT_CELL[ADimension]);
220 
221  for ( ; cov->cont(); ++ *cov)
222  c += (long int) ** cov;
223 
224  delete cov;
225 
226  srand(c);
227  r = double(rand())/RAND_MAX;
228  v = double(rand())/RAND_MAX;
229  b = double(rand())/RAND_MAX;
230 
231  //std::cout<<"c="<<c<<" "<<"r,v,b="<<r<<", "<<v<<", "<<b<<std::endl;
232  }
233 
234  glColor4f(r, v, b, FParameterFace->getBLFace());
235 }
236 //------------------------------------------------------------------------------
237 void CPrecompileFace::drawFilledFace(CDart* ADart, int ANbSommets,
238  bool ABursted)
239 {
240  gluTessBeginPolygon(FTess, NULL);
241  gluTessBeginContour(FTess);
242 
243  GLdouble* data = new GLdouble[(ABursted ? 6 : 3) * (ANbSommets+1)];
244 
245  CVertex pred;
246  int numeroSommet = 0;
247 
248  // Parcours de la face
249  for (CDynamicCoverage01 cov(FParameterGMapV->getDrawingMap(), ADart);
250  cov.cont(); ++cov)
251  {
252  // Récupérer les coordonnées associées au brins
253  CVertex & v = FParameterGMapV->getDrawingMap()->getBurstVertex(* cov);
254 
255  if (cov.prevOperationType() == OP_NONE || pred != v)
256  {
257  data[ (numeroSommet*3)] = v.getX();
258  data[1+(numeroSommet*3)] = v.getY();
259  data[2+(numeroSommet*3)] = v.getZ();
260 
261  gluTessVertex(FTess, &data[numeroSommet*3], &data[numeroSommet*3]);
262  ++ numeroSommet;
263  pred = v;
264  }
265  }
266 
267  gluTessEndContour(FTess);
268  gluTessEndPolygon(FTess);
269 
270  delete [] data;
271 }
272 //******************************************************************************