I would like to copy the image currently drawn onscreen into a texture. It seems like glCopyTexSubImage2D is the best way to accomplish this, but it doesn’t seem to be working.
I’m trying to get this to work first in a simple case. At the first frame I simply draw a red circle in the corner of the screen and store the current frame in a texture. In subsequent frames I draw the stored texture, then draw another red circle at an offset which increases with time, and then copy the current frame back to the texture.
If this were working correctly then I would expect to see all of the red circles which had been drawn in all earlier frames, which would show up as a line which gets longer as the animation proceeds. Instead I simply see a red circle marching across the screen, which seems to indicate that I am not correctly storing the frame data into my texture.
I would very much appreciate any help that anyone could offer.
I’m writing in Java using JOGL, and the relevant code is as follows:
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glDisable(GL2.GL_DEPTH_TEST);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0.0, width, 0.0, height, 0.0, 1.0);
gl.glViewport(0, 0, width, height);
gl.glGenTextures(1, frameTexture, 0);
}
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
GLU glu = new GLU();
GLUquadric quad = glu.gluNewQuadric();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
if(frameNumber > 0) {
// draw the texture stored in frameTexture
gl.glEnable(GL2.GL_TEXTURE_2D);
gl.glBindTexture(GL.GL_TEXTURE_2D, frameTexture[0]);
gl.glBegin(GL2.GL_QUADS);
gl.glBegin(GL2.GL_QUADS);
gl.glTexCoord2f(0, 0);
gl.glVertex2d(0, 0);
gl.glTexCoord2f(width, 0);
gl.glVertex2d(width, 0);
gl.glTexCoord2f(width, height);
gl.glVertex2d(width, height);
gl.glTexCoord2f(0, height);
gl.glVertex2d(0, height);
gl.glEnd();
}
// draw a red circle
double[] oldColor = new double[4];
gl.glGetDoublev(GL2.GL_CURRENT_COLOR, oldColor, 0);
gl.glColor3dv(new double[] {1.0, 0.0, 0.0}, 0);
gl.glPushMatrix();
gl.glTranslated(frameNumber, frameNumber, 0);
glu.gluDisk(quad, 0, 15, 20, 2);
gl.glPopMatrix();
gl.glColor3dv(oldColor, 0);
// update frameTexture with the current frame buffer
gl.glEnable(GL2.GL_TEXTURE_2D);
gl.glBindTexture(GL2.GL_TEXTURE_2D, frameTexture[0]);
gl.glCopyTexSubImage2D(GL2.GL_TEXTURE_2D, 0, 0, 0, 0, 0, width, height);
frameNumber++;
}