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

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

update...

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