Index: trunk/source/visualization/OpenGL/History
===================================================================
--- trunk/source/visualization/OpenGL/History	(revision 944)
+++ trunk/source/visualization/OpenGL/History	(revision 945)
@@ -20,4 +20,7 @@
 History file for visualization/OpenGL
 -------------------------------------
+
+16th March 2009, Laurent Garnier
+ - G4OpenGLViewer : Add a check on max viewport dimensions when setting size.
 
 4rd March 2009, Laurent Garnier
Index: trunk/source/visualization/OpenGL/include/G4OpenGLXViewer.hh
===================================================================
--- trunk/source/visualization/OpenGL/include/G4OpenGLXViewer.hh	(revision 944)
+++ trunk/source/visualization/OpenGL/include/G4OpenGLXViewer.hh	(revision 945)
@@ -80,5 +80,5 @@
   Colormap                          cmap;
   XSetWindowAttributes              swa;
-  GLXDrawable                       win;
+  GLXDrawable                       fGLXWin;
   GLXContext                        cx;
   XEvent                            event;
@@ -98,4 +98,8 @@
   char                              charViewName [100];
 
+  GLubyte* grabPixelsX (int inColor, unsigned int width, unsigned int height);
+  int generateEPSX (const char* filnam,int inColour, unsigned int width,unsigned int height);
+
+
 private:
   G4OpenGLXViewer (const G4OpenGLXViewer&);
Index: trunk/source/visualization/OpenGL/src/G4OpenGLImmediateXViewer.cc
===================================================================
--- trunk/source/visualization/OpenGL/src/G4OpenGLImmediateXViewer.cc	(revision 944)
+++ trunk/source/visualization/OpenGL/src/G4OpenGLImmediateXViewer.cc	(revision 945)
@@ -86,5 +86,5 @@
 
   //Make sure current viewer is attached and clean...
