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),
80 fDisplayHeadTime(false),
81 fDisplayHeadTimeX(-0.9),
82 fDisplayHeadTimeY(-0.9),
83 fDisplayHeadTimeSize(24.),
84 fDisplayHeadTimeRed(0.),
85 fDisplayHeadTimeGreen(1.),
86 fDisplayHeadTimeBlue(1.),
87 fDisplayLightFront(false),
88 fDisplayLightFrontX(0.),
89 fDisplayLightFrontY(0.),
90 fDisplayLightFrontZ(0.),
91 fDisplayLightFrontT(0.),
92 fDisplayLightFrontRed(0.),
93 fDisplayLightFrontGreen(1.),
94 fDisplayLightFrontBlue(0.),
99 fDefaultExportImageFormat(
"pdf"),
100 fExportImageFormat(
"pdf"),
101 fExportFilenameIndex(0),
105 fDefaultExportFilename(
"G4OpenGL"),
107 fGl2psDefaultLineWith(1),
108 fGl2psDefaultPointSize(2),
109 fGlViewInitialized(false),
110 fIsGettingPickInfos(false)
111 #ifdef G4OPENGL_VERSION_2
113 ,fVertexPositionAttribute(0)
114 ,fVertexNormalAttribute(0)
122 fVP.SetAutoRefresh(
true);
123 fDefaultVP.SetAutoRefresh(
true);
128 addExportImageFormat(
"eps");
129 addExportImageFormat(
"ps");
130 addExportImageFormat(
"pdf");
131 addExportImageFormat(
"svg");
134 fExportFilename += fDefaultExportFilename +
"_" + GetShortName().data();
144 G4OpenGLViewer::~G4OpenGLViewer ()
149 void G4OpenGLViewer::InitializeGLView ()
151 #ifdef G4OPENGL_VERSION_2
155 fShaderProgram = glCreateProgram();
156 Shader vertexShader = glCreateShader(GL_VERTEX_SHADER);
157 const char * vSrc = fVboDrawer->getVertexShaderSrc();
158 glShaderSource(vertexShader, 1, &vSrc, NULL);
159 glCompileShader(vertexShader);
160 glAttachShader(fShaderProgram, vertexShader);
162 Shader fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
163 const char * fSrc = fVboDrawer->getFragmentShaderSrc();
164 glShaderSource(fragmentShader, 1, &fSrc, NULL);
165 glCompileShader(fragmentShader);
167 glAttachShader(fShaderProgram, fragmentShader);
168 glLinkProgram(fShaderProgram);
169 glUseProgram(fShaderProgram);
176 fVertexPositionAttribute =
177 glGetAttribLocation(fShaderProgram,
"aVertexPosition");
180 glEnableVertexAttribArray(fVertexPositionAttribute);
183 fpMatrixUniform = glGetUniformLocation(fShaderProgram,
"uPMatrix");
184 fcMatrixUniform = glGetUniformLocation(fShaderProgram,
"uCMatrix");
185 fmvMatrixUniform = glGetUniformLocation(fShaderProgram,
"uMVMatrix");
186 fnMatrixUniform = glGetUniformLocation(fShaderProgram,
"uNMatrix");
187 ftMatrixUniform = glGetUniformLocation(fShaderProgram,
"uTMatrix");
195 fGlViewInitialized =
true;
199 if (fWinSize_x == 0) {
200 fWinSize_x = fVP.GetWindowSizeHintX();
202 if (fWinSize_y == 0) {
203 fWinSize_y = fVP.GetWindowSizeHintY();
206 glClearColor (0.0, 0.0, 0.0, 0.0);
208 #ifndef G4OPENGL_VERSION_2
209 glDisable (GL_LINE_SMOOTH);
210 glDisable (GL_POLYGON_SMOOTH);
217 glDepthFunc (GL_LEQUAL);
218 glDepthMask (GL_TRUE);
221 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
225 void G4OpenGLViewer::ClearView () {
226 ClearViewWithoutFlush();
228 if(!isFramebufferReady()) {
236 void G4OpenGLViewer::ClearViewWithoutFlush () {
239 if(!isFramebufferReady()) {
243 glClearColor (background.GetRed(),
244 background.GetGreen(),
245 background.GetBlue(),
250 glClear (GL_COLOR_BUFFER_BIT);
251 glClear (GL_DEPTH_BUFFER_BIT);
252 glClear (GL_STENCIL_BUFFER_BIT);
256 void G4OpenGLViewer::ResizeWindow(
unsigned int aWidth,
unsigned int aHeight) {
257 if ((fWinSize_x != aWidth) || (fWinSize_y != aHeight)) {
259 fWinSize_y = aHeight;
260 fSizeHasChanged =
true;
262 fSizeHasChanged =
false;
272 void G4OpenGLViewer::ResizeGLView()
279 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
281 if ((dims[0] !=0 ) && (dims[1] !=0)) {
283 if (fWinSize_x > (
unsigned)dims[0]) {
284 G4cerr <<
"Try to resize view greater than max X viewport dimension. Desired size "<<fWinSize_x <<
" is resize to "<< dims[0] <<
G4endl;
285 fWinSize_x = dims[0];
287 if (fWinSize_y > (
unsigned)dims[1]) {
288 G4cerr <<
"Try to resize view greater than max Y viewport dimension. Desired size "<<fWinSize_y <<
" is resize to "<< dims[1] <<
G4endl;
289 fWinSize_y = dims[1];
293 glViewport(0, 0, fWinSize_x,fWinSize_y);
299 void G4OpenGLViewer::SetView () {
301 if (fIsGettingPickInfos)
return;
303 if (!fSceneHandler.GetScene()) {
311 GLfloat lightPosition [4];
312 lightPosition [0] = fVP.GetActualLightpointDirection().x();
313 lightPosition [1] = fVP.GetActualLightpointDirection().y();
314 lightPosition [2] = fVP.GetActualLightpointDirection().z();
315 lightPosition [3] = 0.;
317 GLfloat ambient [] = { 0.2f, 0.2f, 0.2f, 1.f};
318 GLfloat diffuse [] = { 0.8f, 0.8f, 0.8f, 1.f};
319 glEnable (GL_LIGHT0);
320 glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);
321 glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
325 if (fWinSize_y > fWinSize_x) {
328 if (fWinSize_x > fWinSize_y) {
335 = fSceneHandler.GetScene()->GetStandardTargetPoint()
336 + fVP.GetCurrentTargetPoint ();
337 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
338 if(radius<=0.) radius = 1.;
339 const G4double cameraDistance = fVP.GetCameraDistance (radius);
341 targetPoint + cameraDistance * fVP.GetViewpointDirection().
unit();
342 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
343 const GLdouble pfar = fVP.GetFarDistance (cameraDistance, pnear, radius);
344 const GLdouble right = fVP.GetFrontHalfHeight (pnear, radius) * ratioY;
345 const GLdouble left = -right;
346 const GLdouble top = fVP.GetFrontHalfHeight (pnear, radius) * ratioX;
347 const GLdouble bottom = -top;
353 glMatrixMode (GL_PROJECTION);
356 const G4Vector3D scaleFactor = fVP.GetScaleFactor();
357 glScaled(scaleFactor.
x(),scaleFactor.
y(),scaleFactor.
z());
359 if (fVP.GetFieldHalfAngle() == 0.) {
360 g4GlOrtho (left, right, bottom, top, pnear, pfar);
363 g4GlFrustum (left, right, bottom, top, pnear, pfar);
366 glMatrixMode (GL_MODELVIEW);
369 const G4Normal3D& upVector = fVP.GetUpVector ();
371 if (cameraDistance > 1.e-6 * radius) {
372 gltarget = targetPoint;
375 gltarget = targetPoint - radius * fVP.GetViewpointDirection().
unit();
378 const G4Point3D& pCamera = cameraPosition;
380 g4GluLookAt (pCamera.
x(), pCamera.
y(), pCamera.
z(),
381 gltarget.
x(), gltarget.
y(), gltarget.
z(),
382 upVector.
x(), upVector.
y(), upVector.
z());
384 glLightfv (GL_LIGHT0, GL_POSITION, lightPosition);
415 const G4Planes& cutaways = fVP.GetCutawayPlanes();
416 size_t nPlanes = cutaways.size();
417 if (fVP.IsCutaway() &&
421 a[0] = cutaways[0].a();
422 a[1] = cutaways[0].b();
423 a[2] = cutaways[0].c();
424 a[3] = cutaways[0].d();
425 glClipPlane (GL_CLIP_PLANE2, a);
426 glEnable (GL_CLIP_PLANE2);
428 a[0] = cutaways[1].a();
429 a[1] = cutaways[1].b();
430 a[2] = cutaways[1].c();
431 a[3] = cutaways[1].d();
432 glClipPlane (GL_CLIP_PLANE3, a);
433 glEnable (GL_CLIP_PLANE3);
436 a[0] = cutaways[2].a();
437 a[1] = cutaways[2].b();
438 a[2] = cutaways[2].c();
439 a[3] = cutaways[2].d();
440 glClipPlane (GL_CLIP_PLANE4, a);
441 glEnable (GL_CLIP_PLANE4);
444 glDisable (GL_CLIP_PLANE2);
445 glDisable (GL_CLIP_PLANE3);
446 glDisable (GL_CLIP_PLANE4);
450 background = fVP.GetBackgroundColour ();
456 void G4OpenGLViewer::ResetView () {
463 void G4OpenGLViewer::HaloingFirstPass () {
473 glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
476 glDepthMask (GL_TRUE);
477 glDepthFunc (GL_LESS);
481 ChangeLineWidth(3.0);
485 void G4OpenGLViewer::HaloingSecondPass () {
488 glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
489 glDepthFunc (GL_LEQUAL);
490 ChangeLineWidth(1.0);
494 G4String G4OpenGLViewer::Pick(GLdouble x, GLdouble y)
496 std::vector < G4OpenGLViewerPickMap* > pickMap = GetPickDetails(x,y);
498 if (pickMap.size() == 0) {
499 txt +=
"Too many hits. Zoom in to reduce overlaps.";;
501 for (
unsigned int a=0; a< pickMap.size(); a++) {
502 txt += pickMap[a]->print();
508 std::vector < G4OpenGLViewerPickMap* > G4OpenGLViewer::GetPickDetails(GLdouble x, GLdouble y)
510 std::vector < G4OpenGLViewerPickMap* > pickMapVector;
512 const G4int BUFSIZE = 512;
513 GLuint selectBuffer[BUFSIZE];
514 glSelectBuffer(BUFSIZE, selectBuffer);
515 glRenderMode(GL_SELECT);
518 glMatrixMode(GL_PROJECTION);
519 G4double currentProjectionMatrix[16];
520 glGetDoublev(GL_PROJECTION_MATRIX, currentProjectionMatrix);
524 glGetIntegerv(GL_VIEWPORT, viewport);
525 fIsGettingPickInfos =
true;
527 g4GluPickMatrix(x, viewport[3] - y, 5., 5., viewport);
528 glMultMatrixd(currentProjectionMatrix);
529 glMatrixMode(GL_MODELVIEW);
531 GLint hits = glRenderMode(GL_RENDER);
532 fIsGettingPickInfos =
false;
535 GLuint*
p = selectBuffer;
536 for (GLint i = 0; i < hits; ++i) {
537 G4OpenGLViewerPickMap* pickMap =
new G4OpenGLViewerPickMap();
538 GLuint nnames = *p++;
547 for (GLuint j = 0; j < nnames; ++j) {
549 pickMap->setHitNumber(i);
550 pickMap->setSubHitNumber(j);
551 pickMap->setPickName(name);
552 std::map<GLuint, G4AttHolder*>::iterator iter =
553 fOpenGLSceneHandler.fPickMap.find(name);
554 if (iter != fOpenGLSceneHandler.fPickMap.end()) {
556 if(attHolder && attHolder->
GetAttDefs().size()) {
557 for (
size_t iAtt = 0;
558 iAtt < attHolder->
GetAttDefs().size(); ++iAtt) {
559 std::ostringstream oss;
562 pickMap->addAttributes(oss.str());
567 pickMapVector.push_back(pickMap);
570 glMatrixMode(GL_PROJECTION);
572 glMatrixMode(GL_MODELVIEW);
573 return pickMapVector;
576 GLubyte* G4OpenGLViewer::grabPixels
577 (
int inColor,
unsigned int width,
unsigned int height) {
580 GLint swapbytes, lsbfirst, rowlength;
581 GLint skiprows, skippixels, alignment;
587 size = width*height*3;
589 format = GL_LUMINANCE;
590 size = width*height*1;
593 buffer =
new GLubyte[size];
597 glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapbytes);
598 glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirst);
599 glGetIntegerv (GL_UNPACK_ROW_LENGTH, &rowlength);
601 glGetIntegerv (GL_UNPACK_SKIP_ROWS, &skiprows);
602 glGetIntegerv (GL_UNPACK_SKIP_PIXELS, &skippixels);
603 glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignment);
605 glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
606 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
607 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
609 glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
610 glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
611 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
613 glReadBuffer(GL_FRONT);
614 glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, format, GL_UNSIGNED_BYTE, (GLvoid*) buffer);
616 glPixelStorei (GL_UNPACK_SWAP_BYTES, swapbytes);
617 glPixelStorei (GL_UNPACK_LSB_FIRST, lsbfirst);
618 glPixelStorei (GL_UNPACK_ROW_LENGTH, rowlength);
620 glPixelStorei (GL_UNPACK_SKIP_ROWS, skiprows);
621 glPixelStorei (GL_UNPACK_SKIP_PIXELS, skippixels);
622 glPixelStorei (GL_UNPACK_ALIGNMENT, alignment);
627 bool G4OpenGLViewer::printEPS() {
632 size_t len = strlen(setlocale(LC_NUMERIC,NULL));
633 char* oldLocale = (
char*)(malloc(len+1));
634 if(oldLocale!=NULL) strncpy(oldLocale,setlocale(LC_NUMERIC,NULL),len);
635 setlocale(LC_NUMERIC,
"C");
637 if (((fExportImageFormat ==
"eps") || (fExportImageFormat ==
"ps")) && (!fVectoredPs)) {
638 res = printNonVectoredEPS();
640 res = printVectoredEPS();
645 setlocale(LC_NUMERIC,oldLocale);
650 G4cerr <<
"Error saving file... " << getRealPrintFilename().c_str() <<
G4endl;
652 G4cout <<
"File " << getRealPrintFilename().c_str() <<
" size: " << getRealExportWidth() <<
"x" << getRealExportHeight() <<
" has been saved " <<
G4endl;
655 if ( fExportFilenameIndex != -1) {
656 fExportFilenameIndex++;
663 bool G4OpenGLViewer::printVectoredEPS() {
667 bool G4OpenGLViewer::printNonVectoredEPS () {
669 int width = getRealExportWidth();
670 int height = getRealExportHeight();
675 int components,
pos, i;
677 pixels = grabPixels (fPrintColour, width, height);
679 if (pixels == NULL) {
680 G4cerr <<
"Failed to get pixels from OpenGl viewport" <<
G4endl;
688 std::string name = getRealPrintFilename();
689 fp = fopen (name.c_str(),
"w");
691 G4cerr <<
"Can't open filename " << name.c_str() <<
G4endl;
695 fprintf (fp,
"%%!PS-Adobe-2.0 EPSF-1.2\n");
696 fprintf (fp,
"%%%%Title: %s\n", name.c_str());
697 fprintf (fp,
"%%%%Creator: OpenGL pixmap render output\n");
698 fprintf (fp,
"%%%%BoundingBox: 0 0 %d %d\n", width, height);
699 fprintf (fp,
"%%%%EndComments\n");
700 fprintf (fp,
"gsave\n");
701 fprintf (fp,
"/bwproc {\n");
702 fprintf (fp,
" rgbproc\n");
703 fprintf (fp,
" dup length 3 idiv string 0 3 0 \n");
704 fprintf (fp,
" 5 -1 roll {\n");
705 fprintf (fp,
" add 2 1 roll 1 sub dup 0 eq\n");
706 fprintf (fp,
" { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
707 fprintf (fp,
" 3 1 roll 5 -1 roll } put 1 add 3 0 \n");
708 fprintf (fp,
" { 2 1 roll } ifelse\n");
709 fprintf (fp,
" }forall\n");
710 fprintf (fp,
" pop pop pop\n");
711 fprintf (fp,
"} def\n");
712 fprintf (fp,
"systemdict /colorimage known not {\n");
713 fprintf (fp,
" /colorimage {\n");
714 fprintf (fp,
" pop\n");
715 fprintf (fp,
" pop\n");
716 fprintf (fp,
" /rgbproc exch def\n");
717 fprintf (fp,
" { bwproc } image\n");
718 fprintf (fp,
" } def\n");
719 fprintf (fp,
"} if\n");
720 fprintf (fp,
"/picstr %d string def\n", width * components);
721 fprintf (fp,
"%d %d scale\n", width, height);
722 fprintf (fp,
"%d %d %d\n", width, height, 8);
723 fprintf (fp,
"[%d 0 0 %d 0 0]\n", width, height);
724 fprintf (fp,
"{currentfile picstr readhexstring pop}\n");
725 fprintf (fp,
"false %d\n", components);
726 fprintf (fp,
"colorimage\n");
728 curpix = (GLubyte*) pixels;
730 for (i = width*height*components; i>0; i--) {
731 fprintf (fp,
"%02hx ", (
unsigned short)(*(curpix++)));
740 fprintf (fp,
"grestore\n");
741 fprintf (fp,
"showpage\n");
754 bool G4OpenGLViewer::isGl2psWriting() {
756 if (!fGL2PSAction)
return false;
757 if (fGL2PSAction->fileWritingEnabled()) {
764 G4bool G4OpenGLViewer::isFramebufferReady() {
766 #ifdef G4VIS_BUILD_OPENGLQT_DRIVER
769 #ifdef G4VIS_BUILD_OPENGLX_DRIVER
772 #ifdef G4VIS_BUILD_OPENGLXM_DRIVER
775 #ifdef G4VIS_BUILD_OPENGLWIN32_DRIVER
779 #if GL_ARB_framebuffer_object
792 void G4OpenGLViewer::DrawText(
const G4Text& g4text)
795 if (isGl2psWriting()) {
798 G4double size = fSceneHandler.GetMarkerSize(g4text,sizeType);
803 glRasterPos3d(position.
x(),position.
y(),position.
z());
812 gl2psTextOpt(textString.c_str(),
"Times-Roman",GLshort(size),align,0);
816 static G4int callCount = 0;
819 if (callCount <= 1) {
821 "G4OpenGLViewer::DrawText: Not implemented for \""
832 void G4OpenGLViewer::ChangePointSize(
G4double size) {
834 if (isGl2psWriting()) {
835 fGL2PSAction->setPointSize(
int(size));
844 void G4OpenGLViewer::ChangeLineWidth(
G4double width) {
846 if (isGl2psWriting()) {
847 fGL2PSAction->setLineWidth(
int(width));
861 bool G4OpenGLViewer::exportImage(std::string name,
int width,
int height) {
863 if (! setExportFilename(name)) {
867 if ((width != -1) && (height != -1)) {
868 setExportSize(width, height);
871 if (fExportImageFormat ==
"eps") {
872 fGL2PSAction->setExportImageFormat(
GL2PS_EPS);
873 }
else if (fExportImageFormat ==
"ps") {
874 fGL2PSAction->setExportImageFormat(
GL2PS_PS);
875 }
else if (fExportImageFormat ==
"svg") {
876 fGL2PSAction->setExportImageFormat(
GL2PS_SVG);
877 }
else if (fExportImageFormat ==
"pdf") {
878 fGL2PSAction->setExportImageFormat(
GL2PS_PDF);
880 setExportImageFormat(fExportImageFormat,
true);
887 bool G4OpenGLViewer::printGl2PS() {
889 int width = getRealExportWidth();
890 int height = getRealExportHeight();
894 G4OpenGLSceneHandler& oglSceneHandler =
dynamic_cast<G4OpenGLSceneHandler&
>(fSceneHandler);
895 G4OpenGLSceneHandler::FlushAction originalFlushAction = oglSceneHandler.GetFlushAction();
896 oglSceneHandler.SetFlushAction(G4OpenGLSceneHandler::never);
898 if (!fGL2PSAction)
return false;
900 fGL2PSAction->setFileName(getRealPrintFilename().c_str());
917 bool extendBuffer =
true;
918 bool endWriteAction =
false;
919 bool beginWriteAction =
true;
920 bool filePointerOk =
true;
921 while ((extendBuffer) && (! endWriteAction) && (filePointerOk)) {
923 beginWriteAction = fGL2PSAction->enableFileWriting();
929 filePointerOk = fGL2PSAction->fileWritingEnabled();
931 if (beginWriteAction) {
935 fGL2PSAction->setLineWidth(fGl2psDefaultLineWith);
937 fGL2PSAction->setPointSize(fGl2psDefaultPointSize);
940 endWriteAction = fGL2PSAction->disableFileWriting();
943 if ((! endWriteAction) || (! beginWriteAction)) {
944 extendBuffer = fGL2PSAction->extendBufferSize();
948 fGL2PSAction->resetBufferSizeParameters();
950 if (!extendBuffer ) {
951 G4cerr <<
"ERROR: gl2ps buffer size is not big enough to print this geometry. Try to extend it. No output produced"<<
G4endl;
954 if (!beginWriteAction ) {
955 G4cerr <<
"ERROR: saving file "<<getRealPrintFilename().c_str()<<
". Check read/write access. No output produced" <<
G4endl;
958 if (!endWriteAction ) {
965 oglSceneHandler.SetFlushAction(originalFlushAction);
974 unsigned int G4OpenGLViewer::getWinWidth()
const{
978 unsigned int G4OpenGLViewer::getWinHeight()
const{
982 G4bool G4OpenGLViewer::sizeHasChanged() {
983 return fSizeHasChanged;
986 G4int G4OpenGLViewer::getRealExportWidth() {
987 if (fPrintSizeX == -1) {
991 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
994 if ((dims[0] !=0 ) && (dims[1] !=0)) {
995 if (fPrintSizeX > dims[0]){
999 if (fPrintSizeX < -1){
1005 G4int G4OpenGLViewer::getRealExportHeight() {
1006 if (fPrintSizeY == -1) {
1010 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
1013 if ((dims[0] !=0 ) && (dims[1] !=0)) {
1014 if (fPrintSizeY > dims[1]){
1018 if (fPrintSizeY < -1){
1024 void G4OpenGLViewer::setExportSize(
G4int X,
G4int Y) {
1035 bool G4OpenGLViewer::setExportFilename(
G4String name,
G4bool inc) {
1041 if ((name !=
"") && (fExportFilename != name)) {
1042 fExportFilenameIndex=0;
1045 fExportFilenameIndex=-1;
1048 if (name.size() == 0) {
1049 name = getRealPrintFilename().c_str();
1052 std::string extension = name.substr(name.find_last_of(
".") + 1);
1054 if (name.size() != extension.size()) {
1055 if (! setExportImageFormat(extension,
false)) {
1060 fExportFilename = name.substr(0,name.find_last_of(
"."));
1065 std::string G4OpenGLViewer::getRealPrintFilename() {
1066 std::string temp = fExportFilename;
1067 if (fExportFilenameIndex != -1) {
1068 temp += std::string(
"_");
1069 std::ostringstream os;
1070 os << std::setw(4) << std::setfill(
'0') << fExportFilenameIndex;
1071 std::string nb_str = os.str();
1074 temp +=
"."+fExportImageFormat;
1078 GLdouble G4OpenGLViewer::getSceneNearWidth()
1080 if (!fSceneHandler.GetScene()) {
1084 = fSceneHandler.GetScene()->GetStandardTargetPoint()
1085 + fVP.GetCurrentTargetPoint ();
1086 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
1087 if(radius<=0.) radius = 1.;
1088 const G4double cameraDistance = fVP.GetCameraDistance (radius);
1089 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
1090 return 2 * fVP.GetFrontHalfHeight (pnear, radius);
1093 GLdouble G4OpenGLViewer::getSceneFarWidth()
1095 if (!fSceneHandler.GetScene()) {
1099 = fSceneHandler.GetScene()->GetStandardTargetPoint()
1100 + fVP.GetCurrentTargetPoint ();
1101 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
1102 if(radius<=0.) radius = 1.;
1103 const G4double cameraDistance = fVP.GetCameraDistance (radius);
1104 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
1105 const GLdouble pfar = fVP.GetFarDistance (cameraDistance, pnear, radius);
1106 return 2 * fVP.GetFrontHalfHeight (pfar, radius);
1110 GLdouble G4OpenGLViewer::getSceneDepth()
1112 if (!fSceneHandler.GetScene()) {
1116 = fSceneHandler.GetScene()->GetStandardTargetPoint()
1117 + fVP.GetCurrentTargetPoint ();
1118 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
1119 if(radius<=0.) radius = 1.;
1120 const G4double cameraDistance = fVP.GetCameraDistance (radius);
1121 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
1122 return fVP.GetFarDistance (cameraDistance, pnear, radius)- pnear;
1130 rotateSceneInViewDirection(dx,dy);
1133 rotateSceneThetaPhi(dx,0);
1136 rotateSceneThetaPhi(0,dy);
1145 rotateSceneInViewDirection(dx,dy);
1148 rotateSceneThetaPhi(dx,0);
1151 rotateSceneThetaPhi(0,dy);
1158 if (!fSceneHandler.GetScene()) {
1186 vp = fVP.GetViewpointDirection ().
unit ();
1187 up = fVP.GetUpVector ().
unit ();
1189 yprime = (up.
cross(vp)).unit();
1190 zprime = (vp.
cross(yprime)).unit();
1192 if (fVP.GetLightsMoveWithCamera()) {
1193 delta_alpha = dy * fRot_sens;
1194 delta_theta = -dx * fRot_sens;
1196 delta_alpha = -dy * fRot_sens;
1197 delta_theta = dx * fRot_sens;
1203 new_vp = std::cos(delta_alpha) * vp + std::sin(delta_alpha) * zprime;
1208 if (fVP.GetLightsMoveWithCamera()) {
1209 new_up = (new_vp.
cross(yprime)).unit();
1210 if (new_vp.
z()*vp.
z() <0) {
1211 new_up.
set(new_up.
x(),-new_up.
y(),new_up.
z());
1215 if (new_vp.
z()*vp.
z() <0) {
1216 new_up.
set(new_up.
x(),-new_up.
y(),new_up.
z());
1219 fVP.SetUpVector(new_up);
1223 cosalpha = new_up.
dot (new_vp.
unit());
1224 sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
1226 xprime = yprime.
cross (new_up);
1228 a1 = sinalpha * xprime;
1230 a2 = sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
1234 viewPoint = new_vp.
unit() + delta;
1236 fVP.SetViewAndLights (viewPoint);
1242 if (!fSceneHandler.GetScene()) {
1266 vp = fVP.GetViewpointDirection ().
unit();
1267 up = fVP.GetUpVector ().
unit();
1270 up.
z()*vp.
x()-up.
x()*vp.
z(),
1271 up.
x()*vp.
y()-up.
y()*vp.
x());
1273 viewPoint = vp/fRot_sens + (zPrimeVector*dx - up*dy) ;
1274 new_up =
G4Vector3D(viewPoint.
y()*zPrimeVector.
z()-viewPoint.
z()*zPrimeVector.
y(),
1275 viewPoint.
z()*zPrimeVector.
x()-viewPoint.
x()*zPrimeVector.
z(),
1276 viewPoint.
x()*zPrimeVector.
y()-viewPoint.
y()*zPrimeVector.
x());
1282 fVP.SetUpVector(new_upUnit);
1283 fVP.SetViewAndLights (viewPoint);
1287 void G4OpenGLViewer::addExportImageFormat(std::string format) {
1288 fExportImageFormatVector.push_back(format);
1291 bool G4OpenGLViewer::setExportImageFormat(std::string format,
bool quiet) {
1294 for (
unsigned int a=0; a<fExportImageFormatVector.size(); a++) {
1295 list +=fExportImageFormatVector.at(a) +
" ";
1297 if (fExportImageFormatVector.at(a) == format) {
1299 G4cout <<
" Changing export format to \"" << format <<
"\"" <<
G4endl;
1301 if (format != fExportImageFormat) {
1302 fExportFilenameIndex = 0;
1303 fExportImageFormat = format;
1309 if (format.size() == 0) {
1310 G4cout <<
" Current formats availables are : " << list <<
G4endl;
1312 G4cerr <<
" Format \"" << format <<
"\" is not available for the selected viewer. Current formats availables are : " << list <<
G4endl;
1322 void G4OpenGLViewer::g4GluPickMatrix(GLdouble x, GLdouble y, GLdouble width, GLdouble height,
1329 sx = viewport[2] / width;
1330 sy = viewport[3] / height;
1331 tx = (viewport[2] + 2.0 * (viewport[0] - x)) / width;
1332 ty = (viewport[3] + 2.0 * (viewport[1] - y)) / height;
1334 #define M(row, col) mat[col*4+row]
1364 void G4OpenGLViewer::g4GluLookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
1365 GLdouble centerx, GLdouble centery, GLdouble
1367 GLdouble upx, GLdouble upy, GLdouble upz )
1370 GLdouble x[3], y[3], z[3];
1376 z[0] = eyex - centerx;
1377 z[1] = eyey - centery;
1378 z[2] = eyez - centerz;
1379 mag = std::sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
1392 x[0] = y[1] * z[2] - y[2] * z[1];
1393 x[1] = -y[0] * z[2] + y[2] * z[0];
1394 x[2] = y[0] * z[1] - y[1] * z[0];
1397 y[0] = z[1] * x[2] - z[2] * x[1];
1398 y[1] = -z[0] * x[2] + z[2] * x[0];
1399 y[2] = z[0] * x[1] - z[1] * x[0];
1406 mag = std::sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
1413 mag = std::sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
1420 #define M(row,col) mat[col*4+row]
1441 glTranslated(-eyex, -eyey, -eyez);
1444 void G4OpenGLViewer::g4GlOrtho (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
1447 GLdouble a = 2.0 / (right - left);
1448 GLdouble b = 2.0 / (top - bottom);
1449 GLdouble c = -2.0 / (zFar - zNear);
1451 GLdouble tx = - (right + left)/(right - left);
1452 GLdouble ty = - (top + bottom)/(top - bottom);
1453 GLdouble tz = - (zFar + zNear)/(zFar - zNear);
1455 GLdouble ortho[16] = {
1461 glMultMatrixd(ortho);
1466 void G4OpenGLViewer::g4GlFrustum (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
1469 GLdouble deltaX = right - left;
1470 GLdouble deltaY = top - bottom;
1471 GLdouble deltaZ = zFar - zNear;
1473 GLdouble a = 2.0f * zNear / deltaX;
1474 GLdouble b = 2.0f * zNear / deltaY;
1475 GLdouble c = (right + left) / deltaX;
1476 GLdouble d = (top + bottom) / deltaY;
1477 GLdouble e = -(zFar + zNear) / (zFar - zNear);
1478 GLdouble f = -2.0f * zFar * zNear / deltaZ;
1480 GLdouble proj[16] = {
1487 glMultMatrixd(proj);
1492 #ifdef G4OPENGL_VERSION_2
1495 void G4OpenGLViewer::setVboDrawer(G4OpenGLVboDrawer* drawer) {
1496 fVboDrawer = drawer;
1498 G4OpenGLSceneHandler& sh =
dynamic_cast<G4OpenGLSceneHandler&
>(fSceneHandler);
1499 sh.setVboDrawer(fVboDrawer);
1500 }
catch(std::bad_cast exp) { }
1507 std::ostringstream txt;
1511 txt <<
"Hit: " << fHitNumber <<
", Sub-hit: " << fSubHitNumber <<
", PickName: " << fPickName <<
"\n";
1513 for (
unsigned int a=0; a<fAttributes.size(); a++) {
1514 txt << fAttributes[a] <<
"\n";
void set(T x1, T y1, T z1)
BasicVector3D< T > unit() const
const std::vector< const std::vector< G4AttValue > * > & GetAttValues() const
HepGeom::Vector3D< G4double > G4Vector3D
G4Point3D GetPosition() const
G4GLOB_DLL std::ostream G4cout
GL2PSDLL_API GLint gl2psTextOpt(const char *str, const char *fontname, GLshort fontsize, GLint align, GLfloat angle)
std::vector< G4Plane3D > G4Planes
void print(G4double elem)
T dot(const BasicVector3D< T > &v) const
static constexpr double deg
BasicVector3D< T > cross(const BasicVector3D< T > &v) const
static const G4double pos
const std::vector< const std::map< G4String, G4AttDef > * > & GetAttDefs() const
G4GLOB_DLL std::ostream G4cerr