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

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

avant modif pour CVS

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