I have a mite of a problem here. I've been sort of following a
tutorial, but I can't get a particular bit working;
(this method is loading an image, getting a bytebuffer of rasterized pixel data, and storing in a hashmap for later retrieval when binding/drawing)
/**
* Takes a filename and returns a ByteBuffer for JOGL use. We load the image (BufferedImage) file here again,
* rather than reuse the image file from imageLibrary, because that hashmap may be deprecated at a later date.
* This method places the texture data into the textureLibary hashmap, and the size data into the textureSizeLibray
* hashmap
* @param filename of the image file
*/
private void loadTexture(String filename) throws IOException {
String path = new File("").getAbsolutePath() + "\\" + MEDIA_FOLDER + "\\" + IMAGE_FOLDER + "\\" +filename;
BufferedImage buff=ImageIO.read( new File(path)); //load image
//get dimensions
int imgHeight = buff.getHeight();
int imgWidth = buff.getWidth();
//get a raster of the image
Raster r = buff.getRaster(); //(stores details of the individual pixel grid comprising the image)
int[] img = null;
img = r.getPixels(0, 0, buff.getWidth(), buff.getHeight(), img); //gets an int array of pixel data (pos xy, size xy, sample array
//now place the int array contents into a byteBuffer, as OGL uses floats/doubles rather than ints
ByteBuffer texture = ByteBuffer.allocateDirect(buff.getWidth() * buff.getHeight() * 3);
//arg is the capacity; img area times 3 (RGB values)
//place img pixel data from img[] into the buffer
for (int y = 0; y < buff.getHeight(); y++) {
for (int x = 0; x < buff.getWidth(); x++) {
//need to translate to byte format
texture.put((byte) img[(y * buff.getWidth() + x) * 3]);
texture.put((byte) img[(y * buff.getWidth() + x) * 3 + 1]);
texture.put((byte) img[(y * buff.getWidth() + x) * 3 + 2]);
} // x
}//y
//store this data
IntPoint dimensions = new IntPoint(imgHeight, imgWidth);
textureLibrary.put(filename, texture);
textureSizeLibrary.put(dimensions, texture);
Debug.out(this.getClass(), "Stored bytebuffer for texture " + filename + " of size " + dimensions.toString() );
}
Now, I'm getting a (somewhat expected) array overrun error. My understanding is that the aim is to map the RGB values of each pixel from the int[] array of raster data. What I don't understand is the ordering of that when converting the 2 over.... I know the error is on the logical last(?) index (i.e. address 45000 of a length 45000 array), but because I don't know why this guy decided to use those constants, I can't really fix it.
I wish he'd put proper bracketing in, too. Sigh.
EDIT; this -
//now place the int array contents into a byteBuffer, as OGL
- comment is rubbish, I think. The bytebuffer is actually to do with how Java passes data across the HAL for OGL calls, I believe.
EDIT2; might have fixed it, dunno.
byte[] data = ((DataBufferByte) buff.getRaster().getDataBuffer()).getData();
ByteBuffer imageBuffer = ByteBuffer.allocateDirect(data.length);
imageBuffer.order(ByteOrder.nativeOrder());
imageBuffer.put(data, 0, data.length);