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

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

pas de modif, juste du test

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