32 #ifdef G4VIS_BUILD_OPENGL_DRIVER
52 #ifdef G4OPENGL_VERSION_2
64 G4OpenGLViewer::G4OpenGLViewer (G4OpenGLSceneHandler& scene):
66 #ifdef G4OPENGL_VERSION_2
71 fOpenGLSceneHandler(scene),
73 transparency_enabled (
true),
74 antialiasing_enabled (
false),
75 haloing_enabled (
false),
80 fDefaultExportImageFormat(
"pdf"),
81 fExportImageFormat(
"pdf"),
82 fExportFilenameIndex(0),
86 fDefaultExportFilename(
"G4OpenGL"),
88 fGl2psDefaultLineWith(1),
89 fGl2psDefaultPointSize(2),
90 fGlViewInitialized(
false),
91 fIsGettingPickInfos(
false)
92 #ifdef G4OPENGL_VERSION_2
94 ,fVertexPositionAttribute(0)
95 ,fVertexNormalAttribute(0)
103 fVP.SetAutoRefresh(
true);
104 fDefaultVP.SetAutoRefresh(
true);
109 addExportImageFormat(
"eps");
110 addExportImageFormat(
"ps");
111 addExportImageFormat(
"pdf");
112 addExportImageFormat(
"svg");
115 fExportFilename += fDefaultExportFilename +
"_" + GetShortName().data();
125 G4OpenGLViewer::~G4OpenGLViewer ()
130 void G4OpenGLViewer::InitializeGLView ()
132 #ifdef G4OPENGL_VERSION_2
136 fShaderProgram = glCreateProgram();
137 Shader vertexShader = glCreateShader(GL_VERTEX_SHADER);
138 const char * vSrc = fVboDrawer->getVertexShaderSrc();
139 glShaderSource(vertexShader, 1, &vSrc, NULL);
140 glCompileShader(vertexShader);
141 glAttachShader(fShaderProgram, vertexShader);
143 Shader fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
144 const char * fSrc = fVboDrawer->getFragmentShaderSrc();
145 glShaderSource(fragmentShader, 1, &fSrc, NULL);
146 glCompileShader(fragmentShader);
148 glAttachShader(fShaderProgram, fragmentShader);
149 glLinkProgram(fShaderProgram);
150 glUseProgram(fShaderProgram);
157 fVertexPositionAttribute =
158 glGetAttribLocation(fShaderProgram,
"aVertexPosition");
161 glEnableVertexAttribArray(fVertexPositionAttribute);
164 fpMatrixUniform = glGetUniformLocation(fShaderProgram,
"uPMatrix");
165 fcMatrixUniform = glGetUniformLocation(fShaderProgram,
"uCMatrix");
166 fmvMatrixUniform = glGetUniformLocation(fShaderProgram,
"uMVMatrix");
167 fnMatrixUniform = glGetUniformLocation(fShaderProgram,
"uNMatrix");
168 ftMatrixUniform = glGetUniformLocation(fShaderProgram,
"uTMatrix");
176 fGlViewInitialized =
true;
180 if (fWinSize_x == 0) {
181 fWinSize_x = fVP.GetWindowSizeHintX();
183 if (fWinSize_y == 0) {
184 fWinSize_y = fVP.GetWindowSizeHintY();
187 glClearColor (0.0, 0.0, 0.0, 0.0);
189 #ifndef G4OPENGL_VERSION_2
190 glDisable (GL_LINE_SMOOTH);
191 glDisable (GL_POLYGON_SMOOTH);
198 glDepthFunc (GL_LEQUAL);
199 glDepthMask (GL_TRUE);
202 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
206 void G4OpenGLViewer::ClearView () {
207 ClearViewWithoutFlush();
209 if(!isFramebufferReady()) {
217 void G4OpenGLViewer::ClearViewWithoutFlush () {
220 if(!isFramebufferReady()) {
224 glClearColor (background.GetRed(),
225 background.GetGreen(),
226 background.GetBlue(),
231 glClear (GL_COLOR_BUFFER_BIT);
232 glClear (GL_DEPTH_BUFFER_BIT);
233 glClear (GL_STENCIL_BUFFER_BIT);
237 void G4OpenGLViewer::ResizeWindow(
unsigned int aWidth,
unsigned int aHeight) {
238 if ((fWinSize_x != aWidth) || (fWinSize_y != aHeight)) {
240 fWinSize_y = aHeight;
241 fSizeHasChanged =
true;
243 fSizeHasChanged =
false;
253 void G4OpenGLViewer::ResizeGLView()
260 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
262 if ((dims[0] !=0 ) && (dims[1] !=0)) {
264 if (fWinSize_x > (
unsigned)dims[0]) {
265 G4cerr <<
"Try to resize view greater than max X viewport dimension. Desired size "<<fWinSize_x <<
" is resize to "<< dims[0] <<
G4endl;
266 fWinSize_x = dims[0];
268 if (fWinSize_y > (
unsigned)dims[1]) {
269 G4cerr <<
"Try to resize view greater than max Y viewport dimension. Desired size "<<fWinSize_y <<
" is resize to "<< dims[1] <<
G4endl;
270 fWinSize_y = dims[1];
274 glViewport(0, 0, fWinSize_x,fWinSize_y);
280 void G4OpenGLViewer::SetView () {
282 if (fIsGettingPickInfos)
return;
284 if (!fSceneHandler.GetScene()) {
292 GLfloat lightPosition [4];
293 lightPosition [0] = fVP.GetActualLightpointDirection().x();
294 lightPosition [1] = fVP.GetActualLightpointDirection().y();
295 lightPosition [2] = fVP.GetActualLightpointDirection().z();
296 lightPosition [3] = 0.;
298 GLfloat ambient [] = { 0.2f, 0.2f, 0.2f, 1.f};
299 GLfloat diffuse [] = { 0.8f, 0.8f, 0.8f, 1.f};
300 glEnable (GL_LIGHT0);
301 glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);
302 glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
306 if (fWinSize_y > fWinSize_x) {
309 if (fWinSize_x > fWinSize_y) {
316 = fSceneHandler.GetScene()->GetStandardTargetPoint()
317 + fVP.GetCurrentTargetPoint ();
318 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
319 if(radius<=0.) radius = 1.;
320 const G4double cameraDistance = fVP.GetCameraDistance (radius);
322 targetPoint + cameraDistance * fVP.GetViewpointDirection().
unit();
323 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
324 const GLdouble pfar = fVP.GetFarDistance (cameraDistance, pnear, radius);
325 const GLdouble
right = fVP.GetFrontHalfHeight (pnear, radius) * ratioY;
327 const GLdouble top = fVP.GetFrontHalfHeight (pnear, radius) * ratioX;
328 const GLdouble bottom = -top;
334 glMatrixMode (GL_PROJECTION);
337 const G4Vector3D scaleFactor = fVP.GetScaleFactor();
338 glScaled(scaleFactor.
x(),scaleFactor.
y(),scaleFactor.
z());
340 if (fVP.GetFieldHalfAngle() == 0.) {
341 g4GlOrtho (left, right, bottom, top, pnear, pfar);
344 g4GlFrustum (left, right, bottom, top, pnear, pfar);
347 glMatrixMode (GL_MODELVIEW);
350 const G4Normal3D& upVector = fVP.GetUpVector ();
352 if (cameraDistance > 1.
e-6 * radius) {
353 gltarget = targetPoint;
356 gltarget = targetPoint - radius * fVP.GetViewpointDirection().
unit();
359 const G4Point3D& pCamera = cameraPosition;
361 g4GluLookAt (pCamera.
x(), pCamera.
y(), pCamera.
z(),
362 gltarget.
x(), gltarget.
y(), gltarget.
z(),
363 upVector.
x(), upVector.
y(), upVector.
z());
365 glLightfv (GL_LIGHT0, GL_POSITION, lightPosition);
396 const G4Planes& cutaways = fVP.GetCutawayPlanes();
397 size_t nPlanes = cutaways.size();
398 if (fVP.IsCutaway() &&
402 a[0] = cutaways[0].a();
403 a[1] = cutaways[0].b();
404 a[2] = cutaways[0].c();
405 a[3] = cutaways[0].d();
406 glClipPlane (GL_CLIP_PLANE2, a);
407 glEnable (GL_CLIP_PLANE2);
409 a[0] = cutaways[1].a();
410 a[1] = cutaways[1].b();
411 a[2] = cutaways[1].c();
412 a[3] = cutaways[1].d();
413 glClipPlane (GL_CLIP_PLANE3, a);
414 glEnable (GL_CLIP_PLANE3);
417 a[0] = cutaways[2].a();
418 a[1] = cutaways[2].b();
419 a[2] = cutaways[2].c();
420 a[3] = cutaways[2].d();
421 glClipPlane (GL_CLIP_PLANE4, a);
422 glEnable (GL_CLIP_PLANE4);
425 glDisable (GL_CLIP_PLANE2);
426 glDisable (GL_CLIP_PLANE3);
427 glDisable (GL_CLIP_PLANE4);
431 background = fVP.GetBackgroundColour ();
437 void G4OpenGLViewer::ResetView () {
444 void G4OpenGLViewer::HaloingFirstPass () {
454 glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
457 glDepthMask (GL_TRUE);
458 glDepthFunc (GL_LESS);
462 ChangeLineWidth(3.0);
466 void G4OpenGLViewer::HaloingSecondPass () {
469 glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
470 glDepthFunc (GL_LEQUAL);
471 ChangeLineWidth(1.0);
475 G4String G4OpenGLViewer::Pick(GLdouble
x, GLdouble
y)
477 const std::vector < G4OpenGLViewerPickMap* > & pickMap = GetPickDetails(x,y);
479 if (pickMap.size() == 0) {
482 for (
unsigned int a=0; a < pickMap.size(); a++) {
483 if (pickMap[a]->getAttributes().size() > 0) {
484 txt += pickMap[
a]->print();
491 const std::vector < G4OpenGLViewerPickMap* > & G4OpenGLViewer::GetPickDetails(GLdouble x, GLdouble y)
493 static std::vector < G4OpenGLViewerPickMap* > pickMapVector;
494 for (
auto pickMap: pickMapVector) {
497 pickMapVector.clear();
501 glSelectBuffer(BUFSIZE, selectBuffer);
502 glRenderMode(GL_SELECT);
505 glMatrixMode(GL_PROJECTION);
506 G4double currentProjectionMatrix[16];
507 glGetDoublev(GL_PROJECTION_MATRIX, currentProjectionMatrix);
511 glGetIntegerv(GL_VIEWPORT, viewport);
518 fIsGettingPickInfos =
true;
520 g4GluPickMatrix(x, viewport[3] - y, 5., 5., viewport);
521 glMultMatrixd(currentProjectionMatrix);
522 glMatrixMode(GL_MODELVIEW);
524 GLint
hits = glRenderMode(GL_RENDER);
525 fIsGettingPickInfos =
false;
527 G4cout <<
"Too many hits. Zoom in to reduce overlaps." <<
G4endl;
528 goto restoreMatrices;
531 GLuint*
p = selectBuffer;
532 for (GLint i = 0; i <
hits; ++i) {
533 GLuint nnames = *p++;
542 for (GLuint j = 0; j < nnames; ++j) {
544 std::map<GLuint, G4AttHolder*>::iterator iter =
545 fOpenGLSceneHandler.fPickMap.find(name);
546 if (iter != fOpenGLSceneHandler.fPickMap.end()) {
548 if(attHolder && attHolder->
GetAttDefs().size()) {
549 for (
size_t iAtt = 0;
550 iAtt < attHolder->
GetAttDefs().size(); ++iAtt) {
551 std::ostringstream oss;
554 G4OpenGLViewerPickMap* pickMap =
new G4OpenGLViewerPickMap();
561 pickMap->addAttributes(oss.str());
562 pickMap->setHitNumber(i);
563 pickMap->setSubHitNumber(j);
564 pickMap->setPickName(name);
565 pickMapVector.push_back(pickMap);
574 glMatrixMode(GL_PROJECTION);
576 glMatrixMode(GL_MODELVIEW);
578 return pickMapVector;
581 GLubyte* G4OpenGLViewer::grabPixels
582 (
int inColor,
unsigned int width,
unsigned int height) {
585 GLint swapbytes, lsbfirst, rowlength;
586 GLint skiprows, skippixels, alignment;
592 size = width*height*3;
594 format = GL_LUMINANCE;
595 size = width*height*1;
598 buffer =
new GLubyte[size];
602 glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapbytes);
603 glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirst);
604 glGetIntegerv (GL_UNPACK_ROW_LENGTH, &rowlength);
606 glGetIntegerv (GL_UNPACK_SKIP_ROWS, &skiprows);
607 glGetIntegerv (GL_UNPACK_SKIP_PIXELS, &skippixels);
608 glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignment);
610 glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
611 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
612 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
614 glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
615 glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
616 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
618 glReadBuffer(GL_FRONT);
619 glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, format, GL_UNSIGNED_BYTE, (GLvoid*) buffer);
621 glPixelStorei (GL_UNPACK_SWAP_BYTES, swapbytes);
622 glPixelStorei (GL_UNPACK_LSB_FIRST, lsbfirst);
623 glPixelStorei (GL_UNPACK_ROW_LENGTH, rowlength);
625 glPixelStorei (GL_UNPACK_SKIP_ROWS, skiprows);
626 glPixelStorei (GL_UNPACK_SKIP_PIXELS, skippixels);
627 glPixelStorei (GL_UNPACK_ALIGNMENT, alignment);
632 bool G4OpenGLViewer::printEPS() {
637 size_t len = strlen(setlocale(LC_NUMERIC,NULL));
638 char* oldLocale = (
char*)(malloc(len+1));
639 if(oldLocale!=NULL) strncpy(oldLocale,setlocale(LC_NUMERIC,NULL),len);
640 setlocale(LC_NUMERIC,
"C");
642 if (((fExportImageFormat ==
"eps") || (fExportImageFormat ==
"ps")) && (!fVectoredPs)) {
643 res = printNonVectoredEPS();
645 res = printVectoredEPS();
650 setlocale(LC_NUMERIC,oldLocale);
655 G4cerr <<
"Error saving file... " << getRealPrintFilename().c_str() <<
G4endl;
657 G4cout <<
"File " << getRealPrintFilename().c_str() <<
" size: " << getRealExportWidth() <<
"x" << getRealExportHeight() <<
" has been saved " <<
G4endl;
660 if ( fExportFilenameIndex != -1) {
661 fExportFilenameIndex++;
668 bool G4OpenGLViewer::printVectoredEPS() {
672 bool G4OpenGLViewer::printNonVectoredEPS () {
674 int width = getRealExportWidth();
675 int height = getRealExportHeight();
682 pixels = grabPixels (fPrintColour, width, height);
684 if (pixels == NULL) {
685 G4cerr <<
"Failed to get pixels from OpenGl viewport" <<
G4endl;
693 std::string name = getRealPrintFilename();
694 fp = fopen (name.c_str(),
"w");
696 G4cerr <<
"Can't open filename " << name.c_str() <<
G4endl;
700 fprintf (fp,
"%%!PS-Adobe-2.0 EPSF-1.2\n");
701 fprintf (fp,
"%%%%Title: %s\n", name.c_str());
702 fprintf (fp,
"%%%%Creator: OpenGL pixmap render output\n");
703 fprintf (fp,
"%%%%BoundingBox: 0 0 %d %d\n", width, height);
704 fprintf (fp,
"%%%%EndComments\n");
705 fprintf (fp,
"gsave\n");
706 fprintf (fp,
"/bwproc {\n");
707 fprintf (fp,
" rgbproc\n");
708 fprintf (fp,
" dup length 3 idiv string 0 3 0 \n");
709 fprintf (fp,
" 5 -1 roll {\n");
710 fprintf (fp,
" add 2 1 roll 1 sub dup 0 eq\n");
711 fprintf (fp,
" { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
712 fprintf (fp,
" 3 1 roll 5 -1 roll } put 1 add 3 0 \n");
713 fprintf (fp,
" { 2 1 roll } ifelse\n");
714 fprintf (fp,
" }forall\n");
715 fprintf (fp,
" pop pop pop\n");
716 fprintf (fp,
"} def\n");
717 fprintf (fp,
"systemdict /colorimage known not {\n");
718 fprintf (fp,
" /colorimage {\n");
719 fprintf (fp,
" pop\n");
720 fprintf (fp,
" pop\n");
721 fprintf (fp,
" /rgbproc exch def\n");
722 fprintf (fp,
" { bwproc } image\n");
723 fprintf (fp,
" } def\n");
724 fprintf (fp,
"} if\n");
725 fprintf (fp,
"/picstr %d string def\n", width * components);
726 fprintf (fp,
"%d %d scale\n", width, height);
727 fprintf (fp,
"%d %d %d\n", width, height, 8);
728 fprintf (fp,
"[%d 0 0 %d 0 0]\n", width, height);
729 fprintf (fp,
"{currentfile picstr readhexstring pop}\n");
730 fprintf (fp,
"false %d\n", components);
731 fprintf (fp,
"colorimage\n");
733 curpix = (GLubyte*) pixels;
735 for (i = width*height*components; i>0; i--) {
736 fprintf (fp,
"%02hx ", (
unsigned short)(*(curpix++)));
745 fprintf (fp,
"grestore\n");
746 fprintf (fp,
"showpage\n");
759 bool G4OpenGLViewer::isGl2psWriting() {
761 if (!fGL2PSAction)
return false;
762 if (fGL2PSAction->fileWritingEnabled()) {
769 G4bool G4OpenGLViewer::isFramebufferReady() {
771 #ifdef G4VIS_BUILD_OPENGLQT_DRIVER
774 #ifdef G4VIS_BUILD_OPENGLX_DRIVER
777 #ifdef G4VIS_BUILD_OPENGLXM_DRIVER
780 #ifdef G4VIS_BUILD_OPENGLWIN32_DRIVER
784 #if GL_ARB_framebuffer_object
797 void G4OpenGLViewer::DrawText(
const G4Text& g4text)
800 if (isGl2psWriting()) {
803 G4double size = fSceneHandler.GetMarkerSize(g4text,sizeType);
808 glRasterPos3d(position.
x(),position.
y(),position.
z());
817 gl2psTextOpt(textString.c_str(),
"Times-Roman",GLshort(size),align,0);
821 static G4int callCount = 0;
824 if (callCount <= 1) {
826 "G4OpenGLViewer::DrawText: Not implemented for \""
837 void G4OpenGLViewer::ChangePointSize(
G4double size) {
839 if (isGl2psWriting()) {
840 fGL2PSAction->setPointSize(
int(size));
849 void G4OpenGLViewer::ChangeLineWidth(
G4double width) {
851 if (isGl2psWriting()) {
852 fGL2PSAction->setLineWidth(
int(width));
866 bool G4OpenGLViewer::exportImage(std::string name,
int width,
int height) {
868 if (! setExportFilename(name)) {
872 if ((width != -1) && (height != -1)) {
873 setExportSize(width, height);
876 if (fExportImageFormat ==
"eps") {
877 fGL2PSAction->setExportImageFormat(
GL2PS_EPS);
878 }
else if (fExportImageFormat ==
"ps") {
879 fGL2PSAction->setExportImageFormat(
GL2PS_PS);
880 }
else if (fExportImageFormat ==
"svg") {
881 fGL2PSAction->setExportImageFormat(
GL2PS_SVG);
882 }
else if (fExportImageFormat ==
"pdf") {
883 fGL2PSAction->setExportImageFormat(
GL2PS_PDF);
885 setExportImageFormat(fExportImageFormat,
true);
892 bool G4OpenGLViewer::printGl2PS() {
894 int width = getRealExportWidth();
895 int height = getRealExportHeight();
899 G4OpenGLSceneHandler& oglSceneHandler =
dynamic_cast<G4OpenGLSceneHandler&
>(fSceneHandler);
900 G4OpenGLSceneHandler::FlushAction originalFlushAction = oglSceneHandler.GetFlushAction();
901 oglSceneHandler.SetFlushAction(G4OpenGLSceneHandler::never);
903 if (!fGL2PSAction)
return false;
905 fGL2PSAction->setFileName(getRealPrintFilename().c_str());
922 bool extendBuffer =
true;
923 bool endWriteAction =
false;
924 bool beginWriteAction =
true;
925 bool filePointerOk =
true;
926 while ((extendBuffer) && (! endWriteAction) && (filePointerOk)) {
928 beginWriteAction = fGL2PSAction->enableFileWriting();
934 filePointerOk = fGL2PSAction->fileWritingEnabled();
936 if (beginWriteAction) {
940 fGL2PSAction->setLineWidth(fGl2psDefaultLineWith);
942 fGL2PSAction->setPointSize(fGl2psDefaultPointSize);
945 endWriteAction = fGL2PSAction->disableFileWriting();
948 if ((! endWriteAction) || (! beginWriteAction)) {
949 extendBuffer = fGL2PSAction->extendBufferSize();
953 fGL2PSAction->resetBufferSizeParameters();
955 if (!extendBuffer ) {
956 G4cerr <<
"ERROR: gl2ps buffer size is not big enough to print this geometry. Try to extend it. No output produced"<<
G4endl;
959 if (!beginWriteAction ) {
960 G4cerr <<
"ERROR: saving file "<<getRealPrintFilename().c_str()<<
". Check read/write access. No output produced" <<
G4endl;
963 if (!endWriteAction ) {
970 oglSceneHandler.SetFlushAction(originalFlushAction);
979 unsigned int G4OpenGLViewer::getWinWidth()
const{
983 unsigned int G4OpenGLViewer::getWinHeight()
const{
987 G4bool G4OpenGLViewer::sizeHasChanged() {
988 return fSizeHasChanged;
991 G4int G4OpenGLViewer::getRealExportWidth() {
992 if (fPrintSizeX == -1) {
996 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
999 if ((dims[0] !=0 ) && (dims[1] !=0)) {
1000 if (fPrintSizeX > dims[0]){
1004 if (fPrintSizeX < -1){
1010 G4int G4OpenGLViewer::getRealExportHeight() {
1011 if (fPrintSizeY == -1) {
1015 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
1018 if ((dims[0] !=0 ) && (dims[1] !=0)) {
1019 if (fPrintSizeY > dims[1]){
1023 if (fPrintSizeY < -1){
1029 void G4OpenGLViewer::setExportSize(
G4int X,
G4int Y) {
1040 bool G4OpenGLViewer::setExportFilename(
G4String name,
G4bool inc) {
1046 if ((name !=
"") && (fExportFilename != name)) {
1047 fExportFilenameIndex=0;
1050 fExportFilenameIndex=-1;
1053 if (name.size() == 0) {
1054 name = getRealPrintFilename().c_str();
1057 std::string extension = name.substr(name.find_last_of(
".") + 1);
1059 if (name.size() != extension.size()) {
1060 if (! setExportImageFormat(extension,
false)) {
1065 fExportFilename = name.substr(0,name.find_last_of(
"."));
1070 std::string G4OpenGLViewer::getRealPrintFilename() {
1071 std::string temp = fExportFilename;
1072 if (fExportFilenameIndex != -1) {
1073 temp += std::string(
"_");
1074 std::ostringstream os;
1075 os << std::setw(4) << std::setfill(
'0') << fExportFilenameIndex;
1076 std::string nb_str = os.str();
1079 temp +=
"."+fExportImageFormat;
1083 GLdouble G4OpenGLViewer::getSceneNearWidth()
1085 if (!fSceneHandler.GetScene()) {
1089 = fSceneHandler.GetScene()->GetStandardTargetPoint()
1090 + fVP.GetCurrentTargetPoint ();
1091 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
1092 if(radius<=0.) radius = 1.;
1093 const G4double cameraDistance = fVP.GetCameraDistance (radius);
1094 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
1095 return 2 * fVP.GetFrontHalfHeight (pnear, radius);
1098 GLdouble G4OpenGLViewer::getSceneFarWidth()
1100 if (!fSceneHandler.GetScene()) {
1104 = fSceneHandler.GetScene()->GetStandardTargetPoint()
1105 + fVP.GetCurrentTargetPoint ();
1106 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
1107 if(radius<=0.) radius = 1.;
1108 const G4double cameraDistance = fVP.GetCameraDistance (radius);
1109 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
1110 const GLdouble pfar = fVP.GetFarDistance (cameraDistance, pnear, radius);
1111 return 2 * fVP.GetFrontHalfHeight (pfar, radius);
1115 GLdouble G4OpenGLViewer::getSceneDepth()
1117 if (!fSceneHandler.GetScene()) {
1121 = fSceneHandler.GetScene()->GetStandardTargetPoint()
1122 + fVP.GetCurrentTargetPoint ();
1123 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
1124 if(radius<=0.) radius = 1.;
1125 const G4double cameraDistance = fVP.GetCameraDistance (radius);
1126 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
1127 return fVP.GetFarDistance (cameraDistance, pnear, radius)- pnear;
1135 rotateSceneInViewDirection(dx,dy);
1138 rotateSceneThetaPhi(dx,0);
1141 rotateSceneThetaPhi(0,dy);
1150 rotateSceneInViewDirection(dx,dy);
1153 rotateSceneThetaPhi(dx,0);
1156 rotateSceneThetaPhi(0,dy);
1163 if (!fSceneHandler.GetScene()) {
1191 vp = fVP.GetViewpointDirection ().
unit ();
1192 up = fVP.GetUpVector ().
unit ();
1194 yprime = (up.
cross(vp)).unit();
1195 zprime = (vp.
cross(yprime)).unit();
1197 if (fVP.GetLightsMoveWithCamera()) {
1198 delta_alpha = dy * fRot_sens;
1199 delta_theta = -dx * fRot_sens;
1201 delta_alpha = -dy * fRot_sens;
1202 delta_theta = dx * fRot_sens;
1208 new_vp = std::cos(delta_alpha) * vp + std::sin(delta_alpha) * zprime;
1213 if (fVP.GetLightsMoveWithCamera()) {
1214 new_up = (new_vp.
cross(yprime)).unit();
1215 if (new_vp.
z()*vp.
z() <0) {
1216 new_up.
set(new_up.
x(),-new_up.
y(),new_up.
z());
1220 if (new_vp.
z()*vp.
z() <0) {
1221 new_up.
set(new_up.
x(),-new_up.
y(),new_up.
z());
1224 fVP.SetUpVector(new_up);
1228 cosalpha = new_up.
dot (new_vp.
unit());
1229 sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
1231 xprime = yprime.
cross (new_up);
1233 a1 = sinalpha * xprime;
1235 a2 = sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
1241 fVP.SetViewAndLights (viewPoint);
1247 if (!fSceneHandler.GetScene()) {
1271 vp = fVP.GetViewpointDirection ().
unit();
1272 up = fVP.GetUpVector ().
unit();
1275 up.
z()*vp.
x()-up.
x()*vp.
z(),
1276 up.
x()*vp.
y()-up.
y()*vp.
x());
1278 viewPoint = vp/fRot_sens + (zPrimeVector*dx - up*
dy) ;
1279 new_up =
G4Vector3D(viewPoint.
y()*zPrimeVector.
z()-viewPoint.
z()*zPrimeVector.
y(),
1280 viewPoint.
z()*zPrimeVector.
x()-viewPoint.
x()*zPrimeVector.
z(),
1281 viewPoint.
x()*zPrimeVector.
y()-viewPoint.
y()*zPrimeVector.
x());
1287 fVP.SetUpVector(new_upUnit);
1288 fVP.SetViewAndLights (viewPoint);
1292 void G4OpenGLViewer::addExportImageFormat(std::string format) {
1293 fExportImageFormatVector.push_back(format);
1296 bool G4OpenGLViewer::setExportImageFormat(std::string format,
bool quiet) {
1299 for (
unsigned int a=0; a<fExportImageFormatVector.size(); a++) {
1300 list +=fExportImageFormatVector.at(a) +
" ";
1302 if (fExportImageFormatVector.at(a) == format) {
1304 G4cout <<
" Changing export format to \"" << format <<
"\"" <<
G4endl;
1306 if (format != fExportImageFormat) {
1307 fExportFilenameIndex = 0;
1308 fExportImageFormat = format;
1314 if (format.size() == 0) {
1315 G4cout <<
" Current formats availables are : " << list <<
G4endl;
1317 G4cerr <<
" Format \"" << format <<
"\" is not available for the selected viewer. Current formats availables are : " << list <<
G4endl;
1327 void G4OpenGLViewer::g4GluPickMatrix(GLdouble x, GLdouble y, GLdouble width, GLdouble height,
1334 sx = viewport[2] / width;
1335 sy = viewport[3] / height;
1336 tx = (viewport[2] + 2.0 * (viewport[0] -
x)) / width;
1337 ty = (viewport[3] + 2.0 * (viewport[1] -
y)) / height;
1339 #define M(row, col) mat[col*4+row]
1369 void G4OpenGLViewer::g4GluLookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
1370 GLdouble centerx, GLdouble centery, GLdouble
1372 GLdouble upx, GLdouble upy, GLdouble upz )
1375 GLdouble x[3], y[3],
z[3];
1381 z[0] = eyex - centerx;
1382 z[1] = eyey - centery;
1383 z[2] = eyez - centerz;
1384 mag = std::sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
1397 x[0] = y[1] * z[2] - y[2] * z[1];
1398 x[1] = -y[0] * z[2] + y[2] * z[0];
1399 x[2] = y[0] * z[1] - y[1] * z[0];
1402 y[0] = z[1] * x[2] - z[2] * x[1];
1403 y[1] = -z[0] * x[2] + z[2] * x[0];
1404 y[2] = z[0] * x[1] - z[1] * x[0];
1411 mag = std::sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
1418 mag = std::sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
1425 #define M(row,col) mat[col*4+row]
1446 glTranslated(-eyex, -eyey, -eyez);
1449 void G4OpenGLViewer::g4GlOrtho (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
1452 GLdouble a = 2.0 / (right -
left);
1453 GLdouble
b = 2.0 / (top - bottom);
1454 GLdouble
c = -2.0 / (zFar - zNear);
1456 GLdouble tx = - (right +
left)/(right - left);
1457 GLdouble ty = - (top + bottom)/(top - bottom);
1458 GLdouble tz = - (zFar + zNear)/(zFar - zNear);
1460 GLdouble ortho[16] = {
1466 glMultMatrixd(ortho);
1471 void G4OpenGLViewer::g4GlFrustum (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
1474 GLdouble deltaX = right -
left;
1475 GLdouble deltaY = top - bottom;
1476 GLdouble deltaZ = zFar - zNear;
1478 GLdouble a = 2.0f * zNear / deltaX;
1479 GLdouble b = 2.0f * zNear / deltaY;
1480 GLdouble c = (right +
left) / deltaX;
1481 GLdouble
d = (top + bottom) / deltaY;
1482 GLdouble
e = -(zFar + zNear) / (zFar - zNear);
1483 GLdouble
f = -2.0f * zFar * zNear / deltaZ;
1485 GLdouble
proj[16] = {
1492 glMultMatrixd(proj);
1497 #ifdef G4OPENGL_VERSION_2
1500 void G4OpenGLViewer::setVboDrawer(G4OpenGLVboDrawer* drawer) {
1501 fVboDrawer = drawer;
1503 G4OpenGLSceneHandler& sh =
dynamic_cast<G4OpenGLSceneHandler&
>(fSceneHandler);
1504 sh.setVboDrawer(fVboDrawer);
1505 }
catch(std::bad_cast exp) { }
1512 std::ostringstream txt;
1513 for (
unsigned int a=0; a<fAttributes.size(); a++) {
1514 txt << fAttributes[
a];
1515 if (a < fAttributes.size() - 1) txt <<
"\n";