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