glGetTexImage help

Hi there. I wondered if anyone could help me with a problem I’m having. I’m trying create a texture from the framebuffer, and then read it back into a conventional array. I wasn’t having much luck with this, so at the moment, I’m simply creating a texture manually, and trying to read that back. However, when I print out the array, all I’m getting is 0s. If anyone would mind pointing out where I’ve made mistakes, it would be much appreciated! I’ve everywhere to try and figure out what I’m doing wrong to little avail… so apologies if this is a repeat question.


    private static void makeTexture(FloatBuffer imageBuf, int size){
        for (int i = 0; i < size; i++){
          for (int j = 0; j < size; j++){
            imageBuf.put(1);
            imageBuf.put(0);
            imageBuf.put(0);
          }
        }
        imageBuf.rewind();
    }
    
    public void readBackTexture(GL gl){
	   gl.glPushMatrix();

	   int color = GL.GL_RGB;
	   int size = 8;
	
	   FloatBuffer ImageBuf =  BufferUtil.newFloatBuffer(size * size * color);
	   IntBuffer textureIndex = BufferUtil.newIntBuffer(1);
	   	
	   gl.glGenTextures(1,textureIndex);
	   gl.glBindTexture(GL.GL_TEXTURE_2D, textureIndex.get(0));
	   	
	   makeTexture(ImageBuf, size);
	
	   gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
	   gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, color, size, size, 0, GL.GL_RGB, GL.GL_FLOAT, ImageBuf);
	
	   gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
	   gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
	   gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_DECAL);

 	   	   
 	   gl.glEnable (GL.GL_TEXTURE_2D);
 	    gl.glBegin(GL.GL_POLYGON);
 	    	gl.glTexCoord2d(0,0);
 	    	gl.glVertex2d(0, 0);
 	    	gl.glTexCoord2d(0,1);
 	    	gl.glVertex2d(0, 1);
 	    	gl.glTexCoord2d(1,1);
 	    	gl.glVertex2d(1, 1);
 	    	gl.glTexCoord2d(1,0);
 	    	gl.glVertex2d(1, 0);
 	   gl.glEnd();
 	   gl.glEnable (GL.GL_TEXTURE_2D);
 	   
           float output[][][] = new float[size][size][color];

 	   gl.glBindTexture(GL.GL_TEXTURE_2D, textureIndex.get(0));
   	   FloatBuffer outputBuffer =  BufferUtil.newFloatBuffer(size * size * color);
 	   gl.glGetTexImage(GL.GL_TEXTURE_2D, 0, color, GL.GL_FLOAT, outputBuffer);
 	    
     	   for (int i = 0; i < size; i++){
               for (int j = 0; j < size; j++){
         	      output[i][j][0] = outputBuffer.get();
         	      output[i][j][1] = outputBuffer.get();
         	      output[i][j][2] = outputBuffer.get();
               }
           }
      	
           for (int i = 0; i < size; i++){
             for (int j = 0; j < size; j++){
          	   System.out.print(" (" + output[i][j][0] + ", ");
          	   System.out.print(output[i][j][1] + ", ");
          	   System.out.print(output[i][j][2] + ") ");
             }
             System.out.println();
           }

   	   gl.glPopMatrix();
   	   gl.glFlush();
      }

At the moment, the polygon’s being correctly textured in red, but as I said earlier, I just get 0s in my output array. Thanks a lot for any help.

I don’t see anything obviously wrong – maybe someone else can figure it out – but I’d suggest you look at the Screenshot and TextureIO utility classes and their source code, which do the kinds of operations you’re interested in.

I tested this on one of my machines (nVidia GeForce 6600 on WinXP), and it did exactly what it’s supposed to (e.g. the array was filled with [1,0,0] elements). Maybe the problem is in your drivers?