Hi,
Ive been playing around with some code to create a Java2D foreground layer for my GLCanvas, and I have now successfully been able to update the Java2D texture for each frame in acceptable frame-rate (by not creating new BufferedImage, Texture or TextureData for each frame). The only problem is that the texture is upside down, and I cant figure out why this is happening. Probably some silly error, but I cant seem to find it. ???
I use the following code to create the texture:
int width = 200;
int height = 200;
image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
//Paint some stuff on the texture.
Graphics2D g2 = (Graphics2D) image.getGraphics();
g2.setColor(new Color(0.0f, 0.0f, 0.0f, 0.5f));
g2.fillRect(0, 0, width, height);
g2.setColor(new Color(1.0f, 0.0f, 0.0f, 1.0f));
g2.fillRect(10, 10, 180, 50);
//Create Buffer from the BufferedImage
imageBuffer = ByteBuffer.allocateDirect(4 * width * height);
byte data[] = (byte[]) image.getRaster().getDataElements(0, 0, image.getWidth(), image.getHeight(), null);
imageBuffer.clear();
imageBuffer.put(data, 0, data.length);
imageBuffer.rewind();
//Create TextureData from the Buffer
texdata = new TextureData(GL.GL_RGB, width, height, 0, GL.GL_BGRA, GL.GL_UNSIGNED_BYTE, false, false, false, imageBuffer, null);
//Create Texture from the TextureData
//texdata = TextureIO.newTextureData(image, false);
texture = TextureIO.newTexture(texdata);
texture.setTexParameteri(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
texture.setTexParameteri(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
I suspect it has something to do with the following line:
texdata = new TextureData(GL.GL_RGB, width, height, 0, GL.GL_BGRA, GL.GL_UNSIGNED_BYTE, false, false, false, imageBuffer, null);
Very glad if somebody has any pointers to what might be wrong, I’ve attached a screenie, and the class where all this neat action is taking place.
BTW, the code is somewhat based on the PhotoCube app created by Chris Campbell (http://weblogs.java.net/blog/campbell/archive/2006/10/easy_2d3d_mixin.html), which is a nice demo of the GLJPanel.
EDIT: The screenie shows the quad with the Java2D texture, and a rotating triangle in the back, might be difficult to see…