Login  Register

Re: Unable to draw to GLOffscreenAutoDrawable on Raspberry Pi 2

Posted by dagwaging on Nov 04, 2015; 2:30pm
URL: https://forum.jogamp.org/Unable-to-draw-to-GLOffscreenAutoDrawable-on-Raspberry-Pi-2-tp4035723p4035731.html

Here's a more direct example.

Expected behavior: prints "gl color: ffffffff"
Actual behavior: prints "gl color: 00000000"

For context, I'm trying to render an image and then png compress it for network transport.
The simplest way I know of to do so is to use AWTGLReadBufferUtil to create a BufferedImage, then use ImageIO to write it to the HTTP stream. GLReadBufferUtil doesn't produce a BufferedImage, so that doesn't work for my use case.

import java.nio.Buffer;
import java.nio.ByteBuffer;

import com.jogamp.opengl.GL2ES2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLDrawableFactory;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLOffscreenAutoDrawable;
import com.jogamp.opengl.GLProfile;

public class Test {
	private static final int WIDTH = 400;
	private static final int HEIGHT = 400;

	public static void main(String[] args) {
		GLProfile profile = GLProfile.get(GLProfile.GL2ES2);
		GLCapabilities capabilities = new GLCapabilities(profile);

		GLOffscreenAutoDrawable canvas = GLDrawableFactory.getFactory(profile).createOffscreenAutoDrawable(null,
				capabilities, null, WIDTH, HEIGHT);

		canvas.addGLEventListener(new GLEventListener() {
			@Override
			public void init(GLAutoDrawable drawable) {
				drawable.getContext().makeCurrent();
			}
			
			@Override
			public void display(GLAutoDrawable drawable) {
				GL2ES2 gl = drawable.getGL().getGL2ES2();
				gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
				gl.glClear(GL2ES2.GL_COLOR_BUFFER_BIT);

				byte[] data = new byte[4];
				Buffer pixels = ByteBuffer.wrap(data);
				gl.glReadPixels(0, 0, 1, 1, GL2ES2.GL_RGBA, GL2ES2.GL_BYTE, pixels);

				System.out.println(String.format("gl color: \t%02x%02x%02x%02x", data[3], data[0], data[1], data[2]));			
			}
			
			@Override
			public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
			}
			
			@Override
			public void dispose(GLAutoDrawable drawable) {
			}
		});

		canvas.display();
	}
}