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

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

Improvments for print in pixmap. Not finish yet

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