Texture Raster using Java Primatives

Last night I realized that not only does Java not have unsigned primitives, bytes are 32 bit. Pretty different from C++… This situation is giving me some hell when it comes to storing a texture’s raster in memory.

If you guy’s wanted to store a texture using 8bits for r, 8bits for g, 8bits for b, and 8bits for a, how would you go at storing it? Usually this would be cake using an array of unsigned byte, but obviously you can’t do that in Java. Any suggestions would go a long way. Thanks!

Java’s BufferedImage usually stores a RGBA pixel in a 32-bit int. Just a few bitshifts and a mask and you’re done.

So you are saying that you would store all of a pixels values, rgba, in a single integer? Makes sense, and should be easily to implement in an image class.

Any thoughts on if that will cause any trouble binding that int[] to gluBuild2DMipmaps? Would you know what internalFormat, format, and type would be used?

You can use Java’s signed types as unsigned values when passing them to OpenGL. Set the value as if it were an unsigned int (with packed components), or use a byte array (and assign values up to 255, etc.). Then tell OpenGL that the type is UNSIGNED_INT or UNSIGNED_BYTE and it will interpret the bit pattern as expected, even though Java believes the values to be negative.

Awesome, I’ll have to try that later. Thanks for the suggestion!

Bytes are signed, 8 bit. But yeah, if you pass the data in as UNSIGNED_BYTE to OpenGL it’ll work just fine.