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())
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  float x = _imgXSize / 2 * getElement()->getEchelle();
126  float y = _imgYSize / 2 * getElement()->getEchelle();
127  OVector3D vecOrientation = getElement()->getOrientation().toVector3D();
128  TYPoint pt = getElement()->getPosition();
129 
130  // Force le bind sur l’unité 0
131  // Only draw the background if we actually have a valid GL texture
132  if (_glTexture.isCreated() && _visible)
133  {
134  _glTexture.bind(0); // bind to unit 0 explicitly
135  glEnable(GL_TEXTURE_2D);
136  glColor3f(1.f, 1.f, 1.f);
137  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
138 
139  // don't accept fragments if they have zero opacity. this will stop the
140  // zbuffer from be blocked by totally transparent texture fragments.
141  glAlphaFunc(GL_GREATER, (GLclampf)0);
142  glEnable(GL_ALPHA_TEST);
143 
144  // Adjust background image height
145  float fImgZOffset = 0.1f;
146  if (getElement()->getUseEmpriseAsCrbNiv())
147  {
148  fImgZOffset += (float)getElement()->getAltiEmprise();
149  }
150 
151  double angle = 0.0;
152 
153  // On verifie que le vecteur orientation n'est pas nul
154  if (vecOrientation.norme() != 0)
155  {
156  // Calcul de l'angle
157  angle = SIGNE(vecOrientation._y) * acos(vecOrientation._x / vecOrientation.norme());
158  angle = (M_PI / 2.0) - angle;
159  }
160 
161  glTranslatef(0.0f, 0.0f, fImgZOffset);
162 
163  glPushMatrix();
164  glRotatef(angle * 180 / M_PI, 0.0, 0.0, 1.0);
165  glBegin(GL_QUADS);
166 
167  glTexCoord2f(0.0, 0.0);
168  glVertex3f(pt._x - x, pt._y - y, pt._z);
169 
170  glTexCoord2f(0.0, 1.0);
171  glVertex3f(pt._x - x, pt._y + y, pt._z);
172 
173  glTexCoord2f(1.0, 1.0);
174  glVertex3f(pt._x + x, pt._y + y, pt._z);
175 
176  glTexCoord2f(1.0, 0.0);
177  glVertex3f(pt._x + x, pt._y - y, pt._z);
178 
179  glEnd();
180  glPopMatrix();
181 
182  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
183  glDisable(GL_ALPHA_TEST);
184  glDisable(GL_TEXTURE_2D);
185 
186  _glTexture.release(0); // débind propre sur l’unité 0
187  glTranslatef(0.0f, 0.0f, -fImgZOffset);
188  }
189 
190  if (!_glImg.isNull())
191  {
192  ((TYTopographieGraphic*)((TYElementGraphic*)getElement()->getTopographie()->getGraphicObject()))
193  ->setBackgroundImage(_glImg, x, y, pt, vecOrientation);
194  }
195  }
196 
197  if (!getElement()->getUseTopoFile())
198  {
199  ((TYTopographieGraphic*)((TYElementGraphic*)getElement()->getTopographie()->getGraphicObject()))
200  ->unsetBackgroundImage();
201  }
202 
203  getElement()->getTopographie()->getGraphicObject()->display(renderContext);
204  getElement()->getInfrastructure()->getGraphicObject()->display(renderContext);
205  // Sites childs
206  TYSiteNode* pTYSiteNode = getElement(); // az++
207 
208  glTranslatef(0.0f, 0.0f, 0.01f);
209  TYTabSiteNodeGeoNode& TabSite = pTYSiteNode->getListSiteNode(); // az++
210  for (unsigned int i = 0; i < TabSite.size(); i++)
211  {
212  // az++
213  // Le site n'est affiché que s'il est dans le calcul courant
214  if (TabSite.at(i)->getElement()->isInCurrentCalcul())
215  {
216  TYSiteNode* pTYSiteNodeChild = dynamic_cast<TYSiteNode*>(TabSite.at(i)->getElement());
217  float fSiteNodeChildAltitude = pTYSiteNodeChild->getAltiEmprise();
218  // Subsites must be drawn in their parent 3D referential
219  glTranslatef(0.0f, 0.0f, fSiteNodeChildAltitude);
220  TabSite.at(i)->getGraphicObject()->display(renderContext); // az++
221  glTranslatef(0.0f, 0.0f, -fSiteNodeChildAltitude);
222  }
223  } // az++
224 
225  glTranslatef(0.0f, 0.0f, -0.01f + fSiteNodeAltitude);
226 }
227 
228 #if TY_USE_IHM
229 void TYSiteNodeGraphic::connectUpdateSignal(QObject* pReceiver, const char* member)
230 {
231  // _pUpdateSignal->connect(pReceiver, member);
232 }
233 #endif // TY_USE_IHM
234 
235 #if TY_USE_IHM
236 void TYSiteNodeGraphic::disconnectUpdateSignal(QObject* pReceiver, const char* member)
237 {
238  // _pUpdateSignal->disconnect(pReceiver, member);
239 }
240 #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