Hello Guest

glGetTexImage returns curious results

  • 4 Replies
  • 8014 Views
*

Offline Cornix

  • *****
  • 488
glGetTexImage returns curious results
« on: August 01, 2013, 19:33:58 »
Hello.

I am trying to read the contents of a texture. It works but not quite as I expected.
My code looks somewhat like this:
Code: [Select]
byte[] pixels = new byte[width * height * 4];
ByteBuffer buffer = ByteBuffer.allocateDirect(pixels.length).order(ByteOrder.nativeOrder());
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture_id);
GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
buffer.get(pixels);
System.out.println(Arrays.toString(pixels));
However, the array pixels is filled only with the values 0 and -1.
The texture is an image with only white and transparent pixels.

What am I doing wrong?

Thanks in advance.

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: glGetTexImage returns curious results
« Reply #1 on: August 02, 2013, 00:33:54 »
You're not doing anything wrong. The data you get out of OpenGL is in the form of unsigned bytes, but Java only knows how to interpret signed bytes so it is screwing up the output.

A little bitwise op will solve the problem.

Code: [Select]
for(int i = 0; i < pixels.length; i++) {
    System.out.println( pixels[i] & 0xFF );
}

and you thought you could escape the For Loop

*

Offline Cornix

  • *****
  • 488
Re: glGetTexImage returns curious results
« Reply #2 on: August 02, 2013, 12:25:13 »
Thanks for that information.

I was previously trying to get the data as floats but for some reason the program crashed anytime I tried.
It looked like this:
Code: [Select]
float[] pixels = new float[width * height * 4];
ByteBuffer buffer = ByteBuffer.allocateDirect(pixels.length).order(ByteOrder.nativeOrder());
FloatBuffer fBuffer = buffer.asFloatBuffer();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture_id);
GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, GL11.GL_FLOAT, fBuffer);
fBuffer.get(pixels);
System.out.println(Arrays.toString(pixels));

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: glGetTexImage returns curious results
« Reply #3 on: August 02, 2013, 12:40:10 »
Hmm, nothing there stands out as being wrong.

When you say crash, was it a native crash (Like Illegal Memory Address or whatever its called), or a java exception? Either way I'd be interested  to see the stack trace.

*

Offline Cornix

  • *****
  • 488
Re: glGetTexImage returns curious results
« Reply #4 on: August 02, 2013, 14:13:00 »
I noticed the mistake. I had to multiply by 4 again (float byte size) when creating the ByteBuffer.
It was a native crash I got.

Now it works, thanks alot.