PDA

View Full Version : Is there any way to obtain the data by OpenGL?



Rong Yao
03-20-2007, 12:11 PM
When we ask OpenGL to do texture mapping, we can ask OpenGL to interpolate the original data to smooth the texture before mapping. My question is, is the any way to obtain the interpolated data? Thank you.

vbovio
03-20-2007, 01:16 PM
the only way I know to do that, is draw it on a textured quad (2D) and read the data with glReadPixels. Don't know if there is a better way.

Rong Yao
03-20-2007, 02:32 PM
How to draw a textured 2D quad then? How to get the window coordinates and width and height? Could you give some example? Many many thanks.

vbovio
03-20-2007, 05:17 PM
drawing a 2d texture quad needs some steps, first you need to setup your view to 2D (you may have to backup your previous view), then draw the quad, then read the image data, and finally restore your previous view, I'll try to give you some pseudo code, (it is not tested, it is for you to get the idea).


glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, myViewportWidth, 0, myViewportHeight);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
// use GL_LINEAR for smooth drawing
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

int x = 0;
int y = 0;
int w = myBitmapWidth;
int h = myBitmapHeight;

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, myBitmapData);

glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2d(x, y);
glTexCoord2f(1.0f, 0.0f);
glVertex2d(x + w, y);
glTexCoord2f(1.0f, 1.0f);
glVertex2d(x + w, y + h);
glTexCoord2f(0.0f, 1.0f);
glVertex2d(x, y + h);
glEnd();

glDisable(GL_TEXTURE_2D);

glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, myNewData);

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();

Rong Yao
03-21-2007, 02:46 PM
Got the idea. Thank you very much!