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

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

prepartion du tag

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