Render to BufferedImage

Started by beachbum1001, May 15, 2009, 21:46:26

Previous topic - Next topic

beachbum1001

Hello everyone!

I am quite new to lwjgl and OpenGL in general. I was hoping someone could answer a question for me.
My question is, is it possible to render an image through OpenGL and then load it into a BufferedImage?

My reason for asking is because I am currently creating a game in Java using its awt library.
I have written a program for normal mapping, and I plan on using it to create dynamic
lighting for in game objects. This however is painfully slow as I have to run through every
pixel in the image to find its normal light value. I would like to be able to render the
normal map image in OpenGL and then display it in my game. This would enhance performance
quite a bit. If I cannot find an efficient method I will just leave it out.

Thanks for any help

Fool Running

It is very possible to get an OpenGL image to a BufferedImage, but I think you're going about it the wrong way.
I'm assuming that you have a normal map generated and you are really trying to do the lighting calculations to shade an image using the normal map.
Per-pixel lighting is very slow no matter what you try do (since it involves complex calculations at each pixel), unless you put it all on a GPU which is designed to do stuff like that (although still relatively slow).
So the best way to do it would be to make your entire game using OpenGL and have OpenGL do the lighting for you (with pixel shaders) using the normal maps.

If, however, you want to try just using OpenGL to get the lighting data manually, you need to do the following:
-You need to make a PBuffer (an off-screen OpenGL rendering buffer) since you probably don't want users to see an OpenGL window.
-Use OpenGL to draw what you need to the PBuffer
-Call glReadPixels(int x, int y, int width, int height, int format, int type, ByteBuffer pixels) to get the buffer data from OpenGL
-create your new BufferedImage with "new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);"
-put the buffer data into the BufferedImage with "image.getRaster().setDataElements(0, 0, width, height, pixels);"
-Flip the image (since OpenGl's origin is at the bottom-left instead of the top-left)

As you can see, it isn't trivial (read slow) to get the OpenGL image data. I doubt you would gain as much of a performance increase as you think, if any at all.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D