33 #ifdef G4VIS_BUILD_OPENGL_DRIVER
53 #ifdef G4OPENGL_VERSION_2
65 G4OpenGLViewer::G4OpenGLViewer (G4OpenGLSceneHandler& scene):
67 #ifdef G4OPENGL_VERSION_2
72 fOpenGLSceneHandler(scene),
74 transparency_enabled (true),
75 antialiasing_enabled (false),
76 haloing_enabled (false),
81 fDefaultExportImageFormat(
"pdf"),
82 fExportImageFormat(
"pdf"),
83 fExportFilenameIndex(0),
87 fDefaultExportFilename(
"G4OpenGL"),
89 fGl2psDefaultLineWith(1),
90 fGl2psDefaultPointSize(2),
91 fGlViewInitialized(false),
92 fIsGettingPickInfos(false)
93 #ifdef G4OPENGL_VERSION_2
95 ,fVertexPositionAttribute(0)
96 ,fVertexNormalAttribute(0)
104 fVP.SetAutoRefresh(
true);
105 fDefaultVP.SetAutoRefresh(
true);
110 addExportImageFormat(
"eps");
111 addExportImageFormat(
"ps");
112 addExportImageFormat(
"pdf");
113 addExportImageFormat(
"svg");
116 fExportFilename += fDefaultExportFilename +
"_" + GetShortName().data();
126 G4OpenGLViewer::~G4OpenGLViewer ()
131 void G4OpenGLViewer::InitializeGLView ()
133 #ifdef G4OPENGL_VERSION_2
137 fShaderProgram = glCreateProgram();
138 Shader vertexShader = glCreateShader(GL_VERTEX_SHADER);
139 const char * vSrc = fVboDrawer->getVertexShaderSrc();
140 glShaderSource(vertexShader, 1, &vSrc, NULL);
141 glCompileShader(vertexShader);
142 glAttachShader(fShaderProgram, vertexShader);
144 Shader fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
145 const char * fSrc = fVboDrawer->getFragmentShaderSrc();
146 glShaderSource(fragmentShader, 1, &fSrc, NULL);
147 glCompileShader(fragmentShader);
149 glAttachShader(fShaderProgram, fragmentShader);
150 glLinkProgram(fShaderProgram);
151 glUseProgram(fShaderProgram);
158 fVertexPositionAttribute =
159 glGetAttribLocation(fShaderProgram,
"aVertexPosition");
162 glEnableVertexAttribArray(fVertexPositionAttribute);
165 fpMatrixUniform = glGetUniformLocation(fShaderProgram,
"uPMatrix");
166 fcMatrixUniform = glGetUniformLocation(fShaderProgram,
"uCMatrix");
167 fmvMatrixUniform = glGetUniformLocation(fShaderProgram,
"uMVMatrix");
168 fnMatrixUniform = glGetUniformLocation(fShaderProgram,
"uNMatrix");
169 ftMatrixUniform = glGetUniformLocation(fShaderProgram,
"uTMatrix");
177 fGlViewInitialized =
true;
181 if (fWinSize_x == 0) {
182 fWinSize_x = fVP.GetWindowSizeHintX();
184 if (fWinSize_y == 0) {
185 fWinSize_y = fVP.GetWindowSizeHintY();
188 glClearColor (0.0, 0.0, 0.0, 0.0);
190 #ifndef G4OPENGL_VERSION_2
191 glDisable (GL_LINE_SMOOTH);
192 glDisable (GL_POLYGON_SMOOTH);
199 glDepthFunc (GL_LEQUAL);
200 glDepthMask (GL_TRUE);
203 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
207 void G4OpenGLViewer::ClearView () {
208 ClearViewWithoutFlush();
210 if(!isFramebufferReady()) {
218 void G4OpenGLViewer::ClearViewWithoutFlush () {
221 if(!isFramebufferReady()) {
225 glClearColor (background.GetRed(),
226 background.GetGreen(),
227 background.GetBlue(),
232 glClear (GL_COLOR_BUFFER_BIT);
233 glClear (GL_DEPTH_BUFFER_BIT);
234 glClear (GL_STENCIL_BUFFER_BIT);
238 void G4OpenGLViewer::ResizeWindow(
unsigned int aWidth,
unsigned int aHeight) {
239 if ((fWinSize_x != aWidth) || (fWinSize_y != aHeight)) {
241 fWinSize_y = aHeight;
242 fSizeHasChanged =
true;
244 fSizeHasChanged =
false;
254 void G4OpenGLViewer::ResizeGLView()
261 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
263 if ((dims[0] !=0 ) && (dims[1] !=0)) {
265 if (fWinSize_x > (
unsigned)dims[0]) {
266 G4cerr <<
"Try to resize view greater than max X viewport dimension. Desired size "<<fWinSize_x <<
" is resize to "<< dims[0] <<
G4endl;
267 fWinSize_x = dims[0];
269 if (fWinSize_y > (
unsigned)dims[1]) {
270 G4cerr <<
"Try to resize view greater than max Y viewport dimension. Desired size "<<fWinSize_y <<
" is resize to "<< dims[1] <<
G4endl;
271 fWinSize_y = dims[1];
275 glViewport(0, 0, fWinSize_x,fWinSize_y);
281 void G4OpenGLViewer::SetView () {
283 if (fIsGettingPickInfos)
return;
285 if (!fSceneHandler.GetScene()) {
293 GLfloat lightPosition [4];
294 lightPosition [0] = fVP.GetActualLightpointDirection().x();
295 lightPosition [1] = fVP.GetActualLightpointDirection().y();
296 lightPosition [2] = fVP.GetActualLightpointDirection().z();
297 lightPosition [3] = 0.;
299 GLfloat ambient [] = { 0.2f, 0.2f, 0.2f, 1.f};
300 GLfloat diffuse [] = { 0.8f, 0.8f, 0.8f, 1.f};
301 glEnable (GL_LIGHT0);
302 glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);
303 glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
307 if (fWinSize_y > fWinSize_x) {
310 if (fWinSize_x > fWinSize_y) {
317 = fSceneHandler.GetScene()->GetStandardTargetPoint()
318 + fVP.GetCurrentTargetPoint ();
319 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
320 if(radius<=0.) radius = 1.;
321 const G4double cameraDistance = fVP.GetCameraDistance (radius);
323 targetPoint + cameraDistance * fVP.GetViewpointDirection().
unit();
324 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
325 const GLdouble pfar = fVP.GetFarDistance (cameraDistance, pnear, radius);
326 const GLdouble
right = fVP.GetFrontHalfHeight (pnear, radius) * ratioY;
328 const GLdouble top = fVP.GetFrontHalfHeight (pnear, radius) * ratioX;
329 const GLdouble bottom = -top;
335 glMatrixMode (GL_PROJECTION);
338 const G4Vector3D scaleFactor = fVP.GetScaleFactor();
339 glScaled(scaleFactor.
x(),scaleFactor.
y(),scaleFactor.
z());
341 if (fVP.GetFieldHalfAngle() == 0.) {
342 g4GlOrtho (left, right, bottom, top, pnear, pfar);
345 g4GlFrustum (left, right, bottom, top, pnear, pfar);
348 glMatrixMode (GL_MODELVIEW);
351 const G4Normal3D& upVector = fVP.GetUpVector ();
353 if (cameraDistance > 1.
e-6 * radius) {
354 gltarget = targetPoint;
357 gltarget = targetPoint - radius * fVP.GetViewpointDirection().
unit();
360 const G4Point3D& pCamera = cameraPosition;
362 g4GluLookAt (pCamera.
x(), pCamera.
y(), pCamera.
z(),
363 gltarget.
x(), gltarget.
y(), gltarget.
z(),
364 upVector.
x(), upVector.
y(), upVector.
z());
366 glLightfv (GL_LIGHT0, GL_POSITION, lightPosition);
397 const G4Planes& cutaways = fVP.GetCutawayPlanes();
398 size_t nPlanes = cutaways.size();
399 if (fVP.IsCutaway() &&
403 a[0] = cutaways[0].a();
404 a[1] = cutaways[0].b();
405 a[2] = cutaways[0].c();
406 a[3] = cutaways[0].d();
407 glClipPlane (GL_CLIP_PLANE2, a);
408 glEnable (GL_CLIP_PLANE2);
410 a[0] = cutaways[1].a();
411 a[1] = cutaways[1].b();
412 a[2] = cutaways[1].c();
413 a[3] = cutaways[1].d();
414 glClipPlane (GL_CLIP_PLANE3, a);
415 glEnable (GL_CLIP_PLANE3);
418 a[0] = cutaways[2].a();
419 a[1] = cutaways[2].b();
420 a[2] = cutaways[2].c();
421 a[3] = cutaways[2].d();
422 glClipPlane (GL_CLIP_PLANE4, a);
423 glEnable (GL_CLIP_PLANE4);
426 glDisable (GL_CLIP_PLANE2);
427 glDisable (GL_CLIP_PLANE3);
428 glDisable (GL_CLIP_PLANE4);
432 background = fVP.GetBackgroundColour ();
438 void G4OpenGLViewer::ResetView () {
445 void G4OpenGLViewer::HaloingFirstPass () {
455 glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
458 glDepthMask (GL_TRUE);
459 glDepthFunc (GL_LESS);
463 ChangeLineWidth(3.0);
467 void G4OpenGLViewer::HaloingSecondPass () {
470 glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
471 glDepthFunc (GL_LEQUAL);
472 ChangeLineWidth(1.0);
476 G4String G4OpenGLViewer::Pick(GLdouble
x, GLdouble
y)
478 const std::vector < G4OpenGLViewerPickMap* > & pickMap = GetPickDetails(x,y);
480 if (pickMap.size() == 0) {
483 for (
unsigned int a=0; a < pickMap.size(); a++) {
484 if (pickMap[a]->getAttributes().size() > 0) {
485 txt += pickMap[
a]->print();
492 const std::vector < G4OpenGLViewerPickMap* > & G4OpenGLViewer::GetPickDetails(GLdouble x, GLdouble y)
494 static std::vector < G4OpenGLViewerPickMap* > pickMapVector;
495 for (
auto pickMap: pickMapVector) {
498 pickMapVector.clear();
500 const G4int BUFSIZE = 512;
501 GLuint selectBuffer[BUFSIZE];
502 glSelectBuffer(BUFSIZE, selectBuffer);
503 glRenderMode(GL_SELECT);
506 glMatrixMode(GL_PROJECTION);
507 G4double currentProjectionMatrix[16];
508 glGetDoublev(GL_PROJECTION_MATRIX, currentProjectionMatrix);
512 glGetIntegerv(GL_VIEWPORT, viewport);
519 fIsGettingPickInfos =
true;
521 g4GluPickMatrix(x, viewport[3] - y, 5., 5., viewport);
522 glMultMatrixd(currentProjectionMatrix);
523 glMatrixMode(GL_MODELVIEW);
525 GLint
hits = glRenderMode(GL_RENDER);
526 fIsGettingPickInfos =
false;
528 G4cout <<
"Too many hits. Zoom in to reduce overlaps." <<
G4endl;
529 goto restoreMatrices;
532 GLuint*
p = selectBuffer;
533 for (GLint i = 0; i <
hits; ++i) {
534 GLuint nnames = *p++;
543 for (GLuint j = 0; j < nnames; ++j) {
545 std::map<GLuint, G4AttHolder*>::iterator iter =
546 fOpenGLSceneHandler.fPickMap.find(name);
547 if (iter != fOpenGLSceneHandler.fPickMap.end()) {
549 if(attHolder && attHolder->
GetAttDefs().size()) {
550 for (
size_t iAtt = 0;
551 iAtt < attHolder->
GetAttDefs().size(); ++iAtt) {
552 std::ostringstream oss;
555 G4OpenGLViewerPickMap* pickMap =
new G4OpenGLViewerPickMap();
562 pickMap->addAttributes(oss.str());
563 pickMap->setHitNumber(i);
564 pickMap->setSubHitNumber(j);
565 pickMap->setPickName(name);
566 pickMapVector.push_back(pickMap);
575 glMatrixMode(GL_PROJECTION);
577 glMatrixMode(GL_MODELVIEW);
579 return pickMapVector;
582 GLubyte* G4OpenGLViewer::grabPixels
583 (
int inColor,
unsigned int width,
unsigned int height) {
586 GLint swapbytes, lsbfirst, rowlength;
587 GLint skiprows, skippixels, alignment;
593 size = width*height*3;
595 format = GL_LUMINANCE;
596 size = width*height*1;
599 buffer =
new GLubyte[size];
603 glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapbytes);
604 glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirst);
605 glGetIntegerv (GL_UNPACK_ROW_LENGTH, &rowlength);
607 glGetIntegerv (GL_UNPACK_SKIP_ROWS, &skiprows);
608 glGetIntegerv (GL_UNPACK_SKIP_PIXELS, &skippixels);
609 glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignment);
611 glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
612 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
613 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
615 glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
616 glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
617 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
619 glReadBuffer(GL_FRONT);
620 glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, format, GL_UNSIGNED_BYTE, (GLvoid*) buffer);
622 glPixelStorei (GL_UNPACK_SWAP_BYTES, swapbytes);
623 glPixelStorei (GL_UNPACK_LSB_FIRST, lsbfirst);
624 glPixelStorei (GL_UNPACK_ROW_LENGTH, rowlength);
626 glPixelStorei (GL_UNPACK_SKIP_ROWS, skiprows);
627 glPixelStorei (GL_UNPACK_SKIP_PIXELS, skippixels);
628 glPixelStorei (GL_UNPACK_ALIGNMENT, alignment);
633 bool G4OpenGLViewer::printEPS() {
638 size_t len = strlen(setlocale(LC_NUMERIC,NULL));
639 char* oldLocale = (
char*)(malloc(len+1));
640 if(oldLocale!=NULL) strncpy(oldLocale,setlocale(LC_NUMERIC,NULL),len);
641 setlocale(LC_NUMERIC,
"C");
643 if (((fExportImageFormat ==
"eps") || (fExportImageFormat ==
"ps")) && (!fVectoredPs)) {
644 res = printNonVectoredEPS();
646 res = printVectoredEPS();
651 setlocale(LC_NUMERIC,oldLocale);
656 G4cerr <<
"Error saving file... " << getRealPrintFilename().c_str() <<
G4endl;
658 G4cout <<
"File " << getRealPrintFilename().c_str() <<
" size: " << getRealExportWidth() <<
"x" << getRealExportHeight() <<
" has been saved " <<
G4endl;
661 if ( fExportFilenameIndex != -1) {
662 fExportFilenameIndex++;
669 bool G4OpenGLViewer::printVectoredEPS() {
673 bool G4OpenGLViewer::printNonVectoredEPS () {
675 int width = getRealExportWidth();
676 int height = getRealExportHeight();
681 int components,
pos, i;
683 pixels = grabPixels (fPrintColour, width, height);
685 if (pixels == NULL) {
686 G4cerr <<
"Failed to get pixels from OpenGl viewport" <<
G4endl;
694 std::string name = getRealPrintFilename();
695 fp = fopen (name.c_str(),
"w");
697 G4cerr <<
"Can't open filename " << name.c_str() <<
G4endl;
701 fprintf (fp,
"%%!PS-Adobe-2.0 EPSF-1.2\n");
702 fprintf (fp,
"%%%%Title: %s\n", name.c_str());
703 fprintf (fp,
"%%%%Creator: OpenGL pixmap render output\n");
704 fprintf (fp,
"%%%%BoundingBox: 0 0 %d %d\n", width, height);
705 fprintf (fp,
"%%%%EndComments\n");
706 fprintf (fp,
"gsave\n");
707 fprintf (fp,
"/bwproc {\n");
708 fprintf (fp,
" rgbproc\n");
709 fprintf (fp,
" dup length 3 idiv string 0 3 0 \n");
710 fprintf (fp,
" 5 -1 roll {\n");
711 fprintf (fp,
" add 2 1 roll 1 sub dup 0 eq\n");
712 fprintf (fp,
" { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
713 fprintf (fp,
" 3 1 roll 5 -1 roll } put 1 add 3 0 \n");
714 fprintf (fp,
" { 2 1 roll } ifelse\n");
715 fprintf (fp,
" }forall\n");
716 fprintf (fp,
" pop pop pop\n");
717 fprintf (fp,
"} def\n");
718 fprintf (fp,
"systemdict /colorimage known not {\n");
719 fprintf (fp,
" /colorimage {\n");
720 fprintf (fp,
" pop\n");
721 fprintf (fp,
" pop\n");
722 fprintf (fp,
" /rgbproc exch def\n");
723 fprintf (fp,
" { bwproc } image\n");
724 fprintf (fp,
" } def\n");
725 fprintf (fp,
"} if\n");
726 fprintf (fp,
"/picstr %d string def\n", width * components);
727 fprintf (fp,
"%d %d scale\n", width, height);
728 fprintf (fp,
"%d %d %d\n", width, height, 8);
729 fprintf (fp,
"[%d 0 0 %d 0 0]\n", width, height);
730 fprintf (fp,
"{currentfile picstr readhexstring pop}\n");
731 fprintf (fp,
"false %d\n", components);
732 fprintf (fp,
"colorimage\n");
734 curpix = (GLubyte*) pixels;
736 for (i = width*height*components; i>0; i--) {
737 fprintf (fp,
"%02hx ", (
unsigned short)(*(curpix++)));
746 fprintf (fp,
"grestore\n");
747 fprintf (fp,
"showpage\n");
760 bool G4OpenGLViewer::isGl2psWriting() {
762 if (!fGL2PSAction)
return false;
763 if (fGL2PSAction->fileWritingEnabled()) {
770 G4bool G4OpenGLViewer::isFramebufferReady() {
772 #ifdef G4VIS_BUILD_OPENGLQT_DRIVER
775 #ifdef G4VIS_BUILD_OPENGLX_DRIVER
778 #ifdef G4VIS_BUILD_OPENGLXM_DRIVER
781 #ifdef G4VIS_BUILD_OPENGLWIN32_DRIVER
785 #if GL_ARB_framebuffer_object
798 void G4OpenGLViewer::DrawText(
const G4Text& g4text)
801 if (isGl2psWriting()) {
804 G4double size = fSceneHandler.GetMarkerSize(g4text,sizeType);
809 glRasterPos3d(position.
x(),position.
y(),position.
z());
818 gl2psTextOpt(textString.c_str(),
"Times-Roman",GLshort(size),align,0);
822 static G4int callCount = 0;
825 if (callCount <= 1) {
827 "G4OpenGLViewer::DrawText: Not implemented for \""
838 void G4OpenGLViewer::ChangePointSize(
G4double size) {
840 if (isGl2psWriting()) {
841 fGL2PSAction->setPointSize(
int(size));
850 void G4OpenGLViewer::ChangeLineWidth(
G4double width) {
852 if (isGl2psWriting()) {
853 fGL2PSAction->setLineWidth(
int(width));
867 bool G4OpenGLViewer::exportImage(std::string name,
int width,
int height) {
869 if (! setExportFilename(name)) {
873 if ((width != -1) && (height != -1)) {
874 setExportSize(width, height);
877 if (fExportImageFormat ==
"eps") {
878 fGL2PSAction->setExportImageFormat(
GL2PS_EPS);
879 }
else if (fExportImageFormat ==
"ps") {
880 fGL2PSAction->setExportImageFormat(
GL2PS_PS);
881 }
else if (fExportImageFormat ==
"svg") {
882 fGL2PSAction->setExportImageFormat(
GL2PS_SVG);
883 }
else if (fExportImageFormat ==
"pdf") {
884 fGL2PSAction->setExportImageFormat(
GL2PS_PDF);
886 setExportImageFormat(fExportImageFormat,
true);
893 bool G4OpenGLViewer::printGl2PS() {
895 int width = getRealExportWidth();
896 int height = getRealExportHeight();
900 G4OpenGLSceneHandler& oglSceneHandler =
dynamic_cast<G4OpenGLSceneHandler&
>(fSceneHandler);
901 G4OpenGLSceneHandler::FlushAction originalFlushAction = oglSceneHandler.GetFlushAction();
902 oglSceneHandler.SetFlushAction(G4OpenGLSceneHandler::never);
904 if (!fGL2PSAction)
return false;
906 fGL2PSAction->setFileName(getRealPrintFilename().c_str());
923 bool extendBuffer =
true;
924 bool endWriteAction =
false;
925 bool beginWriteAction =
true;
926 bool filePointerOk =
true;
927 while ((extendBuffer) && (! endWriteAction) && (filePointerOk)) {
929 beginWriteAction = fGL2PSAction->enableFileWriting();
935 filePointerOk = fGL2PSAction->fileWritingEnabled();
937 if (beginWriteAction) {
941 fGL2PSAction->setLineWidth(fGl2psDefaultLineWith);
943 fGL2PSAction->setPointSize(fGl2psDefaultPointSize);
946 endWriteAction = fGL2PSAction->disableFileWriting();
949 if ((! endWriteAction) || (! beginWriteAction)) {
950 extendBuffer = fGL2PSAction->extendBufferSize();
954 fGL2PSAction->resetBufferSizeParameters();
956 if (!extendBuffer ) {
957 G4cerr <<
"ERROR: gl2ps buffer size is not big enough to print this geometry. Try to extend it. No output produced"<<
G4endl;
960 if (!beginWriteAction ) {
961 G4cerr <<
"ERROR: saving file "<<getRealPrintFilename().c_str()<<
". Check read/write access. No output produced" <<
G4endl;
964 if (!endWriteAction ) {
971 oglSceneHandler.SetFlushAction(originalFlushAction);
980 unsigned int G4OpenGLViewer::getWinWidth()
const{
984 unsigned int G4OpenGLViewer::getWinHeight()
const{
988 G4bool G4OpenGLViewer::sizeHasChanged() {
989 return fSizeHasChanged;
992 G4int G4OpenGLViewer::getRealExportWidth() {
993 if (fPrintSizeX == -1) {
997 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
1000 if ((dims[0] !=0 ) && (dims[1] !=0)) {
1001 if (fPrintSizeX > dims[0]){
1005 if (fPrintSizeX < -1){
1011 G4int G4OpenGLViewer::getRealExportHeight() {
1012 if (fPrintSizeY == -1) {
1016 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
1019 if ((dims[0] !=0 ) && (dims[1] !=0)) {
1020 if (fPrintSizeY > dims[1]){
1024 if (fPrintSizeY < -1){
1030 void G4OpenGLViewer::setExportSize(
G4int X,
G4int Y) {
1041 bool G4OpenGLViewer::setExportFilename(
G4String name,
G4bool inc) {
1047 if ((name !=
"") && (fExportFilename != name)) {
1048 fExportFilenameIndex=0;
1051 fExportFilenameIndex=-1;
1054 if (name.size() == 0) {
1055 name = getRealPrintFilename().c_str();
1058 std::string extension = name.substr(name.find_last_of(
".") + 1);
1060 if (name.size() != extension.size()) {
1061 if (! setExportImageFormat(extension,
false)) {
1066 fExportFilename = name.substr(0,name.find_last_of(
"."));
1071 std::string G4OpenGLViewer::getRealPrintFilename() {
1072 std::string temp = fExportFilename;
1073 if (fExportFilenameIndex != -1) {
1074 temp += std::string(
"_");
1075 std::ostringstream os;
1076 os << std::setw(4) << std::setfill(
'0') << fExportFilenameIndex;
1077 std::string nb_str = os.str();
1080 temp +=
"."+fExportImageFormat;
1084 GLdouble G4OpenGLViewer::getSceneNearWidth()
1086 if (!fSceneHandler.GetScene()) {
1090 = fSceneHandler.GetScene()->GetStandardTargetPoint()
1091 + fVP.GetCurrentTargetPoint ();
1092 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
1093 if(radius<=0.) radius = 1.;
1094 const G4double cameraDistance = fVP.GetCameraDistance (radius);
1095 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
1096 return 2 * fVP.GetFrontHalfHeight (pnear, radius);
1099 GLdouble G4OpenGLViewer::getSceneFarWidth()
1101 if (!fSceneHandler.GetScene()) {
1105 = fSceneHandler.GetScene()->GetStandardTargetPoint()
1106 + fVP.GetCurrentTargetPoint ();
1107 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
1108 if(radius<=0.) radius = 1.;
1109 const G4double cameraDistance = fVP.GetCameraDistance (radius);
1110 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
1111 const GLdouble pfar = fVP.GetFarDistance (cameraDistance, pnear, radius);
1112 return 2 * fVP.GetFrontHalfHeight (pfar, radius);
1116 GLdouble G4OpenGLViewer::getSceneDepth()
1118 if (!fSceneHandler.GetScene()) {
1122 = fSceneHandler.GetScene()->GetStandardTargetPoint()
1123 + fVP.GetCurrentTargetPoint ();
1124 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
1125 if(radius<=0.) radius = 1.;
1126 const G4double cameraDistance = fVP.GetCameraDistance (radius);
1127 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
1128 return fVP.GetFarDistance (cameraDistance, pnear, radius)- pnear;
1136 rotateSceneInViewDirection(dx,dy);
1139 rotateSceneThetaPhi(dx,0);
1142 rotateSceneThetaPhi(0,dy);
1151 rotateSceneInViewDirection(dx,dy);
1154 rotateSceneThetaPhi(dx,0);
1157 rotateSceneThetaPhi(0,dy);
1164 if (!fSceneHandler.GetScene()) {
1192 vp = fVP.GetViewpointDirection ().
unit ();
1193 up = fVP.GetUpVector ().
unit ();
1195 yprime = (up.
cross(vp)).unit();
1196 zprime = (vp.
cross(yprime)).unit();
1198 if (fVP.GetLightsMoveWithCamera()) {
1199 delta_alpha = dy * fRot_sens;
1200 delta_theta = -dx * fRot_sens;
1202 delta_alpha = -dy * fRot_sens;
1203 delta_theta = dx * fRot_sens;
1209 new_vp = std::cos(delta_alpha) * vp + std::sin(delta_alpha) * zprime;
1214 if (fVP.GetLightsMoveWithCamera()) {
1215 new_up = (new_vp.
cross(yprime)).unit();
1216 if (new_vp.
z()*vp.
z() <0) {
1217 new_up.
set(new_up.
x(),-new_up.
y(),new_up.
z());
1221 if (new_vp.
z()*vp.
z() <0) {
1222 new_up.
set(new_up.
x(),-new_up.
y(),new_up.
z());
1225 fVP.SetUpVector(new_up);
1229 cosalpha = new_up.
dot (new_vp.
unit());
1230 sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
1232 xprime = yprime.
cross (new_up);
1234 a1 = sinalpha * xprime;
1236 a2 = sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
1240 viewPoint = new_vp.
unit() + delta;
1242 fVP.SetViewAndLights (viewPoint);
1248 if (!fSceneHandler.GetScene()) {
1272 vp = fVP.GetViewpointDirection ().
unit();
1273 up = fVP.GetUpVector ().
unit();
1276 up.
z()*vp.
x()-up.
x()*vp.
z(),
1277 up.
x()*vp.
y()-up.
y()*vp.
x());
1279 viewPoint = vp/fRot_sens + (zPrimeVector*dx - up*dy) ;
1280 new_up =
G4Vector3D(viewPoint.
y()*zPrimeVector.
z()-viewPoint.
z()*zPrimeVector.
y(),
1281 viewPoint.
z()*zPrimeVector.
x()-viewPoint.
x()*zPrimeVector.
z(),
1282 viewPoint.
x()*zPrimeVector.
y()-viewPoint.
y()*zPrimeVector.
x());
1288 fVP.SetUpVector(new_upUnit);
1289 fVP.SetViewAndLights (viewPoint);
1293 void G4OpenGLViewer::addExportImageFormat(std::string format) {
1294 fExportImageFormatVector.push_back(format);
1297 bool G4OpenGLViewer::setExportImageFormat(std::string format,
bool quiet) {
1300 for (
unsigned int a=0; a<fExportImageFormatVector.size(); a++) {
1301 list +=fExportImageFormatVector.at(a) +
" ";
1303 if (fExportImageFormatVector.at(a) == format) {
1305 G4cout <<
" Changing export format to \"" << format <<
"\"" <<
G4endl;
1307 if (format != fExportImageFormat) {
1308 fExportFilenameIndex = 0;
1309 fExportImageFormat = format;
1315 if (format.size() == 0) {
1316 G4cout <<
" Current formats availables are : " << list <<
G4endl;
1318 G4cerr <<
" Format \"" << format <<
"\" is not available for the selected viewer. Current formats availables are : " << list <<
G4endl;
1328 void G4OpenGLViewer::g4GluPickMatrix(GLdouble x, GLdouble y, GLdouble width, GLdouble height,
1335 sx = viewport[2] /
width;
1336 sy = viewport[3] / height;
1337 tx = (viewport[2] + 2.0 * (viewport[0] -
x)) /
width;
1338 ty = (viewport[3] + 2.0 * (viewport[1] -
y)) / height;
1340 #define M(row, col) mat[col*4+row]
1370 void G4OpenGLViewer::g4GluLookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
1371 GLdouble centerx, GLdouble centery, GLdouble
1373 GLdouble upx, GLdouble upy, GLdouble upz )
1376 GLdouble x[3], y[3],
z[3];
1382 z[0] = eyex - centerx;
1383 z[1] = eyey - centery;
1384 z[2] = eyez - centerz;
1385 mag = std::sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
1398 x[0] = y[1] * z[2] - y[2] * z[1];
1399 x[1] = -y[0] * z[2] + y[2] * z[0];
1400 x[2] = y[0] * z[1] - y[1] * z[0];
1403 y[0] = z[1] * x[2] - z[2] * x[1];
1404 y[1] = -z[0] * x[2] + z[2] * x[0];
1405 y[2] = z[0] * x[1] - z[1] * x[0];
1412 mag = std::sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
1419 mag = std::sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
1426 #define M(row,col) mat[col*4+row]
1447 glTranslated(-eyex, -eyey, -eyez);
1450 void G4OpenGLViewer::g4GlOrtho (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
1453 GLdouble a = 2.0 / (right -
left);
1454 GLdouble b = 2.0 / (top - bottom);
1455 GLdouble c = -2.0 / (zFar - zNear);
1457 GLdouble tx = - (right +
left)/(right - left);
1458 GLdouble ty = - (top + bottom)/(top - bottom);
1459 GLdouble tz = - (zFar + zNear)/(zFar - zNear);
1461 GLdouble ortho[16] = {
1467 glMultMatrixd(ortho);
1472 void G4OpenGLViewer::g4GlFrustum (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
1475 GLdouble deltaX = right -
left;
1476 GLdouble deltaY = top - bottom;
1477 GLdouble deltaZ = zFar - zNear;
1479 GLdouble a = 2.0f * zNear / deltaX;
1480 GLdouble b = 2.0f * zNear / deltaY;
1481 GLdouble c = (right +
left) / deltaX;
1482 GLdouble
d = (top + bottom) / deltaY;
1483 GLdouble
e = -(zFar + zNear) / (zFar - zNear);
1484 GLdouble
f = -2.0f * zFar * zNear / deltaZ;
1486 GLdouble
proj[16] = {
1493 glMultMatrixd(proj);
1498 #ifdef G4OPENGL_VERSION_2
1501 void G4OpenGLViewer::setVboDrawer(G4OpenGLVboDrawer* drawer) {
1502 fVboDrawer = drawer;
1504 G4OpenGLSceneHandler& sh =
dynamic_cast<G4OpenGLSceneHandler&
>(fSceneHandler);
1505 sh.setVboDrawer(fVboDrawer);
1506 }
catch(std::bad_cast exp) { }
1513 std::ostringstream txt;
1514 for (
unsigned int a=0; a<fAttributes.size(); a++) {
1515 txt << fAttributes[
a];
1516 if (a < fAttributes.size() - 1) txt <<
"\n";
void print(G4double elem)
std::vector< ExP01TrackerHit * > a
static const G4double pos
BasicVector3D< T > unit() const
BasicVector3D< T > cross(const BasicVector3D< T > &v) const
const std::vector< const std::map< G4String, G4AttDef > * > & GetAttDefs() const
static constexpr double deg
GL2PSDLL_API GLint gl2psTextOpt(const char *str, const char *fontname, GLshort fontsize, GLint align, GLfloat angle)
T dot(const BasicVector3D< T > &v) const
G4GLOB_DLL std::ostream G4cerr
HepGeom::Vector3D< G4double > G4Vector3D
const std::vector< const std::vector< G4AttValue > * > & GetAttValues() const
void set(T x1, T y1, T z1)
G4GLOB_DLL std::ostream G4cout
G4Point3D GetPosition() const
std::vector< G4Plane3D > G4Planes