33 #ifdef G4VIS_BUILD_OPENGL_DRIVER    53 #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),
    79 fDisplayHeadTime(false),
    80 fDisplayHeadTimeX(-0.9),
    81 fDisplayHeadTimeY(-0.9),
    82 fDisplayHeadTimeSize(24.),
    83 fDisplayHeadTimeRed(0.),
    84 fDisplayHeadTimeGreen(1.),
    85 fDisplayHeadTimeBlue(1.),
    86 fDisplayLightFront(false),
    87 fDisplayLightFrontX(0.),
    88 fDisplayLightFrontY(0.),
    89 fDisplayLightFrontZ(0.),
    90 fDisplayLightFrontT(0.),
    91 fDisplayLightFrontRed(0.),
    92 fDisplayLightFrontGreen(1.),
    93 fDisplayLightFrontBlue(0.),
    98 fDefaultExportImageFormat(
"pdf"),
    99 fExportImageFormat(
"pdf"),
   100 fExportFilenameIndex(0),
   104 fDefaultExportFilename(
"G4OpenGL"),
   106 fGl2psDefaultLineWith(1),
   107 fGl2psDefaultPointSize(2),
   108 fGlViewInitialized(false),
   109 fIsGettingPickInfos(false)
   110 #ifdef G4OPENGL_VERSION_2
   112 ,fVertexPositionAttribute(0)
   113 ,fVertexNormalAttribute(0)
   121   fVP.SetAutoRefresh(
true);
   122   fDefaultVP.SetAutoRefresh(
true);
   127   addExportImageFormat(
"eps");
   128   addExportImageFormat(
"ps");
   129   addExportImageFormat(
"pdf");
   130   addExportImageFormat(
"svg");
   133   fExportFilename += fDefaultExportFilename + 
"_" + GetShortName().data();
   143 G4OpenGLViewer::~G4OpenGLViewer ()
   148 void G4OpenGLViewer::InitializeGLView () 
   150 #ifdef G4OPENGL_VERSION_2   154     fShaderProgram = glCreateProgram();
   155     Shader vertexShader = glCreateShader(GL_VERTEX_SHADER);
   156     const char * vSrc = fVboDrawer->getVertexShaderSrc();
   157     glShaderSource(vertexShader, 1, &vSrc, NULL);
   158     glCompileShader(vertexShader);
   159     glAttachShader(fShaderProgram, vertexShader);
   161     Shader fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
   162     const char * fSrc = fVboDrawer->getFragmentShaderSrc();
   163     glShaderSource(fragmentShader, 1, &fSrc, NULL);
   164     glCompileShader(fragmentShader);
   166     glAttachShader(fShaderProgram, fragmentShader);
   167     glLinkProgram(fShaderProgram);
   168     glUseProgram(fShaderProgram);
   175     fVertexPositionAttribute =
   176     glGetAttribLocation(fShaderProgram, 
"aVertexPosition");
   179     glEnableVertexAttribArray(fVertexPositionAttribute);
   182     fpMatrixUniform  = glGetUniformLocation(fShaderProgram, 
"uPMatrix");
   183     fcMatrixUniform  = glGetUniformLocation(fShaderProgram, 
"uCMatrix");
   184     fmvMatrixUniform = glGetUniformLocation(fShaderProgram, 
"uMVMatrix");
   185     fnMatrixUniform  = glGetUniformLocation(fShaderProgram, 
"uNMatrix");
   186     ftMatrixUniform  = glGetUniformLocation(fShaderProgram, 
"uTMatrix");
   194     fGlViewInitialized = 
