PDA

View Full Version : Magnified Textures & Reading Pixels



endlosschleife
12-01-2010, 08:27 AM
Hi,

I would like to magnify an 2D image off-screen and read back the pixels of the magnified image. I'm not sure if I'm on the right track, I'm doing the following. What happens is that I get back the original sized image (as if it's the original texture) from glReadPixels. The scaling works if I'm running the same display code on-screen. Is this the right approach, if so, any idea what I'm doing wrong? Thank you!

- Mark

Initialization:

// create FBO (offscreen framebuffer)
glGenFramebuffersEXT(1, &fb);
// bind offscreen buffer
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);

glGenRenderbuffersEXT(1, &depthbuffer);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthbuffer);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, 512, 512);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthbuffer);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

Loading / Display:

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex, 0);

glPushMatrix();
glScalef(2.0, 2.0, 1.0);

glBindTexture(GL_TEXTURE_2D, tex);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(-1, -1);
glTexCoord2f(1, 0);
glVertex2f(1, -1);
glTexCoord2f(1, 1);
glVertex2f(1, 1);
glTexCoord2f(0, 1);
glVertex2f(-1, 1);
glEnd();

glPopMatrix();

Readback:

glReadBuffer( (GLenum) GL_COLOR_ATTACHMENT0_EXT );
glReadPixels(0, 0, 512, 512, GL_RGB, GL_UNSIGNED_BYTE, data);