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

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

resize en cours de factorisation

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