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

Last change on this file since 1239 was 1238, checked in by garnier, 16 years ago

otpimisations

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