source: trunk/source/visualization/OpenGL/src/G4OpenGLViewer.cc @ 946

Last change on this file since 946 was 946, checked in by garnier, 15 years ago

en OGLIX ca marche...

  • Property svn:mime-type set to text/cpp
File size: 21.7 KB
Line 
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: G4OpenGLViewer.cc,v 1.50 2009/03/05 16:36:13 lgarnier Exp $
28// GEANT4 tag $Name:  $
29//
30//
31// Andrew Walkden  27th March 1996
32// OpenGL view - opens window, hard copy, etc.
33
34#ifdef G4VIS_BUILD_OPENGL_DRIVER
35
36#include "G4ios.hh"
37#include "G4OpenGLViewer.hh"
38#include "G4OpenGLSceneHandler.hh"
39#include "G4OpenGLTransform3D.hh"
40#include "G4OpenGL2PSAction.hh"
41
42#include "G4Scene.hh"
43#include "G4VisExtent.hh"
44#include "G4LogicalVolume.hh"
45#include "G4VSolid.hh"
46#include "G4Point3D.hh"
47#include "G4Normal3D.hh"
48#include "G4Plane3D.hh"
49#include "G4AttHolder.hh"
50#include "G4AttCheck.hh"
51
52// GL2PS
53#include "Geant4_gl2ps.h"
54
55#include <sstream>
56
57G4OpenGLViewer::G4OpenGLViewer (G4OpenGLSceneHandler& scene):
58G4VViewer (scene, -1),
59fPrintFilename ("G4OpenGL.eps"),
60fPrintColour (true),
61fVectoredPs (true),
62fPrintSizeX(0),
63fPrintSizeY(0),
64fOpenGLSceneHandler(scene),
65background (G4Colour(0.,0.,0.)),
66transparency_enabled (true),
67antialiasing_enabled (false),
68haloing_enabled (false),
69fStartTime(-DBL_MAX),
70fEndTime(DBL_MAX),
71fFadeFactor(0.),
72fDisplayHeadTime(false),
73fDisplayHeadTimeX(-0.9),
74fDisplayHeadTimeY(-0.9),
75fDisplayHeadTimeSize(24.),
76fDisplayHeadTimeRed(0.),
77fDisplayHeadTimeGreen(1.),
78fDisplayHeadTimeBlue(1.),
79fDisplayLightFront(false),
80fDisplayLightFrontX(0.),
81fDisplayLightFrontY(0.),
82fDisplayLightFrontZ(0.),
83fDisplayLightFrontT(0.),
84fDisplayLightFrontRed(0.),
85fDisplayLightFrontGreen(1.),
86fDisplayLightFrontBlue(0.),
87fPointSize (0)
88{
89  // Make changes to view parameters for OpenGL...
90  fVP.SetAutoRefresh(true);
91  fDefaultVP.SetAutoRefresh(true);
92  fWinSize_x = fVP.GetWindowSizeHintX();
93  fWinSize_y = fVP.GetWindowSizeHintY();
94
95  fGL2PSAction = new G4OpenGL2PSAction();
96
97  //  glClearColor (0.0, 0.0, 0.0, 0.0);
98  //  glClearDepth (1.0);
99  //  glDisable (GL_BLEND);
100  //  glDisable (GL_LINE_SMOOTH);
101  //  glDisable (GL_POLYGON_SMOOTH);
102
103}
104
105G4OpenGLViewer::~G4OpenGLViewer () {}
106
107void G4OpenGLViewer::InitializeGLView ()
108{
109  glClearColor (0.0, 0.0, 0.0, 0.0);
110  glClearDepth (1.0);
111  glDisable (GL_BLEND);
112  glDisable (GL_LINE_SMOOTH);
113  glDisable (GL_POLYGON_SMOOTH);
114
115
116void G4OpenGLViewer::ClearView () {
117  glClearColor (background.GetRed(),
118                background.GetGreen(),
119                background.GetBlue(),
120                1.);
121  glClearDepth (1.0);
122  //Below line does not compile with Mesa includes.
123  //glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
124  glClear (GL_COLOR_BUFFER_BIT);
125  glClear (GL_DEPTH_BUFFER_BIT);
126  glClear (GL_STENCIL_BUFFER_BIT);
127  glFlush ();
128}
129
130
131/**
132 * Set the viewport of the scene
133 * MAXIMUM SIZE is :
134 * GLint dims[2];
135 * glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
136 */
137void G4OpenGLViewer::ResizeGLView()
138{
139  // Check size
140  GLint dims[2];
141  glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
142  if (fWinSize_x > (unsigned)dims[0]) {
143    G4cerr << "Try to resize view greater than max X viewport dimension. Desired size "<<dims[0] <<" is resize to "<<  dims[0] << G4endl;
144    fWinSize_x = dims[0];
145  }
146  if (fWinSize_y > (unsigned)dims[1]) {
147    G4cerr << "Try to resize view greater than max Y viewport dimension. Desired size "<<dims[0] <<" is resize to "<<  dims[1] << G4endl;
148    fWinSize_y = dims[1];
149  }
150  int side = fWinSize_x;
151  if (fWinSize_y < fWinSize_x) side = fWinSize_y;
152  glViewport((fWinSize_x - side) / 2, (fWinSize_y - side) / 2, side, side); 
153}
154
155
156void G4OpenGLViewer::SetView () {
157
158  if (!fSceneHandler.GetScene()) {
159    G4cerr << "G4OpenGLStoredViewer: Creating a Viewer without a scene is not allowed. \nPlease use /vis/scene/create before /vis/open/.... "
160           << G4endl;
161    return;
162  }
163  // Calculates view representation based on extent of object being
164  // viewed and (initial) viewpoint.  (Note: it can change later due
165  // to user interaction via visualization system's GUI.)
166 
167  // Lighting.
168  GLfloat lightPosition [4];
169  lightPosition [0] = fVP.GetActualLightpointDirection().x();
170  lightPosition [1] = fVP.GetActualLightpointDirection().y();
171  lightPosition [2] = fVP.GetActualLightpointDirection().z();
172  lightPosition [3] = 0.;
173  // Light position is "true" light direction, so must come after gluLookAt.
174  GLfloat ambient [] = { 0.2, 0.2, 0.2, 1.};
175  GLfloat diffuse [] = { 0.8, 0.8, 0.8, 1.};
176  glEnable (GL_LIGHT0);
177  glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);
178  glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
179 
180  // Get radius of scene, etc.
181  // Note that this procedure properly takes into account zoom, dolly and pan.
182  const G4Point3D targetPoint
183    = fSceneHandler.GetScene()->GetStandardTargetPoint()
184    + fVP.GetCurrentTargetPoint ();
185  G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
186  if(radius<=0.) radius = 1.;
187  const G4double cameraDistance = fVP.GetCameraDistance (radius);
188  const G4Point3D cameraPosition =
189    targetPoint + cameraDistance * fVP.GetViewpointDirection().unit();
190  const GLdouble pnear  = fVP.GetNearDistance (cameraDistance, radius);
191  const GLdouble pfar   = fVP.GetFarDistance  (cameraDistance, pnear, radius);
192  const GLdouble right  = fVP.GetFrontHalfHeight (pnear, radius);
193  const GLdouble left   = -right;
194  const GLdouble bottom = left;
195  const GLdouble top    = right;
196 
197  // FIXME
198  ResizeGLView();
199  //SHOULD SetWindowsSizeHint()...
200
201  glMatrixMode (GL_PROJECTION); // set up Frustum.
202  glLoadIdentity();
203
204  const G4Vector3D scaleFactor = fVP.GetScaleFactor();
205  glScaled(scaleFactor.x(),scaleFactor.y(),scaleFactor.z());
206 
207  if (fVP.GetFieldHalfAngle() == 0.) {
208    glOrtho (left, right, bottom, top, pnear, pfar);
209  }
210  else {
211    glFrustum (left, right, bottom, top, pnear, pfar);
212  } 
213
214  glMatrixMode (GL_MODELVIEW); // apply further transformations to scene.
215  glLoadIdentity();
216 
217  const G4Normal3D& upVector = fVP.GetUpVector (); 
218  G4Point3D gltarget;
219  if (cameraDistance > 1.e-6 * radius) {
220    gltarget = targetPoint;
221  }
222  else {
223    gltarget = targetPoint - radius * fVP.GetViewpointDirection().unit();
224  }
225
226  const G4Point3D& pCamera = cameraPosition;  // An alias for brevity.
227  gluLookAt (pCamera.x(),  pCamera.y(),  pCamera.z(),       // Viewpoint.
228             gltarget.x(), gltarget.y(), gltarget.z(),      // Target point.
229             upVector.x(), upVector.y(), upVector.z());     // Up vector.
230
231  // Light position is "true" light direction, so must come after gluLookAt.
232  glLightfv (GL_LIGHT0, GL_POSITION, lightPosition);
233
234  // OpenGL no longer seems to reconstruct clipped edges, so, when the
235  // BooleanProcessor is up to it, abandon this and use generic
236  // clipping in G4OpenGLSceneHandler::CreateSectionPolyhedron.  Also,
237  // force kernel visit on change of clipping plane in
238  // G4OpenGLStoredViewer::CompareForKernelVisit.
239  if (fVP.IsSection () ) {  // pair of back to back clip planes.
240    const G4Plane3D& s = fVP.GetSectionPlane ();
241    double sArray[4];
242    sArray[0] = s.a();
243    sArray[1] = s.b();
244    sArray[2] = s.c();
245    sArray[3] = s.d() + radius * 1.e-05;
246    glClipPlane (GL_CLIP_PLANE0, sArray);
247    glEnable (GL_CLIP_PLANE0);
248    sArray[0] = -s.a();
249    sArray[1] = -s.b();
250    sArray[2] = -s.c();
251    sArray[3] = -s.d() + radius * 1.e-05;
252    glClipPlane (GL_CLIP_PLANE1, sArray);
253    glEnable (GL_CLIP_PLANE1);
254  } else {
255    glDisable (GL_CLIP_PLANE0);
256    glDisable (GL_CLIP_PLANE1);
257  }
258
259  const G4Planes& cutaways = fVP.GetCutawayPlanes();
260  size_t nPlanes = cutaways.size();
261  if (fVP.IsCutaway() &&
262      fVP.GetCutawayMode() == G4ViewParameters::cutawayIntersection &&
263      nPlanes > 0) {
264    double a[4];
265    a[0] = cutaways[0].a();
266    a[1] = cutaways[0].b();
267    a[2] = cutaways[0].c();
268    a[3] = cutaways[0].d();
269    glClipPlane (GL_CLIP_PLANE2, a);
270    glEnable (GL_CLIP_PLANE2);
271    if (nPlanes > 1) {
272      a[0] = cutaways[1].a();
273      a[1] = cutaways[1].b();
274      a[2] = cutaways[1].c();
275      a[3] = cutaways[1].d();
276      glClipPlane (GL_CLIP_PLANE3, a);
277      glEnable (GL_CLIP_PLANE3);
278    }
279    if (nPlanes > 2) {
280      a[0] = cutaways[2].a();
281      a[1] = cutaways[2].b();
282      a[2] = cutaways[2].c();
283      a[3] = cutaways[2].d();
284      glClipPlane (GL_CLIP_PLANE4, a);
285      glEnable (GL_CLIP_PLANE4);
286    }
287  } else {
288    glDisable (GL_CLIP_PLANE2);
289    glDisable (GL_CLIP_PLANE3);
290    glDisable (GL_CLIP_PLANE4);
291  }
292
293  // Background.
294  background = fVP.GetBackgroundColour ();
295
296}
297
298void G4OpenGLViewer::HaloingFirstPass () {
299 
300  //To perform haloing, first Draw all information to the depth buffer
301  //alone, using a chunky line width, and then Draw all info again, to
302  //the colour buffer, setting a thinner line width an the depth testing
303  //function to less than or equal, so if two lines cross, the one
304  //passing behind the other will not pass the depth test, and so not
305  //get rendered either side of the infront line for a short distance.
306
307  //First, disable writing to the colo(u)r buffer...
308  glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
309
310  //Now enable writing to the depth buffer...
311  glDepthMask (GL_TRUE);
312  glDepthFunc (GL_LESS);
313  glClearDepth (1.0);
314
315  //Finally, set the line width to something wide...
316  glLineWidth (3.0);
317
318}
319
320void G4OpenGLViewer::HaloingSecondPass () {
321
322  //And finally, turn the colour buffer back on with a sesible line width...
323  glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
324  glDepthFunc (GL_LEQUAL);
325  glLineWidth (1.0);
326
327}
328
329void G4OpenGLViewer::Pick(GLdouble x, GLdouble y)
330{
331  //G4cout << "X: " << x << ", Y: " << y << G4endl;
332  const G4int BUFSIZE = 512;
333  GLuint selectBuffer[BUFSIZE];
334  glSelectBuffer(BUFSIZE, selectBuffer);
335  glRenderMode(GL_SELECT);
336  glInitNames();
337  glPushName(0);
338  glMatrixMode(GL_PROJECTION);
339  G4double currentProjectionMatrix[16];
340  glGetDoublev(GL_PROJECTION_MATRIX, currentProjectionMatrix);
341  glPushMatrix();
342  glLoadIdentity();
343  GLint viewport[4];
344  glGetIntegerv(GL_VIEWPORT, viewport);
345  // Define 5x5 pixel pick area
346  gluPickMatrix(x, viewport[3] - y, 5., 5., viewport);
347  glMultMatrixd(currentProjectionMatrix);
348  glMatrixMode(GL_MODELVIEW);
349  DrawView();
350  GLint hits = glRenderMode(GL_RENDER);
351  if (hits < 0)
352    G4cout << "Too many hits.  Zoom in to reduce overlaps." << G4cout;
353  else if (hits > 0) {
354    //G4cout << hits << " hit(s)" << G4endl;
355    GLuint* p = selectBuffer;
356    for (GLint i = 0; i < hits; ++i) {
357      GLuint nnames = *p++;
358      *p++; //OR GLuint zmin = *p++;
359      *p++; //OR GLuint zmax = *p++;
360      //G4cout << "Hit " << i << ": " << nnames << " names"
361      //     << "\nzmin: " << zmin << ", zmax: " << zmax << G4endl;
362      for (GLuint j = 0; j < nnames; ++j) {
363        GLuint name = *p++;
364        //G4cout << "Name " << j << ": PickName: " << name << G4endl;
365        std::map<GLuint, G4AttHolder*>::iterator iter =
366          fOpenGLSceneHandler.fPickMap.find(name);
367        if (iter != fOpenGLSceneHandler.fPickMap.end()) {
368          G4AttHolder* attHolder = iter->second;
369          if(attHolder && attHolder->GetAttDefs().size()) {
370            for (size_t i = 0; i < attHolder->GetAttDefs().size(); ++i) {
371              G4cout << G4AttCheck(attHolder->GetAttValues()[i],
372                                   attHolder->GetAttDefs()[i]);
373            }
374          }
375        }
376      }
377      G4cout << G4endl;
378    }
379  }
380  glMatrixMode(GL_PROJECTION);
381  glPopMatrix();
382  glMatrixMode(GL_MODELVIEW);
383}
384
385
386
387
388GLubyte* G4OpenGLViewer::grabPixels (int inColor, unsigned int width, unsigned int height) {
389 
390  GLubyte* buffer;
391  GLint swapbytes, lsbfirst, rowlength;
392  GLint skiprows, skippixels, alignment;
393  GLenum format;
394  int size;
395
396  if (inColor) {
397    format = GL_RGB;
398    size = width*height*3;
399  } else {
400    format = GL_LUMINANCE;
401    size = width*height*1;
402  }
403
404  buffer = new GLubyte[size];
405  if (buffer == NULL)
406    return NULL;
407
408  glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapbytes);
409  glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirst);
410  glGetIntegerv (GL_UNPACK_ROW_LENGTH, &rowlength);
411
412  glGetIntegerv (GL_UNPACK_SKIP_ROWS, &skiprows);
413  glGetIntegerv (GL_UNPACK_SKIP_PIXELS, &skippixels);
414  glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignment);
415
416  glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
417  glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
418  glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
419
420  glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
421  glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
422  glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
423
424  glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, format, GL_UNSIGNED_BYTE, (GLvoid*) buffer);
425
426  glPixelStorei (GL_UNPACK_SWAP_BYTES, swapbytes);
427  glPixelStorei (GL_UNPACK_LSB_FIRST, lsbfirst);
428  glPixelStorei (GL_UNPACK_ROW_LENGTH, rowlength);
429 
430  glPixelStorei (GL_UNPACK_SKIP_ROWS, skiprows);
431  glPixelStorei (GL_UNPACK_SKIP_PIXELS, skippixels);
432  glPixelStorei (GL_UNPACK_ALIGNMENT, alignment);
433 
434  return buffer;
435}
436
437void G4OpenGLViewer::printEPS() {
438  bool res;
439#ifdef G4DEBUG_VIS_OGL
440  printf("G4OpenGLViewer::printEPS file:%s Vec:%d\n",fPrintFilename.c_str(),fVectoredPs);
441#endif
442  if (fVectoredPs) {
443    res = printVectoredEPS();
444  } else {
445    res = printNonVectoredEPS();
446  }
447  if (res == false) {
448    G4cerr << "Error while saving file... "<<fPrintFilename.c_str()<< G4endl;
449  } else {
450    G4cout << "File "<<fPrintFilename.c_str()<<" has been saved " << G4endl;
451  }
452}
453
454bool G4OpenGLViewer::printVectoredEPS() {
455  return printGl2PS();
456}
457
458bool G4OpenGLViewer::printNonVectoredEPS () {
459
460  int width = 0;
461  int height = 0;
462
463  if (fPrintSizeX == 0) {
464    width = fWinSize_x;
465  } else {
466    width = fPrintSizeX;
467  }
468  if (fPrintSizeY == 0) {
469    height = fWinSize_y;
470  } else {
471    height = fPrintSizeY;
472  }
473
474#ifdef G4DEBUG_VIS_OGL
475  printf("G4OpenGLViewer::printNonVectoredEPS file:%s Vec:%d X:%d Y:%d col:%d\n",fPrintFilename.c_str(),fVectoredPs,width,height,fPrintColour);
476#endif
477  FILE* fp;
478  GLubyte* pixels;
479  GLubyte* curpix;
480  int components, pos, i;
481
482  pixels = grabPixels (fPrintColour, width, height);
483
484  if (pixels == NULL) {
485      G4cerr << "Failed to get pixels from OpenGl viewport" << G4endl;
486    return false;
487  }
488  if (fPrintColour) {
489    components = 3;
490  } else {
491    components = 1;
492  }
493 
494  fp = fopen (fPrintFilename.c_str(), "w");
495  if (fp == NULL) {
496    G4cerr << "Can't open filename " << fPrintFilename.c_str() << G4endl;
497    return false;
498  }
499 
500  fprintf (fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");
501  fprintf (fp, "%%%%Title: %s\n", fPrintFilename.c_str());
502  fprintf (fp, "%%%%Creator: OpenGL pixmap render output\n");
503  fprintf (fp, "%%%%BoundingBox: 0 0 %d %d\n", width, height);
504  fprintf (fp, "%%%%EndComments\n");
505  fprintf (fp, "gsave\n");
506  fprintf (fp, "/bwproc {\n");
507  fprintf (fp, "    rgbproc\n");
508  fprintf (fp, "    dup length 3 idiv string 0 3 0 \n");
509  fprintf (fp, "    5 -1 roll {\n");
510  fprintf (fp, "    add 2 1 roll 1 sub dup 0 eq\n");
511  fprintf (fp, "    { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
512  fprintf (fp, "       3 1 roll 5 -1 roll } put 1 add 3 0 \n");
513  fprintf (fp, "    { 2 1 roll } ifelse\n");
514  fprintf (fp, "    }forall\n");
515  fprintf (fp, "    pop pop pop\n");
516  fprintf (fp, "} def\n");
517  fprintf (fp, "systemdict /colorimage known not {\n");
518  fprintf (fp, "   /colorimage {\n");
519  fprintf (fp, "       pop\n");
520  fprintf (fp, "       pop\n");
521  fprintf (fp, "       /rgbproc exch def\n");
522  fprintf (fp, "       { bwproc } image\n");
523  fprintf (fp, "   }  def\n");
524  fprintf (fp, "} if\n");
525  fprintf (fp, "/picstr %d string def\n", width * components);
526  fprintf (fp, "%d %d scale\n", width, height);
527  fprintf (fp, "%d %d %d\n", width, height, 8);
528  fprintf (fp, "[%d 0 0 %d 0 0]\n", width, height);
529  fprintf (fp, "{currentfile picstr readhexstring pop}\n");
530  fprintf (fp, "false %d\n", components);
531  fprintf (fp, "colorimage\n");
532 
533  curpix = (GLubyte*) pixels;
534  pos = 0;
535  for (i = width*height*components; i>0; i--) {
536    fprintf (fp, "%02hx ", *(curpix++));
537    if (++pos >= 32) {
538      fprintf (fp, "\n");
539      pos = 0;
540    }
541  }
542  if (pos)
543    fprintf (fp, "\n");
544
545  fprintf (fp, "grestore\n");
546  fprintf (fp, "showpage\n");
547  delete pixels;
548  fclose (fp);
549
550  // Reset for next time (useful is size change)
551  fPrintSizeX = 0;
552  fPrintSizeY = 0;
553
554  return true;
555}
556
557
558bool G4OpenGLViewer::printGl2PS() {
559
560  int width = 0;
561  int height = 0;
562
563  if (fPrintSizeX == 0) {
564    width = fWinSize_x;
565  } else {
566    width = fPrintSizeX;
567  }
568  if (fPrintSizeY == 0) {
569    height = fWinSize_y;
570  } else {
571    height = fPrintSizeY;
572  }
573
574  if (!fGL2PSAction) return false;
575
576
577  fGL2PSAction->setFileName(fPrintFilename.c_str());
578  // try to resize
579  int X = fWinSize_x;
580  int Y = fWinSize_y;
581
582  fWinSize_x = width;
583  fWinSize_y = height;
584
585  ResizeGLView();
586  if (fGL2PSAction->enableFileWriting()) {
587
588    // FIXME : How to get the point size.... ?
589    //    fGL2PSAction->setLineWidth(1);
590    // FIXME : How to get the line width.... ?
591    //    fGL2PSAction->setPointSize(30);
592
593    DrawView ();
594    fGL2PSAction->disableFileWriting();
595  }
596
597  fWinSize_x = X;
598  fWinSize_y = Y;
599  ResizeGLView();
600
601  // Reset for next time (useful is size change)
602  fPrintSizeX = 0;
603  fPrintSizeY = 0;
604
605  return true;
606}
607
608GLdouble G4OpenGLViewer::getSceneNearWidth()
609{
610  const G4Point3D targetPoint
611    = fSceneHandler.GetScene()->GetStandardTargetPoint()
612    + fVP.GetCurrentTargetPoint ();
613  G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
614  if(radius<=0.) radius = 1.;
615  const G4double cameraDistance = fVP.GetCameraDistance (radius);
616  const GLdouble pnear   = fVP.GetNearDistance (cameraDistance, radius);
617  return 2 * fVP.GetFrontHalfHeight (pnear, radius);
618}
619
620GLdouble G4OpenGLViewer::getSceneFarWidth()
621{
622  const G4Point3D targetPoint
623    = fSceneHandler.GetScene()->GetStandardTargetPoint()
624    + fVP.GetCurrentTargetPoint ();
625  G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
626  if(radius<=0.) radius = 1.;
627  const G4double cameraDistance = fVP.GetCameraDistance (radius);
628  const GLdouble pnear   = fVP.GetNearDistance (cameraDistance, radius);
629  const GLdouble pfar    = fVP.GetFarDistance  (cameraDistance, pnear, radius);
630  return 2 * fVP.GetFrontHalfHeight (pfar, radius);
631}
632
633
634GLdouble G4OpenGLViewer::getSceneDepth()
635{
636  const G4Point3D targetPoint
637    = fSceneHandler.GetScene()->GetStandardTargetPoint()
638    + fVP.GetCurrentTargetPoint ();
639  G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
640  if(radius<=0.) radius = 1.;
641  const G4double cameraDistance = fVP.GetCameraDistance (radius);
642  const GLdouble pnear   = fVP.GetNearDistance (cameraDistance, radius);
643  return fVP.GetFarDistance  (cameraDistance, pnear, radius)- pnear;
644}
645
646
647
648void G4OpenGLViewer::rotateScene(G4double dx, G4double dy,G4double deltaRotation)
649{
650
651  G4Vector3D vp;
652  G4Vector3D up;
653 
654  G4Vector3D xprime;
655  G4Vector3D yprime;
656  G4Vector3D zprime;
657 
658  G4double delta_alpha;
659  G4double delta_theta;
660 
661  G4Vector3D new_vp;
662  G4Vector3D new_up;
663 
664  G4double cosalpha;
665  G4double sinalpha;
666 
667  G4Vector3D a1;
668  G4Vector3D a2;
669  G4Vector3D delta;
670  G4Vector3D viewPoint;
671
672   
673  //phi spin stuff here
674 
675  vp = fVP.GetViewpointDirection ().unit ();
676  up = fVP.GetUpVector ().unit ();
677 
678  yprime = (up.cross(vp)).unit();
679  zprime = (vp.cross(yprime)).unit();
680 
681  if (fVP.GetLightsMoveWithCamera()) {
682    delta_alpha = dy * deltaRotation;
683    delta_theta = -dx * deltaRotation;
684  } else {
685    delta_alpha = -dy * deltaRotation;
686    delta_theta = dx * deltaRotation;
687  }   
688 
689  delta_alpha *= deg;
690  delta_theta *= deg;
691 
692  new_vp = std::cos(delta_alpha) * vp + std::sin(delta_alpha) * zprime;
693 
694  // to avoid z rotation flipping
695  // to allow more than 360° rotation
696
697  const G4Point3D targetPoint
698    = fSceneHandler.GetScene()->GetStandardTargetPoint()
699    + fVP.GetCurrentTargetPoint ();
700  G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
701  if(radius<=0.) radius = 1.;
702  const G4double cameraDistance = fVP.GetCameraDistance (radius);
703  const G4Point3D cameraPosition =
704    targetPoint + cameraDistance * fVP.GetViewpointDirection().unit();
705
706  if (fVP.GetLightsMoveWithCamera()) {
707    new_up = (new_vp.cross(yprime)).unit();
708    if (new_vp.z()*vp.z() <0) {
709      new_up.set(new_up.x(),-new_up.y(),new_up.z());
710    }
711  } else {
712    new_up = up;
713    if (new_vp.z()*vp.z() <0) {
714      new_up.set(new_up.x(),-new_up.y(),new_up.z());
715    }
716  }
717  fVP.SetUpVector(new_up);
718  ////////////////
719  // Rotates by fixed azimuthal angle delta_theta.
720 
721  cosalpha = new_up.dot (new_vp.unit());
722  sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
723  yprime = (new_up.cross (new_vp.unit())).unit ();
724  xprime = yprime.cross (new_up);
725  // Projection of vp on plane perpendicular to up...
726  a1 = sinalpha * xprime;
727  // Required new projection...
728  a2 = sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
729  // Required Increment vector...
730  delta = a2 - a1;
731  // So new viewpoint is...
732  viewPoint = new_vp.unit() + delta;
733 
734  fVP.SetViewAndLights (viewPoint);
735}
736
737#endif
Note: See TracBrowser for help on using the repository browser.