Screenshot.writeToFile with wrong canvas

Hi

I tried using the built-in Screenshot utility, but with a funny result. What happened was that it wrote the wrong canvas to file…it wrote the canvas from a Java3D application I didnt even have running at the moment. So instead of getting a white triangle I got a red and yellow Java3D cube. I post my code below (taken from keyPressed event):

		case KeyEvent.VK_F2:
			{
				canvas.getContext().makeCurrent();
				try
				{
				  Screenshot.writeToFile(new File("test2.png"), 640, 480);
				}
				catch(GLException e)
				{
				  e.printStackTrace();
				}
				catch(IOException e)
				{
				  e.printStackTrace();
				}
				canvas.getContext().release();
			}
			break;

Are you 100% sure the makeCurrent call returned CONTEXT_CURRENT?

I noticed we aten’t setting up the read buffer so maybe the screenshot code read the wrong buffer and got back some stale OpenGL information. Do you have a small test case reproducing this problem? If so could you file a bug with the Issue Tracker on the JOGL home page and attach it?

I am not sure about anything…but I do know this; it happened again. Now it saved an image from a previous tested JOGL app I didnt even make myself. I will post my entire code here. I just push F2 a few times with short interval and it happens.

http://studweb.hig.no/020843/screenshot.java

Perhaps its my code, I dont know. I am new to this java ogl programming. I usually code C so all this context and threading is confusing :stuck_out_tongue:

This is no longer an issue. by putting the screenshot code in the display function this never happens. Problem solved.

I think I see what was going on – after display() was called, an implicit SwapBuffers occurred, so when you made the context current in your key listener you weren’t looking at the front buffer but the stale back buffer. Anyway, good to hear it’s working.

(topic highjack)

JOGL sources have SCreenshot.java utility to take a screenshot. Javadocs say its fastest method in opengl to take a screenshot from the scene. It uses gl.glReadPixels method to read pixel values.
https://jogl.dev.java.net/source/browse/jogl/src/classes/com/sun/opengl/util/Screenshot.java?rev=1.3&view=auto&content-type=text/vnd.viewcvs-markup

Is it valid and usable class if I should render my simple 3d model to a series of .png files, still giving me a decent framerate +30-50fps. I don’t need onscreen rendering, its fine to render to a hidden buffer and read pixels each time.

Is there some other lowlevel opengl method more suited to this?

You should be able to render to a GLPbuffer instead of a GLCanvas and use the Screenshot class in exactly the same way.