Accessing data in a ByteBuffer to edit it

Hi,

How do I get the data from a ByteBuffer so I manipulate it? (And then put it back into the same ByteBuffer)

Reason being, I want to flip an image vertically when I load it, that way I have less inconsistencies in regards to UV co-ordinates, from my post process frame buffer, UI and all the loaded models.
Right now, I have to flip the UVs vertically on the loaded models, but not on my frame buffer, apparently OpenGL flips the image if it’s put in normally, I have no idea why.

Why don’t you just use a texture matrix and scale it’s Y axis with -1? This would be the easiest (though probably not the most efficient) solution.

By the way I’m not entirely sure about the ByteBuffer question but I think that after a flip or a rewind you should be able to recieve any element from the buffer using [icode]buffer.get();[/icode] or [icode]buffer.get(index);[/icode], and then put items back to the buffer using [icode]buffer.put(item);[/icode] or [icode]buffer.put(index, item);[/icode]. Simple as that. :slight_smile:

I would prefer to just edit the data, instead of creating more, sure I may use a texture matrix in the future, but for now, I would rather just edit the data.

Using buffer.get(); seems to just return a byte, where as I was kinda hoping to get an array.
ByteBuffer does have buffer.array();, but when I try and do byte[] array = buffer.array(), I
get a UnsupportedOperationException

Off topic: How did you get the little bits of code to look like that? Instead of what happens when you do the normal way.

You can use the code button when creating a post (pound, number, or hashtag symbol; whatever you want to call it). This will create a separate block of code, useful for bigger snippets. This creates [code][/code] tags where you put your code. In order to have it inline (like Panda has it), then you use the tags [icode][/icode].

[icode]buffer.array()[/icode] only works if the buffer is actually backed by an array. However, buffers for OpenGL are directly allocated (at a native memory section). Without copying the data into “managed memory” (e.g. a byte array), you cannot access it as an array.
This is why calling [icode]buffer.array()[/icode] throws an exception (as the documentation states).

You can use the buffer like an array with the get() and put() methods that simply retrieve and place data at an index, so you can use these for the flipping thing.

Do you have access to the code that allocates the ByteBuffer? If so, I would start manipulating the data BEFORE it is placed in the ByteBuffer in the first place.

Ah right I see.
Below is how I load the image.
So how would I “manipulate the data BEFORE it is placed in the ByteBuffer in the first place”, in this case?
Or am I stuck doing it afterwards, since I use PNGDecoder.

try {
	System.out.println("Loading Texture: " + filename);
			
	InputStream texIn = new FileInputStream(Directories.getAssetLoc() + filename);
			
	PNGDecoder decoder = new PNGDecoder(texIn);
					
	setWidth(decoder.getWidth());
	setHeight(decoder.getHeight());
					
	setTexBuffer(ByteBuffer.allocateDirect(4 * getWidth() * getHeight()));
	decoder.decode(getTexBuffer(),  4 * getWidth(), Format.RGBA);
	getTexBuffer().flip();		
											
	texIn.close();
			
	System.out.println("Loading Complete: " + filename + "\n -----------------------------");
} catch (IOException e) {
   e.printStackTrace();
	ErrorCheck.stopOnError(Error.TEXTURELOADIO);
}