-  glXMakeCurrent (dpy, win, cx);
+  glXMakeCurrent (dpy, fGLXWin, cx);
 
   if(style!=G4ViewParameters::hlr &&
Index: trunk/source/visualization/OpenGL/src/G4OpenGLImmediateXmViewer.cc
===================================================================
--- trunk/source/visualization/OpenGL/src/G4OpenGLImmediateXmViewer.cc	(revision 944)
+++ trunk/source/visualization/OpenGL/src/G4OpenGLImmediateXmViewer.cc	(revision 945)
@@ -92,5 +92,5 @@
 
   //Make sure current viewer is attached and clean...
-  glXMakeCurrent (dpy, win, cx);
+  glXMakeCurrent (dpy, fGLXWin, cx);
 
   if(style!=G4ViewParameters::hlr &&
Index: trunk/source/visualization/OpenGL/src/G4OpenGLQtViewer.cc
===================================================================
--- trunk/source/visualization/OpenGL/src/G4OpenGLQtViewer.cc	(revision 944)
+++ trunk/source/visualization/OpenGL/src/G4OpenGLQtViewer.cc	(revision 945)
@@ -32,5 +32,4 @@
 //                     functionality for OpenGL in GEANT4
 //
-// 27/06/2003 : G.Barrand : implementation (at last !).
 
 #ifdef G4VIS_BUILD_OPENGLQT_DRIVER
Index: trunk/source/visualization/OpenGL/src/G4OpenGLStoredSceneHandler.cc
===================================================================
--- trunk/source/visualization/OpenGL/src/G4OpenGLStoredSceneHandler.cc	(revision 944)
+++ trunk/source/visualization/OpenGL/src/G4OpenGLStoredSceneHandler.cc	(revision 945)
@@ -222,14 +222,18 @@
   if (fLastPolymarkers.size() > 0 ) {
 
-    // Loop new poly to check
+    // Loop new point to check
     for (unsigned int b=0; b< polymarker.size();b++) {
-      printf("check %d/%d\n",b,polymarker.size());
+      //      printf("check %d/%d\n",b,polymarker.size());
       res= false;
+
+      // Look for stored point list
       for (unsigned int a=0; a< fLastPolymarkers.size() ;a++) {
-        printf("against vect %d/%d\n",a,fLastPolymarker.size());
+        //        printf("against vect %d/%d\n",a,fLastPolymarker.size());
         std::vector < G4Polymarker > storedPoly = fLastPolymarkers[a];
         int size= storedPoly.size()-1;
-        for (unsigned int aPoly=0; aPoly< storedPoly[a].size() ;aPoly++) {
-          printf("against poly %d/%d\n",aPoly,storedPoly.size());
+
+        // Look for point
+        for (int aPoly=0; aPoly< size ;aPoly++) {
+          //          printf("against poly %d/%d\n",aPoly,storedPoly.size());
           if (( storedPoly[size-aPoly].GetPosition().x() == polymarker[b].x()) &&
               ( storedPoly[size-aPoly].GetPosition().y() == polymarker[b].y()) &&
@@ -238,5 +242,5 @@
             res= true;
             nbDoublons++;
-            printf("G4OpenGLStoredSceneHandler::AddPrimitive  PT %d/%d ALREADY FOUND TOT:%d. OLD pos %d/%d in %d PolyMarkerList STILL %d Marker in list. Point is %f %f %f\n",b,polymarker.size(),  nbDoublons,  aPoly,storedPoly[a].size(),a   ,reste,polymarker[b].x(),polymarker[b].y(),polymarker[b].z());
+            //            printf("G4OpenGLStoredSceneHandler::AddPrimitive  PT %d/%d ALREADY FOUND TOT:%d. OLD pos %d/%d in %d PolyMarkerList STILL %d Marker in list. Point is %f %f %f\n",b,polymarker.size(),  nbDoublons,  aPoly,size,a   ,reste,polymarker[b].x(),polymarker[b].y(),polymarker[b].z());
           }
         }
Index: trunk/source/visualization/OpenGL/src/G4OpenGLStoredViewer.cc
===================================================================
--- trunk/source/visualization/OpenGL/src/G4OpenGLStoredViewer.cc	(revision 944)
+++ trunk/source/visualization/OpenGL/src/G4OpenGLStoredViewer.cc	(revision 945)
@@ -125,4 +125,7 @@
 
 void G4OpenGLStoredViewer::DrawDisplayLists () {
+#ifdef G4DEBUG_VIS_OGL
+      printf("G4OpenGLStoredViewer::DrawDisplayLists \n");
+#endif
 
   const G4Planes& cutaways = fVP.GetCutawayPlanes();
@@ -131,4 +134,7 @@
   size_t nPasses = cutawayUnion? cutaways.size(): 1;
   for (size_t i = 0; i < nPasses; ++i) {
+#ifdef G4DEBUG_VIS_OGL
+    printf("G4OpenGLStoredViewer::DrawDisplayLists cutaway %d/%d\n",i,nPasses);
+#endif
 
     if (cutawayUnion) {
@@ -146,4 +152,7 @@
 
     for (size_t i = 0; i < fG4OpenGLStoredSceneHandler.fTOList.size(); ++i) {
+#ifdef G4DEBUG_VIS_OGL
+      printf("+");
+#endif
       G4OpenGLStoredSceneHandler::TO& to =
 	fG4OpenGLStoredSceneHandler.fTOList[i];
Index: trunk/source/visualization/OpenGL/src/G4OpenGLStoredXViewer.cc
===================================================================
--- trunk/source/visualization/OpenGL/src/G4OpenGLStoredXViewer.cc	(revision 944)
+++ trunk/source/visualization/OpenGL/src/G4OpenGLStoredXViewer.cc	(revision 945)
@@ -78,5 +78,5 @@
 
   //Make sure current viewer is attached and clean...
-  glXMakeCurrent (dpy, win, cx);
+  glXMakeCurrent (dpy, fGLXWin, cx);
 
   G4ViewParameters::DrawingStyle style = GetViewParameters().GetDrawingStyle();
@@ -127,5 +127,5 @@
   GLint renderMode;
   glGetIntegerv(GL_RENDER_MODE, &renderMode);
-  if (renderMode == GL_RENDER) glXSwapBuffers (dpy, win);  
+  if (renderMode == GL_RENDER) glXSwapBuffers (dpy, fGLXWin);  
 }
 
Index: trunk/source/visualization/OpenGL/src/G4OpenGLStoredXmViewer.cc
===================================================================
--- trunk/source/visualization/OpenGL/src/G4OpenGLStoredXmViewer.cc	(revision 944)
+++ trunk/source/visualization/OpenGL/src/G4OpenGLStoredXmViewer.cc	(revision 945)
@@ -87,5 +87,5 @@
 
   //Make sure current viewer is attached and clean...
-  glXMakeCurrent (dpy, win, cx);
+  glXMakeCurrent (dpy, fGLXWin, cx);
 
   G4ViewParameters::DrawingStyle style = GetViewParameters().GetDrawingStyle();
@@ -141,5 +141,5 @@
   GLint renderMode;
   glGetIntegerv(GL_RENDER_MODE, &renderMode);
-  if (renderMode == GL_RENDER) glXSwapBuffers (dpy, win);  
+  if (renderMode == GL_RENDER) glXSwapBuffers (dpy, fGLXWin);  
 }
 
Index: trunk/source/visualization/OpenGL/src/G4OpenGLViewer.cc
===================================================================
--- trunk/source/visualization/OpenGL/src/G4OpenGLViewer.cc	(revision 944)
+++ trunk/source/visualization/OpenGL/src/G4OpenGLViewer.cc	(revision 945)
@@ -131,7 +131,21 @@
 /**
  * Set the viewport of the scene
+ * MAXIMUM SIZE is :
+ * GLint dims[2];
+ * glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
  */
 void G4OpenGLViewer::ResizeGLView()
 {
+  // Check size
+  GLint dims[2];
+  glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
+  if (fWinSize_x > (unsigned)dims[0]) {
+    G4cerr << "Try to resize view greater than max X viewport dimension. Desired size "<<dims[0] <<" is resize to "<<  dims[0] << G4endl;
+    fWinSize_x = dims[0];
+  }
+  if (fWinSize_y > (unsigned)dims[1]) {
+    G4cerr << "Try to resize view greater than max Y viewport dimension. Desired size "<<dims[0] <<" is resize to "<<  dims[1] << G4endl;
+    fWinSize_y = dims[1];
+  }
   int side = fWinSize_x;
   if (fWinSize_y < fWinSize_x) side = fWinSize_y;
@@ -560,4 +574,5 @@
   if (!fGL2PSAction) return false;
 
+
   fGL2PSAction->setFileName(fPrintFilename.c_str());
   // try to resize
@@ -567,6 +582,13 @@
   fWinSize_x = width;
   fWinSize_y = height;
+
   ResizeGLView();
   if (fGL2PSAction->enableFileWriting()) {
+
+    // FIXME : How to get the point size.... ?
+    fGL2PSAction->setLineWidth(1);
+    // FIXME : How to get the line width.... ?
+    fGL2PSAction->setPointSize(30);
+
     DrawView ();
     fGL2PSAction->disableFileWriting();
Index: trunk/source/visualization/OpenGL/src/G4OpenGLXViewer.cc
===================================================================
--- trunk/source/visualization/OpenGL/src/G4OpenGLXViewer.cc	(revision 944)
+++ trunk/source/visualization/OpenGL/src/G4OpenGLXViewer.cc	(revision 945)
@@ -91,5 +91,5 @@
 
 void G4OpenGLXViewer::SetView () {
-  glXMakeCurrent (dpy, win, cx);
+  glXMakeCurrent (dpy, fGLXWin, cx);
   G4OpenGLViewer::SetView ();  
 }
@@ -258,5 +258,5 @@
   class_hints -> res_class = NewString("G4OpenGL");
 
-   win = XCreateWindow (dpy, XRootWindow (dpy, vi -> screen), x_origin, 
+   fGLXWin = XCreateWindow (dpy, XRootWindow (dpy, vi -> screen), x_origin, 
                         y_origin, fWinSize_x, fWinSize_y, 0, vi -> depth,
                         InputOutput, vi -> visual,  
@@ -265,15 +265,15 @@
                         &swa);
   
-   XSetWMProperties (dpy, win, &windowName, &iconName, 0, 0, 
+   XSetWMProperties (dpy, fGLXWin, &windowName, &iconName, 0, 0, 
                      size_hints, wm_hints, class_hints);
   
 // request X to Draw window on screen.
-  XMapWindow (dpy, win);
+  XMapWindow (dpy, fGLXWin);
 
 // Wait for window to appear (wait for an "expose" event).
-  XIfEvent (dpy, &event, G4OpenGLXViewerWaitForNotify, (char*) win);
+  XIfEvent (dpy, &event, G4OpenGLXViewerWaitForNotify, (char*) fGLXWin);
 
 // connect the context to a window
-  Bool success = glXMakeCurrent (dpy, win, cx);
+  Bool success = glXMakeCurrent (dpy, fGLXWin, cx);
   if (!success) {
     fViewId = -1;  // This flags an error.
@@ -404,5 +404,5 @@
     glXMakeCurrent (dpy, None, NULL);
     glXDestroyContext (dpy, cx);
-    if (win) XDestroyWindow (dpy, win); // ...if already deleted in
+    if (fGLXWin) XDestroyWindow (dpy, fGLXWin); // ...if already deleted in
     // sub-class G4OpenGLXmViewer.
     XFlush (dpy);
@@ -415,10 +415,13 @@
   //cout << "print_col_callback requested with file name: " << fPrintFilename << G4endl;
   
+  if (fVectoredPs) {
 #ifdef G4DEBUG_VIS_OGL
-  printf("G4OpenGLXViewer::print \n");
+    printf("G4OpenGLXViewer::print Vectored\n");
 #endif
-  if (fVectoredPs) {
     printEPS();    
   } else {
+#ifdef G4DEBUG_VIS_OGL
+    printf("G4OpenGLXViewer::print non Vectored\n");
+#endif
     //    printEPS ();
     //    return;
@@ -446,22 +449,29 @@
     
     GLXDrawable tmp_win;
-    tmp_win=win;
-    win=glxpmap;
-    
+    tmp_win=fGLXWin;
+    fGLXWin=glxpmap;
+    
+    // This won't work because fGLXWin is not known in G4OpenGLViewer...
+
     glXMakeCurrent (dpy,
-		    win,
+		    fGLXWin,
 		    cx);
         
+    glViewport (0, 0, fWinSize_x, fWinSize_y);
+    
     ClearView ();
     SetView ();
     DrawView ();
     
-    printEPS ();
-    
-    win=tmp_win;
+    std::string file = "G4OpenGL_XPixmap.eps";
+    generateEPSX (file.c_str(),
+                 fPrintColour,
+                 fWinSize_x, fWinSize_y);
+    
+    fGLXWin=tmp_win;
     cx=tmp_cx;
     
     glXMakeCurrent (dpy,
-		    win,
+		    fGLXWin,
 		    cx);
     
@@ -489,3 +499,131 @@
 }
 
+
+GLubyte* G4OpenGLXViewer::grabPixelsX (int inColor, unsigned int width, unsigned int height) {
+	 
+  GLubyte* buffer;
+  GLint swapbytes, lsbfirst, rowlength;
+  GLint skiprows, skippixels, alignment;
+  GLenum format;
+  int size;
+	
+  if (inColor) {
+    format = GL_RGB;
+    size = width*height*3;
+  } else {
+    format = GL_LUMINANCE;
+    size = width*height*1;
+  }
+	
+  buffer = new GLubyte[size];
+  if (buffer == NULL)
+    return NULL;
+	
+  glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapbytes);
+  glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirst);
+  glGetIntegerv (GL_UNPACK_ROW_LENGTH, &rowlength);
+	
+  glGetIntegerv (GL_UNPACK_SKIP_ROWS, &skiprows);
+  glGetIntegerv (GL_UNPACK_SKIP_PIXELS, &skippixels);
+  glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignment);
+	
+  glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
+  glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
+  glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
+	
+  glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
+  glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
+  glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
+	
+  glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, format, GL_UNSIGNED_BYTE, (GLvoid*) buffer);
+	
+  glPixelStorei (GL_UNPACK_SWAP_BYTES, swapbytes);
+  glPixelStorei (GL_UNPACK_LSB_FIRST, lsbfirst);
+  glPixelStorei (GL_UNPACK_ROW_LENGTH, rowlength);
+	 
+  glPixelStorei (GL_UNPACK_SKIP_ROWS, skiprows);
+  glPixelStorei (GL_UNPACK_SKIP_PIXELS, skippixels);
+  glPixelStorei (GL_UNPACK_ALIGNMENT, alignment);
+	 
+  return buffer;
+}
+	
+int G4OpenGLXViewer::generateEPSX (const char* filnam,
+                                  int inColour,
+                                  unsigned int width,
+                                  unsigned int height) {
+	
+  FILE* fp;
+  GLubyte* pixels;
+  GLubyte* curpix;
+  int components, pos, i;
+	
+  pixels = grabPixelsX (inColour, width, height);
+	
+  if (pixels == NULL)
+    return 1;
+  if (inColour) {
+    components = 3;
+  } else {
+    components = 1;
+  }
+	 
+  fp = fopen (filnam, "w");
+  if (fp == NULL) {
+    return 2;
+  }
+	 
+  fprintf (fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");
+  fprintf (fp, "%%%%Title: %s\n", filnam);
+  fprintf (fp, "%%%%Creator: OpenGL pixmap render output\n");
+  fprintf (fp, "%%%%BoundingBox: 0 0 %d %d\n", width, height);
+  fprintf (fp, "%%%%EndComments\n");
+  fprintf (fp, "gsave\n");
+  fprintf (fp, "/bwproc {\n");
+  fprintf (fp, "    rgbproc\n");
+  fprintf (fp, "    dup length 3 idiv string 0 3 0 \n");
+  fprintf (fp, "    5 -1 roll {\n");
+  fprintf (fp, "    add 2 1 roll 1 sub dup 0 eq\n");
+  fprintf (fp, "    { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
+  fprintf (fp, "       3 1 roll 5 -1 roll } put 1 add 3 0 \n");
+  fprintf (fp, "    { 2 1 roll } ifelse\n");
+  fprintf (fp, "    }forall\n");
+  fprintf (fp, "    pop pop pop\n");
+  fprintf (fp, "} def\n");
+  fprintf (fp, "systemdict /colorimage known not {\n");
+  fprintf (fp, "   /colorimage {\n");
+  fprintf (fp, "       pop\n");
+  fprintf (fp, "       pop\n");
+  fprintf (fp, "       /rgbproc exch def\n");
+  fprintf (fp, "       { bwproc } image\n");
+  fprintf (fp, "   }  def\n");
+  fprintf (fp, "} if\n");
+  fprintf (fp, "/picstr %d string def\n", width * components);
+  fprintf (fp, "%d %d scale\n", width, height);
+  fprintf (fp, "%d %d %d\n", width, height, 8);
+  fprintf (fp, "[%d 0 0 %d 0 0]\n", width, height);
+  fprintf (fp, "{currentfile picstr readhexstring pop}\n");
+  fprintf (fp, "false %d\n", components);
+  fprintf (fp, "colorimage\n");
+	 
+  curpix = (GLubyte*) pixels;
+  pos = 0;
+  for (i = width*height*components; i>0; i--) {
+    fprintf (fp, "%02hx ", *(curpix++));
+    if (++pos >= 32) {
+      fprintf (fp, "\n");
+      pos = 0;
+    }
+  }
+  if (pos)
+    fprintf (fp, "\n");
+	
+  fprintf (fp, "grestore\n");
+  fprintf (fp, "showpage\n");
+  delete pixels;
+  fclose (fp);
+  return 0;
+}
+	
+
 #endif
Index: trunk/source/visualization/OpenGL/src/G4OpenGLXmViewer.cc
===================================================================
--- trunk/source/visualization/OpenGL/src/G4OpenGLXmViewer.cc	(revision 944)
+++ trunk/source/visualization/OpenGL/src/G4OpenGLXmViewer.cc	(revision 945)
@@ -635,7 +635,7 @@
   // can bind the OpenGL rendering context to the window.
 
-  win = XtWindow (glxarea);
-
-  glXMakeCurrent (dpy, win, cx);
+  fGLXWin = XtWindow (glxarea);
+
+  glXMakeCurrent (dpy, fGLXWin, cx);
 
   // This should be add AFTER glXMakeCurrent done because it will fire a resizeCallback
@@ -684,5 +684,5 @@
 {
   XtDestroyWidget  (shell);
-  win = 0; // ...to avoid XDestroyWindow in G4OpenGLXViewer base class
+  fGLXWin = 0; // ...to avoid XDestroyWindow in G4OpenGLXViewer base class
   // because XtDestroyWidget has already destroyed it.
   G4Xt::getInstance () ->RemoveShell (shell);
