Geant4-11
G4OpenGLViewer.cc
Go to the documentation of this file.
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//
28//
29// Andrew Walkden 27th March 1996
30// OpenGL view - opens window, hard copy, etc.
31
32#include "G4ios.hh"
34#include "G4OpenGLViewer.hh"
37#include "G4OpenGL2PSAction.hh"
38
39#include "G4Scene.hh"
40#include "G4VisExtent.hh"
41#include "G4LogicalVolume.hh"
42#include "G4VSolid.hh"
43#include "G4Point3D.hh"
44#include "G4Normal3D.hh"
45#include "G4Plane3D.hh"
46#include "G4AttHolder.hh"
47#include "G4AttCheck.hh"
48#include "G4Text.hh"
49
50#ifdef G4OPENGL_VERSION_2
51#include "G4OpenGLVboDrawer.hh"
52#endif
53
54// GL2PS
55#include "Geant4_gl2ps.h"
56
57#include <sstream>
58#include <string>
59#include <iomanip>
60
62G4VViewer (scene, -1),
63#ifdef G4OPENGL_VERSION_2
64fVboDrawer(NULL),
65#endif
66fPrintColour (true),
67fVectoredPs (true),
68fOpenGLSceneHandler(scene),
69background (G4Colour(0.,0.,0.)),
70transparency_enabled (true),
71antialiasing_enabled (false),
72haloing_enabled (false),
73fRot_sens(1.),
74fPan_sens(0.01),
75fWinSize_x(0),
76fWinSize_y(0),
77fDefaultExportImageFormat("pdf"),
78fExportImageFormat("pdf"),
79fExportFilenameIndex(0),
80fPrintSizeX(-1),
81fPrintSizeY(-1),
82fPointSize (0),
83fDefaultExportFilename("G4OpenGL"),
84fSizeHasChanged(0),
85fGl2psDefaultLineWith(1),
86fGl2psDefaultPointSize(2),
87fGlViewInitialized(false),
88fIsGettingPickInfos(false)
89#ifdef G4OPENGL_VERSION_2
90,fShaderProgram(0)
91,fVertexPositionAttribute(0)
92,fVertexNormalAttribute(0)
93,fpMatrixUniform(0)
94,fcMatrixUniform(0)
95,fmvMatrixUniform(0)
96,fnMatrixUniform(0)
97#endif
98{
99 // Make changes to view parameters for OpenGL...
100 fVP.SetAutoRefresh(true);
102
104
105 // add supported export image format
110
111 // Change the default name
113
114 // glClearColor (0.0, 0.0, 0.0, 0.0);
115 // glClearDepth (1.0);
116 // glDisable (GL_BLEND);
117 // glDisable (GL_LINE_SMOOTH);
118 // glDisable (GL_POLYGON_SMOOTH);
119
120}
121
123{
124 delete fGL2PSAction;
125}
126
128{
129#ifdef G4OPENGL_VERSION_2
130 if (fVboDrawer) {
131
132 // First, load a simple shader
133 fShaderProgram = glCreateProgram();
134 Shader vertexShader = glCreateShader(GL_VERTEX_SHADER);
135 const char * vSrc = fVboDrawer->getVertexShaderSrc();
136 glShaderSource(vertexShader, 1, &vSrc, NULL);
137 glCompileShader(vertexShader);
138 glAttachShader(fShaderProgram, vertexShader);
139
140 Shader fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
141 const char * fSrc = fVboDrawer->getFragmentShaderSrc();
142 glShaderSource(fragmentShader, 1, &fSrc, NULL);
143 glCompileShader(fragmentShader);
144
145 glAttachShader(fShaderProgram, fragmentShader);
146 glLinkProgram(fShaderProgram);
147 glUseProgram(fShaderProgram);
148
149 // UniformLocation uColor = getUniformLocation(fShaderProgram, "uColor");
150 // uniform4fv(uColor, [0.0, 0.3, 0.0, 1.0]);
151
152 // Extract the references to the attributes from the shader.
153
154 fVertexPositionAttribute =
155 glGetAttribLocation(fShaderProgram, "aVertexPosition");
156
157
158 glEnableVertexAttribArray(fVertexPositionAttribute);
159
160 // Extract the references the uniforms from the shader
161 fpMatrixUniform = glGetUniformLocation(fShaderProgram, "uPMatrix");
162 fcMatrixUniform = glGetUniformLocation(fShaderProgram, "uCMatrix");
163 fmvMatrixUniform = glGetUniformLocation(fShaderProgram, "uMVMatrix");
164 fnMatrixUniform = glGetUniformLocation(fShaderProgram, "uNMatrix");
165 ftMatrixUniform = glGetUniformLocation(fShaderProgram, "uTMatrix");
166
167 /* glUniformMatrix4fv(fcMatrixUniform, 1, 0, identity);
168 glUniformMatrix4fv(fpMatrixUniform, 1, 0, identity);
169 glUniformMatrix4fv(ftMatrixUniform, 1, 0, identity);
170 glUniformMatrix4fv(fmvMatrixUniform, 1, 0, identity);
171 */
172 // We have to set that in order to avoid calls on opengl commands before all is ready
173 fGlViewInitialized = true;
174 }
175#endif
176
177 if (fWinSize_x == 0) {
179 }
180 if (fWinSize_y == 0) {
182 }
183
184 glClearColor (0.0, 0.0, 0.0, 0.0);
185 glClearDepth (1.0);
186#ifndef G4OPENGL_VERSION_2
187 glDisable (GL_LINE_SMOOTH);
188 glDisable (GL_POLYGON_SMOOTH);
189#endif
190
191// clear the buffers and window?
192 ClearView ();
193 FinishView ();
194
195 glDepthFunc (GL_LEQUAL);
196 glDepthMask (GL_TRUE);
197
198 glEnable (GL_BLEND);
199 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
200
201}
202
205
206 if(!isFramebufferReady()) {
207 return;
208 }
209
210 glFlush();
211}
212
213
215 // Ready for clear ?
216 // See : http://lists.apple.com/archives/mac-opengl/2012/Jul/msg00038.html
217 if(!isFramebufferReady()) {
218 return;
219 }
220
221 glClearColor (background.GetRed(),
224 1.);
225 glClearDepth (1.0);
226 //Below line does not compile with Mesa includes.
227 //glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
228 glClear (GL_COLOR_BUFFER_BIT);
229 glClear (GL_DEPTH_BUFFER_BIT);
230 glClear (GL_STENCIL_BUFFER_BIT);
231}
232
233
234void G4OpenGLViewer::ResizeWindow(unsigned int aWidth, unsigned int aHeight) {
235 if ((fWinSize_x != aWidth) || (fWinSize_y != aHeight)) {
236 fWinSize_x = aWidth;
237 fWinSize_y = aHeight;
238 fSizeHasChanged = true;
239 } else {
240 fSizeHasChanged = false;
241 }
242}
243
251{
252 // Check size
253 GLint dims[2];
254 dims[0] = 0;
255 dims[1] = 0;
256
257 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
258
259 if ((dims[0] !=0 ) && (dims[1] !=0)) {
260
261 if (fWinSize_x > (unsigned)dims[0]) {
262 G4cerr << "Try to resize view greater than max X viewport dimension. Desired size "<<fWinSize_x <<" is resize to "<< dims[0] << G4endl;
263 fWinSize_x = dims[0];
264 }
265 if (fWinSize_y > (unsigned)dims[1]) {
266 G4cerr << "Try to resize view greater than max Y viewport dimension. Desired size "<<fWinSize_y <<" is resize to "<< dims[1] << G4endl;
267 fWinSize_y = dims[1];
268 }
269 }
270
271 glViewport(0, 0, fWinSize_x,fWinSize_y);
272
273
274}
275
276
278 // if getting pick infos, should not resize the view.
279 if (fIsGettingPickInfos) return;
280
281 if (!fSceneHandler.GetScene()) {
282 return;
283 }
284 // Calculates view representation based on extent of object being
285 // viewed and (initial) viewpoint. (Note: it can change later due
286 // to user interaction via visualization system's GUI.)
287
288 // Lighting.
289 GLfloat lightPosition [4];
290 lightPosition [0] = fVP.GetActualLightpointDirection().x();
291 lightPosition [1] = fVP.GetActualLightpointDirection().y();
292 lightPosition [2] = fVP.GetActualLightpointDirection().z();
293 lightPosition [3] = 0.;
294 // Light position is "true" light direction, so must come after gluLookAt.
295 GLfloat ambient [] = { 0.2f, 0.2f, 0.2f, 1.f};
296 GLfloat diffuse [] = { 0.8f, 0.8f, 0.8f, 1.f};
297 glEnable (GL_LIGHT0);
298 glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);
299 glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
300
301 G4double ratioX = 1;
302 G4double ratioY = 1;
303 if (fWinSize_y > fWinSize_x) {
304 ratioX = ((G4double)fWinSize_y) / ((G4double)fWinSize_x);
305 }
306 if (fWinSize_x > fWinSize_y) {
307 ratioY = ((G4double)fWinSize_x) / ((G4double)fWinSize_y);
308 }
309
310 // Get radius of scene, etc.
311 // Note that this procedure properly takes into account zoom, dolly and pan.
312 const G4Point3D targetPoint
316 if(radius<=0.) radius = 1.;
317 const G4double cameraDistance = fVP.GetCameraDistance (radius);
318 const G4Point3D cameraPosition =
319 targetPoint + cameraDistance * fVP.GetViewpointDirection().unit();
320 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
321 const GLdouble pfar = fVP.GetFarDistance (cameraDistance, pnear, radius);
322 const GLdouble right = fVP.GetFrontHalfHeight (pnear, radius) * ratioY;
323 const GLdouble left = -right;
324 const GLdouble top = fVP.GetFrontHalfHeight (pnear, radius) * ratioX;
325 const GLdouble bottom = -top;
326
327 // FIXME
328 ResizeGLView();
329 //SHOULD SetWindowsSizeHint()...
330
331 glMatrixMode (GL_PROJECTION); // set up Frustum.
332 glLoadIdentity();
333
334 const G4Vector3D scaleFactor = fVP.GetScaleFactor();
335 glScaled(scaleFactor.x(),scaleFactor.y(),scaleFactor.z());
336
337 if (fVP.GetFieldHalfAngle() == 0.) {
338 g4GlOrtho (left, right, bottom, top, pnear, pfar);
339 }
340 else {
341 g4GlFrustum (left, right, bottom, top, pnear, pfar);
342 }
343
344 glMatrixMode (GL_MODELVIEW); // apply further transformations to scene.
345 glLoadIdentity();
346
347 const G4Normal3D& upVector = fVP.GetUpVector ();
348 G4Point3D gltarget;
349 if (cameraDistance > 1.e-6 * radius) {
350 gltarget = targetPoint;
351 }
352 else {
353 gltarget = targetPoint - radius * fVP.GetViewpointDirection().unit();
354 }
355
356 const G4Point3D& pCamera = cameraPosition; // An alias for brevity.
357
358 g4GluLookAt (pCamera.x(), pCamera.y(), pCamera.z(), // Viewpoint.
359 gltarget.x(), gltarget.y(), gltarget.z(), // Target point.
360 upVector.x(), upVector.y(), upVector.z()); // Up vector.
361 // Light position is "true" light direction, so must come after gluLookAt.
362 glLightfv (GL_LIGHT0, GL_POSITION, lightPosition);
363
364 // The idea is to use back-to-back clipping planes. This can cut an object
365 // down to just a few pixels, which can make it difficult to see. So, for
366 // now, comment this out and use the generic (Boolean) method, via
367 // G4VSolid* G4OpenGLSceneHandler::CreateSectionSolid ()
368 // { return G4VSceneHandler::CreateSectionSolid(); }
369// if (fVP.IsSection () ) { // pair of back to back clip planes.
370// const G4Plane3D& sp = fVP.GetSectionPlane ();
371// double sArray[4];
372// sArray[0] = sp.a();
373// sArray[1] = sp.b();
374// sArray[2] = sp.c();
375// sArray[3] = sp.d() + radius * 1.e-05;
376// glClipPlane (GL_CLIP_PLANE0, sArray);
377// glEnable (GL_CLIP_PLANE0);
378// sArray[0] = -sp.a();
379// sArray[1] = -sp.b();
380// sArray[2] = -sp.c();
381// sArray[3] = -sp.d() + radius * 1.e-05;
382// glClipPlane (GL_CLIP_PLANE1, sArray);
383// glEnable (GL_CLIP_PLANE1);
384// } else {
385// glDisable (GL_CLIP_PLANE0);
386// glDisable (GL_CLIP_PLANE1);
387// }
388
389 // What we call intersection of cutaways is easy in OpenGL. You
390 // just keep cutting. Unions are more tricky - you have to have
391 // multiple passes and this is handled in
392 // G4OpenGLImmediate/StoredViewer::ProcessView.
393 const G4Planes& cutaways = fVP.GetCutawayPlanes();
394 size_t nPlanes = cutaways.size();
395 if (fVP.IsCutaway() &&
397 nPlanes > 0) {
398 double a[4];
399 a[0] = cutaways[0].a();
400 a[1] = cutaways[0].b();
401 a[2] = cutaways[0].c();
402 a[3] = cutaways[0].d();
403 glClipPlane (GL_CLIP_PLANE2, a);
404 glEnable (GL_CLIP_PLANE2);
405 if (nPlanes > 1) {
406 a[0] = cutaways[1].a();
407 a[1] = cutaways[1].b();
408 a[2] = cutaways[1].c();
409 a[3] = cutaways[1].d();
410 glClipPlane (GL_CLIP_PLANE3, a);
411 glEnable (GL_CLIP_PLANE3);
412 }
413 if (nPlanes > 2) {
414 a[0] = cutaways[2].a();
415 a[1] = cutaways[2].b();
416 a[2] = cutaways[2].c();
417 a[3] = cutaways[2].d();
418 glClipPlane (GL_CLIP_PLANE4, a);
419 glEnable (GL_CLIP_PLANE4);
420 }
421 } else {
422 glDisable (GL_CLIP_PLANE2);
423 glDisable (GL_CLIP_PLANE3);
424 glDisable (GL_CLIP_PLANE4);
425 }
426
427 // Background.
429
430}
431
432
433
436 fRot_sens = 1;
437 fPan_sens = 0.01;
438}
439
440
442
443 //To perform haloing, first Draw all information to the depth buffer
444 //alone, using a chunky line width, and then Draw all info again, to
445 //the colour buffer, setting a thinner line width an the depth testing
446 //function to less than or equal, so if two lines cross, the one
447 //passing behind the other will not pass the depth test, and so not
448 //get rendered either side of the infront line for a short distance.
449
450 //First, disable writing to the colo(u)r buffer...
451 glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
452
453 //Now enable writing to the depth buffer...
454 glDepthMask (GL_TRUE);
455 glDepthFunc (GL_LESS);
456 glClearDepth (1.0);
457
458 //Finally, set the line width to something wide...
459 ChangeLineWidth(3.0);
460
461}
462
464
465 //And finally, turn the colour buffer back on with a sesible line width...
466 glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
467 glDepthFunc (GL_LEQUAL);
468 ChangeLineWidth(1.0);
469
470}
471
472G4String G4OpenGLViewer::Pick(GLdouble x, GLdouble y)
473{
474 const std::vector < G4OpenGLViewerPickMap* > & pickMap = GetPickDetails(x,y);
475 G4String txt = "";
476 if (pickMap.size() == 0) {
477// txt += "No hits recorded.";;
478 } else {
479 for (unsigned int a=0; a < pickMap.size(); a++) {
480 if (pickMap[a]->getAttributes().size() > 0) {
481 txt += pickMap[a]->print();
482 }
483 }
484 }
485 return txt;
486}
487
488const std::vector < G4OpenGLViewerPickMap* > & G4OpenGLViewer::GetPickDetails(GLdouble x, GLdouble y)
489{
490 static std::vector < G4OpenGLViewerPickMap* > pickMapVector;
491 for (auto pickMap: pickMapVector) {
492 delete pickMap;
493 }
494 pickMapVector.clear();
495
496 const G4int BUFSIZE = 512;
497 GLuint selectBuffer[BUFSIZE];
498 glSelectBuffer(BUFSIZE, selectBuffer);
499 glRenderMode(GL_SELECT);
500 glInitNames();
501 glPushName(0);
502 glMatrixMode(GL_PROJECTION);
503 G4double currentProjectionMatrix[16];
504 glGetDoublev(GL_PROJECTION_MATRIX, currentProjectionMatrix);
505 glPushMatrix();
506 glLoadIdentity();
507 GLint viewport[4];
508 glGetIntegerv(GL_VIEWPORT, viewport);
509/* G4cout
510 << "viewport, x,y: "
511 << viewport[0] << ',' << viewport[1] << ',' << viewport[2] << ',' << viewport[3]
512 << ", " << x << ',' << y
513 << G4endl;
514*/
515 fIsGettingPickInfos = true;
516 // Define 5x5 pixel pick area
517 g4GluPickMatrix(x, viewport[3] - y, 5., 5., viewport);
518 glMultMatrixd(currentProjectionMatrix);
519 glMatrixMode(GL_MODELVIEW);
520 DrawView();
521 GLint hits = glRenderMode(GL_RENDER);
522 fIsGettingPickInfos = false;
523 if (hits < 0) {
524 G4cout << "Too many hits. Zoom in to reduce overlaps." << G4endl;
525 goto restoreMatrices;
526 }
527 if (hits > 0) {
528 GLuint* p = selectBuffer;
529 for (GLint i = 0; i < hits; ++i) {
530 GLuint nnames = *p++;
531 // This bit of debug code or...
532 //GLuint zmin = *p++;
533 //GLuint zmax = *p++;
534 //G4cout << "Hit " << i << ": " << nnames << " names"
535 // << "\nzmin: " << zmin << ", zmax: " << zmax << G4endl;
536 // ...just increment the pointer
537 p++;
538 p++;
539 for (GLuint j = 0; j < nnames; ++j) {
540 GLuint name = *p++;
541 std::map<GLuint, G4AttHolder*>::iterator iter =
543 if (iter != fOpenGLSceneHandler.fPickMap.end()) {
544 G4AttHolder* attHolder = iter->second;
545 if(attHolder && attHolder->GetAttDefs().size()) {
546 for (size_t iAtt = 0;
547 iAtt < attHolder->GetAttDefs().size(); ++iAtt) {
548 std::ostringstream oss;
549 oss << G4AttCheck(attHolder->GetAttValues()[iAtt],
550 attHolder->GetAttDefs()[iAtt]);
552// G4cout
553// << "i,j, attHolder->GetAttDefs().size(): "
554// << i << ',' << j
555// << ", " << attHolder->GetAttDefs().size()
556// << G4endl;
557// G4cout << "G4OpenGLViewer::GetPickDetails: " << oss.str() << G4endl;
558 pickMap->addAttributes(oss.str());
559 pickMap->setHitNumber(i);
560 pickMap->setSubHitNumber(j);
561 pickMap->setPickName(name);
562 pickMapVector.push_back(pickMap);
563 }
564 }
565 }
566 }
567 }
568 }
569
570restoreMatrices:
571 glMatrixMode(GL_PROJECTION);
572 glPopMatrix();
573 glMatrixMode(GL_MODELVIEW);
574
575 return pickMapVector;
576}
577
579(int inColor, unsigned int width, unsigned int height) {
580
581 GLubyte* buffer;
582 GLint swapbytes, lsbfirst, rowlength;
583 GLint skiprows, skippixels, alignment;
584 GLenum format;
585 int size;
586
587 if (inColor) {
588 format = GL_RGB;
589 size = width*height*3;
590 } else {
591 format = GL_LUMINANCE;
592 size = width*height*1;
593 }
594
595 buffer = new GLubyte[size];
596 if (buffer == NULL)
597 return NULL;
598
599 glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapbytes);
600 glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirst);
601 glGetIntegerv (GL_UNPACK_ROW_LENGTH, &rowlength);
602
603 glGetIntegerv (GL_UNPACK_SKIP_ROWS, &skiprows);
604 glGetIntegerv (GL_UNPACK_SKIP_PIXELS, &skippixels);
605 glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignment);
606
607 glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
608 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
609 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
610
611 glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
612 glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
613 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
614
615 glReadBuffer(GL_FRONT);
616 glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, format, GL_UNSIGNED_BYTE, (GLvoid*) buffer);
617
618 glPixelStorei (GL_UNPACK_SWAP_BYTES, swapbytes);
619 glPixelStorei (GL_UNPACK_LSB_FIRST, lsbfirst);
620 glPixelStorei (GL_UNPACK_ROW_LENGTH, rowlength);
621
622 glPixelStorei (GL_UNPACK_SKIP_ROWS, skiprows);
623 glPixelStorei (GL_UNPACK_SKIP_PIXELS, skippixels);
624 glPixelStorei (GL_UNPACK_ALIGNMENT, alignment);
625
626 return buffer;
627}
628
630 return printGl2PS();
631}
632
634
635 int width = getRealExportWidth();
636 int height = getRealExportHeight();
637
638 FILE* fp;
639 GLubyte* pixels;
640 GLubyte* curpix;
641 int components, pos, i;
642
643 pixels = grabPixels (fPrintColour, width, height);
644
645 if (pixels == NULL) {
646 G4cerr << "Failed to get pixels from OpenGl viewport" << G4endl;
647 return false;
648 }
649 if (fPrintColour) {
650 components = 3;
651 } else {
652 components = 1;
653 }
654 std::string name = getRealPrintFilename();
655 fp = fopen (name.c_str(), "w");
656 if (fp == NULL) {
657 G4cerr << "Can't open filename " << name.c_str() << G4endl;
658 return false;
659 }
660
661 fprintf (fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");
662 fprintf (fp, "%%%%Title: %s\n", name.c_str());
663 fprintf (fp, "%%%%Creator: OpenGL pixmap render output\n");
664 fprintf (fp, "%%%%BoundingBox: 0 0 %d %d\n", width, height);
665 fprintf (fp, "%%%%EndComments\n");
666 fprintf (fp, "gsave\n");
667 fprintf (fp, "/bwproc {\n");
668 fprintf (fp, " rgbproc\n");
669 fprintf (fp, " dup length 3 idiv string 0 3 0 \n");
670 fprintf (fp, " 5 -1 roll {\n");
671 fprintf (fp, " add 2 1 roll 1 sub dup 0 eq\n");
672 fprintf (fp, " { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
673 fprintf (fp, " 3 1 roll 5 -1 roll } put 1 add 3 0 \n");
674 fprintf (fp, " { 2 1 roll } ifelse\n");
675 fprintf (fp, " }forall\n");
676 fprintf (fp, " pop pop pop\n");
677 fprintf (fp, "} def\n");
678 fprintf (fp, "systemdict /colorimage known not {\n");
679 fprintf (fp, " /colorimage {\n");
680 fprintf (fp, " pop\n");
681 fprintf (fp, " pop\n");
682 fprintf (fp, " /rgbproc exch def\n");
683 fprintf (fp, " { bwproc } image\n");
684 fprintf (fp, " } def\n");
685 fprintf (fp, "} if\n");
686 fprintf (fp, "/picstr %d string def\n", width * components);
687 fprintf (fp, "%d %d scale\n", width, height);
688 fprintf (fp, "%d %d %d\n", width, height, 8);
689 fprintf (fp, "[%d 0 0 %d 0 0]\n", width, height);
690 fprintf (fp, "{currentfile picstr readhexstring pop}\n");
691 fprintf (fp, "false %d\n", components);
692 fprintf (fp, "colorimage\n");
693
694 curpix = (GLubyte*) pixels;
695 pos = 0;
696 for (i = width*height*components; i>0; i--) {
697 fprintf (fp, "%02hx ", (unsigned short)(*(curpix++)));
698 if (++pos >= 32) {
699 fprintf (fp, "\n");
700 pos = 0;
701 }
702 }
703 if (pos)
704 fprintf (fp, "\n");
705
706 fprintf (fp, "grestore\n");
707 fprintf (fp, "showpage\n");
708 delete [] pixels;
709 fclose (fp);
710
711 // Reset for next time (useful is size change)
712 // fPrintSizeX = -1;
713 // fPrintSizeY = -1;
714
715 return true;
716}
717
721
722 if (!fGL2PSAction) return false;
724 return true;
725 }
726 return false;
727}
728
729
731 bool check = false;
732#ifdef G4VIS_BUILD_OPENGLQT_DRIVER
733 check = true;
734#endif
735#ifdef G4VIS_BUILD_OPENGLX_DRIVER
736 check = false;
737#endif
738#ifdef G4VIS_BUILD_OPENGLXM_DRIVER
739 check = false;
740#endif
741#ifdef G4VIS_BUILD_OPENGLWIN32_DRIVER
742 check = false;
743#endif
744
745#if GL_ARB_framebuffer_object
746 if (check) {
747// if ( glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_UNDEFINED) {
748// return false;
749// }
750 }
751#endif
752 return true;
753}
754
755
756/* Draw Gl2Ps text if needed
757 */
759{
760 // gl2ps or GL window ?
761 if (isGl2psWriting()) {
762
764 G4double size = fSceneHandler.GetMarkerSize(g4text,sizeType);
765 G4Point3D position = g4text.GetPosition();
766
767 G4String textString = g4text.GetText();
768
769 glRasterPos3d(position.x(),position.y(),position.z());
770 GLint align = GL2PS_TEXT_B;
771
772 switch (g4text.GetLayout()) {
773 case G4Text::left: align = GL2PS_TEXT_BL; break;
774 case G4Text::centre: align = GL2PS_TEXT_B; break;
775 case G4Text::right: align = GL2PS_TEXT_BR;
776 }
777
778 gl2psTextOpt(textString.c_str(),"Times-Roman",GLshort(size),align,0);
779
780 } else {
781
782 static G4int callCount = 0;
783 ++callCount;
784 //if (callCount <= 10 || callCount%100 == 0) {
785 if (callCount <= 1) {
786 G4cout <<
787 "G4OpenGLViewer::DrawText: Not implemented for \""
788 << fName <<
789 "\"\n Called with "
790 << g4text
791 << G4endl;
792 }
793 }
794}
795
799
800 if (isGl2psWriting()) {
801 fGL2PSAction->setPointSize(int(size));
802 } else {
803 glPointSize (size);
804 }
805}
806
807
811
812 if (isGl2psWriting()) {
813 fGL2PSAction->setLineWidth(int(width));
814 } else {
815 glLineWidth (width);
816 }
817}
818
827bool G4OpenGLViewer::exportImage(std::string name, int width, int height) {
828
829 if (! setExportFilename(name)) {
830 return false;
831 }
832
833 if ((width != -1) && (height != -1)) {
834 setExportSize(width, height);
835 }
836
837 if (fExportImageFormat == "eps") {
839 } else if (fExportImageFormat == "ps") {
841 } else if (fExportImageFormat == "svg") {
843 } else if (fExportImageFormat == "pdf") {
845 } else {
846 setExportImageFormat(fExportImageFormat,true); // will display a message if this format is not correct for the current viewer
847 return false;
848 }
849
850 bool res;
851
852 // Change the LC_NUMERIC value in order to have "." separtor and not ","
853 // This case is only useful for French, Canadien...
854 size_t len = strlen(setlocale(LC_NUMERIC,NULL));
855 char* oldLocale = (char*)(malloc(len+1));
856 if(oldLocale!=NULL) strncpy(oldLocale,setlocale(LC_NUMERIC,NULL),len);
857 setlocale(LC_NUMERIC,"C");
858
859 if (((fExportImageFormat == "eps") || (fExportImageFormat == "ps")) && (!fVectoredPs)) {
860 res = printNonVectoredEPS();
861 } else {
862 res = printVectoredEPS();
863 }
864
865 // restore the local
866 if (oldLocale) {
867 setlocale(LC_NUMERIC,oldLocale);
868 free(oldLocale);
869 }
870
871 if (res == false) {
872 G4cerr << "Error saving file... " << getRealPrintFilename().c_str() << G4endl;
873 } else {
874 G4cout << "File " << getRealPrintFilename().c_str() << " size: " << getRealExportWidth() << "x" << getRealExportHeight() << " has been saved " << G4endl;
875
876 // increment index if necessary
877 if ( fExportFilenameIndex != -1) {
879 }
880 }
881
882 return res;
883}
884
885
887
888 int width = getRealExportWidth();
889 int height = getRealExportHeight();
890 bool res = true;
891
892 // no need to redraw at each new primitive for printgl2PS
893 G4OpenGLSceneHandler& oglSceneHandler = dynamic_cast<G4OpenGLSceneHandler&>(fSceneHandler);
894 G4OpenGLSceneHandler::FlushAction originalFlushAction = oglSceneHandler.GetFlushAction();
896
897 if (!fGL2PSAction) return false;
898
900 // try to resize
901 int X = fWinSize_x;
902 int Y = fWinSize_y;
903
904 fWinSize_x = width;
905 fWinSize_y = height;
906 // Laurent G. 16/03/10 : Not the good way to do.
907 // We should draw in a new offscreen context instead of
908 // resizing and drawing in current window...
909 // This should be solve when we will do an offscreen method
910 // to render OpenGL
911 // See :
912 // http://developer.apple.com/Mac/library/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_offscreen/opengl_offscreen.html
913 // http://www.songho.ca/opengl/gl_fbo.html
914
915 ResizeGLView();
916 bool extendBuffer = true;
917 bool endWriteAction = false;
918 bool beginWriteAction = true;
919 bool filePointerOk = true;
920 while ((extendBuffer) && (! endWriteAction) && (filePointerOk)) {
921
922 beginWriteAction = fGL2PSAction->enableFileWriting();
923 // 3 cases :
924 // - true
925 // - false && ! fGL2PSAction->fileWritingEnabled() => bad file name
926 // - false && fGL2PSAction->fileWritingEnabled() => buffer size problem ?
927
928 filePointerOk = fGL2PSAction->fileWritingEnabled();
929
930 if (beginWriteAction) {
931
932 // Set the viewport
933 // By default, we choose the line width (trajectories...)
935 // By default, we choose the point size (markers...)
937
938 DrawView ();
939 endWriteAction = fGL2PSAction->disableFileWriting();
940 }
941 if (filePointerOk) {
942 if ((! endWriteAction) || (! beginWriteAction)) {
943 extendBuffer = fGL2PSAction->extendBufferSize();
944 }
945 }
946 }
948
949 if (!extendBuffer ) {
950 G4cerr << "ERROR: gl2ps buffer size is not big enough to print this geometry. Try to extend it. No output produced"<< G4endl;
951 res = false;
952 }
953 if (!beginWriteAction ) {
954 G4cerr << "ERROR: saving file "<<getRealPrintFilename().c_str()<<". Check read/write access. No output produced" << G4endl;
955 res = false;
956 }
957 if (!endWriteAction ) {
958 G4cerr << "gl2ps error. No output produced" << G4endl;
959 res = false;
960 }
961 fWinSize_x = X;
962 fWinSize_y = Y;
963
964 oglSceneHandler.SetFlushAction(originalFlushAction);
965
966 // Reset for next time (useful is size change)
967 // fPrintSizeX = 0;
968 // fPrintSizeY = 0;
969
970 return res;
971}
972
973unsigned int G4OpenGLViewer::getWinWidth() const{
974 return fWinSize_x;
975}
976
977unsigned int G4OpenGLViewer::getWinHeight() const{
978 return fWinSize_y;
979}
980
982 return fSizeHasChanged;
983}
984
986 if (fPrintSizeX == -1) {
987 return fWinSize_x;
988 }
989 GLint dims[2];
990 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
991
992 // L.Garnier 01-2010: Some problems with mac 10.6
993 if ((dims[0] !=0 ) && (dims[1] !=0)) {
994 if (fPrintSizeX > dims[0]){
995 return dims[0];
996 }
997 }
998 if (fPrintSizeX < -1){
999 return 0;
1000 }
1001 return fPrintSizeX;
1002}
1003
1005 if (fPrintSizeY == -1) {
1006 return fWinSize_y;
1007 }
1008 GLint dims[2];
1009 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
1010
1011 // L.Garnier 01-2010: Some problems with mac 10.6
1012 if ((dims[0] !=0 ) && (dims[1] !=0)) {
1013 if (fPrintSizeY > dims[1]){
1014 return dims[1];
1015 }
1016 }
1017 if (fPrintSizeY < -1){
1018 return 0;
1019 }
1020 return fPrintSizeY;
1021}
1022
1024 fPrintSizeX = X;
1025 fPrintSizeY = Y;
1026}
1027
1035 if (name == "!") {
1036 name = "";
1037 }
1038
1039 if (inc) {
1040 if ((name != "") && (fExportFilename != name)) {
1042 }
1043 } else {
1045 }
1046
1047 if (name.size() == 0) {
1048 name = getRealPrintFilename().c_str();
1049 } else {
1050 // guess format by extention
1051 std::string extension = name.substr(name.find_last_of(".") + 1);
1052 // If there is a dot in the name the above might find rubbish, so...
1053 if (extension.size() >= 3 && extension.size() <= 4) { // Possible extension
1054 if (setExportImageFormat(extension, false)) { // Extension found
1055 fExportFilename = name.substr(0,name.find_last_of("."));
1056 } else { // No viable extension found
1057 return false;
1058 }
1059 } else { // Assume name is already the required without-extension part
1061 }
1062 }
1063 return true;
1064}
1065
1067 std::string temp = fExportFilename;
1068 if (fExportFilenameIndex != -1) {
1069 temp += std::string("_");
1070 std::ostringstream os;
1071 os << std::setw(4) << std::setfill('0') << fExportFilenameIndex;
1072 std::string nb_str = os.str();
1073 temp += nb_str;
1074 }
1075 temp += "."+fExportImageFormat;
1076 return temp;
1077}
1078
1080{
1081 if (!fSceneHandler.GetScene()) {
1082 return 0;
1083 }
1084 const G4Point3D targetPoint
1088 if(radius<=0.) radius = 1.;
1089 const G4double cameraDistance = fVP.GetCameraDistance (radius);
1090 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
1091 return 2 * fVP.GetFrontHalfHeight (pnear, radius);
1092}
1093
1095{
1096 if (!fSceneHandler.GetScene()) {
1097 return 0;
1098 }
1099 const G4Point3D targetPoint
1103 if(radius<=0.) radius = 1.;
1104 const G4double cameraDistance = fVP.GetCameraDistance (radius);
1105 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
1106 const GLdouble pfar = fVP.GetFarDistance (cameraDistance, pnear, radius);
1107 return 2 * fVP.GetFrontHalfHeight (pfar, radius);
1108}
1109
1110
1112{
1113 if (!fSceneHandler.GetScene()) {
1114 return 0;
1115 }
1116 const G4Point3D targetPoint
1120 if(radius<=0.) radius = 1.;
1121 const G4double cameraDistance = fVP.GetCameraDistance (radius);
1122 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
1123 return fVP.GetFarDistance (cameraDistance, pnear, radius)- pnear;
1124}
1125
1126
1127
1129{
1132 } else {
1133 if( dx != 0) {
1134 rotateSceneThetaPhi(dx,0);
1135 }
1136 if( dy != 0) {
1137 rotateSceneThetaPhi(0,dy);
1138 }
1139 }
1140}
1141
1142
1144{
1147 } else {
1148 if( dx != 0) {
1149 rotateSceneThetaPhi(dx,0);
1150 }
1151 if( dy != 0) {
1152 rotateSceneThetaPhi(0,dy);
1153 }
1154 }
1155}
1156
1158{
1159 if (!fSceneHandler.GetScene()) {
1160 return;
1161 }
1162
1163 G4Vector3D vp;
1164 G4Vector3D up;
1165
1166 G4Vector3D xprime;
1167 G4Vector3D yprime;
1168 G4Vector3D zprime;
1169
1170 G4double delta_alpha;
1171 G4double delta_theta;
1172
1173 G4Vector3D new_vp;
1174 G4Vector3D new_up;
1175
1176 G4double cosalpha;
1177 G4double sinalpha;
1178
1179 G4Vector3D a1;
1180 G4Vector3D a2;
1181 G4Vector3D delta;
1182 G4Vector3D viewPoint;
1183
1184
1185 //phi spin stuff here
1186
1187 vp = fVP.GetViewpointDirection ().unit ();
1188 up = fVP.GetUpVector ().unit ();
1189
1190 yprime = (up.cross(vp)).unit();
1191 zprime = (vp.cross(yprime)).unit();
1192
1194 delta_alpha = dy * fRot_sens;
1195 delta_theta = -dx * fRot_sens;
1196 } else {
1197 delta_alpha = -dy * fRot_sens;
1198 delta_theta = dx * fRot_sens;
1199 }
1200
1201 delta_alpha *= CLHEP::deg;
1202 delta_theta *= CLHEP::deg;
1203
1204 new_vp = std::cos(delta_alpha) * vp + std::sin(delta_alpha) * zprime;
1205
1206 // to avoid z rotation flipping
1207 // to allow more than 360∞ rotation
1208
1210 new_up = (new_vp.cross(yprime)).unit();
1211 if (new_vp.z()*vp.z() <0) {
1212 new_up.set(new_up.x(),-new_up.y(),new_up.z());
1213 }
1214 } else {
1215 new_up = up;
1216 if (new_vp.z()*vp.z() <0) {
1217 new_up.set(new_up.x(),-new_up.y(),new_up.z());
1218 }
1219 }
1220 fVP.SetUpVector(new_up);
1222 // Rotates by fixed azimuthal angle delta_theta.
1223
1224 cosalpha = new_up.dot (new_vp.unit());
1225 sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
1226 yprime = (new_up.cross (new_vp.unit())).unit ();
1227 xprime = yprime.cross (new_up);
1228 // Projection of vp on plane perpendicular to up...
1229 a1 = sinalpha * xprime;
1230 // Required new projection...
1231 a2 = sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
1232 // Required Increment vector...
1233 delta = a2 - a1;
1234 // So new viewpoint is...
1235 viewPoint = new_vp.unit() + delta;
1236
1237 fVP.SetViewAndLights (viewPoint);
1238}
1239
1240
1242{
1243 if (!fSceneHandler.GetScene()) {
1244 return;
1245 }
1246
1247 G4Vector3D vp;
1248 G4Vector3D up;
1249
1250 G4Vector3D xprime;
1251 G4Vector3D yprime;
1252 G4Vector3D zprime;
1253
1254 G4Vector3D new_vp;
1255 G4Vector3D new_up;
1256
1257 G4Vector3D a1;
1258 G4Vector3D a2;
1259 G4Vector3D delta;
1260 G4Vector3D viewPoint;
1261
1262 dx = dx/100;
1263 dy = dy/100;
1264
1265 //phi spin stuff here
1266
1267 vp = fVP.GetViewpointDirection ().unit();
1268 up = fVP.GetUpVector ().unit();
1269
1270 G4Vector3D zPrimeVector = G4Vector3D(up.y()*vp.z()-up.z()*vp.y(),
1271 up.z()*vp.x()-up.x()*vp.z(),
1272 up.x()*vp.y()-up.y()*vp.x());
1273
1274 viewPoint = vp/fRot_sens + (zPrimeVector*dx - up*dy) ;
1275 new_up = G4Vector3D(viewPoint.y()*zPrimeVector.z()-viewPoint.z()*zPrimeVector.y(),
1276 viewPoint.z()*zPrimeVector.x()-viewPoint.x()*zPrimeVector.z(),
1277 viewPoint.x()*zPrimeVector.y()-viewPoint.y()*zPrimeVector.x());
1278
1279 G4Vector3D new_upUnit = new_up.unit();
1280
1281
1282
1283 fVP.SetUpVector(new_upUnit);
1284 fVP.SetViewAndLights (viewPoint);
1285}
1286
1287
1289 fExportImageFormatVector.push_back(format);
1290}
1291
1292bool G4OpenGLViewer::setExportImageFormat(std::string format, bool quiet) {
1293 bool found = false;
1294 std::string list;
1295 for (unsigned int a=0; a<fExportImageFormatVector.size(); a++) {
1296 list +=fExportImageFormatVector.at(a) + " ";
1297
1298 if (fExportImageFormatVector.at(a) == format) {
1299 if (! quiet) {
1300 G4cout << " Changing export format to \"" << format << "\"" << G4endl;
1301 }
1302 if (format != fExportImageFormat) {
1304 fExportImageFormat = format;
1305 }
1306 return true;
1307 }
1308 }
1309 if (! found) {
1310 if (format.size() == 0) {
1311 G4cout << " Current formats availables are : " << list << G4endl;
1312 } else {
1313 G4cerr << " Format \"" << format << "\" is not available for the selected viewer. Current formats availables are : " << list << G4endl;
1314 }
1315 }
1316 return false;
1317}
1318
1319
1320// From MESA implementation :
1321// http://www.techques.com/question/1-8660454/gluPickMatrix-code-from-Mesa
1322
1323void G4OpenGLViewer::g4GluPickMatrix(GLdouble x, GLdouble y, GLdouble width, GLdouble height,
1324 GLint viewport[4])
1325 {
1326 GLdouble mat[16];
1327 GLdouble sx, sy;
1328 GLdouble tx, ty;
1329
1330 sx = viewport[2] / width;
1331 sy = viewport[3] / height;
1332 tx = (viewport[2] + 2.0 * (viewport[0] - x)) / width;
1333 ty = (viewport[3] + 2.0 * (viewport[1] - y)) / height;
1334
1335#define M(row, col) mat[col*4+row]
1336 M(0, 0) = sx;
1337 M(0, 1) = 0.0;
1338 M(0, 2) = 0.0;
1339 M(0, 3) = tx;
1340 M(1, 0) = 0.0;
1341 M(1, 1) = sy;
1342 M(1, 2) = 0.0;
1343 M(1, 3) = ty;
1344 M(2, 0) = 0.0;
1345 M(2, 1) = 0.0;
1346 M(2, 2) = 1.0;
1347 M(2, 3) = 0.0;
1348 M(3, 0) = 0.0;
1349 M(3, 1) = 0.0;
1350 M(3, 2) = 0.0;
1351 M(3, 3) = 1.0;
1352#undef M
1353
1354 glMultMatrixd(mat);
1355}
1356
1357
1358
1359
1360
1361// From MESA implementation :
1362// https://github.com/jlamarche/iOS-OpenGLES-Stuff/blob/master/Wavefront%20OBJ%20Loader/Classes/gluLookAt.m
1363// or http://www.daniweb.com/software-development/game-development/threads/308901/lookat-matrix-source-code
1364
1365void G4OpenGLViewer::g4GluLookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
1366 GLdouble centerx, GLdouble centery, GLdouble
1367 centerz,
1368 GLdouble upx, GLdouble upy, GLdouble upz )
1369{
1370 GLdouble mat[16];
1371 GLdouble x[3], y[3], z[3];
1372 GLdouble mag;
1373
1374 /* Make rotation matrix */
1375
1376 /* Z vector */
1377 z[0] = eyex - centerx;
1378 z[1] = eyey - centery;
1379 z[2] = eyez - centerz;
1380 mag = std::sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
1381 if (mag) { /* mpichler, 19950515 */
1382 z[0] /= mag;
1383 z[1] /= mag;
1384 z[2] /= mag;
1385 }
1386
1387 /* Y vector */
1388 y[0] = upx;
1389 y[1] = upy;
1390 y[2] = upz;
1391
1392 /* X vector = Y cross Z */
1393 x[0] = y[1] * z[2] - y[2] * z[1];
1394 x[1] = -y[0] * z[2] + y[2] * z[0];
1395 x[2] = y[0] * z[1] - y[1] * z[0];
1396
1397 /* Recompute Y = Z cross X */
1398 y[0] = z[1] * x[2] - z[2] * x[1];
1399 y[1] = -z[0] * x[2] + z[2] * x[0];
1400 y[2] = z[0] * x[1] - z[1] * x[0];
1401
1402 /* mpichler, 19950515 */
1403 /* cross product gives area of parallelogram, which is < 1.0 for
1404 * non-perpendicular unit-length vectors; so normalize x, y here
1405 */
1406
1407 mag = std::sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
1408 if (mag) {
1409 x[0] /= mag;
1410 x[1] /= mag;
1411 x[2] /= mag;
1412 }
1413
1414 mag = std::sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
1415 if (mag) {
1416 y[0] /= mag;
1417 y[1] /= mag;
1418 y[2] /= mag;
1419 }
1420
1421#define M(row,col) mat[col*4+row]
1422 M(0, 0) = x[0];
1423 M(0, 1) = x[1];
1424 M(0, 2) = x[2];
1425 M(0, 3) = 0.0;
1426 M(1, 0) = y[0];
1427 M(1, 1) = y[1];
1428 M(1, 2) = y[2];
1429 M(1, 3) = 0.0;
1430 M(2, 0) = z[0];
1431 M(2, 1) = z[1];
1432 M(2, 2) = z[2];
1433 M(2, 3) = 0.0;
1434 M(3, 0) = 0.0;
1435 M(3, 1) = 0.0;
1436 M(3, 2) = 0.0;
1437 M(3, 3) = 1.0;
1438#undef M
1439 glMultMatrixd(mat);
1440
1441 /* Translate Eye to Origin */
1442 glTranslated(-eyex, -eyey, -eyez);
1443}
1444
1445void G4OpenGLViewer::g4GlOrtho (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
1446 // glOrtho (left, right, bottom, top, near, far);
1447
1448 GLdouble a = 2.0 / (right - left);
1449 GLdouble b = 2.0 / (top - bottom);
1450 GLdouble c = -2.0 / (zFar - zNear);
1451
1452 GLdouble tx = - (right + left)/(right - left);
1453 GLdouble ty = - (top + bottom)/(top - bottom);
1454 GLdouble tz = - (zFar + zNear)/(zFar - zNear);
1455
1456 GLdouble ortho[16] = {
1457 a, 0, 0, 0,
1458 0, b, 0, 0,
1459 0, 0, c, 0,
1460 tx, ty, tz, 1
1461 };
1462 glMultMatrixd(ortho);
1463
1464}
1465
1466
1467void G4OpenGLViewer::g4GlFrustum (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
1468 // glFrustum (left, right, bottom, top, near, far);
1469
1470 GLdouble deltaX = right - left;
1471 GLdouble deltaY = top - bottom;
1472 GLdouble deltaZ = zFar - zNear;
1473
1474 GLdouble a = 2.0f * zNear / deltaX;
1475 GLdouble b = 2.0f * zNear / deltaY;
1476 GLdouble c = (right + left) / deltaX;
1477 GLdouble d = (top + bottom) / deltaY;
1478 GLdouble e = -(zFar + zNear) / (zFar - zNear);
1479 GLdouble f = -2.0f * zFar * zNear / deltaZ;
1480
1481 GLdouble proj[16] = {
1482 a, 0, 0, 0,
1483 0, b, 0, 0,
1484 c, d, e, -1.0f,
1485 0, 0, f, 0
1486 };
1487
1488 glMultMatrixd(proj);
1489
1490}
1491
1492
1493#ifdef G4OPENGL_VERSION_2
1494
1495// Associate the VBO drawer to the OpenGLViewer and the OpenGLSceneHandler
1496void G4OpenGLViewer::setVboDrawer(G4OpenGLVboDrawer* drawer) {
1497 fVboDrawer = drawer;
1498 try {
1500 sh.setVboDrawer(fVboDrawer);
1501 } catch(std::bad_cast exp) { }
1502}
1503
1504#endif
1505
1506
1508 std::ostringstream txt;
1509 for (unsigned int a=0; a<fAttributes.size(); a++) {
1510 txt << fAttributes[a];
1511 if (a < fAttributes.size() - 1) txt << "\n";
1512 }
1513 return txt.str();
1514}
G4double Y(G4double density)
static const G4double pos
#define M(row, col)
double G4double
Definition: G4Types.hh:83
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
HepGeom::Vector3D< G4double > G4Vector3D
Definition: G4Vector3D.hh:34
std::vector< G4Plane3D > G4Planes
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
#define gl2psTextOpt
Definition: Geant4_gl2ps.h:44
const std::vector< const std::vector< G4AttValue > * > & GetAttValues() const
Definition: G4AttHolder.hh:59
const std::vector< const std::map< G4String, G4AttDef > * > & GetAttDefs() const
Definition: G4AttHolder.hh:61
G4double GetBlue() const
Definition: G4Colour.hh:154
G4double GetRed() const
Definition: G4Colour.hh:152
G4double GetGreen() const
Definition: G4Colour.hh:153
bool enableFileWriting()
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
void setExportImageFormat(unsigned int)
void setLineWidth(int)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
bool disableFileWriting()
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
bool fileWritingEnabled() const
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
void setPointSize(int)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
void setFileName(const char *)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
bool extendBufferSize()
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
void resetBufferSizeParameters()
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
static void SetFlushAction(FlushAction action)
static FlushAction GetFlushAction()
std::map< GLuint, G4AttHolder * > fPickMap
void setSubHitNumber(G4int n)
void setHitNumber(G4int n)
void addAttributes(G4String att)
void setPickName(G4int n)
std::vector< G4String > fAttributes
unsigned int fWinSize_y
G4String fExportFilename
G4int getRealExportHeight()
void rotateSceneInViewDirection(G4double dx, G4double dy)
void g4GluPickMatrix(GLdouble x, GLdouble y, GLdouble width, GLdouble height, GLint viewport[4])
void g4GlFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far)
std::vector< std::string > fExportImageFormatVector
void g4GluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, GLdouble upz)
void rotateSceneToggle(G4double dx, G4double dy)
bool setExportImageFormat(std::string format, bool quiet=false)
unsigned int getWinHeight() const
std::string fExportImageFormat
void ClearViewWithoutFlush()
void ResizeWindow(unsigned int, unsigned int)
unsigned int getWinWidth() const
bool setExportFilename(G4String name, G4bool inc=true)
G4OpenGLViewer(G4OpenGLSceneHandler &scene)
void addExportImageFormat(std::string format)
G4String fDefaultExportFilename
void ChangeLineWidth(G4double width)
bool printNonVectoredEPS()
virtual void DrawText(const G4Text &)
GLdouble getSceneFarWidth()
void setExportSize(G4int, G4int)
virtual G4String Pick(GLdouble x, GLdouble y)
G4OpenGL2PSAction * fGL2PSAction
void rotateScene(G4double dx, G4double dy)
void ChangePointSize(G4double size)
GLdouble getSceneDepth()
G4bool isFramebufferReady()
GLubyte * grabPixels(int inColor, unsigned int width, unsigned int height)
virtual bool exportImage(std::string name="", int width=-1, int height=-1)
void g4GlOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far)
G4bool isGl2psWriting()
void rotateSceneThetaPhi(G4double dx, G4double dy)
G4bool sizeHasChanged()
G4OpenGLSceneHandler & fOpenGLSceneHandler
G4int getRealExportWidth()
virtual ~G4OpenGLViewer()
unsigned int fWinSize_x
void HaloingSecondPass()
std::string getRealPrintFilename()
GLdouble getSceneNearWidth()
const std::vector< G4OpenGLViewerPickMap * > & GetPickDetails(GLdouble x, GLdouble y)
const G4VisExtent & GetExtent() const
const G4Point3D & GetStandardTargetPoint() const
Definition: G4Text.hh:72
Layout GetLayout() const
G4String GetText() const
@ centre
Definition: G4Text.hh:76
@ right
Definition: G4Text.hh:76
@ left
Definition: G4Text.hh:76
G4Point3D GetPosition() const
G4Scene * GetScene() const
G4double GetMarkerSize(const G4VMarker &, MarkerSizeType &)
virtual void DrawView()=0
G4VSceneHandler & fSceneHandler
Definition: G4VViewer.hh:215
G4String fName
Definition: G4VViewer.hh:217
G4ViewParameters fDefaultVP
Definition: G4VViewer.hh:220
const G4String & GetShortName() const
G4ViewParameters fVP
Definition: G4VViewer.hh:219
virtual void FinishView()
Definition: G4VViewer.cc:101
virtual void ResetView()
void SetViewAndLights(const G4Vector3D &viewpointDirection)
const G4Vector3D & GetScaleFactor() const
void SetAutoRefresh(G4bool)
CutawayMode GetCutawayMode() const
G4double GetCameraDistance(G4double radius) const
unsigned int GetWindowSizeHintX() const
G4bool IsCutaway() const
G4Vector3D & GetActualLightpointDirection()
const G4Colour & GetBackgroundColour() const
const G4Vector3D & GetViewpointDirection() const
const G4Point3D & GetCurrentTargetPoint() const
G4double GetFarDistance(G4double cameraDistance, G4double nearDistance, G4double radius) const
G4double GetFieldHalfAngle() const
G4double GetFrontHalfHeight(G4double nearDistance, G4double radius) const
const G4Vector3D & GetUpVector() const
void SetUpVector(const G4Vector3D &upVector)
const G4Planes & GetCutawayPlanes() const
RotationStyle GetRotationStyle() const
unsigned int GetWindowSizeHintY() const
G4bool GetLightsMoveWithCamera() const
G4double GetNearDistance(G4double cameraDistance, G4double radius) const
G4double GetExtentRadius() const
Definition: G4VisExtent.cc:75
BasicVector3D< T > cross(const BasicVector3D< T > &v) const
BasicVector3D< T > unit() const
void set(T x1, T y1, T z1)
T dot(const BasicVector3D< T > &v) const
#define GL2PS_SVG
Definition: gl2ps.h:133
#define GL2PS_TEXT_BL
Definition: gl2ps.h:198
#define GL2PS_TEXT_BR
Definition: gl2ps.h:199
#define GL2PS_EPS
Definition: gl2ps.h:130
#define GL2PS_PDF
Definition: gl2ps.h:132
#define GL2PS_PS
Definition: gl2ps.h:129
#define GL2PS_TEXT_B
Definition: gl2ps.h:197
#define BUFSIZE
Definition: liblist.c:40
static constexpr double deg
const char * name(G4int ptype)
#define buffer
Definition: xmlparse.cc:628