Unable to draw to GLOffscreenAutoDrawable on Raspberry Pi 2

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Unable to draw to GLOffscreenAutoDrawable on Raspberry Pi 2

dagwaging
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.
Reply | Threaded
Open this post in threaded view
|

Re: Unable to draw to GLOffscreenAutoDrawable on Raspberry Pi 2

gouessej
Administrator
Hi

Please don't call your GLEventListener "GL", it can only cause some confusion. Please provide a SSCCE. Your code demonstrates nothing as you draw nothing in display(GLAutoDrawable). Using AWT on a Raspberry Pi isn't a very good idea, you can use GLReadBufferUtil with TextureData instead of AWTGLReadBufferUtil.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Unable to draw to GLOffscreenAutoDrawable on Raspberry Pi 2

Xerxes Rånby
In reply to this post by dagwaging
your example do not show when you call getImage.
I would recommend you to call getImage from within display(GLAutoDrawable drawable)
gl may be undefined if you call getImage outside display.
Reply | Threaded
Open this post in threaded view
|

Re: Unable to draw to GLOffscreenAutoDrawable on Raspberry Pi 2

dagwaging
In reply to this post by gouessej
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();
	}
}
Reply | Threaded
Open this post in threaded view
|

Re: Unable to draw to GLOffscreenAutoDrawable on Raspberry Pi 2

gouessej
Administrator
Your example can't work because the buffers haven't been swapped yet when you call glReadPixels. Just call canvas.display() a second time and you'll see what I mean.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Unable to draw to GLOffscreenAutoDrawable on Raspberry Pi 2

dagwaging
Calling display a second time also prints all zeros. Calling capabilities.setDoubleBuffered(false); doesn't help either.
Reply | Threaded
Open this post in threaded view
|

Re: Unable to draw to GLOffscreenAutoDrawable on Raspberry Pi 2

gouessej
Administrator
Have you looked at our unit tests?
Julien Gouesse | Personal blog | Website