Generate picture in OpenGL/ Java OpenGL help information


I try to generate pictures from an OpenGL scene… I found many examples in the forums archives but coded in C++ that I do not know.

I actually work with Java and I would like to know if somebody would have a Java code example or knows where I
could found help… like a forum for GL4

Thanks a lot

Hi !

If I remeber correct gl4java has a mailinglist where you could try, another way is to sign up to suns Java forums, they are very helpful there (



Have you more information about his mailing list ?

For the java web site I had already tried but it is difficult to get replies concerning OpenGL…


Isn’t it pretty much the same though? glReadPixels to grab your image and then save it in whatever format you want

I think Gavin is right. It should be pretty much the same. Grab the data with glReadPixels and save to the format you want.

One thing you may have to watch for, however is byte order. If I remember correctly, Java stores everything as big-endian no matter what the underlying architecture is.

And then there’s the question of converting everything to bytes correctly. Java doesn’t have pointers so it’s not as simple as writing x bytes from the memory address n.

I haven’t worked with the file stream objects too extensively in Java, but I would think there would be some sort of binary file writer that would let you handle the conversion, though.


I have two questions more:

In java, I declare a table of Long for taking the values gave by glReadPixel. I do not understand if this is bytes values that are stocked in the table.You talk about convertion to Bytes. Can you explain that ?

And how can I set up the size of my table ?

Thanks a lot

At almost the lowest level, everything can be thought of as bytes. (At the absolutely lowest level is bits, but I have never seen any API that lets you write individual bits to a file before so we’ll just ignore that for now…)

A long in Java is made up of 4 bytes if I remember correctly. In C/C++ if you want to write those exact bytes to a file you do something like…

long n = 1;

fwrite(file, &n, 4);

The bytes that would get written would depend on weather the system was little-endian or big-endian. Intel/AMD chips for instance are little endian and so if you were to look at the above file in a hex editor you would see bytes like so…

Byte 0: 0x01
Byte 1: 0x00
Byte 2: 0x00
Byte 3: 0x00

On a big-endian system it would be

Byte 0: 0x00
Byte 1: 0x00
Byte 2: 0x00
Byte 3: 0x01

So now you have the problem that you are working with Java, which is strongly typed. So far as I know, you can’t just cast a long to a byte array. You can mask out the individual bytes by doing something like so…

long n = 10;
byte b0;
byte b1;
byte b2;
byte b3;
b3 = (byte)(n & 0xFF);
b2 = (byte)((n & 0xFF00) >> 8);
b1 = (byte)((n & 0xFF0000) >> 16);
b0 = (byte)((n & 0xFF000000) >> 24);

But that seems like a lot of work, so I’d look for some class that lets you do the conversion from different data types to a byte array before doing that. I would think one would have to exist.

All the stuff up to this point is mainly going to apply to writing the file format headers.

For getting the pixel data, you should be getting a byte array rather than a long array so you don’t have to do the long to byte conversions all over. RGB image formats tend to have 1 byte for each component. (e.g. if you got a byte array for 1 pixel of an RGB format, you’d have 3 bytes returned, the first R, second G, third B)

So… with all that in mind, it seems to me that writing an image file reader/writer in Java is a bit more challenging than in C/C++. If you want to attempt to do so, I’d recommend looking at a fairly simple format like TGA first. has the full specification for how the bytes should be written to a file for that and many other formats.

If you don’t feel up to trying all that yourself, there may already be libraries someone has written that you could use.

Whew… this is getting longer than I originally intended. It’s easy to see why I much prefer C/C++ to Java when it comes to low-level graphics programming…

Hi ,

thank you very much for your explanation…
it is getting more clear now…