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

Last change on this file since 948 was 948, checked in by garnier, 16 years ago

Works for OGLSXm et non vector

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