32#ifdef G4VIS_BUILD_OPENGL_DRIVER
52#ifdef G4OPENGL_VERSION_2
64G4OpenGLViewer::G4OpenGLViewer (G4OpenGLSceneHandler& scene):
66#ifdef G4OPENGL_VERSION_2
71fOpenGLSceneHandler(scene),
73transparency_enabled (true),
74antialiasing_enabled (false),
75haloing_enabled (false),
80fDefaultExportImageFormat(
"pdf"),
81fExportImageFormat(
"pdf"),
82fExportFilenameIndex(0),
86fDefaultExportFilename(
"G4OpenGL"),
88fGl2psDefaultLineWith(1),
89fGl2psDefaultPointSize(2),
90fGlViewInitialized(false),
91fIsGettingPickInfos(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();
125G4OpenGLViewer::~G4OpenGLViewer ()
130void 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);
206void G4OpenGLViewer::ClearView () {
207 ClearViewWithoutFlush();
209 if(!isFramebufferReady()) {
217void 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);
237void 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;
253void 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);
280void 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;
326 const GLdouble left = -right;
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 ();
437void G4OpenGLViewer::ResetView () {
444void G4OpenGLViewer::HaloingFirstPass () {
454 glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
457 glDepthMask (GL_TRUE);
458 glDepthFunc (GL_LESS);
462 ChangeLineWidth(3.0);
466void G4OpenGLViewer::HaloingSecondPass () {
469 glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
470 glDepthFunc (GL_LEQUAL);
471 ChangeLineWidth(1.0);
475G4String 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();
491const std::vector < G4OpenGLViewerPickMap* > & G4OpenGLViewer::GetPickDetails(GLdouble x, GLdouble y)
493 static std::vector < G4OpenGLViewerPickMap* > pickMapVector;
494 for (
auto pickMap: pickMapVector) {
497 pickMapVector.clear();
499 const G4int BUFSIZE = 512;
500 GLuint selectBuffer[BUFSIZE];
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;
581GLubyte* 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);
632bool 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++;
668bool G4OpenGLViewer::printVectoredEPS() {
672bool G4OpenGLViewer::printNonVectoredEPS () {
674 int width = getRealExportWidth();
675 int height = getRealExportHeight();
680 int components,
pos, i;
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");
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");
759bool G4OpenGLViewer::isGl2psWriting() {
761 if (!fGL2PSAction)
return false;
762 if (fGL2PSAction->fileWritingEnabled()) {
769G4bool 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
797void G4OpenGLViewer::DrawText(
const G4Text& g4text)
800 if (isGl2psWriting()) {
803 G4double size = fSceneHandler.GetMarkerSize(g4text,sizeType);
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 \""
837void G4OpenGLViewer::ChangePointSize(
G4double size) {
839 if (isGl2psWriting()) {
840 fGL2PSAction->setPointSize(
int(size));
849void G4OpenGLViewer::ChangeLineWidth(
G4double width) {
851 if (isGl2psWriting()) {
852 fGL2PSAction->setLineWidth(
int(width));
866bool 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);
892bool 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);
979unsigned int G4OpenGLViewer::getWinWidth()
const{
983unsigned int G4OpenGLViewer::getWinHeight()
const{
987G4bool G4OpenGLViewer::sizeHasChanged() {
988 return fSizeHasChanged;
991G4int 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){
1010G4int 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){
1029void G4OpenGLViewer::setExportSize(
G4int X,
G4int Y) {
1040bool 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 (extension.size() >= 3 && extension.size() <= 4) {
1060 if (setExportImageFormat(extension,
false)) {
1061 fExportFilename =
name.substr(0,
name.find_last_of(
"."));
1066 fExportFilename =
name;
1072std::string G4OpenGLViewer::getRealPrintFilename() {
1073 std::string temp = fExportFilename;
1074 if (fExportFilenameIndex != -1) {
1075 temp += std::string(
"_");
1076 std::ostringstream os;
1077 os << std::setw(4) << std::setfill(
'0') << fExportFilenameIndex;
1078 std::string nb_str = os.str();
1081 temp +=
"."+fExportImageFormat;
1085GLdouble G4OpenGLViewer::getSceneNearWidth()
1087 if (!fSceneHandler.GetScene()) {
1091 = fSceneHandler.GetScene()->GetStandardTargetPoint()
1092 + fVP.GetCurrentTargetPoint ();
1093 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
1094 if(radius<=0.) radius = 1.;
1095 const G4double cameraDistance = fVP.GetCameraDistance (radius);
1096 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
1097 return 2 * fVP.GetFrontHalfHeight (pnear, radius);
1100GLdouble G4OpenGLViewer::getSceneFarWidth()
1102 if (!fSceneHandler.GetScene()) {
1106 = fSceneHandler.GetScene()->GetStandardTargetPoint()
1107 + fVP.GetCurrentTargetPoint ();
1108 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
1109 if(radius<=0.) radius = 1.;
1110 const G4double cameraDistance = fVP.GetCameraDistance (radius);
1111 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
1112 const GLdouble pfar = fVP.GetFarDistance (cameraDistance, pnear, radius);
1113 return 2 * fVP.GetFrontHalfHeight (pfar, radius);
1117GLdouble G4OpenGLViewer::getSceneDepth()
1119 if (!fSceneHandler.GetScene()) {
1123 = fSceneHandler.GetScene()->GetStandardTargetPoint()
1124 + fVP.GetCurrentTargetPoint ();
1125 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
1126 if(radius<=0.) radius = 1.;
1127 const G4double cameraDistance = fVP.GetCameraDistance (radius);
1128 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
1129 return fVP.GetFarDistance (cameraDistance, pnear, radius)- pnear;
1137 rotateSceneInViewDirection(dx,dy);
1140 rotateSceneThetaPhi(dx,0);
1143 rotateSceneThetaPhi(0,dy);
1152 rotateSceneInViewDirection(dx,dy);
1155 rotateSceneThetaPhi(dx,0);
1158 rotateSceneThetaPhi(0,dy);
1165 if (!fSceneHandler.GetScene()) {
1193 vp = fVP.GetViewpointDirection ().
unit ();
1194 up = fVP.GetUpVector ().
unit ();
1196 yprime = (up.
cross(vp)).unit();
1197 zprime = (vp.
cross(yprime)).unit();
1199 if (fVP.GetLightsMoveWithCamera()) {
1200 delta_alpha = dy * fRot_sens;
1201 delta_theta = -dx * fRot_sens;
1203 delta_alpha = -dy * fRot_sens;
1204 delta_theta = dx * fRot_sens;
1207 delta_alpha *= CLHEP::deg;
1208 delta_theta *= CLHEP::deg;
1210 new_vp = std::cos(delta_alpha) * vp + std::sin(delta_alpha) * zprime;
1215 if (fVP.GetLightsMoveWithCamera()) {
1216 new_up = (new_vp.
cross(yprime)).unit();
1217 if (new_vp.
z()*vp.
z() <0) {
1218 new_up.
set(new_up.
x(),-new_up.
y(),new_up.
z());
1222 if (new_vp.
z()*vp.
z() <0) {
1223 new_up.
set(new_up.
x(),-new_up.
y(),new_up.
z());
1226 fVP.SetUpVector(new_up);
1230 cosalpha = new_up.
dot (new_vp.
unit());
1231 sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
1232 yprime = (new_up.
cross (new_vp.
unit())).unit ();
1233 xprime = yprime.
cross (new_up);
1235 a1 = sinalpha * xprime;
1237 a2 = sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
1241 viewPoint = new_vp.
unit() + delta;
1243 fVP.SetViewAndLights (viewPoint);
1249 if (!fSceneHandler.GetScene()) {
1273 vp = fVP.GetViewpointDirection ().
unit();
1274 up = fVP.GetUpVector ().
unit();
1277 up.
z()*vp.
x()-up.
x()*vp.
z(),
1278 up.
x()*vp.
y()-up.
y()*vp.
x());
1280 viewPoint = vp/fRot_sens + (zPrimeVector*dx - up*dy) ;
1281 new_up =
G4Vector3D(viewPoint.
y()*zPrimeVector.
z()-viewPoint.
z()*zPrimeVector.
y(),
1282 viewPoint.
z()*zPrimeVector.
x()-viewPoint.
x()*zPrimeVector.
z(),
1283 viewPoint.
x()*zPrimeVector.
y()-viewPoint.
y()*zPrimeVector.
x());
1289 fVP.SetUpVector(new_upUnit);
1290 fVP.SetViewAndLights (viewPoint);
1294void G4OpenGLViewer::addExportImageFormat(std::string format) {
1295 fExportImageFormatVector.push_back(format);
1298bool G4OpenGLViewer::setExportImageFormat(std::string format,
bool quiet) {
1301 for (
unsigned int a=0; a<fExportImageFormatVector.size(); a++) {
1302 list +=fExportImageFormatVector.at(a) +
" ";
1304 if (fExportImageFormatVector.at(a) == format) {
1306 G4cout <<
" Changing export format to \"" << format <<
"\"" <<
G4endl;
1308 if (format != fExportImageFormat) {
1309 fExportFilenameIndex = 0;
1310 fExportImageFormat = format;
1316 if (format.size() == 0) {
1317 G4cout <<
" Current formats availables are : " << list <<
G4endl;
1319 G4cerr <<
" Format \"" << format <<
"\" is not available for the selected viewer. Current formats availables are : " << list <<
G4endl;
1329void G4OpenGLViewer::g4GluPickMatrix(GLdouble x, GLdouble y, GLdouble width, GLdouble height,
1336 sx = viewport[2] / width;
1337 sy = viewport[3] / height;
1338 tx = (viewport[2] + 2.0 * (viewport[0] - x)) / width;
1339 ty = (viewport[3] + 2.0 * (viewport[1] - y)) / height;
1341#define M(row, col) mat[col*4+row]
1371void G4OpenGLViewer::g4GluLookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
1372 GLdouble centerx, GLdouble centery, GLdouble
1374 GLdouble upx, GLdouble upy, GLdouble upz )
1377 GLdouble x[3], y[3], z[3];
1383 z[0] = eyex - centerx;
1384 z[1] = eyey - centery;
1385 z[2] = eyez - centerz;
1386 mag = std::sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
1399 x[0] = y[1] * z[2] - y[2] * z[1];
1400 x[1] = -y[0] * z[2] + y[2] * z[0];
1401 x[2] = y[0] * z[1] - y[1] * z[0];
1404 y[0] = z[1] * x[2] - z[2] * x[1];
1405 y[1] = -z[0] * x[2] + z[2] * x[0];
1406 y[2] = z[0] * x[1] - z[1] * x[0];
1413 mag = std::sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
1420 mag = std::sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
1427#define M(row,col) mat[col*4+row]
1448 glTranslated(-eyex, -eyey, -eyez);
1451void G4OpenGLViewer::g4GlOrtho (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
1454 GLdouble a = 2.0 / (right - left);
1455 GLdouble b = 2.0 / (top - bottom);
1456 GLdouble c = -2.0 / (zFar - zNear);
1458 GLdouble tx = - (right + left)/(right - left);
1459 GLdouble ty = - (top + bottom)/(top - bottom);
1460 GLdouble tz = - (zFar + zNear)/(zFar - zNear);
1462 GLdouble ortho[16] = {
1468 glMultMatrixd(ortho);
1473void G4OpenGLViewer::g4GlFrustum (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
1476 GLdouble deltaX = right - left;
1477 GLdouble deltaY = top - bottom;
1478 GLdouble deltaZ = zFar - zNear;
1480 GLdouble a = 2.0f * zNear / deltaX;
1481 GLdouble b = 2.0f * zNear / deltaY;
1482 GLdouble c = (right + left) / deltaX;
1483 GLdouble d = (top + bottom) / deltaY;
1484 GLdouble e = -(zFar + zNear) / (zFar - zNear);
1485 GLdouble f = -2.0f * zFar * zNear / deltaZ;
1487 GLdouble proj[16] = {
1494 glMultMatrixd(proj);
1499#ifdef G4OPENGL_VERSION_2
1502void G4OpenGLViewer::setVboDrawer(G4OpenGLVboDrawer* drawer) {
1503 fVboDrawer = drawer;
1505 G4OpenGLSceneHandler& sh =
dynamic_cast<G4OpenGLSceneHandler&
>(fSceneHandler);
1506 sh.setVboDrawer(fVboDrawer);
1507 }
catch(std::bad_cast exp) { }
1513G4String G4OpenGLViewerPickMap::print() {
1514 std::ostringstream txt;
1515 for (
unsigned int a=0; a<fAttributes.size(); a++) {
1516 txt << fAttributes[a];
1517 if (a < fAttributes.size() - 1) txt <<
"\n";
HepGeom::Vector3D< G4double > G4Vector3D
std::vector< G4Plane3D > G4Planes
G4GLOB_DLL std::ostream G4cerr
G4GLOB_DLL std::ostream G4cout
const std::vector< const std::vector< G4AttValue > * > & GetAttValues() const
const std::vector< const std::map< G4String, G4AttDef > * > & GetAttDefs() const
G4Point3D GetPosition() const
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
const char * name(G4int ptype)