Geant4  10.02.p03
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 94206 2015-11-09 08:11:59Z 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 <qevent.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 
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  resize(fVP.GetWindowSizeHintX(),fVP.GetWindowSizeHintY());
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  fQGLWidgetInitialiseCompleted = false;
80  CreateMainWindow (this,QString(GetName()));
81  CreateFontLists ();
82 
83  fQGLWidgetInitialiseCompleted = true;
84 }
85 
86 void G4OpenGLImmediateQtViewer::initializeGL () {
87 
88 #ifndef G4OPENGL_VERSION_2
89  InitializeGLView ();
90 #else
91  QGLShaderProgram *aQGLShaderProgram = new QGLShaderProgram (context());
92  fShaderProgram = aQGLShaderProgram->programId ();
93 
94  aQGLShaderProgram->addShaderFromSourceCode(QGLShader::Vertex,
95  fVboDrawer->getVertexShaderSrc());
96 
97  aQGLShaderProgram->addShaderFromSourceCode(QGLShader::Fragment,
98  fVboDrawer->getFragmentShaderSrc());
99 
100  aQGLShaderProgram->link();
101  aQGLShaderProgram->bind();
102 
103  fVertexPositionAttribute = glGetAttribLocation(fShaderProgram, "aVertexPosition");
104  fcMatrixUniform = glGetUniformLocation(fShaderProgram, "uCMatrix");
105  fpMatrixUniform = glGetUniformLocation(fShaderProgram, "uPMatrix");
106  ftMatrixUniform = glGetUniformLocation(fShaderProgram, "uTMatrix");
107  fmvMatrixUniform = glGetUniformLocation(fShaderProgram, "uMVMatrix");
108 
109  // Load identity at beginning
110  float identity[16] = {
111  1.0f, 0, 0, 0,
112  0, 1.0f, 0, 0,
113  0, 0, 1.0f, 0,
114  0, 0, 0, 1.0f
115  };
116  glUniformMatrix4fv (fcMatrixUniform, 1, 0, identity);
117  glUniformMatrix4fv (fpMatrixUniform, 1, 0, identity);
118  glUniformMatrix4fv (ftMatrixUniform, 1, 0, identity);
119  glUniformMatrix4fv(fmvMatrixUniform, 1, 0, identity);
120 
121  glUseProgram(fShaderProgram);
122 
123  setInitialized(); // Should be removed when fuse Wt and Qt
124 
125 #endif
126 
127  // If a double buffer context has been forced upon us, ignore the
128  // back buffer for this OpenGLImmediate view.
129  // glDrawBuffer (GL_FRONT); // FIXME : Ne marche pas avec cette ligne, mais affiche le run correctement...
130 
131  if (fSceneHandler.GetScene() == 0) {
132  fHasToRepaint =false;
133  } else {
134  fHasToRepaint =true;
135  }
136 
137  // Set the component visible
138  setVisible(true) ;
139 
140  // and update it immediatly before wait for SessionStart() (batch mode)
141 // QCoreApplication::sendPostedEvents () ;
142 }
143 
144 
145 void G4OpenGLImmediateQtViewer::DrawView() {
146 #ifdef G4MULTITHREADED
148  updateQWidget();
149  }
150 #else
151  updateQWidget();
152 #endif
153 }
154 
155 
156 void G4OpenGLImmediateQtViewer::ComputeView () {
157 
158  makeCurrent();
159  // If a double buffer context has been forced upon us, ignore the
160  // back buffer for this OpenGLImmediate view.
161  // glDrawBuffer (GL_FRONT);
162 
163  G4ViewParameters::DrawingStyle dstyle = GetViewParameters().GetDrawingStyle();
164 
165  if(dstyle!=G4ViewParameters::hlr &&
166  haloing_enabled) {
167 
168  HaloingFirstPass ();
169  NeedKernelVisit ();
170  ProcessView ();
171  FinishView();
172  HaloingSecondPass ();
173 
174  }
175 
176  NeedKernelVisit (); // Always need to visit G4 kernel.
177  ProcessView ();
178 
179  if (isRecording()) {
180  savePPMToTemp();
181  }
182 
183  fHasToRepaint = true;
184 }
185 
189 void G4OpenGLImmediateQtViewer::resizeGL(
190  int aWidth
191 ,int aHeight)
192 {
193  if ((aWidth > 0) && (aHeight > 0)) {
194  ResizeWindow(aWidth,aHeight);
195  fHasToRepaint = sizeHasChanged();
196  }
197 }
198 
199 
200 void G4OpenGLImmediateQtViewer::paintGL()
201 {
202  updateToolbarAndMouseContextMenu();
203 
204  if (fPaintEventLock) {
205 // return ;
206  }
207  if (!fQGLWidgetInitialiseCompleted) {
208  fQGLWidgetInitialiseCompleted= true;
209  return;
210  }
211  if ((getWinWidth() == 0) && (getWinHeight() == 0)) {
212  return;
213  }
214 
215  // DO NOT RESIZE IF SIZE HAS NOT CHANGE
216  if ( !fHasToRepaint) {
217  // L. Garnier : Trap to get the size with mac OSX 10.6 and Qt 4.6(devel)
218  // Tested on Qt4.5 on mac, 4.4 on windows, 4.5 on unbuntu
219  int sw = 0;
220  int sh = 0;
221  if (!isMaximized() && !isFullScreen()) {
222  sw = normalGeometry().width();
223  sh = normalGeometry().height();
224  } else {
225  sw = frameGeometry().width();
226  sh = frameGeometry().height();
227  }
228  if ((getWinWidth() == (unsigned int)sw) &&(getWinHeight() == (unsigned int)sh)) {
229  return;
230 
231  } else if ((sw == 0) && (sh == 0)) { // NOT A TOP LEVEL WIDGET
232  if (((getWinWidth() == (unsigned int)width())) &&(getWinHeight() == (unsigned int) height())) {
233  return;
234  }
235  }
236  }
237 
238  SetView();
239 
240  ClearView (); //ok, put the background correct
241  ComputeView();
242 
243  fHasToRepaint = false; // could be set to false by ComputeView
244 
245  fPaintEventLock = false;
246 }
247 
248 void G4OpenGLImmediateQtViewer::mousePressEvent(QMouseEvent *event)
249 {
250  G4MousePressEvent(event);
251 }
252 
253 void G4OpenGLImmediateQtViewer::keyPressEvent (QKeyEvent * event)
254 {
255  G4keyPressEvent(event);
256 }
257 
258 void G4OpenGLImmediateQtViewer::keyReleaseEvent (QKeyEvent * event)
259 {
260  G4keyReleaseEvent(event);
261 }
262 
263 void G4OpenGLImmediateQtViewer::wheelEvent (QWheelEvent * event)
264 {
265  G4wheelEvent(event);
266 }
267 
268 void G4OpenGLImmediateQtViewer::showEvent (QShowEvent *)
269 {
270  fHasToRepaint = true;
271 }
272 
273 
278 void G4OpenGLImmediateQtViewer::mouseDoubleClickEvent(QMouseEvent *)
279 {
280  G4MouseDoubleClickEvent();
281 }
282 
283 void G4OpenGLImmediateQtViewer::mouseReleaseEvent(QMouseEvent *event)
284 {
285  G4MouseReleaseEvent(event);
286 }
287 
288 void G4OpenGLImmediateQtViewer::mouseMoveEvent(QMouseEvent *event)
289 {
290  G4MouseMoveEvent(event);
291 }
292 
293 
294 void G4OpenGLImmediateQtViewer::contextMenuEvent(QContextMenuEvent *e)
295 {
296  G4manageContextMenuEvent(e);
297 }
298 
299 void G4OpenGLImmediateQtViewer::paintEvent(QPaintEvent *) {
300  if ( fHasToRepaint) {
301  updateGL();
302  }
303 }
304 
305 
306 void G4OpenGLImmediateQtViewer::updateQWidget() {
307  fHasToRepaint= true;
308  updateGL();
309  updateSceneTreeComponentTreeWidgetInfos();
310  repaint();
311  fHasToRepaint= false;
312 }
313 
314 
315 void G4OpenGLImmediateQtViewer::ShowView (
316 )
317 {
320  fHasToRepaint = true;
321  activateWindow();
322 }
323 #endif
G4String name
Definition: TRTMaterials.hh:40
#define width
const G4String & GetName() const
G4int G4GetThreadId()
Definition: G4Threading.cc:134