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

Last change on this file since 1333 was 1332, checked in by garnier, 15 years ago

changement des shorcuts

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