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

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

Ok en SXm, mais Pb sur le retore du contexte

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