Binding LWJGL and Swing/AWT (easily)

Started by gregorypierce, January 21, 2005, 05:00:07

Previous topic - Next topic

markush

Quote from: "gregorypierce"I expect the next code update will be the final and we'll have something that will allow Swing and actually browser compatibility.

Any news on this?
I'd like to switch from JoGL to lwjgl, but without swing integration I can't...

gregorypierce

Its coming. Got distracted by a couple of OSX bugs that needed fixin. Done this week for certain :)

Mattbooth

Did this get finished?

I'd like to import aLWJGL windown into some swing components if possible?

Cheers

princec

We've got AWT integration now if that'll do you. Should work perfectly well if you use it carefully.

Cas :)

Mattbooth

Hi cas

Is there a link somwhere mate?

princec

It's currently just in the CVS, but a release is imminent.

Cas :)

Mattbooth

But i'm guessing not in the next week or so when i could do with it  :shock:

How do you get it out of the CVS?  (probable dumb question i know)

Thanks

Skippy0

I would strongly advice not to use BufferedImage.setRGB(), instead
use MemoryImageSource.newPixels(...) backed by a int[].

Just check setRGB() and what an endless sequence of conversions
are done. It's just dead-slow.

// init

int width = 256;
int height = 512;
int pixels = width * height;
int bytesPerPixel = 3; // RGB

ByteBuffer frameData = BufferUtils.createByteBuffer(pixels*bytesPerPixel);
byte[] pixBytes = new byte[pixels*bytesPerPixel];
int[] pix = new int[pixels];
MemoryImageSource sourceImage = new MemoryImageSource(width, height, pix, 0, width);
sourceImage.setAnimated(true);
Image image = createImage(sourceImage);

// every frame

GL.read....(frameData);
frameData.rewind();
frameData.get(pixBytes, 0, pixBytes.length);

for(int i=0, j=0; i<pix.length; i++)
{
    int c = 0xFF << 24;
    c |= (pixBytes[j++] & 0xFF) << 16;
    c |= (pixBytes[j++] & 0xFF) << 8;
    c |= (pixBytes[j++] & 0xFF) << 0;
   pix[i] = c;
}

imageSouce.newPixels();

g.drawImage(image, 0, 0, null);


Cannot use the IntBuffer as we read only 24bits per pixel.

What are your opinions?

Mattbooth

Does anyone have some simple example like drawing a cube using the awt code coz i cant make head nor tails of it.....

renanse

In jME we don't use setRGB either, we create our BufferedImage and then directly access the DataBuffer of the Raster...  which can be manipulated as an array of ints...

int[] ibuf = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();


Our data is grabbed from an IntBuffer that is populated from the GL thread (throttled though to only grab every X ms) using:
   public void grabScreenContents(IntBuffer buff, int x, int y, int w, int h) {
        GL11.glReadPixels(x, y, w, h, GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE,
                        buff);
    }


Then it's a simple matter to copy from the IntBuffer to the int array backing the BufferedImage:
buf.clear(); // Note: clear() resets marks and positions, 
							 //       but not data in buffer.
				//Grab pixel information and set it to the BufferedImage info.
				for (int x = height; --x >= 0; ) {
					buf.get(ibuf, x * width, width);
				}


You go on to draw the image as normal.

The copying portions of this code are extremely fast, the slow part is just glReadPixels, which we throttle to only happen a certain number of times per second...  There's no real reason to do it any faster than screen refresh rate for example.

That all said, I'm anxious to see .96 lwjgl with it's direct integration into AWT (or so I've heard.)

jjones7947

greg wrote:
Quote
I expect the next code update will be the final and we'll have something that will allow Swing and actually browser compatibility.

Did this ever go anywhere?
Jim

princec

AWTGLCanvas does everything nicely now. Try it out. Just remember it's a heavyweight component.

Cas :)

rainforest

Dear Cas

Please give me the location of the download.

rainforest.
.:: A journey of thousand miles starts with a single step ::.