ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4OpenGLImmediateQtViewer.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4OpenGLImmediateQtViewer.cc
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 //
28 //
29 // Class G4OpenGLImmediateQtViewer : a class derived from G4OpenGLQtViewer and
30 // G4OpenGLImmediateViewer.
31 
32 #ifdef G4VIS_BUILD_OPENGLQT_DRIVER
33 
36 
37 #include "G4ios.hh"
38 #ifdef G4MULTITHREADED
39 #include "G4Threading.hh"
40 #endif
41 #include <qapplication.h>
42 #include <qtabwidget.h>
43 
44 #ifdef G4OPENGL_VERSION_2
45 #include <qglshaderprogram.h>
46 #endif
47 
48 
49 G4OpenGLImmediateQtViewer::G4OpenGLImmediateQtViewer
50 (G4OpenGLImmediateSceneHandler& sceneHandler,
51  const G4String& name):
52  G4VViewer (sceneHandler, sceneHandler.IncrementViewCount (), name),
53  G4OpenGLViewer (sceneHandler),
54  G4OpenGLQtViewer (sceneHandler),
55  G4OpenGLImmediateViewer (sceneHandler)
56 {
57  fQGLWidgetInitialiseCompleted = false;
58 
59  setFocusPolicy(Qt::StrongFocus); // enable keybord events
60  fHasToRepaint = false;
61  fPaintEventLock = false;
62 
63  // Create a new drawer
64  // register the QtDrawer to the OpenGLViewer
65 #ifdef G4OPENGL_VERSION_2
66  setVboDrawer(new G4OpenGLVboDrawer(this,"OGL-VBO"));
67 #endif
68 
69  fUpdateGLLock = false;
70 
71  if (fViewId < 0) return; // In case error in base class instantiation.
72 }
73 
74 G4OpenGLImmediateQtViewer::~G4OpenGLImmediateQtViewer() {
75  makeCurrent();
76 }
77 
78 void G4OpenGLImmediateQtViewer::Initialise() {
79  makeCurrent();
80 
81  fQGLWidgetInitialiseCompleted = false;
82  CreateMainWindow (this,QString(GetName()));
83 
84  glDrawBuffer (GL_BACK);
85 
86  // set the good tab active
87  if (QGLWidget::parentWidget()) {
88  QTabWidget *parentTab = dynamic_cast<QTabWidget*> (QGLWidget::parentWidget()->parent()) ;
89  if (parentTab) {
90  parentTab->setCurrentIndex(parentTab->count()-1);
91  }
92  }
93 
94  fQGLWidgetInitialiseCompleted = true;
95 }
96 
97 void G4OpenGLImmediateQtViewer::initializeGL () {
98 
99 #ifndef G4OPENGL_VERSION_2
100  InitializeGLView ();
101 #else
102  QGLShaderProgram *aQGLShaderProgram = new QGLShaderProgram (context());
103  fShaderProgram = aQGLShaderProgram->programId ();
104 
105  aQGLShaderProgram->addShaderFromSourceCode(QGLShader::Vertex,
106  fVboDrawer->getVertexShaderSrc());
107 
108  aQGLShaderProgram->addShaderFromSourceCode(QGLShader::Fragment,
109  fVboDrawer->getFragmentShaderSrc());
110 
111  aQGLShaderProgram->link();
112  aQGLShaderProgram->bind();
113 
114  fVertexPositionAttribute = glGetAttribLocation(fShaderProgram, "aVertexPosition");
115  fcMatrixUniform = glGetUniformLocation(fShaderProgram, "uCMatrix");
116  fpMatrixUniform = glGetUniformLocation(fShaderProgram, "uPMatrix");
117  ftMatrixUniform = glGetUniformLocation(fShaderProgram, "uTMatrix");
118  fmvMatrixUniform = glGetUniformLocation(fShaderProgram, "uMVMatrix");
119 
120  // Load identity at beginning
121  float identity[16] = {
122  1.0f, 0, 0, 0,
123  0, 1.0f, 0, 0,
124  0, 0, 1.0f, 0,
125  0, 0, 0, 1.0f
126  };
127  glUniformMatrix4fv (fcMatrixUniform, 1, 0, identity);
128  glUniformMatrix4fv (fpMatrixUniform, 1, 0, identity);
129  glUniformMatrix4fv (ftMatrixUniform, 1, 0, identity);
130  glUniformMatrix4fv(fmvMatrixUniform, 1, 0, identity);
131 
132  glUseProgram(fShaderProgram);
133 
134  setInitialized(); // Should be removed when fuse Wt and Qt
135 
136 #endif
137 
138  // If a double buffer context has been forced upon us, ignore the
139  // back buffer for this OpenGLImmediate view.
140  // glDrawBuffer (GL_FRONT); // FIXME : Ne marche pas avec cette ligne, mais affiche le run correctement...
141 
142  if (fSceneHandler.GetScene() == 0) {
143  fHasToRepaint =false;
144  } else {
145  fHasToRepaint =true;
146  }
147 
148  // Set the component visible
149 
150  // and update it immediatly before wait for SessionStart() (batch mode)
151 // QCoreApplication::sendPostedEvents () ;
152 }
153 
154 
155 void G4OpenGLImmediateQtViewer::DrawView() {
156 #ifdef G4MULTITHREADED
158  updateQWidget();
159  }
160 #else
161  updateQWidget();
162 #endif
163 }
164 
165 
166 void G4OpenGLImmediateQtViewer::ComputeView () {
167 
168  makeCurrent();
169  // If a double buffer context has been forced upon us, ignore the
170  // back buffer for this OpenGLImmediate view.
171  // glDrawBuffer (GL_FRONT);
172 
173  G4ViewParameters::DrawingStyle dstyle = GetViewParameters().GetDrawingStyle();
174 
175  if(dstyle!=G4ViewParameters::hlr &&
176  haloing_enabled) {
177 
178  HaloingFirstPass ();
179  NeedKernelVisit ();
180  ProcessView ();
181  FinishView();
182  HaloingSecondPass ();
183 
184  }
185 
186  NeedKernelVisit (); // Always need to visit G4 kernel.
187  ProcessView ();
188 
189  if (isRecording()) {
190  savePPMToTemp();
191  }
192 
193  fHasToRepaint = true;
194 }
195 
199 void G4OpenGLImmediateQtViewer::resizeGL(
200  int aWidth
201 ,int aHeight)
202 {
203  if ((aWidth > 0) && (aHeight > 0)) {
204  ResizeWindow(aWidth,aHeight);
205  fHasToRepaint = sizeHasChanged();
206  }
207 }
208 
209 
210 void G4OpenGLImmediateQtViewer::paintGL()
211 {
212  updateToolbarAndMouseContextMenu();
213 
214  if (fPaintEventLock) {
215 // return ;
216  }
217  if (!fQGLWidgetInitialiseCompleted) {
218  fPaintEventLock = false;
219  return;
220  }
221  if ((getWinWidth() == 0) && (getWinHeight() == 0)) {
222  return;
223  }
224 
225  // DO NOT RESIZE IF SIZE HAS NOT CHANGE
226  if ( !fHasToRepaint) {
227  // L. Garnier : Trap to get the size with mac OSX 10.6 and Qt 4.6(devel)
228  // Tested on Qt4.5 on mac, 4.4 on windows, 4.5 on unbuntu
229  int sw = 0;
230  int sh = 0;
231  if (!isMaximized() && !isFullScreen()) {
232  sw = normalGeometry().width();
233  sh = normalGeometry().height();
234  } else {
235  sw = frameGeometry().width();
236  sh = frameGeometry().height();
237  }
238  if ((getWinWidth() == (unsigned int)sw) &&(getWinHeight() == (unsigned int)sh)) {
239  return;
240 
241  } else if ((sw == 0) && (sh == 0)) { // NOT A TOP LEVEL WIDGET
242  if (((getWinWidth() == (unsigned int)width())) &&(getWinHeight() == (unsigned int) height())) {
243  return;
244  }
245  }
246  }
247 
248  SetView();
249 
250  ClearView (); //ok, put the background correct
251  ComputeView();
252 
253  fHasToRepaint = false; // could be set to false by ComputeView
254 
255  fPaintEventLock = false;
256 }
257 
258 void G4OpenGLImmediateQtViewer::mousePressEvent(QMouseEvent *event)
259 {
260  G4MousePressEvent(event);
261 }
262 
263 void G4OpenGLImmediateQtViewer::keyPressEvent (QKeyEvent * event)
264 {
265  G4keyPressEvent(event);
266 }
267 
268 void G4OpenGLImmediateQtViewer::keyReleaseEvent (QKeyEvent * event)
269 {
270  G4keyReleaseEvent(event);
271 }
272 
273 void G4OpenGLImmediateQtViewer::wheelEvent (QWheelEvent * event)
274 {
275  G4wheelEvent(event);
276 }
277 
278 void G4OpenGLImmediateQtViewer::showEvent (QShowEvent *)
279 {
280  if (fQGLWidgetInitialiseCompleted) {
281  fHasToRepaint = true;
282  }
283 }
284 
285 
290 void G4OpenGLImmediateQtViewer::mouseDoubleClickEvent(QMouseEvent *)
291 {
292  G4MouseDoubleClickEvent();
293 }
294 
295 void G4OpenGLImmediateQtViewer::mouseReleaseEvent(QMouseEvent *event)
296 {
297  G4MouseReleaseEvent(event);
298 }
299 
300 void G4OpenGLImmediateQtViewer::mouseMoveEvent(QMouseEvent *event)
301 {
302  G4MouseMoveEvent(event);
303 }
304 
305 
306 void G4OpenGLImmediateQtViewer::contextMenuEvent(QContextMenuEvent *e)
307 {
308  G4manageContextMenuEvent(e);
309 }
310 
311 void G4OpenGLImmediateQtViewer::paintEvent(QPaintEvent *) {
312  if (! fQGLWidgetInitialiseCompleted) {
313  return;
314  }
315  // Force a repaint next time if the FRAMEBUFFER is not READY
316  fHasToRepaint = isFramebufferReady();
317  if ( fHasToRepaint) {
318  updateGL();
319  }
320 }
321 
322 
323 void G4OpenGLImmediateQtViewer::updateQWidget() {
324  if (fUpdateGLLock) {
325  return;
326  }
327 
328  if (! isCurrentWidget()){
329  return;
330  }
331 
332  fUpdateGLLock = true;
333  fHasToRepaint= true;
334  repaint();
335  updateViewerPropertiesTableWidget();
336  updateSceneTreeWidget();
337  fUpdateGLLock= false;
338 }
339 
340 
341 void G4OpenGLImmediateQtViewer::ShowView (
342 )
343 
344 
345 {
346  fHasToRepaint = true;
347  activateWindow();
348 }
349 #endif