Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYPolygonGraphic.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2024> <EDF-DTG> <FRANCE>
3  * This file is part of Code_TYMPAN (R).
4  * Code_TYMPAN (R) is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * Code_TYMPAN (R) is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License along
13  * with Code_TYMPAN (R). If not, see <https://www.gnu.org/licenses/>.
14  */
15 
25 
26 #include "gui/gl/TYRenderContext.h"
27 #include <QtOpenGL>
28 
29 // portability
30 
31 #ifndef STDCALL
32  #ifdef _WIN32
33  #define STDCALL __stdcall
34  #else
35  #define STDCALL
36  //__attribute__((stdcall))
37  #endif
38 #endif
39 
40 #include "TYPolygonGraphic.h"
41 
42 #include <math.h>
43 
45 {
46  _altimetrie = false;
47  _gLUtesselator = NULL;
48  _textureBg = false;
49 }
50 
52 {
53  if (_gLUtesselator)
54  {
55  gluDeleteTess(_gLUtesselator);
56  }
57 }
58 
59 void TYPolygonGraphic::update(bool force /*=false*/)
60 {
62 }
63 
64 void STDCALL beginCallback(GLenum which)
65 {
66  glBegin(which);
67 }
69 {
70  glEnd();
71 }
72 void STDCALL vertexCallback(GLdouble* vertex)
73 {
74  glTexCoord2dv(vertex + 3);
75  glVertex3dv(vertex);
76 }
77 void errorCallback(GLenum errorCode)
78 {
79  const unsigned char* sError = gluErrorString(errorCode);
80  printf("Erreur OpenGL %d : %s\n", errorCode, sError);
81 }
82 
84 {
86 
87  size_t nbPts = getElement()->getNbPts();
88  for (size_t i = 0; i < nbPts; i++)
89  {
90  OPoint3D pt = getElement()->getPoint(i);
91  boundingBox.Enlarge((float)(pt._x), (float)(pt._y), (float)(pt._z));
92  }
93  return boundingBox;
94 }
95 
97 {
98  size_t nbPts = getElement()->getNbPts();
99 
100  if (_highlight)
101  {
102  float color[4];
103  glGetFloatv(GL_CURRENT_COLOR, color);
104  if (renderContext.pass == TYRenderPass::Overlay)
105  {
106  drawName(renderContext);
107  }
108  glColor3f(color[0], color[1], color[2]);
109  }
110 
111  if (_visible)
112  {
113  // Calcul de la normale a la face
114  OVector3D normal = getElement()->normal();
115  normal.normalize();
116 
117  OPoint3D topLeftTexture;
118  double xSize = 0, ySize = 0;
119  if (_textureBg)
120  {
121  topLeftTexture._x = _bgImagePosition._x - _semiXBg;
122  topLeftTexture._y = _bgImagePosition._y - _semiYBg;
123  xSize = _semiXBg * 2;
124  ySize = _semiYBg * 2;
125  }
126 
127  if ((_altimetrie) && (nbPts == 3) && (!_texture))
128  {
129  glBegin(GL_TRIANGLES); // az++
130 
131  OPoint3D pt1 = getElement()->getPoint(0);
132  OPoint3D pt2 = getElement()->getPoint(1);
133  OPoint3D pt3 = getElement()->getPoint(2);
134 
135  OPoint3D pt = pt1;
136  glNormal3f(normal._x, normal._y, normal._z);
137  if (_textureBg)
138  {
139  double x = pt._x - topLeftTexture._x;
140  double y = pt._y - topLeftTexture._y;
141  if ((x >= 0) && (y >= 0))
142  {
143  glTexCoord2f(x / xSize, y / ySize);
144  }
145  else
146  {
147  glColor3f(_color0[0], _color0[1], _color0[2]);
148  }
149  }
150  else
151  {
152  glColor3f(_color0[0], _color0[1], _color0[2]);
153  }
154  glVertex3f(pt._x, pt._y, pt._z);
155 
156  pt = pt2;
157  if (_textureBg)
158  {
159  double x = pt._x - topLeftTexture._x;
160  double y = pt._y - topLeftTexture._y;
161  if ((x >= 0) && (y >= 0))
162  {
163  glTexCoord2f(x / xSize, y / ySize);
164  }
165  else
166  {
167  glColor3f(_color1[0], _color1[1], _color1[2]);
168  }
169  }
170  else
171  {
172  glColor3f(_color1[0], _color1[1], _color1[2]);
173  }
174  glVertex3f(pt._x, pt._y, pt._z);
175 
176  pt = pt3;
177  if (_textureBg)
178  {
179  double x = pt._x - topLeftTexture._x;
180  double y = pt._y - topLeftTexture._y;
181  if ((x >= 0) && (y >= 0))
182  {
183  glTexCoord2f(x / xSize, y / ySize);
184  }
185  else
186  {
187  glColor3f(_color2[0], _color2[1], _color2[2]);
188  }
189  }
190  else
191  {
192  glColor3f(_color2[0], _color2[1], _color2[2]);
193  }
194  glVertex3f(pt._x, pt._y, pt._z);
195 
196  glEnd();
197 
199  {
200  OPoint3D p1 = getElement()->getCenter();
201  OVector3D n = getElement()->normal();
202  n = n * 5;
203  displayNormal(n, p1);
204  }
205  }
206  else
207  {
208  double(*vertex)[5] = new double[nbPts][5];
209 
210  if (_gLUtesselator)
211  {
212  gluDeleteTess(_gLUtesselator);
213  }
214  _gLUtesselator = gluNewTess();
215  gluTessCallback(_gLUtesselator, GLU_TESS_VERTEX, (void(STDCALL*)(void))vertexCallback);
216  gluTessCallback(_gLUtesselator, GLU_TESS_BEGIN, (void(STDCALL*)(void))beginCallback);
217  gluTessCallback(_gLUtesselator, GLU_TESS_END, (void(STDCALL*)(void))endCallback);
218  gluTessCallback(_gLUtesselator, GLU_TESS_ERROR, (void(STDCALL*)(void))errorCallback);
219 
220  gluTessBeginPolygon(_gLUtesselator, NULL);
221  gluTessNormal(_gLUtesselator, normal._x, normal._y, normal._z);
222 
223  gluTessBeginContour(_gLUtesselator);
224  const TYTabPoint* pts = &(getElement()->getPoints());
225 
226  TYNameManager::get()->enable(false);
227 
228  // On recupere les coordonnees du rectangle englobant;
229  TYRectangle rect = getElement()->getBoundingRect();
230 
231  TYNameManager::get()->enable(true);
232 
233  double minX = NAN, minY = NAN;
234  minX = rect.getMinX();
235  minY = rect.getMinY();
236 
237  // Echelle des textures (1 = une texture par m)
238  int scale = 2;
239 
240  for (size_t i = 0; i < nbPts; i++)
241  {
242  // Coordonnees
243  vertex[i][0] = (*pts)[i]._x;
244  vertex[i][1] = (*pts)[i]._y;
245  vertex[i][2] = (*pts)[i]._z;
246 
247  // Coordonnees de texture.
248  vertex[i][3] = (vertex[i][0] - minX) / scale;
249  vertex[i][4] = (vertex[i][1] - minY) / scale;
250 
251  gluTessVertex(_gLUtesselator, (GLdouble*)(*pts)[i]._value, (GLdouble*)vertex[i]);
252  }
253  gluTessEndContour(_gLUtesselator);
254  gluTessEndPolygon(_gLUtesselator);
255 
257  {
258  OPoint3D p1 = getElement()->getCenter();
259  OVector3D n = getElement()->normal();
260  n = n * 5;
261  displayNormal(n, p1);
262  }
263 
264  delete[] vertex;
265  }
266  }
267 }
268 
269 void TYPolygonGraphic::setAltimetrieColor(double color0[3], double color1[3], double color2[3])
270 {
271  _altimetrie = true;
272 
273  for (int i = 0; i < 3; i++)
274  {
275  _color0[i] = color0[i];
276  _color1[i] = color1[i];
277  _color2[i] = color2[i];
278  }
279 }
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)
Contexte de rendu utilisé par les fonctions d'affichage.
@ Overlay
The current render pass is for overlay elements.
The box class.
Definition: 3d.h:1346
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(TYRenderContext &renderContext)
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.
OBox boundingBox() const
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.
GLUtesselator * _gLUtesselator
virtual ~TYPolygonGraphic()
virtual OBox computeBoundingBox() const
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)
virtual void display(TYRenderContext &renderContext)
double getMinX() const
double getMinY() const
const TYRenderPass pass