true;
   199   fWinSize_x = fVP.GetWindowSizeHintX();
   200   fWinSize_y = fVP.GetWindowSizeHintY();
   202   glClearColor (0.0, 0.0, 0.0, 0.0);
   204 #ifndef G4OPENGL_VERSION_2   205   glDisable (GL_LINE_SMOOTH);
   206   glDisable (GL_POLYGON_SMOOTH);
   213   glDepthFunc (GL_LEQUAL);
   214   glDepthMask (GL_TRUE);
   217   glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   221 void G4OpenGLViewer::ClearView () {
   222   ClearViewWithoutFlush();
   227 void G4OpenGLViewer::ClearViewWithoutFlush () {
   230 #if GL_EXT_framebuffer_object   236   glClearColor (background.GetRed(),
   237                 background.GetGreen(),
   238                 background.GetBlue(),
   243   glClear (GL_COLOR_BUFFER_BIT);
   244   glClear (GL_DEPTH_BUFFER_BIT);
   245   glClear (GL_STENCIL_BUFFER_BIT);
   249 void G4OpenGLViewer::ResizeWindow(
unsigned int aWidth, 
unsigned int aHeight) {
   250   if ((fWinSize_x != aWidth) || (fWinSize_y != aHeight)) {
   252     fWinSize_y = aHeight;
   253     fSizeHasChanged = 
true;
   255     fSizeHasChanged = 
false;
   265 void G4OpenGLViewer::ResizeGLView()
   272   glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
   274   if ((dims[0] !=0 ) && (dims[1] !=0)) {
   276     if (fWinSize_x > (
unsigned)dims[0]) {
   277       G4cerr << 
"Try to resize view greater than max X viewport dimension. Desired size "<<fWinSize_x <<
" is resize to "<<  dims[0] << 
G4endl;
   278       fWinSize_x = dims[0];
   280     if (fWinSize_y > (
unsigned)dims[1]) {
   281       G4cerr << 
"Try to resize view greater than max Y viewport dimension. Desired size "<<fWinSize_y <<
" is resize to "<<  dims[1] << 
G4endl;
   282       fWinSize_y = dims[1];
   286   glViewport(0, 0, fWinSize_x,fWinSize_y);   
   292 void G4OpenGLViewer::SetView () {
   294   if (fIsGettingPickInfos) 
return;
   296   if (!fSceneHandler.GetScene()) {
   304   GLfloat lightPosition [4];
   305   lightPosition [0] = fVP.GetActualLightpointDirection().x();
   306   lightPosition [1] = fVP.GetActualLightpointDirection().y();
   307   lightPosition [2] = fVP.GetActualLightpointDirection().z();
   308   lightPosition [3] = 0.;
   310   GLfloat ambient [] = { 0.2f, 0.2f, 0.2f, 1.f};
   311   GLfloat diffuse [] = { 0.8f, 0.8f, 0.8f, 1.f};
   312   glEnable (GL_LIGHT0);
   313   glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);
   314   glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
   318   if (fWinSize_y > fWinSize_x) {
   321   if (fWinSize_x > fWinSize_y) {
   328     = fSceneHandler.GetScene()->GetStandardTargetPoint()
   329     + fVP.GetCurrentTargetPoint ();
   330   G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
   331   if(radius<=0.) radius = 1.;
   332   const G4double cameraDistance = fVP.GetCameraDistance (radius);
   334     targetPoint + cameraDistance * fVP.GetViewpointDirection().
unit();
   335   const GLdouble pnear  = fVP.GetNearDistance (cameraDistance, radius);
   336   const GLdouble pfar   = fVP.GetFarDistance  (cameraDistance, pnear, radius);
   337   const GLdouble 
right  = fVP.GetFrontHalfHeight (pnear, radius) * ratioY;
   339   const GLdouble top    = fVP.GetFrontHalfHeight (pnear, radius) * ratioX;
   340   const GLdouble bottom = -top;
   346   glMatrixMode (GL_PROJECTION); 
   349   const G4Vector3D scaleFactor = fVP.GetScaleFactor();
   350   glScaled(scaleFactor.
x(),scaleFactor.
y(),scaleFactor.
z());
   352   if (fVP.GetFieldHalfAngle() == 0.) {
   353     g4GlOrtho (left, right, bottom, top, pnear, pfar);
   356     g4GlFrustum (left, right, bottom, top, pnear, pfar);
   359   glMatrixMode (GL_MODELVIEW); 
   362   const G4Normal3D& upVector = fVP.GetUpVector ();  
   364   if (cameraDistance > 1.
e-6 * radius) {
   365     gltarget = targetPoint;
   368     gltarget = targetPoint - radius * fVP.GetViewpointDirection().
unit();
   371   const G4Point3D& pCamera = cameraPosition;  
   373   g4GluLookAt (pCamera.
x(),  pCamera.
y(),  pCamera.
z(),       
   374              gltarget.
x(), gltarget.
y(), gltarget.
z(),      
   375              upVector.
x(), upVector.
y(), upVector.
z());     
   377   glLightfv (GL_LIGHT0, GL_POSITION, lightPosition);
   391     sArray[3] = sp.
d() + radius * 1.e-05;
   392     glClipPlane (GL_CLIP_PLANE0, sArray);
   393     glEnable (GL_CLIP_PLANE0);
   397     sArray[3] = -sp.
d() + radius * 1.e-05;
   398     glClipPlane (GL_CLIP_PLANE1, sArray);
   399     glEnable (GL_CLIP_PLANE1);
   401     glDisable (GL_CLIP_PLANE0);
   402     glDisable (GL_CLIP_PLANE1);
   409   const G4Planes& cutaways = fVP.GetCutawayPlanes();
   410   size_t nPlanes = cutaways.size();
   411   if (fVP.IsCutaway() &&
   415     a[0] = cutaways[0].a();
   416     a[1] = cutaways[0].b();
   417     a[2] = cutaways[0].c();
   418     a[3] = cutaways[0].d();
   419     glClipPlane (GL_CLIP_PLANE2, a);
   420     glEnable (GL_CLIP_PLANE2);
   422       a[0] = cutaways[1].a();
   423       a[1] = cutaways[1].b();
   424       a[2] = cutaways[1].c();
   425       a[3] = cutaways[1].d();
   426       glClipPlane (GL_CLIP_PLANE3, a);
   427       glEnable (GL_CLIP_PLANE3);
   430       a[0] = cutaways[2].a();
   431       a[1] = cutaways[2].b();
   432       a[2] = cutaways[2].c();
   433       a[3] = cutaways[2].d();
   434       glClipPlane (GL_CLIP_PLANE4, a);
   435       glEnable (GL_CLIP_PLANE4);
   438     glDisable (GL_CLIP_PLANE2);
   439     glDisable (GL_CLIP_PLANE3);
   440     glDisable (GL_CLIP_PLANE4);
   444   background = fVP.GetBackgroundColour ();
   450 void G4OpenGLViewer::ResetView () {
   457 void G4OpenGLViewer::HaloingFirstPass () {
   467   glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
   470   glDepthMask (GL_TRUE);
   471   glDepthFunc (GL_LESS);
   475   ChangeLineWidth(3.0);
   479 void G4OpenGLViewer::HaloingSecondPass () {
   482   glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
   483   glDepthFunc (GL_LEQUAL);
   484   ChangeLineWidth(1.0);
   488 G4String G4OpenGLViewer::Pick(GLdouble 
x, GLdouble 
y)
   490   std::vector < G4OpenGLViewerPickMap* >  pickMap = GetPickDetails(x,y);
   492   if (pickMap.size() == 0) {
   493     txt += 
"Too many hits.  Zoom in to reduce overlaps.";;
   495     for (
unsigned int a=0; a< pickMap.size(); a++) {
   496       txt += pickMap[
a]->print();
   502 std::vector < G4OpenGLViewerPickMap* > G4OpenGLViewer::GetPickDetails(GLdouble x, GLdouble y)
   504   std::vector < G4OpenGLViewerPickMap* > pickMapVector;
   506   std::ostringstream oss;
   509   glSelectBuffer(BUFSIZE, selectBuffer);
   510   glRenderMode(GL_SELECT);
   513   glMatrixMode(GL_PROJECTION);
   514   G4double currentProjectionMatrix[16];
   515   glGetDoublev(GL_PROJECTION_MATRIX, currentProjectionMatrix);
   519   glGetIntegerv(GL_VIEWPORT, viewport);
   520   fIsGettingPickInfos = 
true;
   522   g4GluPickMatrix(x, viewport[3] - y, 5., 5., viewport);
   523   glMultMatrixd(currentProjectionMatrix);
   524   glMatrixMode(GL_MODELVIEW);
   526   GLint 
hits = glRenderMode(GL_RENDER);
   527   fIsGettingPickInfos = 
false;
   530     GLuint* p = selectBuffer;
   531     for (GLint i = 0; i < 
hits; ++i) {
   532       G4OpenGLViewerPickMap* pickMap = 
new G4OpenGLViewerPickMap();
   533       GLuint nnames = *p++;
   542       for (GLuint j = 0; j < nnames; ++j) {
   545         pickMap->setHitNumber(i);
   546         pickMap->setSubHitNumber(j);
   547         pickMap->setPickName(name);
   548     std::map<GLuint, G4AttHolder*>::iterator iter =
   549       fOpenGLSceneHandler.fPickMap.find(name);
   550     if (iter != fOpenGLSceneHandler.fPickMap.end()) {
   552       if(attHolder && attHolder->
GetAttDefs().size()) {
   553         for (
size_t iAtt = 0;
   554          iAtt < attHolder->
GetAttDefs().size(); ++iAtt) {
   557               pickMap->addAttributes(oss.str());
   562       pickMapVector.push_back(pickMap);
   565   glMatrixMode(GL_PROJECTION);
   567   glMatrixMode(GL_MODELVIEW);
   568   return pickMapVector;
   571 GLubyte* G4OpenGLViewer::grabPixels
   572 (
int inColor, 
unsigned int width, 
unsigned int height) {
   575   GLint swapbytes, lsbfirst, rowlength;
   576   GLint skiprows, skippixels, alignment;
   582     size = width*height*3;
   584     format = GL_LUMINANCE;
   585     size = width*height*1;
   588   buffer = 
new GLubyte[size];
   592   glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapbytes);
   593   glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirst);
   594   glGetIntegerv (GL_UNPACK_ROW_LENGTH, &rowlength);
   596   glGetIntegerv (GL_UNPACK_SKIP_ROWS, &skiprows);
   597   glGetIntegerv (GL_UNPACK_SKIP_PIXELS, &skippixels);
   598   glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignment);
   600   glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
   601   glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
   602   glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
   604   glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
   605   glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
   606   glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
   608   glReadBuffer(GL_FRONT);
   609   glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, format, GL_UNSIGNED_BYTE, (GLvoid*) buffer);
   611   glPixelStorei (GL_UNPACK_SWAP_BYTES, swapbytes);
   612   glPixelStorei (GL_UNPACK_LSB_FIRST, lsbfirst);
   613   glPixelStorei (GL_UNPACK_ROW_LENGTH, rowlength);
   615   glPixelStorei (GL_UNPACK_SKIP_ROWS, skiprows);
   616   glPixelStorei (GL_UNPACK_SKIP_PIXELS, skippixels);
   617   glPixelStorei (GL_UNPACK_ALIGNMENT, alignment);
   622 bool G4OpenGLViewer::printEPS() {
   627   size_t len = strlen(setlocale(LC_NUMERIC,NULL));
   628   char* oldLocale = (
char*)(malloc(len+1));
   629   if(oldLocale!=NULL) strncpy(oldLocale,setlocale(LC_NUMERIC,NULL),len);
   630   setlocale(LC_NUMERIC,
"C");
   632   if (((fExportImageFormat == 
"eps") || (fExportImageFormat == 
"ps")) && (!fVectoredPs)) {
   633     res = printNonVectoredEPS();
   635     res = printVectoredEPS();
   640     setlocale(LC_NUMERIC,oldLocale);
   645     G4cerr << 
"Error saving file... " << getRealPrintFilename().c_str() << 
G4endl;
   647     G4cout << 
"File " << getRealPrintFilename().c_str() << 
" size: " << getRealExportWidth() << 
"x" << getRealExportHeight() << 
" has been saved " << 
G4endl;
   650     if ( fExportFilenameIndex != -1) {
   651       fExportFilenameIndex++;
   658 bool G4OpenGLViewer::printVectoredEPS() {
   662 bool G4OpenGLViewer::printNonVectoredEPS () {
   664   int width = getRealExportWidth();
   665   int height = getRealExportHeight();
   670   int components, 
pos, i;
   672   pixels = grabPixels (fPrintColour, width, height);
   674   if (pixels == NULL) {
   675       G4cerr << 
"Failed to get pixels from OpenGl viewport" << 
G4endl;
   683   std::string name = getRealPrintFilename();
   684   fp = fopen (name.c_str(), 
"w");
   686     G4cerr << 
"Can't open filename " << name.c_str() << 
G4endl;
   690   fprintf (fp, 
"%%!PS-Adobe-2.0 EPSF-1.2\n");
   691   fprintf (fp, 
"%%%%Title: %s\n", name.c_str());
   692   fprintf (fp, 
"%%%%Creator: OpenGL pixmap render output\n");
   693   fprintf (fp, 
"%%%%BoundingBox: 0 0 %d %d\n", width, height);
   694   fprintf (fp, 
"%%%%EndComments\n");
   695   fprintf (fp, 
"gsave\n");
   696   fprintf (fp, 
"/bwproc {\n");
   697   fprintf (fp, 
"    rgbproc\n");
   698   fprintf (fp, 
"    dup length 3 idiv string 0 3 0 \n");
   699   fprintf (fp, 
"    5 -1 roll {\n");
   700   fprintf (fp, 
"    add 2 1 roll 1 sub dup 0 eq\n");
   701   fprintf (fp, 
"    { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
   702   fprintf (fp, 
"       3 1 roll 5 -1 roll } put 1 add 3 0 \n");
   703   fprintf (fp, 
"    { 2 1 roll } ifelse\n");
   704   fprintf (fp, 
"    }forall\n");
   705   fprintf (fp, 
"    pop pop pop\n");
   706   fprintf (fp, 
"} def\n");
   707   fprintf (fp, 
"systemdict /colorimage known not {\n");
   708   fprintf (fp, 
"   /colorimage {\n");
   709   fprintf (fp, 
"       pop\n");
   710   fprintf (fp, 
"       pop\n");
   711   fprintf (fp, 
"       /rgbproc exch def\n");
   712   fprintf (fp, 
"       { bwproc } image\n");
   713   fprintf (fp, 
"   }  def\n");
   714   fprintf (fp, 
"} if\n");
   715   fprintf (fp, 
"/picstr %d string def\n", width * components);
   716   fprintf (fp, 
"%d %d scale\n", width, height);
   717   fprintf (fp, 
"%d %d %d\n", width, height, 8);
   718   fprintf (fp, 
"[%d 0 0 %d 0 0]\n", width, height);
   719   fprintf (fp, 
"{currentfile picstr readhexstring pop}\n");
   720   fprintf (fp, 
"false %d\n", components);
   721   fprintf (fp, 
"colorimage\n");
   723   curpix = (GLubyte*) pixels;
   725   for (i = width*height*components; i>0; i--) {
   726     fprintf (fp, 
"%02hx ", (
unsigned short)(*(curpix++)));
   735   fprintf (fp, 
"grestore\n");
   736   fprintf (fp, 
"showpage\n");
   749 bool G4OpenGLViewer::isGl2psWriting() {
   751   if (!fGL2PSAction) 
return false;
   752   if (fGL2PSAction->fileWritingEnabled()) {
   761 void G4OpenGLViewer::DrawText(
const G4Text& g4text)
   764   if (isGl2psWriting()) {
   767     G4double size = fSceneHandler.GetMarkerSize(g4text,sizeType);
   772     glRasterPos3d(position.
x(),position.
y(),position.
z());
   781     gl2psTextOpt(textString.c_str(),
"Times-Roman",GLshort(size),align,0);
   785     static G4int callCount = 0;
   788     if (callCount <= 1) {
   790     "G4OpenGLViewer::DrawText: Not implemented for \""   801 void G4OpenGLViewer::ChangePointSize(
G4double size) {
   803   if (isGl2psWriting()) {
   804     fGL2PSAction->setPointSize(
int(size));
   813 void G4OpenGLViewer::ChangeLineWidth(
G4double width) {
   815   if (isGl2psWriting()) {
   816     fGL2PSAction->setLineWidth(
int(width));
   830 bool G4OpenGLViewer::exportImage(std::string name, 
int width, 
int height) {
   832   if (! setExportFilename(name)) {
   836   if ((width != -1) && (height != -1)) {
   837     setExportSize(width, height);
   840   if (fExportImageFormat == 
"eps") {
   841     fGL2PSAction->setExportImageFormat(
GL2PS_EPS);
   842   } 
else if (fExportImageFormat == 
"ps") {
   843     fGL2PSAction->setExportImageFormat(
GL2PS_PS);
   844   } 
else if (fExportImageFormat == 
"svg") {
   845     fGL2PSAction->setExportImageFormat(
GL2PS_SVG);
   846   } 
else if (fExportImageFormat == 
"pdf") {
   847     fGL2PSAction->setExportImageFormat(
GL2PS_PDF);
   849     setExportImageFormat(fExportImageFormat,
true); 
   856 bool G4OpenGLViewer::printGl2PS() {
   858   int width = getRealExportWidth();
   859   int height = getRealExportHeight();
   863   G4OpenGLSceneHandler& oglSceneHandler = 
dynamic_cast<G4OpenGLSceneHandler&
>(fSceneHandler);
   864   G4int drawInterval = oglSceneHandler.GetEventsDrawInterval();
   865   oglSceneHandler.SetEventsDrawInterval(1000); 
   867   if (!fGL2PSAction) 
return false;
   869   fGL2PSAction->setFileName(getRealPrintFilename().c_str());
   886    bool extendBuffer = 
true;
   887    bool endWriteAction = 
false;
   888    bool beginWriteAction = 
true;
   889    bool filePointerOk = 
true;
   890    while ((extendBuffer) && (! endWriteAction) && (filePointerOk)) {
   892      beginWriteAction = fGL2PSAction->enableFileWriting();
   898      filePointerOk = fGL2PSAction->fileWritingEnabled();
   900      if (beginWriteAction) {
   904        fGL2PSAction->setLineWidth(fGl2psDefaultLineWith);
   906        fGL2PSAction->setPointSize(fGl2psDefaultPointSize);
   909        endWriteAction = fGL2PSAction->disableFileWriting();
   912        if ((! endWriteAction) || (! beginWriteAction)) {
   913          extendBuffer = fGL2PSAction->extendBufferSize();
   917    fGL2PSAction->resetBufferSizeParameters();
   919    if (!extendBuffer ) {
   920      G4cerr << 
"ERROR: gl2ps buffer size is not big enough to print this geometry. Try to extend it. No output produced"<< 
G4endl;
   923    if (!beginWriteAction ) {
   924      G4cerr << 
"ERROR: saving file "<<getRealPrintFilename().c_str()<<
". Check read/write access. No output produced" << 
G4endl;
   927    if (!endWriteAction ) {
   934   oglSceneHandler.SetEventsDrawInterval(drawInterval);
   943 unsigned int G4OpenGLViewer::getWinWidth()
 const{
   947 unsigned int G4OpenGLViewer::getWinHeight()
 const{
   951 G4bool G4OpenGLViewer::sizeHasChanged() {
   952   return fSizeHasChanged;
   955 G4int G4OpenGLViewer::getRealExportWidth() {
   956   if (fPrintSizeX == -1) {
   960   glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
   963   if ((dims[0] !=0 ) && (dims[1] !=0)) {
   964     if (fPrintSizeX > dims[0]){
   968   if (fPrintSizeX < -1){
   974 G4int G4OpenGLViewer::getRealExportHeight() {
   975   if (fPrintSizeY == -1) {
   979   glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
   982   if ((dims[0] !=0 ) && (dims[1] !=0)) {
   983     if (fPrintSizeY > dims[1]){
   987   if (fPrintSizeY < -1){
   993 void G4OpenGLViewer::setExportSize(
G4int X, 
G4int Y) {
  1004 bool G4OpenGLViewer::setExportFilename(
G4String name,
G4bool inc) {
  1010     if ((name != 
"") && (fExportFilename != name)) {
  1011       fExportFilenameIndex=0;
  1014     fExportFilenameIndex=-1;
  1017   if (name.size() == 0) {
  1018     name = getRealPrintFilename().c_str();
  1021     std::string extension = name.substr(name.find_last_of(
".") + 1);
  1023     if (name.size() != extension.size()) {
  1024       if (! setExportImageFormat(extension, 
false)) {
  1029     fExportFilename = name.substr(0,name.find_last_of(
"."));
  1034 std::string G4OpenGLViewer::getRealPrintFilename() {
  1035   std::string temp = fExportFilename;
  1036   if (fExportFilenameIndex != -1) {
  1037     temp += std::string(
"_");
  1038     std::ostringstream os;
  1039     os << fExportFilenameIndex;
  1040     std::string nb_str = os.str();
  1043   temp += 
"."+fExportImageFormat;
  1047 GLdouble G4OpenGLViewer::getSceneNearWidth()
  1049   if (!fSceneHandler.GetScene()) {
  1053     = fSceneHandler.GetScene()->GetStandardTargetPoint()
  1054     + fVP.GetCurrentTargetPoint ();
  1055   G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
  1056   if(radius<=0.) radius = 1.;
  1057   const G4double cameraDistance = fVP.GetCameraDistance (radius);
  1058   const GLdouble pnear   = fVP.GetNearDistance (cameraDistance, radius);
  1059   return 2 * fVP.GetFrontHalfHeight (pnear, radius);
  1062 GLdouble G4OpenGLViewer::getSceneFarWidth()
  1064   if (!fSceneHandler.GetScene()) {
  1068     = fSceneHandler.GetScene()->GetStandardTargetPoint()
  1069     + fVP.GetCurrentTargetPoint ();
  1070   G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
  1071   if(radius<=0.) radius = 1.;
  1072   const G4double cameraDistance = fVP.GetCameraDistance (radius);
  1073   const GLdouble pnear   = fVP.GetNearDistance (cameraDistance, radius);
  1074   const GLdouble pfar    = fVP.GetFarDistance  (cameraDistance, pnear, radius);
  1075   return 2 * fVP.GetFrontHalfHeight (pfar, radius);
  1079 GLdouble G4OpenGLViewer::getSceneDepth()
  1081   if (!fSceneHandler.GetScene()) {
  1085     = fSceneHandler.GetScene()->GetStandardTargetPoint()
  1086     + fVP.GetCurrentTargetPoint ();
  1087   G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
  1088   if(radius<=0.) radius = 1.;
  1089   const G4double cameraDistance = fVP.GetCameraDistance (radius);
  1090   const GLdouble pnear   = fVP.GetNearDistance (cameraDistance, radius);
  1091   return fVP.GetFarDistance  (cameraDistance, pnear, radius)- pnear;
  1099     rotateSceneInViewDirection(dx,dy);
  1102       rotateSceneThetaPhi(dx,0);
  1105       rotateSceneThetaPhi(0,dy);
  1114     rotateSceneInViewDirection(dx,dy);
  1117       rotateSceneThetaPhi(dx,0);
  1120       rotateSceneThetaPhi(0,dy);
  1127   if (!fSceneHandler.GetScene()) {
  1155   vp = fVP.GetViewpointDirection ().
unit ();
  1156   up = fVP.GetUpVector ().
unit ();
  1158   yprime = (up.
cross(vp)).unit();
  1159   zprime = (vp.
cross(yprime)).unit();
  1161   if (fVP.GetLightsMoveWithCamera()) {
  1162     delta_alpha = dy * fRot_sens;
  1163     delta_theta = -dx * fRot_sens;
  1165     delta_alpha = -dy * fRot_sens;
  1166     delta_theta = dx * fRot_sens;
  1172   new_vp = std::cos(delta_alpha) * vp + std::sin(delta_alpha) * zprime;
  1177   if (fVP.GetLightsMoveWithCamera()) {
  1178     new_up = (new_vp.
cross(yprime)).unit();
  1179     if (new_vp.
z()*vp.
z() <0) {
  1180       new_up.
set(new_up.
x(),-new_up.
y(),new_up.
z());
  1184     if (new_vp.
z()*vp.
z() <0) {
  1185       new_up.
set(new_up.
x(),-new_up.
y(),new_up.
z());
  1188   fVP.SetUpVector(new_up);
  1192   cosalpha = new_up.
dot (new_vp.
unit());
  1193   sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
  1195   xprime = yprime.
cross (new_up);
  1197   a1 = sinalpha * xprime;
  1199   a2 = sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
  1203   viewPoint = new_vp.
unit() + delta;
  1205   fVP.SetViewAndLights (viewPoint);
  1211   if (!fSceneHandler.GetScene()) {
  1235   vp = fVP.GetViewpointDirection ().
unit();
  1236   up = fVP.GetUpVector ().
unit();
  1239                              up.
z()*vp.
x()-up.
x()*vp.
z(),
  1240                              up.
x()*vp.
y()-up.
y()*vp.
x());
  1242   viewPoint = vp/fRot_sens + (zPrimeVector*dx - up*dy) ;
  1243   new_up = 
G4Vector3D(viewPoint.
y()*zPrimeVector.
z()-viewPoint.
z()*zPrimeVector.
y(),
  1244                        viewPoint.
z()*zPrimeVector.
x()-viewPoint.
x()*zPrimeVector.
z(),
  1245                        viewPoint.
x()*zPrimeVector.
y()-viewPoint.
y()*zPrimeVector.
x());
  1251    fVP.SetUpVector(new_upUnit);
  1252    fVP.SetViewAndLights (viewPoint);
  1256 void G4OpenGLViewer::addExportImageFormat(std::string format) {
  1257   fExportImageFormatVector.push_back(format);
  1260 bool G4OpenGLViewer::setExportImageFormat(std::string format, 
bool quiet) {
  1263   for (
unsigned int a=0; a<fExportImageFormatVector.size(); a++) {
  1264     list +=fExportImageFormatVector.at(a) + 
" ";
  1266     if (fExportImageFormatVector.at(a) == format) {
  1268         G4cout << 
" Changing export format to \"" << format << 
"\"" << 
G4endl;
  1270       if (format != fExportImageFormat) {
  1271         fExportFilenameIndex = 0;
  1272         fExportImageFormat = format;
  1278     if (format.size() == 0) {
  1279       G4cout << 
" Current formats availables are : " << list << 
G4endl;
  1281       G4cerr << 
" Format \"" << format << 
"\" is not available for the selected viewer. Current formats availables are : " << list << 
G4endl;
  1291 void G4OpenGLViewer::g4GluPickMatrix(GLdouble x, GLdouble y, GLdouble width, GLdouble height,
  1298     sx = viewport[2] / 
width;
  1299     sy = viewport[3] / height;
  1300     tx = (viewport[2] + 2.0 * (viewport[0] - 
x)) / 
width;
  1301     ty = (viewport[3] + 2.0 * (viewport[1] - 
y)) / height;
  1303 #define M(row, col) mat[col*4+row]  1333 void G4OpenGLViewer::g4GluLookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
  1334                         GLdouble centerx, GLdouble centery, GLdouble
  1336                         GLdouble upx, GLdouble upy, GLdouble upz )
  1339     GLfloat x[3], y[3], 
z[3];
  1345     z[0] = eyex - centerx;
  1346     z[1] = eyey - centery;
  1347     z[2] = eyez - centerz;
  1348     mag = std::sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
  1361     x[0] = y[1] * z[2] - y[2] * z[1];
  1362     x[1] = -y[0] * z[2] + y[2] * z[0];
  1363     x[2] = y[0] * z[1] - y[1] * z[0];
  1366     y[0] = z[1] * x[2] - z[2] * x[1];
  1367     y[1] = -z[0] * x[2] + z[2] * x[0];
  1368     y[2] = z[0] * x[1] - z[1] * x[0];
  1375     mag = std::sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
  1382     mag = std::sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
  1389 #define M(row,col)  mat[col*4+row]  1410     glTranslatef(-eyex, -eyey, -eyez);
  1413 void G4OpenGLViewer::g4GlOrtho (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
  1416   GLdouble a = 2.0 / (right - 
left);
  1417   GLdouble 
b = 2.0 / (top - bottom);
  1418   GLdouble 
c = -2.0 / (zFar - zNear);
  1420   GLdouble tx = - (right + 
left)/(right - left);
  1421   GLdouble ty = - (top + bottom)/(top - bottom);
  1422   GLdouble tz = - (zFar + zNear)/(zFar - zNear);
  1424   GLdouble ortho[16] = {
  1430   glMultMatrixd(ortho);
  1435 void G4OpenGLViewer::g4GlFrustum (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
  1438   GLdouble deltaX = right - 
left;
  1439   GLdouble deltaY = top - bottom;
  1440   GLdouble deltaZ = zFar - zNear;
  1442   GLdouble a = 2.0f * zNear / deltaX;
  1443   GLdouble b = 2.0f * zNear / deltaY;
  1444   GLdouble c = (right + 
left) / deltaX;
  1445   GLdouble 
d = (top + bottom) / deltaY;
  1446   GLdouble 
e = -(zFar + zNear) / (zFar - zNear);
  1447   GLdouble 
f = -2.0f * zFar * zNear / deltaZ;
  1449   GLdouble 
proj[16] = {
  1456   glMultMatrixd(proj);
  1461 #ifdef G4OPENGL_VERSION_2  1464 void G4OpenGLViewer::setVboDrawer(G4OpenGLVboDrawer* drawer) {
  1465   fVboDrawer = drawer;
  1467     G4OpenGLSceneHandler& sh = 
dynamic_cast<G4OpenGLSceneHandler&
>(fSceneHandler);
  1468     sh.setVboDrawer(fVboDrawer);
  1469   } 
catch(std::bad_cast exp) { }
  1476   std::ostringstream txt;
  1480   txt << 
"Hit: " << fHitNumber << 
", Sub-hit: " << fSubHitNumber << 
", PickName: " << fPickName << 
"\n";
  1482   for (
unsigned int a=0; a<fAttributes.size(); a++) {
  1483     txt << fAttributes[
a] << 
"\n";
 
T dot(const BasicVector3D< T > &v) const
 
const std::vector< const std::vector< G4AttValue > * > & GetAttValues() const
 
void set(T x1, T y1, T z1)
 
G4Point3D GetPosition() const
 
HepGeom::Vector3D< G4double > G4Vector3D
 
const std::vector< const std::map< G4String, G4AttDef > * > & GetAttDefs() const
 
BasicVector3D< T > cross(const BasicVector3D< T > &v) 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)
 
BasicVector3D< T > unit() const
 
static const G4double pos
 
G4GLOB_DLL std::ostream G4cerr