33 #ifdef G4VIS_BUILD_OPENGL_DRIVER
53 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
63 G4int G4OpenGLViewer::fPrintSizeX = -1;
64 G4int G4OpenGLViewer::fPrintSizeY = -1;
65 G4String G4OpenGLViewer::fPrintFilename =
"G4OpenGL";
66 int G4OpenGLViewer::fPrintFilenameIndex = 0;
68 G4OpenGLViewer::G4OpenGLViewer (G4OpenGLSceneHandler& scene):
70 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
75 fOpenGLSceneHandler(scene),
77 transparency_enabled (true),
78 antialiasing_enabled (false),
79 haloing_enabled (false),
83 fDisplayHeadTime(false),
84 fDisplayHeadTimeX(-0.9),
85 fDisplayHeadTimeY(-0.9),
86 fDisplayHeadTimeSize(24.),
87 fDisplayHeadTimeRed(0.),
88 fDisplayHeadTimeGreen(1.),
89 fDisplayHeadTimeBlue(1.),
90 fDisplayLightFront(false),
91 fDisplayLightFrontX(0.),
92 fDisplayLightFrontY(0.),
93 fDisplayLightFrontZ(0.),
94 fDisplayLightFrontT(0.),
95 fDisplayLightFrontRed(0.),
96 fDisplayLightFrontGreen(1.),
97 fDisplayLightFrontBlue(0.),
104 fGl2psDefaultLineWith(1),
105 fGl2psDefaultPointSize(2)
107 #ifdef G4DEBUG_VIS_OGL
108 printf(
"G4OpenGLViewer:: Creation\n");
111 fVP.SetAutoRefresh(
true);
112 fDefaultVP.SetAutoRefresh(
true);
124 G4OpenGLViewer::~G4OpenGLViewer ()
129 void G4OpenGLViewer::InitializeGLView ()
131 #ifdef G4OPENGL_VERSION_2
133 const char *fragmentShaderSrc =
135 "precision highp float;\n"
138 "varying vec3 vLightWeighting;\n"
139 "uniform vec4 uPointColor; // Point Color\n"
141 "void main(void) {\n"
142 " vec4 matColor = uPointColor;\n"
143 " gl_FragColor = vec4(matColor.rgb, matColor.a);\n"
148 const char *vertexShaderSrc =
149 "attribute vec3 aVertexPosition;\n"
150 "attribute vec3 aVertexNormal;\n"
152 "uniform mat4 uMVMatrix; // [M]odel[V]iew matrix\n"
153 "uniform mat4 uCMatrix; // Client-side manipulated [C]amera matrix\n"
154 "uniform mat4 uPMatrix; // Perspective [P]rojection matrix\n"
155 "uniform mat4 uNMatrix; // [N]ormal transformation\n"
156 "// uNMatrix is the transpose of the inverse of uCMatrix * uMVMatrix\n"
157 "uniform mat4 uTMatrix; // [T]ransformation matrix\n"
158 "uniform float uPointSize; // Point size\n"
160 "varying vec3 vLightWeighting;\n"
162 "void main(void) {\n"
163 " // Calculate the position of this vertex\n"
164 " gl_Position = uPMatrix * uCMatrix * uMVMatrix * uTMatrix * vec4(aVertexPosition, 1.0);\n"
166 " // Phong shading\n"
167 " vec3 transformedNormal = normalize((uNMatrix * vec4(normalize(aVertexNormal), 0)).xyz);\n"
168 " vec3 lightingDirection = normalize(vec3(1, 1, 1));\n"
169 " float directionalLightWeighting = max(dot(transformedNormal, lightingDirection), 0.0);\n"
170 " vec3 uAmbientLightColor = vec3(0.2, 0.2, 0.2);\n"
171 " vec3 uDirectionalColor = vec3(0.8, 0.8, 0.8);\n"
172 " gl_PointSize = uPointSize;\n"
173 " vLightWeighting = uAmbientLightColor + uDirectionalColor * directionalLightWeighting;\n"
176 vertexShader_ = vertexShaderSrc;
177 fragmentShader_ = fragmentShaderSrc;
182 Shader fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
183 const char *frag = fragmentShader_.c_str();
184 glShaderSource(fragmentShader, 1, &frag, NULL);
185 glCompileShader(fragmentShader);
186 Shader vertexShader = glCreateShader(GL_VERTEX_SHADER);
187 const char *vert = vertexShader_.c_str();
188 glShaderSource(vertexShader, 1, &vert, NULL);
189 glCompileShader(vertexShader);
190 shaderProgram_ = glCreateProgram();
191 glAttachShader(shaderProgram_, vertexShader);
192 glAttachShader(shaderProgram_, fragmentShader);
193 glLinkProgram(shaderProgram_);
194 glUseProgram(shaderProgram_);
201 vertexPositionAttribute_ =
202 glGetAttribLocation(shaderProgram_,
"aVertexPosition");
203 glEnableVertexAttribArray(vertexPositionAttribute_);
206 pMatrixUniform_ = glGetUniformLocation(shaderProgram_,
"uPMatrix");
207 cMatrixUniform_ = glGetUniformLocation(shaderProgram_,
"uCMatrix");
208 mvMatrixUniform_ = glGetUniformLocation(shaderProgram_,
"uMVMatrix");
209 nMatrixUniform_ = glGetUniformLocation(shaderProgram_,
"uNMatrix");
210 tMatrixUniform_ = glGetUniformLocation(shaderProgram_,
"uTMatrix");
214 #ifdef G4DEBUG_VIS_OGL
215 printf(
"G4OpenGLViewer::InitializeGLView\n");
218 fWinSize_x = fVP.GetWindowSizeHintX();
219 fWinSize_y = fVP.GetWindowSizeHintY();
221 glClearColor (0.0, 0.0, 0.0, 0.0);
223 glDisable (GL_LINE_SMOOTH);
224 glDisable (GL_POLYGON_SMOOTH);
230 glDepthFunc (GL_LEQUAL);
231 glDepthMask (GL_TRUE);
234 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
236 #ifdef G4DEBUG_VIS_OGL
237 printf(
"G4OpenGLViewer::InitializeGLView END\n");
241 void G4OpenGLViewer::ClearView () {
242 #ifdef G4DEBUG_VIS_OGL
243 printf(
"G4OpenGLViewer::ClearView\n");
245 glClearColor (background.GetRed(),
246 background.GetGreen(),
247 background.GetBlue(),
252 glClear (GL_COLOR_BUFFER_BIT);
253 glClear (GL_DEPTH_BUFFER_BIT);
254 glClear (GL_STENCIL_BUFFER_BIT);
255 #ifdef G4DEBUG_VIS_OGL
256 printf(
"G4OpenGLViewer::ClearView flush\n");
262 void G4OpenGLViewer::ResizeWindow(
unsigned int aWidth,
unsigned int aHeight) {
263 if ((fWinSize_x != aWidth) || (fWinSize_y != aHeight)) {
265 fWinSize_y = aHeight;
266 fSizeHasChanged =
true;
268 fSizeHasChanged =
false;
278 void G4OpenGLViewer::ResizeGLView()
280 #ifdef G4DEBUG_VIS_OGL
281 printf(
"G4OpenGLViewer::ResizeGLView %d %d %#lx\n",fWinSize_x,fWinSize_y,(
unsigned long)
this);
288 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
290 if ((dims[0] !=0 ) && (dims[1] !=0)) {
292 if (fWinSize_x > (
unsigned)dims[0]) {
293 G4cerr <<
"Try to resize view greater than max X viewport dimension. Desired size "<<fWinSize_x <<
" is resize to "<< dims[0] <<
G4endl;
294 fWinSize_x = dims[0];
296 if (fWinSize_y > (
unsigned)dims[1]) {
297 G4cerr <<
"Try to resize view greater than max Y viewport dimension. Desired size "<<fWinSize_y <<
" is resize to "<< dims[1] <<
G4endl;
298 fWinSize_y = dims[1];
302 glViewport(0, 0, fWinSize_x,fWinSize_y);
308 void G4OpenGLViewer::SetView () {
310 if (!fSceneHandler.GetScene()) {
318 GLfloat lightPosition [4];
319 lightPosition [0] = fVP.GetActualLightpointDirection().x();
320 lightPosition [1] = fVP.GetActualLightpointDirection().y();
321 lightPosition [2] = fVP.GetActualLightpointDirection().z();
322 lightPosition [3] = 0.;
324 GLfloat ambient [] = { 0.2, 0.2, 0.2, 1.};
325 GLfloat diffuse [] = { 0.8, 0.8, 0.8, 1.};
326 glEnable (GL_LIGHT0);
327 glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);
328 glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
332 if (fWinSize_y > fWinSize_x) {
335 if (fWinSize_x > fWinSize_y) {
342 = fSceneHandler.GetScene()->GetStandardTargetPoint()
343 + fVP.GetCurrentTargetPoint ();
344 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
345 if(radius<=0.) radius = 1.;
346 const G4double cameraDistance = fVP.GetCameraDistance (radius);
348 targetPoint + cameraDistance * fVP.GetViewpointDirection().
unit();
349 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
350 const GLdouble pfar = fVP.GetFarDistance (cameraDistance, pnear, radius);
351 const GLdouble
right = fVP.GetFrontHalfHeight (pnear, radius) * ratioY;
353 const GLdouble top = fVP.GetFrontHalfHeight (pnear, radius) * ratioX;
354 const GLdouble bottom = -top;
360 glMatrixMode (GL_PROJECTION);
363 const G4Vector3D scaleFactor = fVP.GetScaleFactor();
364 glScaled(scaleFactor.
x(),scaleFactor.
y(),scaleFactor.
z());
366 if (fVP.GetFieldHalfAngle() == 0.) {
367 glOrtho (left, right, bottom, top, pnear, pfar);
370 glFrustum (left, right, bottom, top, pnear, pfar);
373 glMatrixMode (GL_MODELVIEW);
376 const G4Normal3D& upVector = fVP.GetUpVector ();
378 if (cameraDistance > 1.e-6 * radius) {
379 gltarget = targetPoint;
382 gltarget = targetPoint - radius * fVP.GetViewpointDirection().
unit();
385 const G4Point3D& pCamera = cameraPosition;
386 gluLookAt (pCamera.
x(), pCamera.
y(), pCamera.
z(),
387 gltarget.
x(), gltarget.
y(), gltarget.
z(),
388 upVector.
x(), upVector.
y(), upVector.
z());
391 glLightfv (GL_LIGHT0, GL_POSITION, lightPosition);
405 sArray[3] = sp.
d() + radius * 1.e-05;
406 glClipPlane (GL_CLIP_PLANE0, sArray);
407 glEnable (GL_CLIP_PLANE0);
411 sArray[3] = -sp.
d() + radius * 1.e-05;
412 glClipPlane (GL_CLIP_PLANE1, sArray);
413 glEnable (GL_CLIP_PLANE1);
415 glDisable (GL_CLIP_PLANE0);
416 glDisable (GL_CLIP_PLANE1);
423 const G4Planes& cutaways = fVP.GetCutawayPlanes();
424 size_t nPlanes = cutaways.size();
425 if (fVP.IsCutaway() &&
429 a[0] = cutaways[0].a();
430 a[1] = cutaways[0].b();
431 a[2] = cutaways[0].c();
432 a[3] = cutaways[0].d();
433 glClipPlane (GL_CLIP_PLANE2, a);
434 glEnable (GL_CLIP_PLANE2);
436 a[0] = cutaways[1].a();
437 a[1] = cutaways[1].b();
438 a[2] = cutaways[1].c();
439 a[3] = cutaways[1].d();
440 glClipPlane (GL_CLIP_PLANE3, a);
441 glEnable (GL_CLIP_PLANE3);
444 a[0] = cutaways[2].a();
445 a[1] = cutaways[2].b();
446 a[2] = cutaways[2].c();
447 a[3] = cutaways[2].d();
448 glClipPlane (GL_CLIP_PLANE4, a);
449 glEnable (GL_CLIP_PLANE4);
452 glDisable (GL_CLIP_PLANE2);
453 glDisable (GL_CLIP_PLANE3);
454 glDisable (GL_CLIP_PLANE4);
458 background = fVP.GetBackgroundColour ();
464 void G4OpenGLViewer::ResetView () {
471 void G4OpenGLViewer::HaloingFirstPass () {
481 glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
484 glDepthMask (GL_TRUE);
485 glDepthFunc (GL_LESS);
489 ChangeLineWidth(3.0);
493 void G4OpenGLViewer::HaloingSecondPass () {
496 glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
497 glDepthFunc (GL_LEQUAL);
498 ChangeLineWidth(1.0);
502 void G4OpenGLViewer::Pick(GLdouble
x, GLdouble
y)
507 glSelectBuffer(BUFSIZE, selectBuffer);
508 glRenderMode(GL_SELECT);
511 glMatrixMode(GL_PROJECTION);
512 G4double currentProjectionMatrix[16];
513 glGetDoublev(GL_PROJECTION_MATRIX, currentProjectionMatrix);
517 glGetIntegerv(GL_VIEWPORT, viewport);
519 gluPickMatrix(x, viewport[3] - y, 5., 5., viewport);
520 glMultMatrixd(currentProjectionMatrix);
521 glMatrixMode(GL_MODELVIEW);
523 GLint
hits = glRenderMode(GL_RENDER);
525 G4cout <<
"Too many hits. Zoom in to reduce overlaps." <<
G4endl;
528 GLuint*
p = selectBuffer;
529 for (GLint i = 0; i <
hits; ++i) {
530 GLuint nnames = *p++;
539 for (GLuint j = 0; j < nnames; ++j) {
542 <<
", Sub-hit: " << j
543 <<
", PickName: " << name <<
G4endl;
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) {
560 glMatrixMode(GL_PROJECTION);
562 glMatrixMode(GL_MODELVIEW);
568 GLubyte* G4OpenGLViewer::grabPixels (
int inColor,
unsigned int width,
unsigned int height) {
571 GLint swapbytes, lsbfirst, rowlength;
572 GLint skiprows, skippixels, alignment;
578 size = width*height*3;
580 format = GL_LUMINANCE;
581 size = width*height*1;
584 buffer =
new GLubyte[size];
588 glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapbytes);
589 glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirst);
590 glGetIntegerv (GL_UNPACK_ROW_LENGTH, &rowlength);
592 glGetIntegerv (GL_UNPACK_SKIP_ROWS, &skiprows);
593 glGetIntegerv (GL_UNPACK_SKIP_PIXELS, &skippixels);
594 glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignment);
596 glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
597 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
598 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
600 glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
601 glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
602 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
604 glReadBuffer(GL_FRONT);
605 glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, format, GL_UNSIGNED_BYTE, (GLvoid*) buffer);
607 glPixelStorei (GL_UNPACK_SWAP_BYTES, swapbytes);
608 glPixelStorei (GL_UNPACK_LSB_FIRST, lsbfirst);
609 glPixelStorei (GL_UNPACK_ROW_LENGTH, rowlength);
611 glPixelStorei (GL_UNPACK_SKIP_ROWS, skiprows);
612 glPixelStorei (GL_UNPACK_SKIP_PIXELS, skippixels);
613 glPixelStorei (GL_UNPACK_ALIGNMENT, alignment);
618 void G4OpenGLViewer::printEPS() {
620 #ifdef G4DEBUG_VIS_OGL
621 printf(
"G4OpenGLViewer::printEPS file:%s Vec:%d Name:%s\n",getRealPrintFilename().c_str(),fVectoredPs,GetName().c_str());
626 size_t len = strlen(setlocale(LC_NUMERIC,NULL));
627 char* oldLocale = (
char*)(
malloc(len+1));
628 if(oldLocale!=NULL) strncpy(oldLocale,setlocale(LC_NUMERIC,NULL),len);
629 setlocale(LC_NUMERIC,
"C");
632 res = printVectoredEPS();
634 res = printNonVectoredEPS();
639 setlocale(LC_NUMERIC,oldLocale);
644 G4cerr <<
"Error while saving file... "<<getRealPrintFilename().c_str()<<
G4endl;
646 G4cout <<
"File "<<getRealPrintFilename().c_str()<<
" has been saved " <<
G4endl;
650 if ( fPrintFilenameIndex != -1) {
651 fPrintFilenameIndex++;
654 #ifdef G4DEBUG_VIS_OGL
655 printf(
"G4OpenGLViewer::printEPS END\n");
659 bool G4OpenGLViewer::printVectoredEPS() {
663 bool G4OpenGLViewer::printNonVectoredEPS () {
665 int width = getRealPrintSizeX();
666 int height = getRealPrintSizeY();
668 #ifdef G4DEBUG_VIS_OGL
669 printf(
"G4OpenGLViewer::printNonVectoredEPS file:%s Vec:%d X:%d Y:%d col:%d fWinX:%d fWinY:%d\n",getRealPrintFilename().c_str(),fVectoredPs,width,height,fPrintColour,fWinSize_x,fWinSize_y);
674 int components,
pos, i;
676 pixels = grabPixels (fPrintColour, width, height);
678 if (pixels == NULL) {
679 G4cerr <<
"Failed to get pixels from OpenGl viewport" <<
G4endl;
687 std::string name = getRealPrintFilename();
688 fp = fopen (name.c_str(),
"w");
690 G4cerr <<
"Can't open filename " << name.c_str() <<
G4endl;
694 fprintf (fp,
"%%!PS-Adobe-2.0 EPSF-1.2\n");
695 fprintf (fp,
"%%%%Title: %s\n", name.c_str());
696 fprintf (fp,
"%%%%Creator: OpenGL pixmap render output\n");
697 fprintf (fp,
"%%%%BoundingBox: 0 0 %d %d\n", width, height);
698 fprintf (fp,
"%%%%EndComments\n");
699 fprintf (fp,
"gsave\n");
700 fprintf (fp,
"/bwproc {\n");
701 fprintf (fp,
" rgbproc\n");
702 fprintf (fp,
" dup length 3 idiv string 0 3 0 \n");
703 fprintf (fp,
" 5 -1 roll {\n");
704 fprintf (fp,
" add 2 1 roll 1 sub dup 0 eq\n");
705 fprintf (fp,
" { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
706 fprintf (fp,
" 3 1 roll 5 -1 roll } put 1 add 3 0 \n");
707 fprintf (fp,
" { 2 1 roll } ifelse\n");
708 fprintf (fp,
" }forall\n");
709 fprintf (fp,
" pop pop pop\n");
710 fprintf (fp,
"} def\n");
711 fprintf (fp,
"systemdict /colorimage known not {\n");
712 fprintf (fp,
" /colorimage {\n");
713 fprintf (fp,
" pop\n");
714 fprintf (fp,
" pop\n");
715 fprintf (fp,
" /rgbproc exch def\n");
716 fprintf (fp,
" { bwproc } image\n");
717 fprintf (fp,
" } def\n");
718 fprintf (fp,
"} if\n");
719 fprintf (fp,
"/picstr %d string def\n", width * components);
720 fprintf (fp,
"%d %d scale\n", width, height);
721 fprintf (fp,
"%d %d %d\n", width, height, 8);
722 fprintf (fp,
"[%d 0 0 %d 0 0]\n", width, height);
723 fprintf (fp,
"{currentfile picstr readhexstring pop}\n");
724 fprintf (fp,
"false %d\n", components);
725 fprintf (fp,
"colorimage\n");
727 curpix = (GLubyte*) pixels;
729 for (i = width*height*components; i>0; i--) {
730 fprintf (fp,
"%02hx ", (
unsigned short)(*(curpix++)));
739 fprintf (fp,
"grestore\n");
740 fprintf (fp,
"showpage\n");
753 bool G4OpenGLViewer::isGl2psWriting() {
755 if (!fGL2PSAction)
return false;
756 if (fGL2PSAction->fileWritingEnabled()) {
765 void G4OpenGLViewer::DrawText(
const G4Text& g4text)
768 if (isGl2psWriting()) {
771 G4double size = fSceneHandler.GetMarkerSize(g4text,sizeType);
775 const char* textCString = textString.c_str();
777 glRasterPos3d(position.
x(),position.
y(),position.
z());
786 gl2psTextOpt(textCString,
"Times-Roman",GLshort(size),align,0);
790 static G4int callCount = 0;
793 if (callCount <= 1) {
795 "G4OpenGLViewer::DrawText: Not implemented for \""
806 void G4OpenGLViewer::ChangePointSize(
G4double size) {
808 if (isGl2psWriting()) {
809 fGL2PSAction->setPointSize(
int(size));
818 void G4OpenGLViewer::ChangeLineWidth(
G4double width) {
820 if (isGl2psWriting()) {
821 fGL2PSAction->setLineWidth(
int(width));
828 bool G4OpenGLViewer::printGl2PS() {
830 int width = getRealPrintSizeX();
831 int height = getRealPrintSizeY();
833 if (!fGL2PSAction)
return false;
835 fGL2PSAction->setFileName(getRealPrintFilename().c_str());
852 bool extendBuffer =
true;
853 bool endWriteAction =
false;
854 bool beginWriteAction =
true;
855 while ((extendBuffer) && (! endWriteAction)) {
857 beginWriteAction = fGL2PSAction->enableFileWriting();
858 if (beginWriteAction) {
863 fGL2PSAction->setLineWidth(fGl2psDefaultLineWith);
865 fGL2PSAction->setPointSize(fGl2psDefaultPointSize);
868 endWriteAction = fGL2PSAction->disableFileWriting();
870 if ((! endWriteAction) || (! beginWriteAction)) {
871 extendBuffer = fGL2PSAction->extendBufferSize();
874 fGL2PSAction->resetBufferSizeParameters();
876 if (!extendBuffer ) {
877 G4cerr <<
"gl2ps buffer size is not big enough to print this geometry. Thy to extend it. No output produced"<<
G4endl;
879 if (!beginWriteAction ) {
880 G4cerr <<
"Error while writing in the file "<<getRealPrintFilename().c_str()<<
". Check read/write access No output produced" <<
G4endl;
882 if (!endWriteAction ) {
895 unsigned int G4OpenGLViewer::getWinWidth()
const{
899 unsigned int G4OpenGLViewer::getWinHeight()
const{
903 G4bool G4OpenGLViewer::sizeHasChanged() {
904 return fSizeHasChanged;
907 G4int G4OpenGLViewer::getRealPrintSizeX() {
908 if (fPrintSizeX == -1) {
912 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
915 if ((dims[0] !=0 ) && (dims[1] !=0)) {
916 if (fPrintSizeX > dims[0]){
920 if (fPrintSizeX < -1){
926 G4int G4OpenGLViewer::getRealPrintSizeY() {
927 if (fPrintSizeY == -1) {
931 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
934 if ((dims[0] !=0 ) && (dims[1] !=0)) {
935 if (fPrintSizeY > dims[1]){
939 if (fPrintSizeY < -1){
945 void G4OpenGLViewer::setPrintSize(
G4int X,
G4int Y) {
952 fPrintFilename =
name;
954 fPrintFilename =
"G4OpenGL";
957 fPrintFilenameIndex=0;
959 fPrintFilenameIndex=-1;
963 std::string G4OpenGLViewer::getRealPrintFilename() {
964 std::string temp = fPrintFilename;
965 if (fPrintFilenameIndex != -1) {
966 temp += std::string(
"_");
967 std::ostringstream os;
968 os << fPrintFilenameIndex;
969 std::string nb_str = os.str();
976 GLdouble G4OpenGLViewer::getSceneNearWidth()
978 if (!fSceneHandler.GetScene()) {
982 = fSceneHandler.GetScene()->GetStandardTargetPoint()
983 + fVP.GetCurrentTargetPoint ();
984 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
985 if(radius<=0.) radius = 1.;
986 const G4double cameraDistance = fVP.GetCameraDistance (radius);
987 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
988 return 2 * fVP.GetFrontHalfHeight (pnear, radius);
991 GLdouble G4OpenGLViewer::getSceneFarWidth()
993 if (!fSceneHandler.GetScene()) {
997 = fSceneHandler.GetScene()->GetStandardTargetPoint()
998 + fVP.GetCurrentTargetPoint ();
999 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
1000 if(radius<=0.) radius = 1.;
1001 const G4double cameraDistance = fVP.GetCameraDistance (radius);
1002 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
1003 const GLdouble pfar = fVP.GetFarDistance (cameraDistance, pnear, radius);
1004 return 2 * fVP.GetFrontHalfHeight (pfar, radius);
1008 GLdouble G4OpenGLViewer::getSceneDepth()
1010 if (!fSceneHandler.GetScene()) {
1014 = fSceneHandler.GetScene()->GetStandardTargetPoint()
1015 + fVP.GetCurrentTargetPoint ();
1016 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
1017 if(radius<=0.) radius = 1.;
1018 const G4double cameraDistance = fVP.GetCameraDistance (radius);
1019 const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius);
1020 return fVP.GetFarDistance (cameraDistance, pnear, radius)- pnear;
1028 rotateSceneInViewDirection(dx,dy);
1031 rotateSceneThetaPhi(dx,0);
1034 rotateSceneThetaPhi(0,dy);
1043 rotateSceneInViewDirection(dx,dy);
1046 rotateSceneThetaPhi(dx,0);
1049 rotateSceneThetaPhi(0,dy);
1056 if (!fSceneHandler.GetScene()) {
1084 vp = fVP.GetViewpointDirection ().
unit ();
1085 up = fVP.GetUpVector ().
unit ();
1087 yprime = (up.
cross(vp)).unit();
1088 zprime = (vp.
cross(yprime)).unit();
1090 if (fVP.GetLightsMoveWithCamera()) {
1091 delta_alpha = dy * fRot_sens;
1092 delta_theta = -dx * fRot_sens;
1094 delta_alpha = -dy * fRot_sens;
1095 delta_theta = dx * fRot_sens;
1101 new_vp = std::cos(delta_alpha) * vp + std::sin(delta_alpha) * zprime;
1106 if (fVP.GetLightsMoveWithCamera()) {
1107 new_up = (new_vp.
cross(yprime)).unit();
1108 if (new_vp.
z()*vp.
z() <0) {
1109 new_up.
set(new_up.
x(),-new_up.
y(),new_up.
z());
1113 if (new_vp.
z()*vp.
z() <0) {
1114 new_up.
set(new_up.
x(),-new_up.
y(),new_up.
z());
1117 fVP.SetUpVector(new_up);
1121 cosalpha = new_up.
dot (new_vp.
unit());
1122 sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
1124 xprime = yprime.
cross (new_up);
1126 a1 = sinalpha * xprime;
1128 a2 = sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
1132 viewPoint = new_vp.
unit() + delta;
1134 fVP.SetViewAndLights (viewPoint);
1140 if (!fSceneHandler.GetScene()) {
1164 #ifdef G4DEBUG_VIS_OGL
1165 printf(
"G4OpenGLViewer::rotateScene dx:%f dy:%f delta:%f\n",dx,dy, fRot_sens);
1168 vp = fVP.GetViewpointDirection ().
unit();
1169 up = fVP.GetUpVector ().
unit();
1172 up.
z()*vp.
x()-up.
x()*vp.
z(),
1173 up.
x()*vp.
y()-up.
y()*vp.
x());
1175 viewPoint = vp/fRot_sens + (zPrimeVector*dx - up*dy) ;
1176 new_up =
G4Vector3D(viewPoint.
y()*zPrimeVector.
z()-viewPoint.
z()*zPrimeVector.
y(),
1177 viewPoint.
z()*zPrimeVector.
x()-viewPoint.
x()*zPrimeVector.
z(),
1178 viewPoint.
x()*zPrimeVector.
y()-viewPoint.
y()*zPrimeVector.
x());
1184 fVP.SetUpVector(new_upUnit);
1185 fVP.SetViewAndLights (viewPoint);
1188 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
1191 void G4OpenGLViewer::setWtDrawer(G4OpenGLWtDrawer* drawer) {
1194 G4OpenGLSceneHandler& sh =
dynamic_cast<G4OpenGLSceneHandler&
>(fSceneHandler);
1195 sh.setWtDrawer(fWtDrawer);
1196 }
catch(std::bad_cast exp) { }
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
printf("%d Experimental points found\n", nlines)
T dot(const BasicVector3D< T > &v) const
void * malloc(size_t __size)
BasicVector3D< T > cross(const BasicVector3D< T > &v) const
const std::vector< const std::map< G4String, G4AttDef > * > & GetAttDefs() const
G4GLOB_DLL std::ostream G4cerr