BufferImage in lwjgl?

Started by Droa, April 08, 2012, 18:09:20

Previous topic - Next topic

Droa

Hi guys.

How i was going to make a periodically update of a minimap, drawn in its own thread, as its kinda heavy processing.
However i cant really find a way to make a lwjgl version of a bufferedimage, i can draw as maby a texture, or something?

the way i draw the minimap, really lowers the fps, as i have to paint it in each loop, and if i could thread it, i could make it update as a low prioity process, maby each 2 sec, and send the drawn image to the screen as a drawn texture.

but is that possible? i can only see lwjgl supports direct drawing to the screen?

Fool Running

Ideally, for speed reasons, you should draw right to the byte buffer that holds the image data. It's very complicated and slow to create a buffered image, draw to it, extract the pixel data into a byte buffer, and then create the texture from that.
If you absolutely need to use a buffered image, you can do the following (taken from some old texture-loading code I had laying around):
BufferedImage image = ...;
WritableRaster raster = image.getRaster();

if(raster.getTransferType() != DataBuffer.TYPE_BYTE)
    throw new RuntimeException("unknown transfer type");

switch(raster.getNumDataElements()){
    case 1: type = TextureType.lightmap; break;
    case 3: type = TextureType.rgb; break;
    case 4: type = TextureType.rgba; break;
    default: throw new RuntimeException("Unknown image type"); 
}
            
byte[] data = (byte[])raster.getDataElements(0, 0, image.getWidth(), image.getHeight(), null);
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

CodeBunny

OpenGL does not just support rendering to the screen (bold because this is important for any advanced rendering). Is supports rendering to any framebuffer. The default framebuffer is the display.

However, you can create other frambuffers. One way of doing this is to render directly to a texture, then use the created texture as normal. You should do something like this.

There is a guide to Render To Texture on the wiki, you should use that.