We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › CopyTexImage2D Doesn't Seem to Work
Page Index Toggle Pages: 1
CopyTexImage2D Doesn't Seem to Work (Read 869 times)
CopyTexImage2D Doesn't Seem to Work
Jul 21st, 2009, 4:46am
 
I'm trying to use glCopyTexImage2D to copy a portion of my sketch to a texture. However, I can seem to get it to do anything! I've been over the documentation many times, and I've spent a few fruitless hours googling, with no luck.

Before someone suggests it, yes, I tried doing this with a FBO. It kind of worked, except there was weird tearing all over the middle of the copied portion, such that nothing but black would show up except stuff near the edges.

Since it might be a machine issue, I'm using a C2D MBP with a Radeon X1600 graphics card, running OS X 10.5.7.

Quote:
import processing.opengl.*;
import javax.media.opengl.*;
import com.sun.opengl.util.texture.*;
GL gl;

int[] tex = { 0 };

int texSize = 512;
int quadlist;

void setup() {
 size(1024, 768, OPENGL);
 frameRate(30.0);
 
 setupGL();
}

void draw() {
 background(255);
 gl = ((PGraphicsOpenGL)g).beginGL();
 gl.glDisable(GL.GL_DEPTH_TEST);
 gl.glClear(GL.GL_COLOR_BUFFER_BIT);
 
 // move to center & rotate
 gl.glPushMatrix();
 gl.glTranslatef(width/2.0, height/2.0, 0.0);
 gl.glRotatef(frameCount, 0.0, 0.0, 1.0);
 gl.glScalef(512.0/texSize, 512.0/texSize, 1.0);
 
 // draw the outline of the rect
 gl.glColor4f(0.0, 0.0, 0.0, 1.0);
 int lw = 4;
 gl.glLineWidth(lw);
 glLine(-(texSize/2 - 10 + lw), -(texSize/2 - 10 + lw), (texSize/2 - 10 + lw), -(texSize/2 - 10 + lw));
 glLine((texSize/2 - 10 + lw), -(texSize/2 - 10 + lw), (texSize/2 - 10 + lw), (texSize/2 - 10 + lw));
 glLine((texSize/2 - 10 + lw), (texSize/2 - 10 + lw), -(texSize/2 - 10 + lw), (texSize/2 - 10 + lw));
 glLine(-(texSize/2 - 10 + lw), (texSize/2 - 10 + lw), -(texSize/2 - 10 + lw), -(texSize/2 - 10 + lw));
 
 gl.glPopMatrix();

 // bind texture
 gl.glEnable(GL.GL_TEXTURE_2D);
 gl.glBindTexture(GL.GL_TEXTURE_2D, tex[0]);
 
 // copy the screen into the texture
 gl.glCopyTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB,
                        0, 0, 512, 512, 0);
 gl.glBindTexture(GL.GL_TEXTURE_2D, 0);
 
 // draw the texture
 gl.glBindTexture(GL.GL_TEXTURE_2D, tex[0]);
 gl.glTranslatef(50.0, 50.0, 0.0);
 gl.glScalef(100.0, 100.0, 1.0);
 gl.glCallList(quadlist);

 ((PGraphicsOpenGL)g).endGL();
}

void setupGL() {
 gl = ((PGraphicsOpenGL)g).gl;
 
 // set the swap interval to eliminate screen tearing
 gl.setSwapInterval(1);
 
 quadlist = gl.glGenLists(1);
 gl.glNewList(quadlist, GL.GL_COMPILE);
 gl.glBegin(GL.GL_QUADS);
 gl.glTexCoord2f(0.0f, 0.0f);  gl.glVertex2f(-0.5f, -0.5f);
 gl.glTexCoord2f(1.0f, 0.0f);  gl.glVertex2f( 0.5f, -0.5f);
 gl.glTexCoord2f(1.0f, 1.0f);  gl.glVertex2f( 0.5f,  0.5f);
 gl.glTexCoord2f(0.0f, 1.0f);  gl.glVertex2f(-0.5f,  0.5f);
 gl.glEnd();
 gl.glEndList();
 
 gl.glGenTextures(1, tex, 0);    
 gl.glBindTexture(GL.GL_TEXTURE_2D, tex[0]);
 gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
 gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
 gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
 gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
 gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB,
                 texSize, texSize, 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, null);
                 
 gl.glClearColor(1.0, 1.0, 1.0, 1.0);
}

void glLine(float x1, float y1, float x2, float y2) {
 gl.glBegin(GL.GL_LINES);
 gl.glVertex2f(x1, y1);
 gl.glVertex2f(x2, y2);
 gl.glEnd();
}


Can anyone figure out why this isn't working?
Re: CopyTexImage2D Doesn't Seem to Work
Reply #1 - Oct 4th, 2009, 12:35pm
 
Have you solved this already?

Just set the color to white before drawing the quad:

Code:
gl.glScalef(100.0, 100.0, 1.0);
gl.glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
gl.glCallList(quadlist);
Page Index Toggle Pages: 1