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