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

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

update before cvs

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