Login  Register

Unable to draw to GLOffscreenAutoDrawable on Raspberry Pi 2

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

I'm trying to create a console application to do some shader rendering, and I'm a bit rusty on my OpenGL so this might just be a stupid mistake on my part, but I can't get anything besides black to render on my GLOffscreenAutoDrawable. Even checking the pixel color immediately after clearing the canvas to white results in black, either I'm not getting the pixel values correctly or I'm not drawing correctly and I can't figure out which. Using AWTGLReadBufferUtil to get a BufferedImage also results in a black image, which leads me to believe that the latter problem is occurring.

Here's my code, just to be sure:

import java.awt.image.BufferedImage;
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;
import com.jogamp.opengl.util.awt.AWTGLReadBufferUtil;

public class GL implements GLEventListener {
	private int width;
	private int height;
	private GL2ES2 gl;

	public GL(int width, int height) {
		this.width = width;
		this.height = height;
		
		GLProfile profile = GLProfile.get(GLProfile.GL2ES2);
		GLCapabilities capabilities = new GLCapabilities(profile);
		capabilities.setDoubleBuffered(false);
		capabilities.setOnscreen(false);
		capabilities.setAlphaBits(8);

		GLOffscreenAutoDrawable canvas = GLDrawableFactory.getFactory(profile).createOffscreenAutoDrawable(null,
				capabilities, null, width, height);

		System.out.println(canvas.getChosenGLCapabilities());


		canvas.addGLEventListener(this);
		canvas.display();
	}
	
	public int getWidth() {
		return width;
	}
	
	public int getHeight() {
		return height;
	}

	public BufferedImage getImage() {
		AWTGLReadBufferUtil util = new AWTGLReadBufferUtil(gl.getGLProfile(), true);
		BufferedImage result = util.readPixelsToBufferedImage(gl, true);
		return result;
	}

	@Override
	public void init(GLAutoDrawable drawable) {
		drawable.getContext().makeCurrent();
	}

	@Override
	public void display(GLAutoDrawable drawable) {
		gl = drawable.getGL().getGL2ES2();
		gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
		gl.glClear(GL2ES2.GL_COLOR_BUFFER_BIT | GL2ES2.GL_DEPTH_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 dispose(GLAutoDrawable drawable) {
	}

	@Override
	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
	}
}

Many thanks for any help.