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

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

suppression de la fin des methodes de print inutiles

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