Login  Register

Re: Implement AWT in JOGL

Posted by gouessej on Feb 23, 2016; 10:22am
URL: https://forum.jogamp.org/Implement-AWT-in-JOGL-tp4036268p4036326.html

Hi

At first, create a direct NIO buffer (3 bytes per pixel : red, green and blue):
ByteBuffer buffer = Buffers.newDirectByteBuffer(drawable.getSurfaceWidth() * drawable.getSurfaceHeight() * 3);

Then, create the TextureData object:
TextureData texData = new TextureData(drawable.getGLProfile(), GL.GL_RGB, drawable.getSurfaceWidth(), drawable.getSurfaceHeight(), 0, GLPixelBuffer.GLPixelAttributes.convert(drawable.getGL(), 3, false), false, false, false, buffer, null);

After that, use those lines of code to set a RGB value:
https://github.com/gouessej/Ardor3D/blob/9b8b30718a8b82221d1a75e4dce23dea8a19836c/ardor3d-core/src/main/java/com/ardor3d/image/util/ImageUtils.java#L346

Create the texture from the texture data:
http://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/opengl/util/texture/TextureIO.html#newTexture(com.jogamp.opengl.util.texture.TextureData)

Call texture.bind() so that OpenGL knows which texture is in use and use glTexCoord to pass the texture coordinates with the vertex coordinates, look at the glVertex calls and add your glTexCoord calls:
https://gist.github.com/gouessej/3420e2b6f632efdddf98

Finally, update the texture at runtime, use glTexSubImage2D:
https://github.com/gouessej/Ardor3D/blob/9b8b30718a8b82221d1a75e4dce23dea8a19836c/ardor3d-jogl/src/main/java/com/ardor3d/renderer/jogl/JoglRenderer.java#L628

N.B: minimize the surface of update, don't always update the whole texture. If the performance isn't satisfying, you'll need something much more complicated...

Sorry, it's not trivial but it gives you everything required to port your code from AWT to JOGL.
Julien Gouesse | Personal blog | Website