Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYSiteNodeGraphic.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 
21 #include <qopengltexture.h>
24 #include "TYSiteNodeGraphic.h"
26 
28  : TYElementGraphic(pElement), _glTexture(QOpenGLTexture::Target2D)
29 {
30  _imgXSize = 0;
31  _imgYSize = 0;
32  _visible = true;
33 }
34 
36 {
37  if (_glTexture.isCreated())
38  {
39  _glTexture.destroy();
40  }
41 }
42 
43 void TYSiteNodeGraphic::update(bool force /*=false*/)
44 {
46 }
47 
48 void TYSiteNodeGraphic::getChilds(TYListPtrTYElementGraphic& childs, bool recursif /*=true*/)
49 {
50  TYElementGraphic* pTYElementGraphic = nullptr;
51 
52  pTYElementGraphic = getElement()->getTopographie()->getGraphicObject().getRealPointer();
53  childs.push_back(pTYElementGraphic);
54  if (recursif)
55  {
56  pTYElementGraphic->getChilds(childs, recursif);
57  }
58 
59  pTYElementGraphic = getElement()->getInfrastructure()->getGraphicObject().getRealPointer();
60  childs.push_back(pTYElementGraphic);
61  if (recursif)
62  {
63  pTYElementGraphic->getChilds(childs, recursif);
64  }
65 
66  // Sites childs
67  TYSiteNode* pTYSiteNode = getElement();
68  TYTabSiteNodeGeoNode& TabSite = pTYSiteNode->getListSiteNode();
69  for (unsigned int i = 0; i < TabSite.size(); i++)
70  {
71  pTYElementGraphic = TabSite.at(i)->getGraphicObject().getRealPointer();
72  childs.push_back(pTYElementGraphic);
73  if (recursif)
74  {
75  pTYElementGraphic->getChilds(childs, recursif);
76  }
77  }
78 }
79 
81 {
82  if (!getElement()->isInCurrentCalcul())
83  {
84  return;
85  }
86 
87  TYElementGraphic::display(renderContext);
88 
89  // Draw site node displayed in modeler site at z=0
90  // Its subsites shall be drawn is same 3D referential
91  float fSiteNodeAltitude = (float)getElement()->getAltiEmprise();
92  glTranslatef(0.0f, 0.0f, -fSiteNodeAltitude);
93 
94  if ((getElement()->getUseTopoFile()) && (_visible))
95  {
96  TYSiteNode* pSite = getElement();
97  QString TopoFile = pSite->getTopoFileName();
98  if (_sOldTopoFile != TopoFile)
99  {
100  _glImg.load(TopoFile);
101  _sOldTopoFile = TopoFile;
102  if (!_glImg.isNull())
103  {
104  _glImg = _glImg.mirrored();
105  _imgXSize = _glImg.width();
106  _imgYSize = _glImg.height();
107 
108  if (_glTexture.isCreated())
109  {
110  _glTexture.destroy();
111  }
112  _glTexture.setMinificationFilter(QOpenGLTexture::Nearest);
113  _glTexture.setMagnificationFilter(QOpenGLTexture::Nearest);
114  _glTexture.setWrapMode(QOpenGLTexture::Repeat);
115  _glTexture.setData(_glImg, QOpenGLTexture::GenerateMipMaps);
116  }
117  else
118  {
119  // Failed to load → make sure we don't try to bind later
120  if (_glTexture.isCreated())
121  _glTexture.destroy();
122  }
123  }
124 
125  // Force le bind sur l’unité 0
126  // Only draw the background if we actually have a valid GL texture
127  if (_glTexture.isCreated())
128  {
129  _glTexture.bind(0); // bind to unit 0 explicitly
130  glEnable(GL_TEXTURE_2D);
131  glColor3f(1.f, 1.f, 1.f);
132  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
133 
134  // don't accept fragments if they have zero opacity. this will stop the
135  // zbuffer from be blocked by totally transparent texture fragments.
136  glAlphaFunc(GL_GREATER, (GLclampf)0);
137  glEnable(GL_ALPHA_TEST);
138 
139  TYPoint pt = getElement()->getPosition();
140 
141  // Adjust background image height
142  float fImgZOffset = 0.1f;
143  if (getElement()->getUseEmpriseAsCrbNiv())
144  {
145  fImgZOffset += (float)getElement()->getAltiEmprise();
146  }
147 
148  double angle = 0.0;
149  OVector3D vecOrientation = getElement()->getOrientation().toVector3D();
150 
151  // On verifie que le vecteur orientation n'est pas nul
152  if (vecOrientation.norme() != 0)
153  {
154  // Calcul de l'angle
155  angle = SIGNE(vecOrientation._y) * acos(vecOrientation._x / vecOrientation.norme());
156  angle = (M_PI / 2.0) - angle;
157  }
158 
159  float x = _imgXSize / 2 * getElement()->getEchelle();
160  float y = _imgYSize / 2 * getElement()->getEchelle();
161 
162  glTranslatef(0.0f, 0.0f, fImgZOffset);
163 
164  glPushMatrix();
165  glRotatef(angle * 180 / M_PI, 0.0, 0.0, 1.0);
166  glBegin(GL_QUADS);
167 
168  glTexCoord2f(0.0, 0.0);
169  glVertex3f(pt._x - x, pt._y - y, pt._z);
170 
171  glTexCoord2f(0.0, 1.0);
172  glVertex3f(pt._x - x, pt._y + y, pt._z);
173 
174  glTexCoord2f(1.0, 1.0);
175  glVertex3f(pt._x + x, pt._y + y, pt._z);
176 
177  glTexCoord2f(1.0, 0.0);
178  glVertex3f(pt._x + x, pt._y - y, pt._z);
179 
180  glEnd();
181  glPopMatrix();
182 
183  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
184  glDisable(GL_ALPHA_TEST);
185  glDisable(GL_TEXTURE_2D);
186 
187  _glTexture.release(0); // débind propre sur l’unité 0
188  glTranslatef(0.0f, 0.0f, -fImgZOffset);
189 
190  if (!_glImg.isNull())
191  {
192  ((TYTopographieGraphic*)((TYElementGraphic*)getElement()
193  ->getTopographie()
194  ->getGraphicObject()))
195  ->setBackgroundImage(_glImg, x, y, pt, vecOrientation);
196  }
197  }
198  }
199 
200  if (!getElement()->getUseTopoFile())
201  {
202  ((TYTopographieGraphic*)((TYElementGraphic*)getElement()->getTopographie()->getGraphicObject()))
203  ->unsetBackgroundImage();
204  }
205 
206  getElement()->getTopographie()->getGraphicObject()->display(renderContext);
207  getElement()->getInfrastructure()->getGraphicObject()->display(renderContext);
208  // Sites childs
209  TYSiteNode* pTYSiteNode = getElement(); // az++
210 
211  glTranslatef(0.0f, 0.0f, 0.01f);
212  TYTabSiteNodeGeoNode& TabSite = pTYSiteNode->getListSiteNode(); // az++
213  for (unsigned int i = 0; i < TabSite.size(); i++)
214  {
215  // az++
216  // Le site n'est affiché que s'il est dans le calcul courant
217  if (TabSite.at(i)->getElement()->isInCurrentCalcul())
218  {
219  TYSiteNode* pTYSiteNodeChild = dynamic_cast<TYSiteNode*>(TabSite.at(i)->getElement());
220  float fSiteNodeChildAltitude = pTYSiteNodeChild->getAltiEmprise();
221  // Subsites must be drawn in their parent 3D referential
222  glTranslatef(0.0f, 0.0f, fSiteNodeChildAltitude);
223  TabSite.at(i)->getGraphicObject()->display(renderContext); // az++
224  glTranslatef(0.0f, 0.0f, -fSiteNodeChildAltitude);
225  }
226  } // az++
227 
228  glTranslatef(0.0f, 0.0f, -0.01f + fSiteNodeAltitude);
229 }
230 
231 #if TY_USE_IHM
232 void TYSiteNodeGraphic::connectUpdateSignal(QObject* pReceiver, const char* member)
233 {
234  // _pUpdateSignal->connect(pReceiver, member);
235 }
236 #endif // TY_USE_IHM
237 
238 #if TY_USE_IHM
239 void TYSiteNodeGraphic::disconnectUpdateSignal(QObject* pReceiver, const char* member)
240 {
241  // _pUpdateSignal->disconnect(pReceiver, member);
242 }
243 #endif // TY_USE_IHM
double SIGNE(double a)
Return the number sign.
Definition: 3d.h:78
list< TYElementGraphic * > TYListPtrTYElementGraphic
List de pointeur de TYElement.
Contexte de rendu utilisé par les fonctions d'affichage.
Representation graphique d'un ensemble de sites (fichier header)
std::vector< LPTYSiteNodeGeoNode > TYTabSiteNodeGeoNode
Collection de noeuds geometriques de type TYSiteNode.
Definition: TYSiteNode.h:40
Representation graphique d'une topographie (fichier header)
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 vector class.
Definition: 3d.h:298
double norme() const
Computes the length of this vector.
Definition: 3d.cpp:215
classe graphique pour un element de base
bool _visible
Inique si l'element est visible.
virtual void update(bool force=false)
virtual void getChilds(TYListPtrTYElementGraphic &childs, bool recursif=true)
virtual void display(TYRenderContext &renderContext)
virtual void update(bool force=false)
TYSiteNodeGraphic(TYSiteNode *pElement)
QOpenGLTexture _glTexture
virtual void getChilds(TYListPtrTYElementGraphic &childs, bool recursif=true)
virtual void display(TYRenderContext &renderContext)
const double getAltiEmprise() const
Definition: TYSiteNode.h:141
TYTabSiteNodeGeoNode & getListSiteNode()
Definition: TYSiteNode.h:346
QString getTopoFileName() const
Definition: TYSiteNode.h:241
classe graphique pour une topographie
#define M_PI
Pi.
Definition: color.cpp:25