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