Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYOpenGLRenderer.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 
23 
24 #include "TYOpenGLRenderer.h"
25 
27 static const float ElementPickerTolerance = 0.01f;
28 
29 bool GetADisplayList(GLuint& displayList)
30 {
31  if (0 == displayList)
32  {
33  displayList = glGenLists(1);
34  if (0 == displayList)
35  {
36  GLenum errorCode = glGetError();
37  const unsigned char* sError = gluErrorString(errorCode);
38  printf("Erreur OpenGL %d : %s\n", errorCode, sError);
39  return false;
40  }
41  }
42  return true;
43 }
44 
46 {
47  _pElement = NULL;
48  _displayList = 0; // az++
49  _displayListOverlay = 0; // az++
50  _pActiveCamera = NULL;
51  _pBackgroundColor = new double[3];
52  m_renderType = 0;
53  _pModelerElement = NULL;
54 }
55 
57 {
58  if (_displayList) // az++
59  {
60  glDeleteLists(_displayList, 1);
61  }
62  if (_displayListOverlay) // az++
63  {
64  glDeleteLists(_displayListOverlay, 1);
65  }
66  delete[] _pBackgroundColor;
67 }
68 
69 void TYOpenGLRenderer::init(int width, int height)
70 {
71  // glClearColor(1.0, 1.0, 1.0, 1.0);
72  glClearColor(_pBackgroundColor[0], _pBackgroundColor[1], _pBackgroundColor[2], 1.0);
73  glColor3f(1.0, 0.0, 0.0);
74 
75  // Lights
76  glEnable(GL_LIGHTING);
77  glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
78 
79  static GLfloat ambient[] = {0.1f, 0.1f, 0.1f, 1.0f};
80  static GLfloat ambientBIS[] = {0.8f, 0.8f, 0.8f, 0.8f};
81  static GLfloat diffuse[] = {0.8f, 0.8f, 0.8f, 1.0f};
82  static GLfloat specular[] = {0.8f, 0.8f, 0.8f, 1.0f};
83  static GLfloat shininess = 50.;
84  glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientBIS);
85  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
86  glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
87  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
88  glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
89 
90  glViewport(0, 0, width, height);
91  glMatrixMode(GL_PROJECTION);
92  glLoadIdentity();
93  m_width = width;
94  m_height = height;
95  if (_pActiveCamera)
96  {
98  }
99 }
100 
102 {
104  {
105  if (pElement)
106  {
107  // Chercher le GeoNode correspondant a pElement (le repere dans lequel est exprime pElement)
108  TYGeometryNode* pGeoNode = pElement;
109  if (pElement->isA("TYGeometryNode"))
110  {
111  pGeoNode = (TYGeometryNode*)pElement;
112  }
113  else
114  {
115  pGeoNode = (TYGeometryNode*)pElement->GetGeoNodeParent();
116  }
117 
118  if (pGeoNode)
119  {
120  LPTYElementGraphic pTYElementGraphic = pGeoNode->getGraphicObject();
121  TYGeometryNodeGraphic* pGeoNodeGraphic =
122  (TYGeometryNodeGraphic*)pTYElementGraphic.getRealPointer();
123  pGeoNodeGraphic->displayPushingParentMatrix(_pModelerElement, GL_COMPILE, pDansCeRepere);
124  }
125  }
126  }
127 }
128 
130 {
131  // Gestion de la BB globale d'affichage : a revoir
132  OBox reset; // az++
133  TYElementGraphic::_globalBoundingBox = reset; // az++
134  // Affectation de la displayList principale:
136  {
137  glNewList(_displayList, GL_COMPILE);
138  drawElement();
139  glEndList();
140  }
141 }
142 
144 {
145  OpenGLRender();
146 }
147 
148 void TYOpenGLRenderer::OpenGLRender(GLenum mode /*= GL_RENDER*/, int x /* = 0*/, int y /* = 0*/)
149 {
150  GLint polygon_mode[2];
151  glGetIntegerv(GL_POLYGON_MODE, polygon_mode);
152 
153  // do the render library specific stuff
154  this->OpenGLDeviceRender(mode, x, y);
155 
156  glPolygonMode(GL_FRONT_AND_BACK, polygon_mode[0]);
157 }
158 
159 void TYOpenGLRenderer::OpenGLDeviceRender(GLenum mode /*= GL_RENDER*/, int x /* = 0*/, int y /* = 0*/)
160 {
161  if (_pActiveCamera)
162  {
164  }
165 
166  // set matrix mode for actors
167  glMatrixMode(GL_MODELVIEW);
168 
169  this->OpenGLUpdateGeometry(mode, x, y);
170 
171  // clean up the model view matrix set up by the camera
172  glMatrixMode(GL_MODELVIEW);
173  glPopMatrix();
174 }
175 
176 int TYOpenGLRenderer::OpenGLUpdateGeometry(GLenum mode /*= GL_RENDER*/, int x /* = 0*/, int y /* = 0*/)
177 {
178  // Gestion des lumieres
179  if (_pActiveCamera)
180  {
181  // place la lumiere 0 sur la camera
182  double xc, yc, zc;
183  _pActiveCamera->getPosition(xc, yc, zc);
184  _tabLights[0]->setPosition(OPoint3D(xc, yc, zc));
185  }
186  for (int il = 0; il < _tabLights.size(); il++)
187  {
188  _tabLights[il]->render();
189  }
190 
191  // Materials
192  float colorSpec[] = {0.0, 0.0, 0.0, 1.0};
193  float colorShine[] = {127.0};
194  glMaterialfv(GL_FRONT, GL_SPECULAR, colorSpec);
195  glMaterialfv(GL_FRONT, GL_SHININESS, colorShine);
196  glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
197  glEnable(GL_COLOR_MATERIAL);
198 
199  // Antialiasing
200  glEnable(GL_LINE_SMOOTH);
201  glEnable(GL_BLEND);
202  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
203  glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
204 
205  glEnable(GL_NORMALIZE);
206  glEnable(GL_DEPTH_TEST);
207  glDepthRange(0, 1);
208 
209  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
210 
211  if (mode == GL_SELECT)
212  {
213  GLint viewport[4];
214  GLfloat proj[16];
215 
216  glGetFloatv(GL_PROJECTION_MATRIX, proj);
217 
218  glGetIntegerv(GL_VIEWPORT, viewport);
219 
220  glMatrixMode(GL_PROJECTION);
221  glPushMatrix();
222  glLoadIdentity();
223  gluPickMatrix((GLdouble)x, (GLdouble)y, 5, 5, viewport);
224  glMultMatrixf(proj);
225 
226  glMatrixMode(GL_MODELVIEW);
227  glPushMatrix();
228  drawElement(GL_SELECT);
229  glPopMatrix();
230  glMatrixMode(GL_PROJECTION);
231  glPopMatrix();
232  }
233  else
234  {
235  // CLM-NT35: Init la liste avant d'ajouter les elements
236  // display overlay text for selected elements (localized)
237  glNewList(_displayListOverlay, GL_COMPILE);
238  std::vector<TYElement*>::iterator iterElements;
239  for (iterElements = _tabSelectedElements.begin(); iterElements < _tabSelectedElements.end();
240  iterElements++)
241  {
242  TYGeometryNode* pGeoNode = TYGeometryNode::GetGeoNode(*iterElements);
244  // For elements without GeoNode like TYPointControl, display in GL_COMPILE mode the correponding
245  // graphic object
246  if (!pGeoNode)
247  {
248  if (*iterElements != nullptr)
249  {
250  LPTYElementGraphic pTYElementGraphic = (*iterElements)->getGraphicObject();
251  if (pTYElementGraphic != nullptr)
252  {
253  pTYElementGraphic->display(_pModelerElement, GL_COMPILE);
254  }
255  }
256  }
257  updateDisplayListOverlay(pGeoNode, pRootGeometryNode);
258  }
259  glEndList();
260  }
261 
262  glMatrixMode(GL_MODELVIEW);
263  glPushMatrix();
264  if (_displayList) // az++
265  {
266  glCallList(_displayList);
267  }
268  if (_displayListOverlay) // az++
269  {
270  glCallList(_displayListOverlay); // az++
271  }
272  glPopMatrix();
273 
274  // render the TYAtcor2D list
275  std::vector<OGLElement*>::iterator iter;
276  for (iter = _tabOGLElement.begin(); iter < _tabOGLElement.end(); iter++)
277  {
278  glPushMatrix();
279  (*iter)->render();
280  glPopMatrix();
281  }
282 
283  return 0;
284 }
285 
287 {
288  LPTYElementGraphic pGraphObj = NULL;
289  if (_pElement)
290  {
291  pGraphObj = _pElement->getGraphicObject();
292 
293  if (dynamic_cast<TYSiteNode*>(_pElement) != nullptr)
294  {
295  TYElement* pParent = _pElement->getParent();
296  if (pParent && pParent->isA("TYProjet"))
297  {
298  pGraphObj = pParent->getGraphicObject();
299  }
300  }
301 
302  glRotatef(-90.0, 1.0, 0.0, 0.0);
303  if (pGraphObj)
304  {
305  pGraphObj->display(_pModelerElement, mode);
306  }
307  }
308 }
309 
311 {
312  bool _bFinded = false;
313  std::vector<OGLElement*>::iterator iter;
314  for (iter = _tabOGLElement.begin(); !_bFinded && (iter < _tabOGLElement.end()); iter++)
315  {
316  if ((OGLElement*)(*iter) == pOGLElement)
317  {
318  _bFinded = true;
319  }
320  }
321  if (!_bFinded)
322  {
323  _tabOGLElement.push_back(pOGLElement);
324  }
325 }
326 
328 {
329  _tabLights.push_back(pOGLElement);
330 }
331 
333 {
334  _tabLights.clear();
335 }
336 
337 std::vector<OGLLightElement*> TYOpenGLRenderer::getLights()
338 {
339  return _tabLights;
340 }
341 
343 {
344  std::vector<OGLElement*>::iterator iter;
345  bool _bFinded = false;
346  for (iter = _tabOGLElement.begin(); !_bFinded && (iter < _tabOGLElement.end()); iter++)
347  {
348  if ((OGLElement*)(*iter) == pOGLElement)
349  {
350  _bFinded = true;
351  _tabOGLElement.erase(iter);
352  break;
353  }
354  }
355 }
356 
358 {
359  bool _bFinded = false;
360  std::vector<TYElement*>::iterator iter;
361  for (iter = _tabSelectedElements.begin(); !_bFinded && (iter < _tabSelectedElements.end()); iter++)
362  {
363  if ((TYElement*)(*iter) == pElement)
364  {
365  _bFinded = true;
366  }
367  }
368  if (!_bFinded)
369  {
370  _tabSelectedElements.push_back(pElement);
371  }
372 }
373 
375 {
376  std::vector<TYElement*>::iterator iter;
377  bool _bFinded = false;
378  for (iter = _tabSelectedElements.begin(); !_bFinded && (iter < _tabSelectedElements.end()); iter++)
379  {
380  if ((TYElement*)(*iter) == pElement)
381  {
382  _bFinded = true;
383  _tabSelectedElements.erase(iter);
384  break;
385  }
386  }
387 }
388 
390 {
391  _tabSelectedElements.clear();
392 }
393 
395 {
396  _pModelerElement = pModelerElement;
397 }
398 
400 {
401  return _pModelerElement;
402 }
bool GetADisplayList(GLuint &displayList)
Realise le rendu VTK et le rendu OpenGL (fichier header)
The box class.
Definition: 3d.h:1294
void lookAt()
Definition: OGLCamera.cpp:80
void getPosition(double &x, double &y, double &z)
Definition: OGLCamera.cpp:861
void setSize(int w, int h)
Definition: OGLCamera.cpp:74
The 3D point class.
Definition: 3d.h:487
bool isA(const char *className) const
Definition: TYElement.cpp:65
T * getRealPointer()
Definition: smartptr.h:291
static OBox _globalBoundingBox
virtual void display(TYElement *pModelerElement=nullptr, GLenum mode=GL_RENDER)
TYElement * getParent() const
Definition: TYElement.h:699
classe graphique pour un GeometryNode
void displayPushingParentMatrix(TYElement *pModelerElement, GLenum mode, TYGeometryNode *pDansCeRepere)
TYGeometryNode * GetGeoNodeParent() const
static TYGeometryNode * GetGeoNode(TYElement *pElement)
std::vector< TYElement * > _tabSelectedElements
Elements selectionnes.
void updateDisplayList(void)
void removeSelectedElement(TYElement *pElement)
void addLight(OGLLightElement *pOGLElementLight)
void updateDisplayListOverlay(TYGeometryNode *pElement, TYGeometryNode *pDansCeRepere)
void addOGLElement(OGLElement *pOGLElement)
void removeOGLElement(OGLElement *pOGLElement)
TYElement * getModelerElement()
std::vector< OGLLightElement * > _tabLights
Liste des lumieres dans la scene 3D.
void OpenGLRender(GLenum mode=GL_RENDER, int x=0, int y=0)
GLuint _displayList
Display liste globale.
int OpenGLUpdateGeometry(GLenum mode=GL_RENDER, int x=0, int y=0)
void OpenGLDeviceRender(GLenum mode=GL_RENDER, int x=0, int y=0)
OGLCamera * _pActiveCamera
std::vector< OGLElement * > _tabOGLElement
double * _pBackgroundColor
std::vector< OGLLightElement * > getLights()
GLuint _displayListOverlay
Display liste pour les deplacements.
void setModelerElement(TYElement *pElement)
void init(int width, int height)
void drawElement(GLenum mode=GL_RENDER)
void addSelectedElement(TYElement *pElement)
TYElement * _pElement
Element a representer.
TYElement * _pModelerElement
0 --> render all, 1 --> render 2D, 2 --> render 3D