Ignore:
Timestamp:
Mar 25, 2009, 11:12:05 AM (15 years ago)
Author:
garnier
Message:

bug fix in glReadPixels

Location:
trunk/source/visualization/OpenGL
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/source/visualization/OpenGL/History

    r949 r950  
    1 $Id: History,v 1.129 2009/03/05 16:36:13 lgarnier Exp $
     1$Id: History,v 1.131 2009/03/24 13:50:07 lgarnier Exp $
    22-------------------------------------------------------------------
    33
     
    2424 - G4OpenGLViewer : Add a fix and resizing when X/Y size was even (lack of precision
    2525   in a /2)
     26
     2718th March 2009  John Allison
     28- G4OpenGLViewerMessenger.cc: printMode and transparency are available
     29  for all OGL windows (they were previously inadvertently hidden by
     30  an OGLS requirement).
    2631
    273216th March 2009, Laurent Garnier
  • trunk/source/visualization/OpenGL/include/G4OpenGLXViewer.hh

    r946 r950  
    9898  char                              charViewName [100];
    9999
    100   GLubyte* grabPixelsX (int inColor, unsigned int width, unsigned int height);
     100  bool grabPixelsX (unsigned int width, unsigned int height,GLenum format, GLubyte* buffer);
    101101  int generateEPSX (const char* filnam,int inColour, unsigned int width,unsigned int height);
    102102
  • trunk/source/visualization/OpenGL/src/G4OpenGLViewer.cc

    r949 r950  
    164164  // SPECIAL CASE
    165165  if ((fWinSize_x - side)%2) {
    166     fWinSize_x --;
     166    //    fWinSize_x --;
     167
     168    side = fWinSize_x;
     169    if (fWinSize_y < fWinSize_x) side = fWinSize_y;
    167170  }
    168171  if ((fWinSize_y - side)%2) {
    169     fWinSize_y --;
    170   }
    171  
    172   GLint width = (fWinSize_x - side) / 2;
    173   GLint height = (fWinSize_y - side) / 2;
    174  
    175   glViewport(width, height, side, side); 
     172    //    fWinSize_y --;
     173
     174    side = fWinSize_x;
     175    if (fWinSize_y < fWinSize_x) side = fWinSize_y;
     176  }
     177 
     178  GLint X = (fWinSize_x - side) / 2;
     179  GLint Y = (fWinSize_y - side) / 2;
     180 
     181  printf("G4OpenGLViewer::ResizeGLView X:%d Y:%d W:%d H:%d --side%d\n",(fWinSize_x - side) / 2,(fWinSize_y - side) / 2,fWinSize_x,fWinSize_y,side);
     182  glViewport(X, Y, side, side);
     183  //    glViewport(0, 0, fWinSize_x,fWinSize_y); 
     184 
     185
    176186}
    177187
  • trunk/source/visualization/OpenGL/src/G4OpenGLXViewer.cc

    r949 r950  
    511511   
    512512    //    InitializeGLView ();
    513    
     513    int winX=fWinSize_x;
     514    int winY=fWinSize_y;
     515
     516    for (int tstX  = winX-2;tstX <=winX+2;tstX++) {
     517      for (int tstY  = winY-2;tstY <=winY+2;tstY++) {
     518        fWinSize_y = tstY;
     519        fWinSize_x = tstX;
     520        std::string file = "G4OpenGL_XPixmap";
     521        file += tstX;
     522        file +="x";
     523        file +=tstY;
     524        file +=".eps";
     525
    514526    // clear the buffers and window.
    515527    SetView();
     
    540552
    541553   
    542     std::string file = "G4OpenGL_XPixmap.eps";
    543554    generateEPSX (file.c_str(),
    544555                 fPrintColour,
    545556                 fWinSize_x, fWinSize_y);
    546557   
     558      }
     559    }
    547560    fGLXWin=tmp_win;
    548561    cx=tmp_cx;
     
    607620
    608621
    609 GLubyte* G4OpenGLXViewer::grabPixelsX (int inColor, unsigned int width, unsigned int height) {
     622bool G4OpenGLXViewer::grabPixelsX (unsigned int width, unsigned int height,GLenum format,GLubyte* buffer) {
    610623#ifdef G4DEBUG_VIS_OGL
    611624  GLint viewport[4];
     
    614627#endif
    615628         
    616   //  GLubyte* buffer;
    617   GLint* bufferI;
    618629  GLint swapbytes, lsbfirst, rowlength;
    619630  GLint skiprows, skippixels, alignment;
    620   GLenum format;
    621   int size;
    622        
    623   if (inColor) {
    624     format = GL_RGB;
    625     size = width*height*3;
     631
     632  unsigned int lineSize = 0;
     633  if (format == GL_RGB){
     634    lineSize = width*3;
    626635  } else {
    627     format = GL_LUMINANCE;
    628     size = width*height*1;
    629   }
    630        
    631   unsigned char *buffer = new unsigned char [ size] ;
    632   buffer = new GLubyte[size];
    633   bufferI = new int[size];
    634   if (buffer == NULL)
    635     return NULL;
    636   for (int y = 0; y<size; y++) {
    637     buffer[y] = 0;
    638   }
     636    lineSize = width*1;
     637  }
     638 
     639  GLubyte* lineBuffer = new GLubyte[lineSize];
     640  if (lineBuffer == NULL)
     641    return false;
     642  for (int y = 0; y<lineSize; y++) {
     643    lineBuffer[y] = 0;
     644  }
     645
    639646  glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapbytes);
    640647  glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirst);
     
    645652  glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignment);
    646653       
    647 //   glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
    648 //   glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
    649 //   glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
    650        
    651 //   glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
    652 //   glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
    653 //   glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
    654        
    655   glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, format, GL_UNSIGNED_BYTE, (GLvoid*) buffer);
    656   //  glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, format, GL_INT, bufferI);
    657        
    658   glXWaitGL (); //Wait for effects of all previous OpenGL commands to
     654  glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
     655  glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
     656  glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
     657       
     658  glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
     659  glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
     660  glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
     661       
     662  // FIXME L.Garnier 25 03 2009
     663  // Try to read at first a block of width by height
     664  // But on mac osX 10.5.6 on some window size their is
     665  // some stranges effects:
     666  // Pixels should came 3 values by 3 values but on border
     667  // it seems that with some window size there is a lack of
     668  // 1 or 2 values. For example a gray level picture like
     669  // (1,1,1) (2,2,2) (3,3,3)
     670  // (4,4,4) (5,5,5) (6,6,6)
     671  // (7,7,7) (8,8,8) (9,9,9)
     672  // appear as (with all buffer values init to 999) :
     673  // (1,1,1) (2,2,2) (3,3,3)
     674  // (999,999,4) (4,4,5) (5,5,6)
     675  // (6,6,999) (999,999,7) (7,7,8)
     676  // STRANGE EFFECT....
     677
     678  // Then we read line by line...and NO problem !
     679
     680
     681  for (int i=0;i<height;i++) {
     682    glReadPixels (0, i, (GLsizei)width, 1, format, GL_UNSIGNED_BYTE, (GLvoid*) lineBuffer);
     683    glXWaitGL (); //Wait for effects of all previous OpenGL commands to
     684    //    printf("\n%d ",i);
     685    for (int j=0;j<lineSize;j++) {
     686      buffer[j+i*lineSize] = lineBuffer[j];
     687      //      printf("%d ",buffer[j]);
     688    }
     689  }
    659690  //                 //be propagated before progressing.
    660691  glFlush ();
    661692
    662 //   printf("-----------\n-----------\n-----------\n-----------\n-----------\n%d %d %d\n",width, height,size);
    663   //  char * pixels = new char [3 * width * height];
    664   //  glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, GL_DEPTH_COMPONENT, GL_FLOAT, pixels);
    665  
    666   for (int y = 0; y<height*3; y++) {
    667     printf("\n%d : ",y/3);
    668     for (int x = 0; x<width*3; x++) {
    669       printf("%d ",buffer[y*width+x]);
    670   //     if (buffer[y*width+x] == 0)
    671 //         printf("");
    672 //       else  if (buffer[y*width+x] == 255)
    673 //         printf("0");
    674      }
    675   }
    676  
    677 //   printf("-----------\n-----------\n-----------\n-----------\n-----------\n");
     693  delete [] lineBuffer;
    678694 
    679695  glPixelStorei (GL_UNPACK_SWAP_BYTES, swapbytes);
     
    686702         
    687703 
    688   return buffer;
     704  return true;
    689705}
    690706       
     
    698714  GLubyte* curpix;
    699715  int components, pos, i;
     716
     717  GLenum format;
     718  int size;
     719       
     720  if (inColour) {
     721    format = GL_RGB;
     722    size = width*height*3;
     723  } else {
     724    format = GL_LUMINANCE;
     725    size = width*height*1;
     726  }
     727       
     728  pixels = new GLubyte[size];
     729  if (pixels == NULL)
     730    return NULL;
     731  for (int y = 0; y<size; y++) {
     732    pixels[y] = 0;
     733  }
     734
    700735       
    701736#ifdef G4DEBUG_VIS_OGL
     
    704739 
    705740
    706   pixels = grabPixelsX (inColour, width, height);
     741  if (!grabPixelsX (width, height,format,pixels)) {
     742    return 1;
     743  }
    707744#ifdef G4DEBUG_VIS_OGL
    708745    printf("--\n--\n--\n");
    709746#endif
    710747       
    711   if (pixels == NULL)
    712     return 1;
    713748  if (inColour) {
    714749    components = 3;
     
    772807  delete pixels;
    773808  fclose (fp);
     809
     810#ifdef G4DEBUG_VIS_OGL
     811  printf("G4OpenGLXViewer::generateEPSX END\n");
     812#endif
     813
    774814  return 0;
    775815}
Note: See TracChangeset for help on using the changeset viewer.