Geant4  10.03
G4OpenGLImmediateWtViewer.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: G4OpenGLImmediateWtViewer.cc 85263 2014-10-27 08:58:31Z gcosmo $
28 //
29 //
30 // Class G4OpenGLImmediateWtViewer : a class derived from G4OpenGLWtViewer and
31 // G4OpenGLImmediateViewer.
32 
33 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
34 
37 
38 #include "G4ios.hh"
39 #define G4DEBUG_VIS_OGL 1
40 
41 G4OpenGLImmediateWtViewer::G4OpenGLImmediateWtViewer
42 (G4OpenGLImmediateSceneHandler& sceneHandler,
43  Wt::WContainerWidget* aParent,
44  const G4String& name):
45  G4VViewer (sceneHandler, sceneHandler.IncrementViewCount (), name),
46  G4OpenGLViewer (sceneHandler),
47  G4OpenGLWtViewer (sceneHandler),
48  G4OpenGLImmediateViewer (sceneHandler),
49  Wt::WGLWidget(aParent)
50 
51 {
52 // Create a new drawer
53  // register the WtDrawer to the OpenGLViewer
54  setVboDrawer(new G4OpenGLVboDrawer(this,"OGL-ES"));
55 
56  // Add the GL Widget to its parent
57  aParent->addWidget(this);
58 
59  fHasToRepaint = false;
60  fIsRepainting = false;
61 
62 #ifdef G4DEBUG_VIS_OGL
63  printf("G4OpenGLImmediateWtViewer INIT\n");
64 #endif
65 
66 
67  if (fViewId < 0) return; // In case error in base class instantiation.
68 }
69 
70 G4OpenGLImmediateWtViewer::~G4OpenGLImmediateWtViewer() {
71 }
72 
73 void G4OpenGLImmediateWtViewer::Initialise() {
74 #ifdef G4DEBUG_VIS_OGL
75  printf("G4OpenGLImmediateWtViewer::Initialise \n");
76 #endif
77  fReadyToPaint = false;
78  CreateMainWindow (this,Wt::WString(fName));
79  CreateFontLists ();
80 
81  fReadyToPaint = true;
82 }
83 
84 void G4OpenGLImmediateWtViewer::initializeGL () {
85 
86  InitializeGLView ();
87 
88  // If a double buffer context has been forced upon us, ignore the
89  // back buffer for this OpenGLImmediate view.
90  // glDrawBuffer (GL_FRONT); // FIXME : Ne marche pas avec cette ligne, mais affiche le run correctement...
91 
92  if (fSceneHandler.GetScene() == 0) {
93  fHasToRepaint =false;
94  } else {
95  fHasToRepaint =true;
96  }
97 
98  // In order to know where to look at, calculate the centerpoint of the
99  // scene
100  double cx, cy, cz;
101  cx = cy = cz = 0.;
102 
103  // Transform the world so that we look at the centerpoint of the scene
104  Wt::WMatrix4x4 worldTransform;
105  worldTransform.lookAt(
106  cx, cy, cz + 10, // camera position
107  cx, cy, cz, // looking at
108  0, 1, 0); // 'up' vector
109 
110  // We want to be able to change the camera position client-side. In
111  // order to do so, the world transformation matrix must be stored in
112  // a matrix that can be manipulated from JavaScript.
113  jsMatrix_ = createJavaScriptMatrix4();
114  setJavaScriptMatrix4(jsMatrix_, worldTransform);
115 
116  // This installs a client-side mouse handler that modifies the
117  // world transformation matrix. Like WMatrix4x4::lookAt, this works
118  // by specifying a center point and an up direction; mouse movements
119  // will allow the camera to be moved around the center point.
120  setClientSideLookAtHandler(jsMatrix_, // the name of the JS matrix
121  cx, cy, cz, // the center point
122  0, 1, 0, // the up direction
123  0.005, 0.005); // 'speed' factors
124  // Alternative: this installs a client-side mouse handler that allows
125  // to 'walk' around: go forward, backward, turn left, turn right, ...
126  //setClientSideWalkHandler(jsMatrix_, 0.05, 0.005);
127 
128 
129  // Set the clear color to a transparant background
130  glClearColor(0, 0, 0, 0);
131 
132  // Reset Z-buffer, enable Z-buffering
133  glClearDepth(1);
134  glEnable(DEPTH_TEST);
135  glDepthFunc(LEQUAL);
136 #ifdef G4DEBUG_VIS_OGL
137  printf("G4OpenGLWtViewer initializeGL END\n");
138 #endif
139 
140  // Set the component visible
141  show() ;
142 
143 }
144 
145 void G4OpenGLImmediateWtViewer::DrawView() {
146  updateWWidget();
147 }
148 
149 
150 void G4OpenGLImmediateWtViewer::ComputeView () {
151 
152 #ifdef G4DEBUG_VIS_OGL
153  printf("G4OpenGLWtViewer::ComputeView %d %d VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n",getWinWidth(), getWinHeight());
154 #endif
155 
156  // If a double buffer context has been forced upon us, ignore the
157  // back buffer for this OpenGL view.
158  // glDrawBuffer (GL_FRONT);
159 
160  G4ViewParameters::DrawingStyle dstyle = GetViewParameters().GetDrawingStyle();
161 
162  if(dstyle!=G4ViewParameters::hlr &&
163  haloing_enabled) {
164 
165  HaloingFirstPass ();
166  NeedKernelVisit ();
167  ProcessView ();
168  FinishView();
169 #ifdef G4DEBUG_VIS_OGL
170  printf("G4OpenGLWtViewer::ComputeView First ProcessView ok\n");
171 #endif
172  HaloingSecondPass ();
173 
174  }
175 
176  NeedKernelVisit (); // Always need to visit G4 kernel.
177  ProcessView ();
178 
179 /* FIXME
180  if (isRecording()) {
181  savePPMToTemp();
182  }
183  */
184 
185 #ifdef G4DEBUG_VIS_OGL
186  printf("G4OpenGLWtViewer::ComputeView %d %d ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n",getWinWidth(), getWinHeight());
187 #endif
188  fHasToRepaint = true;
189 }
190 
194 void G4OpenGLImmediateWtViewer::resizeGL(
195  int width
196 ,int height)
197 {
198 #ifdef G4DEBUG_VIS_OGL
199  printf("G4OpenGLImmediateWtViewer resizeGL %d %d\n",width,height);
200 #endif
201  G4OpenGLWtViewer::resizeGL(width,height);
202 
203  // Set the viewport size.
204  glViewport(0, 0, width, height);
205 
206  // Set projection matrix to some fixed values
207 /* Wt::WMatrix4x4 proj;
208  proj.perspective(45, ((double)width)/height, 1, 40);
209  glUniformMatrix4(fpMatrixUniform, proj);
210 */
211  SetView();
212  // updateWWidget();
213 }
214 
215 
216 void G4OpenGLImmediateWtViewer::paintGL() {
217 
218  if (fIsRepainting) {
219  // return ;
220  }
221  if (!fReadyToPaint) {
222  fReadyToPaint= true;
223  return;
224  }
225  if ((getWinWidth() == 0) && (getWinHeight() == 0)) {
226  return;
227  }
228 
229  // DO NOT RESIZE IF SIZE HAS NOT CHANGE
230  if ( !fHasToRepaint) {
231  double sw = 0;
232  double sh = 0;
233  // if (!isMaximized() && !isFullScreen()) {
234  sw = width().value();
235  sh = height().value();
236  // } else {
237  // sw = frameGeometry().width();
238  // sh = frameGeometry().height();
239  // }
240  if ((getWinWidth() == sw) &&(getWinHeight() == sh)) {
241  return;
242 
243  } else if ((sw == 0) && (sh == 0)) { // NOT A TOP LEVEL WIDGET
244  if (((getWinWidth() == width().value())) &&(getWinHeight() == height().value())) {
245  return;
246  }
247  }
248  }
249 #ifdef G4DEBUG_VIS_OGL
250  printf("G4OpenGLImmediateWtViewer::paintGL VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV ready %d\n",fReadyToPaint);
251 #endif
252 
253  SetView();
254 
255  ClearView(); //ok, put the background correct
256 
257  // SHOULD BE REMOVED
258 
259  // Configure the shader: set the uniforms
260  // Uniforms are 'configurable constants' in a shader: they are
261  // identical for every point that has to be drawn.
262  // Set the camera transformation to the value of a client-side JS matrix
263  glUniformMatrix4(fcMatrixUniform, jsMatrix_);
264  // Often, a model matrix is used to move the model around. We're happy
265  // with the location of the model, so we leave it as the unit matrix
266  Wt::WMatrix4x4 modelMatrix;
267  glUniformMatrix4(fmvMatrixUniform, modelMatrix);
268  // The next one is a bit complicated. In desktop OpenGL, a shader
269  // has the gl_NormalMatrix matrix available in the shader language,
270  // a matrix that is used to transform normals to e.g. implement proper
271  // Phong shading (google will help you to find a detailed explanation
272  // of why you need it). It is the transposed inverse of the model view
273  // matrix. Unfortunately, this matrix is not available in WebGL, so if
274  // you want to do phong shading, you must calculate it yourself.
275  // Wt provides methods to calculate the transposed inverse of a matrix,
276  // when client-side JS matrices are involved. Here, we inverse-transpose
277  // the product of the client-side camera matrix and the model matrix.
278  glUniformMatrix4(fnMatrixUniform, (jsMatrix_ * modelMatrix).inverted().transposed());
279 
280  // Create a new Buffer
281  Buffer objBuffer_2 = glCreateBuffer(); //glGenBuffers(1,&objBuffer_2)
282 
283  // Bind this buffer
284  glBindBuffer(GL_ARRAY_BUFFER, objBuffer_2);
285  // SHOULD BE REMOVED END
286 
287 
288  ComputeView();
289 
290  fHasToRepaint = false; // could be set to false by ComputeView
291 
292 #ifdef G4DEBUG_VIS_OGL
293  printf("G4OpenGLImmediateQtViewer::paintGL ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ready %d\n\n\n",fReadyToPaint);
294 #endif
295  fIsRepainting = false;
296 
297 }
298 
299 
300 void G4OpenGLImmediateWtViewer::mousePressEvent(Wt::WMouseEvent *event)
301 {
302  // boutons et position
303  G4MousePressEvent(event);
304 }
305 
306 void G4OpenGLImmediateWtViewer::keyPressEvent (Wt::WKeyEvent *event)
307 {
308  G4keyPressEvent(event);
309 }
310 
323 void G4OpenGLImmediateWtViewer::mouseDoubleClickEvent(Wt::WMouseEvent *)
324 {
325  G4MouseDoubleClickEvent();
326 }
327 
328 
329 void G4OpenGLImmediateWtViewer::mouseReleaseEvent(Wt::WMouseEvent )
330 {
331  G4MouseReleaseEvent();
332 }
333 
334 
335 void G4OpenGLImmediateWtViewer::mouseMoveEvent(Wt::WMouseEvent *event)
336 {
337  G4MouseMoveEvent(event);
338 }
339 
340 
348 void G4OpenGLImmediateWtViewer::paintEvent(Wt::WPaintDevice * /* painter */) {
349  if ( fHasToRepaint) {
350  updateGL();
351  }
352 }
353 
354 
355 
356 void G4OpenGLImmediateWtViewer::FinishView()
357 {
358 #ifdef G4DEBUG_VIS_OGL
359  printf("G4OpenGLWtViewer::FinishView() \n");
360 #endif
361  flush ();
362 
363  // L. Garnier 10/2009 : Not necessary and cause problems on mac OS X 10.6
364  // fWindow->swapBuffers ();
365 }
366 
367 
368 
369 void G4OpenGLImmediateWtViewer::popMatrix() {
370 }
371 
372 void G4OpenGLImmediateWtViewer::pushMatrix() {
373 }
374 
375 void G4OpenGLImmediateWtViewer::multMatrixd(const GLdouble* /* m */) {
376  // mMatrix = mMatrix * m;
377 }
378 
379 void G4OpenGLImmediateWtViewer::loadIdentity() {
380  mMatrix.setToIdentity ();
381 }
382 
383 
384 void G4OpenGLImmediateWtViewer::setMatrixUniforms() {
385  /*
386  UniformLocation pUniform = getUniformLocation(shaderProgram, "uPMatrix");
387  uniformMatrix4fv(pUniform, false, new Float32Array(perspectiveMatrix.flatten()));
388 
389  UniformLocation mvUniform = getUniformLocation(shaderProgram, "uMVMatrix");
390  uniformMatrix4fv(mvUniform, false, new Float32Array(mvMatrix.flatten()));
391  */
392 }
393 
394 
395 void G4OpenGLImmediateWtViewer::ShowView (
396 )
397 {
400 #ifdef G4DEBUG_VIS_OGL
401  printf("G4OpenGLImmediateWtViewer ShowView\n");
402 #endif
403  repaintSlot();
404  // activateWindow();
405 }
406 
407 
408 void G4OpenGLImmediateWtViewer::updateWWidget() {
409 #ifdef G4DEBUG_VIS_OGL
410  printf("G4OpenGLImmediateWtViewer updateWWidget\n");
411 #endif
412  fHasToRepaint= true;
413  // updateGL();
414  repaintGL(PAINT_GL | RESIZE_GL);
415  // paintGL() ;
416  fHasToRepaint= false;
417 #ifdef G4DEBUG_VIS_OGL
418  printf("G4OpenGLImmediateWtViewer updateWWidget END\n");
419 #endif
420 }
421 
422 
423 #endif
G4String fName
Definition: G4AttUtils.hh:55
#define width
const char * name(G4int ptype)