gl.glReadPixels(...) and GL.GL_COLOR_INDEX

I´m working with game and need to use swing components on top of GLCanvas.
(or at least somehow show swing components on top of GLCanvas).

I´am using gl.glReadPixels(…) to read the framebuffer and then painting the framebuffer´s data
as image to JPanel´s background. Unfortunately glReadPixels(…) is too slow to be used with each game
frame if it´s called with GL.GL_RGB. It´s fast if its called with GL_COLOR_INDEX but I don´t know how to use
data obtained with GL_COLOR_INDEX.

Questions:
Well, what is that color index?
Is it possible to get RGB value from color index?

Use GL_RGBA and GL_UNSIGNED_INT_8_8_8_8_REV as is done in the GLJPanel sources. That should be a bit faster although not perfect. Watch this forum for a major announcement within the next week and a half about huge speed improvements doing the kind of operations you’re interested in.

Thanks, GL_RGBA and GL_UNSIGNED_INT_8_8_8_8_REV seems to be as fast as GL_RGB & GL_UNSIGNED_BYTE & Bytebuffer combination. That´s good but it still slow when compared to GL_COLOR_INDEX. If using GL_COLOR_INDEX and passing integer array to glReadPixels(…) it fills that array completely with zeros (or it does nothing with that array).
I found this kind of info about GL_COLOR_INDEX:

GL_COLOR_INDEX
> Color indexes are read from the color buffer selected by glReadBuffer. Each index is converted to fixed point,
> shifted left or right, depending on the value and sign of GL_INDEX_SHIFT, and added to GL_INDEX_OFFSET.
> If GL_MAP_COLOR is GL_TRUE, indexes are replaced by their mappings in the table GL_PIXEL_MAP_I_TO_I.

…but can´t understand what it means or how it is related to RGB.

Color index mode assumes you’re working with a color palette rather than a true color visual / pixel format, and JOGL doesn’t support anything aside from true color.

OK.

I also tried this kind of workarounds:

  1. Put JPanel on top of GLCanvas so that they are overlapping
    using OverlayLayout. JPanel has transparent background, panel.setOpaque(false).
    So far only the GLCanvas shows up.

  2. Trying to paint to GLCanvas using Graphics-object: canvas.getGraphics();
    Problem is that drawing done with Graphics object does not alter framebuffer,
    so all paintings must be done after swapBuffers() is called and that causes
    heavy flickering. I tried also to use bufferstrategy with GLCanvas calling canvas.createBufferStrategy(2),
    and filled the bufferstrategy´s backbuffer with transparent color and after that performed actual
    Java2D drawings. This only made things worse, ´cause JOGL´s and bufferstrategy´s backbuffers are separate,
    calling canvas.swapBuffers() and then canvas.getBufferStrategy().show() resulted visible overpainting
    and flickering.

Not very successfull …yet :slight_smile:

You need to use the GLJPanel if you want to interoperate with Java2D. Currently it is pretty slow even when the pbuffer-based hardware acceleration is working, but that will improve soon.

I use these for GLCanvas:

glCanvas.setNoAutoRedrawMode(true);
glCanvas.setAutoSwapBufferMode(false);

Calling setAutoSwapBufferMode(false) for GLJPanel just throws NullPointerException.

Btw. I downloaded JOGL sources from https://jogl.dev.java.net/files/documents/27/17108/jogl-src.zip
but those sources seems not be complete 'cause there are no sources for ex. GL and GLU classes.

You can’t place lightweight widgets on top of heavyweight ones (this is documented in the JOGL User’s Guide and elsewhere) and any attempts to use the Graphics object from the GLCanvas are inherently non-portable. If you want to mix Java2D and JOGL you need to use the GLJPanel instead of the GLCanvas.

You also need to use Ant to build the JOGL sources; GL.java, GLU.java, etc. are autogenerated during the build process.