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

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

en test...ca marche encore...

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