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

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

passage de print OK

  • Property svn:mime-type set to text/cpp
File size: 23.3 KB
RevLine 
[529]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//
[975]27// $Id: G4OpenGLViewer.cc,v 1.53 2009/04/08 16:55:44 lgarnier Exp $
[873]28// GEANT4 tag $Name:  $
[529]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"
[923]40#include "G4OpenGL2PSAction.hh"
[529]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"
[593]49#include "G4AttHolder.hh"
50#include "G4AttCheck.hh"
[918]51
[977]52#include "G4OpenGLXViewer.hh"
53
[923]54// GL2PS
55#include "Geant4_gl2ps.h"
56
[593]57#include <sstream>
[529]58
59G4OpenGLViewer::G4OpenGLViewer (G4OpenGLSceneHandler& scene):
60G4VViewer (scene, -1),
[916]61fPrintFilename ("G4OpenGL.eps"),
62fPrintColour (true),
63fVectoredPs (true),
[941]64fPrintSizeX(0),
65fPrintSizeY(0),
[593]66fOpenGLSceneHandler(scene),
[529]67background (G4Colour(0.,0.,0.)),
68transparency_enabled (true),
69antialiasing_enabled (false),
70haloing_enabled (false),
[789]71fStartTime(-DBL_MAX),
72fEndTime(DBL_MAX),
[529]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.),
[916]88fDisplayLightFrontBlue(0.),
89fPointSize (0)
[529]90{
91  // Make changes to view parameters for OpenGL...
92  fVP.SetAutoRefresh(true);
93  fDefaultVP.SetAutoRefresh(true);
[897]94  fWinSize_x = fVP.GetWindowSizeHintX();
95  fWinSize_y = fVP.GetWindowSizeHintY();
[529]96
[921]97  fGL2PSAction = new G4OpenGL2PSAction();
98
[529]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 () {
[959]119#ifdef G4DEBUG_VIS_OGL
[948]120  printf("G4OpenGLViewer::ClearView\n");
[959]121#endif
[529]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);
[955]132#ifdef G4DEBUG_VIS_OGL
133  printf("G4OpenGLViewer::ClearView flush\n");
134#endif
[529]135  glFlush ();
136}
137
[906]138
139/**
140 * Set the viewport of the scene
[945]141 * MAXIMUM SIZE is :
142 * GLint dims[2];
143 * glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
[906]144 */
145void G4OpenGLViewer::ResizeGLView()
146{
[959]147#ifdef G4DEBUG_VIS_OGL
[949]148  printf("G4OpenGLViewer::ResizeGLView %d %d\n",fWinSize_x,fWinSize_y);
[959]149#endif
[945]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  }
[949]161  GLsizei side = fWinSize_x;
[906]162  if (fWinSize_y < fWinSize_x) side = fWinSize_y;
[949]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) {
[950]175    //    fWinSize_x --;
176
177    side = fWinSize_x;
178    if (fWinSize_y < fWinSize_x) side = fWinSize_y;
[949]179  }
180  if ((fWinSize_y - side)%2) {
[950]181    //    fWinSize_y --;
182
183    side = fWinSize_x;
184    if (fWinSize_y < fWinSize_x) side = fWinSize_y;
[949]185  }
186 
[950]187  GLint X = (fWinSize_x - side) / 2;
188  GLint Y = (fWinSize_y - side) / 2;
[949]189 
[959]190#ifdef G4DEBUG_VIS_OGL
[950]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);
[959]192#endif
[950]193  glViewport(X, Y, side, side);
194  //    glViewport(0, 0, fWinSize_x,fWinSize_y); 
195 
196
[906]197}
198
199
[529]200void G4OpenGLViewer::SetView () {
[858]201
202  if (!fSceneHandler.GetScene()) {
[908]203    G4cerr << "G4OpenGLStoredViewer: Creating a Viewer without a scene is not allowed. \nPlease use /vis/scene/create before /vis/open/.... "
[858]204           << G4endl;
205    return;
206  }
[529]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();
[906]234  const GLdouble pnear  = fVP.GetNearDistance (cameraDistance, radius);
235  const GLdouble pfar   = fVP.GetFarDistance  (cameraDistance, pnear, radius);
[529]236  const GLdouble right  = fVP.GetFrontHalfHeight (pnear, radius);
237  const GLdouble left   = -right;
238  const GLdouble bottom = left;
239  const GLdouble top    = right;
240 
[906]241  // FIXME
242  ResizeGLView();
[908]243  //SHOULD SetWindowsSizeHint()...
[906]244
[529]245  glMatrixMode (GL_PROJECTION); // set up Frustum.
246  glLoadIdentity();
247
[906]248  const G4Vector3D scaleFactor = fVP.GetScaleFactor();
249  glScaled(scaleFactor.x(),scaleFactor.y(),scaleFactor.z());
[529]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);
[908]256  } 
[906]257
[529]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.
[906]274
[529]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
[593]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
[754]430
[593]431
[914]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
[980]440#ifdef G4DEBUG_VIS_OGL
441  printf("G4OpenGLViewer::grabPixels\n");
442#endif
[914]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
[980]471  glReadBuffer(GL_FRONT);
[914]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
[941]485void G4OpenGLViewer::printEPS() {
486  bool res;
[942]487#ifdef G4DEBUG_VIS_OGL
488  printf("G4OpenGLViewer::printEPS file:%s Vec:%d\n",fPrintFilename.c_str(),fVectoredPs);
489#endif
[941]490  if (fVectoredPs) {
[980]491#ifdef G4DEBUG_VIS_OGL
492    printf("G4OpenGLViewer::printEPS Vectored\n");
493#endif
[941]494    res = printVectoredEPS();
495  } else {
[980]496#ifdef G4DEBUG_VIS_OGL
497    printf("G4OpenGLViewer::printEPS NON Vectored\n");
498#endif
[977]499
500    G4OpenGLXViewer* pOGLXViewer = dynamic_cast<G4OpenGLXViewer*>(this);
[980]501//     if (pOGLXViewer) {
502//       res = printNonVectoredEPS();
503//       pOGLXViewer->printEPS();
504//       fPrintFilename = "G4OpenGLViewer_Print.eps";
505//     } else {
[977]506      res = printNonVectoredEPS();
[980]507//     }
[941]508  }
509  if (res == false) {
510    G4cerr << "Error while saving file... "<<fPrintFilename.c_str()<< G4endl;
511  } else {
512    G4cout << "File "<<fPrintFilename.c_str()<<" has been saved " << G4endl;
513  }
514}
[914]515
[941]516bool G4OpenGLViewer::printVectoredEPS() {
517  return printGl2PS();
518}
519
520bool G4OpenGLViewer::printNonVectoredEPS () {
521
522  int width = 0;
523  int height = 0;
524
525  if (fPrintSizeX == 0) {
526    width = fWinSize_x;
527  } else {
528    width = fPrintSizeX;
529  }
530  if (fPrintSizeY == 0) {
531    height = fWinSize_y;
532  } else {
533    height = fPrintSizeY;
534  }
535
[942]536#ifdef G4DEBUG_VIS_OGL
537  printf("G4OpenGLViewer::printNonVectoredEPS file:%s Vec:%d X:%d Y:%d col:%d\n",fPrintFilename.c_str(),fVectoredPs,width,height,fPrintColour);
538#endif
[914]539  FILE* fp;
540  GLubyte* pixels;
541  GLubyte* curpix;
542  int components, pos, i;
543
[941]544  pixels = grabPixels (fPrintColour, width, height);
[914]545
[941]546  if (pixels == NULL) {
547      G4cerr << "Failed to get pixels from OpenGl viewport" << G4endl;
548    return false;
549  }
550  if (fPrintColour) {
[914]551    components = 3;
552  } else {
553    components = 1;
554  }
555 
[938]556  fp = fopen (fPrintFilename.c_str(), "w");
[914]557  if (fp == NULL) {
[941]558    G4cerr << "Can't open filename " << fPrintFilename.c_str() << G4endl;
559    return false;
[914]560  }
561 
562  fprintf (fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");
[938]563  fprintf (fp, "%%%%Title: %s\n", fPrintFilename.c_str());
[914]564  fprintf (fp, "%%%%Creator: OpenGL pixmap render output\n");
565  fprintf (fp, "%%%%BoundingBox: 0 0 %d %d\n", width, height);
566  fprintf (fp, "%%%%EndComments\n");
567  fprintf (fp, "gsave\n");
568  fprintf (fp, "/bwproc {\n");
569  fprintf (fp, "    rgbproc\n");
570  fprintf (fp, "    dup length 3 idiv string 0 3 0 \n");
571  fprintf (fp, "    5 -1 roll {\n");
572  fprintf (fp, "    add 2 1 roll 1 sub dup 0 eq\n");
573  fprintf (fp, "    { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
574  fprintf (fp, "       3 1 roll 5 -1 roll } put 1 add 3 0 \n");
575  fprintf (fp, "    { 2 1 roll } ifelse\n");
576  fprintf (fp, "    }forall\n");
577  fprintf (fp, "    pop pop pop\n");
578  fprintf (fp, "} def\n");
579  fprintf (fp, "systemdict /colorimage known not {\n");
580  fprintf (fp, "   /colorimage {\n");
581  fprintf (fp, "       pop\n");
582  fprintf (fp, "       pop\n");
583  fprintf (fp, "       /rgbproc exch def\n");
584  fprintf (fp, "       { bwproc } image\n");
585  fprintf (fp, "   }  def\n");
586  fprintf (fp, "} if\n");
587  fprintf (fp, "/picstr %d string def\n", width * components);
588  fprintf (fp, "%d %d scale\n", width, height);
589  fprintf (fp, "%d %d %d\n", width, height, 8);
590  fprintf (fp, "[%d 0 0 %d 0 0]\n", width, height);
591  fprintf (fp, "{currentfile picstr readhexstring pop}\n");
592  fprintf (fp, "false %d\n", components);
593  fprintf (fp, "colorimage\n");
594 
595  curpix = (GLubyte*) pixels;
596  pos = 0;
597  for (i = width*height*components; i>0; i--) {
598    fprintf (fp, "%02hx ", *(curpix++));
599    if (++pos >= 32) {
600      fprintf (fp, "\n");
601      pos = 0;
602    }
603  }
604  if (pos)
605    fprintf (fp, "\n");
606
607  fprintf (fp, "grestore\n");
608  fprintf (fp, "showpage\n");
609  delete pixels;
610  fclose (fp);
[941]611
612  // Reset for next time (useful is size change)
613  fPrintSizeX = 0;
614  fPrintSizeY = 0;
615
616  return true;
[914]617}
618
[918]619
[941]620bool G4OpenGLViewer::printGl2PS() {
[918]621
[941]622  int width = 0;
623  int height = 0;
624
625  if (fPrintSizeX == 0) {
626    width = fWinSize_x;
627  } else {
628    width = fPrintSizeX;
629  }
630  if (fPrintSizeY == 0) {
631    height = fWinSize_y;
632  } else {
633    height = fPrintSizeY;
634  }
635
[938]636  if (!fGL2PSAction) return false;
[929]637
[938]638  fGL2PSAction->setFileName(fPrintFilename.c_str());
639  // try to resize
640  int X = fWinSize_x;
641  int Y = fWinSize_y;
[929]642
[938]643  fWinSize_x = width;
644  fWinSize_y = height;
[980]645  glReadBuffer(GL_FRONT);
[938]646  ResizeGLView();
[921]647  if (fGL2PSAction->enableFileWriting()) {
[945]648
[948]649    // By default, we choose the line width (trajectories...)
650    fGL2PSAction->setLineWidth(1);
651    // By default, we choose the point size (markers...)
652    fGL2PSAction->setPointSize(2);
[945]653
[931]654    DrawView ();
[921]655    fGL2PSAction->disableFileWriting();
656  }
[929]657
[938]658  fWinSize_x = X;
659  fWinSize_y = Y;
660  ResizeGLView();
[929]661
[941]662  // Reset for next time (useful is size change)
663  fPrintSizeX = 0;
664  fPrintSizeY = 0;
665
[938]666  return true;
[918]667}
668
[712]669GLdouble G4OpenGLViewer::getSceneNearWidth()
670{
671  const G4Point3D targetPoint
672    = fSceneHandler.GetScene()->GetStandardTargetPoint()
673    + fVP.GetCurrentTargetPoint ();
674  G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
675  if(radius<=0.) radius = 1.;
676  const G4double cameraDistance = fVP.GetCameraDistance (radius);
677  const GLdouble pnear   = fVP.GetNearDistance (cameraDistance, radius);
678  return 2 * fVP.GetFrontHalfHeight (pnear, radius);
679}
680
681GLdouble G4OpenGLViewer::getSceneFarWidth()
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  const GLdouble pfar    = fVP.GetFarDistance  (cameraDistance, pnear, radius);
691  return 2 * fVP.GetFrontHalfHeight (pfar, radius);
692}
693
694
695GLdouble G4OpenGLViewer::getSceneDepth()
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 GLdouble pnear   = fVP.GetNearDistance (cameraDistance, radius);
704  return fVP.GetFarDistance  (cameraDistance, pnear, radius)- pnear;
705}
706
707
[593]708
[798]709void G4OpenGLViewer::rotateScene(G4double dx, G4double dy,G4double deltaRotation)
710{
711
712  G4Vector3D vp;
713  G4Vector3D up;
714 
715  G4Vector3D xprime;
716  G4Vector3D yprime;
717  G4Vector3D zprime;
718 
719  G4double delta_alpha;
720  G4double delta_theta;
721 
722  G4Vector3D new_vp;
723  G4Vector3D new_up;
724 
725  G4double cosalpha;
726  G4double sinalpha;
727 
728  G4Vector3D a1;
729  G4Vector3D a2;
730  G4Vector3D delta;
731  G4Vector3D viewPoint;
732
733   
734  //phi spin stuff here
735 
736  vp = fVP.GetViewpointDirection ().unit ();
737  up = fVP.GetUpVector ().unit ();
738 
739  yprime = (up.cross(vp)).unit();
740  zprime = (vp.cross(yprime)).unit();
741 
742  if (fVP.GetLightsMoveWithCamera()) {
743    delta_alpha = dy * deltaRotation;
744    delta_theta = -dx * deltaRotation;
745  } else {
746    delta_alpha = -dy * deltaRotation;
747    delta_theta = dx * deltaRotation;
748  }   
749 
750  delta_alpha *= deg;
751  delta_theta *= deg;
752 
753  new_vp = std::cos(delta_alpha) * vp + std::sin(delta_alpha) * zprime;
754 
755  // to avoid z rotation flipping
756  // to allow more than 360° rotation
[847]757
[801]758  const G4Point3D targetPoint
759    = fSceneHandler.GetScene()->GetStandardTargetPoint()
760    + fVP.GetCurrentTargetPoint ();
761  G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
762  if(radius<=0.) radius = 1.;
763  const G4double cameraDistance = fVP.GetCameraDistance (radius);
764  const G4Point3D cameraPosition =
765    targetPoint + cameraDistance * fVP.GetViewpointDirection().unit();
766
[798]767  if (fVP.GetLightsMoveWithCamera()) {
768    new_up = (new_vp.cross(yprime)).unit();
769    if (new_vp.z()*vp.z() <0) {
770      new_up.set(new_up.x(),-new_up.y(),new_up.z());
771    }
772  } else {
773    new_up = up;
774    if (new_vp.z()*vp.z() <0) {
775      new_up.set(new_up.x(),-new_up.y(),new_up.z());
776    }
777  }
778  fVP.SetUpVector(new_up);
779  ////////////////
780  // Rotates by fixed azimuthal angle delta_theta.
781 
782  cosalpha = new_up.dot (new_vp.unit());
783  sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
784  yprime = (new_up.cross (new_vp.unit())).unit ();
785  xprime = yprime.cross (new_up);
786  // Projection of vp on plane perpendicular to up...
787  a1 = sinalpha * xprime;
788  // Required new projection...
789  a2 = sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
790  // Required Increment vector...
791  delta = a2 - a1;
792  // So new viewpoint is...
793  viewPoint = new_vp.unit() + delta;
794 
795  fVP.SetViewAndLights (viewPoint);
796}
797
[529]798#endif
Note: See TracBrowser for help on using the repository browser.