Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYRenderWindow.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 
24 #include <qglobal.h>
25 #include <qmatrix4x4.h>
26 #include <qopengldebug.h>
27 #include <qopenglwidget.h>
28 #include <qpoint.h>
29 #include <qsurfaceformat.h>
30 #include <qtextstream.h>
31 #include <qvector3d.h>
32 #include "TYRenderWindow.h"
33 #include "gui/app/TYModelerFrame.h"
36 
37 #define IMG(id) OLocalizator::getPicture("TYRenderWindow", (id))
38 
39 TYRenderWindow::TYRenderWindow(QWidget* parent, const char* name)
40  : QOpenGLWidget(parent), _cachedViewport(nullptr, QRect())
41 {
42  _viewportNeedsUpdate = true;
43  // The cached viewport is initially invalid but getViewport will eventually set it to a valid value
44  setObjectName(name);
46 }
47 
49 {
50  _initNeeded = true;
51  _showInfos = false;
52 
53  // Ensure that the widget's opengl context has debugging enabled
54  QSurfaceFormat debugFormat(format());
55  debugFormat.setMajorVersion(4);
56  debugFormat.setMinorVersion(3);
57  debugFormat.setProfile(QSurfaceFormat::CompatibilityProfile);
58  debugFormat.setOptions(QSurfaceFormat::DebugContext | QSurfaceFormat::DeprecatedFunctions);
59  setFormat(debugFormat);
60 
61  _pGLLogger = new QOpenGLDebugLogger(context());
62  connect(_pGLLogger, &QOpenGLDebugLogger::messageLogged, this, &TYRenderWindow::handleLoggedMessage);
63 
65 
66  // Label info
67  OColor oColor;
70  oColor.r = oColor.g = oColor.b = 0.5;
72  _pOGLTextInfoLabel->setFont(IMG("id_font"));
76 
77  // just enable one double buffering
78  // by default, we use the one of vtk
79  /*#ifdef QT_DOUBLEBUFFER
80  // use the double buffering of QT
81  _pRenderWindow->SetSwapBuffers(0);
82  #else
83  // use the double buffering of vtk
84  setAutoBufferSwap(false);
85  #endif*/
86 
87  // Gestion du focus
88  setFocusProxy(parentWidget());
89 }
90 
92 {
93  makeCurrent();
94  delete _pGLLogger;
95  delete _pOGLTextInfoLabel;
96  //_pInfoLabelMapper->Delete();
97  delete _pRenderer; // az++
98  doneCurrent();
99 }
100 
102 {
103  return _pRenderer;
104 }
105 
107 {
108  _showInfos = state;
110 
111  // if (!_showInfos) {
112  // _pInfoLabelMapper->SetInput("");
113  // }
114 
115  update();
116 }
117 
119 {
120  _pGLLogger->initialize();
121 
122  if (!qEnvironmentVariableIsSet("TYMPAN_DEBUG_OPENGL_ALL_MESSAGES"))
123  {
124  // Only show errors by default
125  _pGLLogger->disableMessages();
126  _pGLLogger->enableMessages(QOpenGLDebugMessage::AnySource, QOpenGLDebugMessage::ErrorType);
127  }
128 
129  if (qEnvironmentVariableIsSet("TYMPAN_DEBUG_OPENGL_SYNC"))
130  {
131  _pGLLogger->startLogging(QOpenGLDebugLogger::SynchronousLogging);
132  }
133  else
134  {
135  _pGLLogger->startLogging(QOpenGLDebugLogger::AsynchronousLogging);
136  }
138 }
139 
140 void TYRenderWindow::resizeGL(int w, int h)
141 {
142  _viewportNeedsUpdate = true;
143  _pActiveCamera->setSize(width() * devicePixelRatioF(), height() * devicePixelRatioF());
144 }
145 
147 {
148  _pRenderer->init();
149 
150  clock_t timeBefore = clock();
151  TYModelerFrame* modelerFrame = (TYModelerFrame*)parent();
152  TYRenderContext renderContext = {
155  getViewport(),
156  modelerFrame->getElement(),
157  };
158  _pRenderer->OpenGLRender(renderContext);
159  clock_t timeAfter = clock();
160 
161  double timeElaspedInSec = ((double)(timeAfter - timeBefore)) / (double)CLOCKS_PER_SEC;
162 
163  if (_showInfos)
164  {
165  writeOutputMsg(QString("FPS = %1").arg(1.0 / timeElaspedInSec, 0, 'f', 2));
166  _pOGLTextInfoLabel->setTextToDisplay(QString("FPS = %1").arg(1.0 / timeElaspedInSec, 0, 'f', 2));
167  }
168 }
169 
171 {
173  {
175  _pActiveCamera, QRect(0, 0, width() * devicePixelRatioF(), height() * devicePixelRatioF()));
176  _viewportNeedsUpdate = false;
177  }
178  return _cachedViewport;
179 }
180 
181 QPoint TYRenderWindow::toViewport(QPoint point)
182 {
183  return point * devicePixelRatioF();
184 }
185 
186 static inline const char* printableDebugMessageSource(QOpenGLDebugMessage::Source source)
187 {
188  switch (source)
189  {
190  case QOpenGLDebugMessage::APISource:
191  return "API";
192  case QOpenGLDebugMessage::WindowSystemSource:
193  return "Window system";
194  case QOpenGLDebugMessage::ShaderCompilerSource:
195  return "Shader compiler";
196  case QOpenGLDebugMessage::ThirdPartySource:
197  return "Third party";
198  case QOpenGLDebugMessage::ApplicationSource:
199  return "Application";
200  case QOpenGLDebugMessage::OtherSource:
201  return "Other";
202  case QOpenGLDebugMessage::AnySource:
203  case QOpenGLDebugMessage::InvalidSource:
204  default:
205  return ""; // Shouldn't happen
206  }
207 }
208 
209 static inline const char* printableDebugMessageType(QOpenGLDebugMessage::Type messageType)
210 {
211  switch (messageType)
212  {
213  case QOpenGLDebugMessage::ErrorType:
214  return "Error";
215  case QOpenGLDebugMessage::DeprecatedBehaviorType:
216  return "Deprecated behavior";
217  case QOpenGLDebugMessage::UndefinedBehaviorType:
218  return "Undefined behavior";
219  case QOpenGLDebugMessage::PortabilityType:
220  return "Portability";
221  case QOpenGLDebugMessage::PerformanceType:
222  return "Performance";
223  case QOpenGLDebugMessage::MarkerType:
224  return "Marker";
225  case QOpenGLDebugMessage::GroupPushType:
226  return "Group Push";
227  case QOpenGLDebugMessage::GroupPopType:
228  return "Group Pop";
229  case QOpenGLDebugMessage::OtherType:
230  return "Other";
231  case QOpenGLDebugMessage::InvalidType:
232  case QOpenGLDebugMessage::AnyType:
233  default:
234  return ""; // Shouldn't happen
235  }
236 }
237 
238 static inline const char* printableDebugMessageSeverity(QOpenGLDebugMessage::Severity severity)
239 {
240  switch (severity)
241  {
242  case QOpenGLDebugMessage::HighSeverity:
243  return "High";
244  case QOpenGLDebugMessage::MediumSeverity:
245  return "Medium";
246  case QOpenGLDebugMessage::LowSeverity:
247  return "Low";
248  case QOpenGLDebugMessage::NotificationSeverity:
249  return "Notification";
250  case QOpenGLDebugMessage::InvalidSeverity:
251  case QOpenGLDebugMessage::AnySeverity:
252  default:
253  return ""; // Shouldn't happen
254  }
255 }
256 
257 static const char* OGL_DEBUG_MESSAGE_FORMAT = "OpenGL message\n"
258  "==============\n"
259  "Type = %1\n"
260  "Source = %2\n"
261  "Severity = %3\n"
262  "Message = %4\n";
263 
264 void TYRenderWindow::handleLoggedMessage(QOpenGLDebugMessage message)
265 {
266  QString output = QString(OGL_DEBUG_MESSAGE_FORMAT)
267  .arg(printableDebugMessageType(message.type()))
268  .arg(printableDebugMessageSource(message.source()))
269  .arg(printableDebugMessageSeverity(message.severity()))
270  .arg(message.message());
271  QTextStream(stdout) << output << Qt::endl;
272 }
void writeOutputMsg(QString msg)
Affiche un message dans la fenetre de sortie.
pour l'application Tympan (fichier header)
Classe generique pour une fenetre de modeleur (fichier header)
Contexte de rendu utilisé par les fonctions d'affichage.
@ Display
The current render is intended to be displayed on screen.
@ Default
The current render pass is for 3D elements.
#define IMG(id)
association du graphic (OpenGL) au modeleur (fichier header)
const char * name
Definition: color.h:31
float b
Definition: color.h:33
float r
Definition: color.h:33
float g
Definition: color.h:33
void setSize(int w, int h)
Definition: OGLCamera.cpp:78
void setVisibility(bool bVisible)
Definition: OGLElement.h:51
void setDisplayPosition(double displayPositionX, double displayPositionY)
Definition: OGLElement.h:46
void setColor(const OColor &oColor)
void setTextToDisplay(const QString &qsText)
void setFont(const QString &qsFontPath)
Generic class for a modeler window.
LPTYElement getElement()
Realise le rendu VTK et le rendu OpenGL.
void addOGLElement(OGLElement *pOGLElement)
void OpenGLRender(TYRenderContext &renderContext, int x=0, int y=0)
void handleLoggedMessage(QOpenGLDebugMessage debugMessage)
virtual void initializeGL()
const TYRenderViewport & getViewport() const
OGLCamera * _pActiveCamera
TYRenderViewport _cachedViewport
virtual void resizeGL(int w, int h)
QPoint toViewport(QPoint point)
QOpenGLDebugLogger * _pGLLogger
TYOpenGLRenderer * _pRenderer
void showInfos(bool state)
TYOpenGLRenderer * getRenderer()
virtual void paintGL()
OGLTextElement * _pOGLTextInfoLabel
TYRenderWindow(QWidget *pParent=0, const char *name=0)