Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYPolygonGraphic.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
25 
26 // portability
27 
28 #ifndef STDCALL
29  #ifdef _WIN32
30  #define STDCALL __stdcall
31  #else
32  #define STDCALL
33  //__attribute__((stdcall))
34  #endif
35 #endif
36 
37 #include "TYPolygonGraphic.h"
38 
39 #include <math.h>
40 
42 {
43  _altimetrie = false;
44  _gLUtesselator = NULL;
45  _textureBg = false;
46 }
47 
49 {
50  if (_gLUtesselator)
51  {
52  gluDeleteTess(_gLUtesselator);
53  }
54 }
55 
56 void TYPolygonGraphic::update(bool force /*=false*/)
57 {
59 }
60 
61 void STDCALL beginCallback(GLenum which)
62 {
63  glBegin(which);
64 }
66 {
67  glEnd();
68 }
69 void STDCALL vertexCallback(GLdouble* vertex)
70 {
71  glTexCoord2dv(vertex + 3);
72  glVertex3dv(vertex);
73 }
74 void errorCallback(GLenum errorCode)
75 {
76  const unsigned char* sError = gluErrorString(errorCode);
77  printf("Erreur OpenGL %d : %s\n", errorCode, sError);
78 }
79 
81 {
82  OBox reset;
83  _boundingBox = reset;
84 
85  size_t nbPts = getElement()->getNbPts();
86  for (size_t i = 0; i < nbPts; i++)
87  {
88  OPoint3D pt = getElement()->getPoint(i);
89  _boundingBox.Enlarge((float)(pt._x), (float)(pt._y), (float)(pt._z));
90  }
91 }
92 
93 void TYPolygonGraphic::display(TYElement* pModelerElement /*= nullptr*/, GLenum mode /*= GL_RENDER*/)
94 {
95  size_t nbPts = getElement()->getNbPts();
96 
97  if (_highlight)
98  {
99  float color[4];
100  glGetFloatv(GL_CURRENT_COLOR, color);
101 
102  if (_bFirstDisp)
103  {
105  _bFirstDisp = false;
106  }
108  if (mode == GL_COMPILE)
109  {
110  drawName(pModelerElement);
111  }
112  glColor3f(color[0], color[1], color[2]);
113  }
114 
115  if (_visible)
116  {
117  // Calcul de la normale a la face
118  OVector3D normal = getElement()->normal();
119  normal.normalize();
120 
121  OPoint3D topLeftTexture;
122  double xSize = 0, ySize = 0;
123  if (_textureBg)
124  {
125  topLeftTexture._x = _bgImagePosition._x - _semiXBg;
126  topLeftTexture._y = _bgImagePosition._y - _semiYBg;
127  xSize = _semiXBg * 2;
128  ySize = _semiYBg * 2;
129  }
130 
131  if ((_altimetrie) && (nbPts == 3) && (!_texture))
132  {
133  glBegin(GL_TRIANGLES); // az++
134 
135  OPoint3D pt1 = getElement()->getPoint(0);
136  OPoint3D pt2 = getElement()->getPoint(1);
137  OPoint3D pt3 = getElement()->getPoint(2);
138 
139  OPoint3D pt = pt1;
140  glNormal3f(normal._x, normal._y, normal._z);
141  if (_textureBg)
142  {
143  double x = pt._x - topLeftTexture._x;
144  double y = pt._y - topLeftTexture._y;
145  if ((x >= 0) && (y >= 0))
146  {
147  glTexCoord2f(x / xSize, y / ySize);
148  }
149  else
150  {
151  glColor3f(_color0[0], _color0[1], _color0[2]);
152  }
153  }
154  else
155  {
156  glColor3f(_color0[0], _color0[1], _color0[2]);
157  }
158  glVertex3f(pt._x, pt._y, pt._z);
159 
160  pt = pt2;
161  if (_textureBg)
162  {
163  double x = pt._x - topLeftTexture._x;
164  double y = pt._y - topLeftTexture._y;
165  if ((x >= 0) && (y >= 0))
166  {
167  glTexCoord2f(x / xSize, y / ySize);
168  }
169  else
170  {
171  glColor3f(_color1[0], _color1[1], _color1[2]);
172  }
173  }
174  else
175  {
176  glColor3f(_color1[0], _color1[1], _color1[2]);
177  }
178  glVertex3f(pt._x, pt._y, pt._z);
179 
180  pt = pt3;
181  if (_textureBg)
182  {
183  double x = pt._x - topLeftTexture._x;
184  double y = pt._y - topLeftTexture._y;
185  if ((x >= 0) && (y >= 0))
186  {
187  glTexCoord2f(x / xSize, y / ySize);
188  }
189  else
190  {
191  glColor3f(_color2[0], _color2[1], _color2[2]);
192  }
193  }
194  else
195  {
196  glColor3f(_color2[0], _color2[1], _color2[2]);
197  }
198  glVertex3f(pt._x, pt._y, pt._z);
199 
200  glEnd();
201 
203  {
204  OPoint3D p1 = getElement()->getCenter();
205  OVector3D n = getElement()->normal();
206  n = n * 5;
207  displayNormal(n, p1);
208  }
209  }
210  else
211  {
212  double(*vertex)[5] = new double[nbPts][5];
213 
214  if (_gLUtesselator)
215  {
216  gluDeleteTess(_gLUtesselator);
217  }
218  _gLUtesselator = gluNewTess();
219  gluTessCallback(_gLUtesselator, GLU_TESS_VERTEX, (void(STDCALL*)(void))vertexCallback);
220  gluTessCallback(_gLUtesselator, GLU_TESS_BEGIN, (void(STDCALL*)(void))beginCallback);
221  gluTessCallback(_gLUtesselator, GLU_TESS_END, (void(STDCALL*)(void))endCallback);
222  gluTessCallback(_gLUtesselator, GLU_TESS_ERROR, (void(STDCALL*)(void))errorCallback);
223 
224  gluTessBeginPolygon(_gLUtesselator, NULL);
225  gluTessNormal(_gLUtesselator, normal._x, normal._y, normal._z);
226 
227  gluTessBeginContour(_gLUtesselator);
228  const TYTabPoint* pts = &(getElement()->getPoints());
229 
230  TYNameManager::get()->enable(false);
231 
232  // On recupere les coordonnees du rectangle englobant;
233  TYRectangle rect = getElement()->getBoundingRect();
234 
235  TYNameManager::get()->enable(true);
236 
237  double minX = NAN, minY = NAN;
238  minX = rect.getMinX();
239  minY = rect.getMinY();
240 
241  // Echelle des textures (1 = une texture par m)
242  int scale = 2;
243 
244  for (size_t i = 0; i < nbPts; i++)
245  {
246  // Coordonnees
247  vertex[i][0] = (*pts)[i]._x;
248  vertex[i][1] = (*pts)[i]._y;
249  vertex[i][2] = (*pts)[i]._z;
250 
251  // Coordonnees de texture.
252  vertex[i][3] = (vertex[i][0] - minX) / scale;
253  vertex[i][4] = (vertex[i][1] - minY) / scale;
254 
255  gluTessVertex(_gLUtesselator, (GLdouble*)(*pts)[i]._value, (GLdouble*)vertex[i]);
256  }
257  gluTessEndContour(_gLUtesselator);
258  gluTessEndPolygon(_gLUtesselator);
259 
261  {
262  OPoint3D p1 = getElement()->getCenter();
263  OVector3D n = getElement()->normal();
264  n = n * 5;
265  displayNormal(n, p1);
266  }
267 
268  delete[] vertex;
269  }
270 
271  // Calcul du volume englobant pour le fit:
273  }
274 }
275 
276 void TYPolygonGraphic::setAltimetrieColor(double color0[3], double color1[3], double color2[3])
277 {
278  _altimetrie = true;
279 
280  for (int i = 0; i < 3; i++)
281  {
282  _color0[i] = color0[i];
283  _color1[i] = color1[i];
284  _color2[i] = color2[i];
285  }
286 }
std::vector< TYPoint > TYTabPoint
Collection de TYPoint.
Definition: TYDefines.h:340
#define STDCALL
void STDCALL endCallback()
void STDCALL vertexCallback(GLdouble *vertex)
void errorCallback(GLenum errorCode)
void STDCALL beginCallback(GLenum which)
Representation graphique d'un polygone (fichier header)
The box class.
Definition: 3d.h:1294
virtual void Enlarge(const OPoint3D &pt)
Enlarge the box with the point if the point is outside the box.
Definition: 3d.cpp:1614
double _y
y coordinate of OCoord3D
Definition: 3d.h:283
double _z
z coordinate of OCoord3D
Definition: 3d.h:284
double _x
x coordinate of OCoord3D
Definition: 3d.h:282
The 3D point class.
Definition: 3d.h:487
The 3D vector class.
Definition: 3d.h:298
OVector3D normal(const OVector3D &vector2, const OVector3D &vector3) const
Computes the normal with this vector and 2 others.
Definition: 3d.cpp:220
void normalize()
Normalizes this vector.
Definition: 3d.cpp:225
classe graphique pour un element de base
void drawName(TYElement *pModelerElement=nullptr)
static OBox _globalBoundingBox
bool _visible
Inique si l'element est visible.
static bool _gDrawNormals
Indique si les normals doivent etre visible.
bool _texture
Indique si le texturing est active pour cet element.
bool _highlight
Indique si le highlight est active pour cet element.
void displayNormal(OVector3D normal, OPoint3D p1)
virtual void update(bool force=false)
void enable(bool enable)
Active la generation de nom.
Definition: TYNameManager.h:64
static TYNameManager * get()
Retourne l'instance singleton.
virtual void display(TYElement *pModelerElement=nullptr, GLenum mode=GL_RENDER)
GLUtesselator * _gLUtesselator
virtual void computeBoundingBox()
virtual ~TYPolygonGraphic()
void setAltimetrieColor(double color0[3], double color1[3], double color2[3])
double _color0[3]
Pour les polygones de l'altimetrie chaque sommet a une couleur differente.
virtual void update(bool force=false)
bool _altimetrie
Indique si ce polygone fait partie de l'altimetrie.
TYPolygonGraphic(TYPolygon *pElement)
double getMinX() const
double getMinY() const