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

Last change on this file since 1039 was 1039, checked in by garnier, 17 years ago

remise a jour

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