Hi,
I found a very strange problem when applying a texture onto a quad.
First I generate a texture in the initialize function:
//read data from convert.img
read_img("convert.img", texture_data, 256, 256);
// create opengl texture object
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexImage2D(GL_TEXTURE_2D, 0, 1, 256, 256, 0, GL_LUMINANCE, GL_FLOAT, texture_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Then in my reshape function, I set the projection matrix:
void myReshape(int width, int height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-256,256,256,-256, -4000, 4000);
glMatrixMode(GL_MODELVIEW);
}
and in my display function, I draw a quad and applied a texture onto it. To test whether what I did is right, I read the values from the framebuffer using glReadPixels:
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0,0,0,0);
glLoadIdentity();
glTranslatef(0,0,-1000.1f);
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, id);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(-128,-128,0);
glTexCoord2f(1,0); glVertex3f(128,-128,0);
glTexCoord2f(1,1); glVertex3f(128,128,0);
glTexCoord2f(0,1); glVertex3f(-128,128,0);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
glutSwapBuffers();
int nWidth = 512;
float* pImage = new float[nWidth*nWidth];
glReadPixels(0,0, nWidth, nWidth, GL_LUMINANCE, GL_FLOAT, pImage);
imdebug("lum b=32f w=%d h=%d %p", nWidth, nWidth, pImage);
delete[] pImage;
}
But the data retrieved from the framebuffer is not the one displayed in the window. It is just similar with the image displayed in the window.
This is the image displayed in the window:
This is the image retrevied from the framebuffer:
I have tried to solve this problem for two days, but I found nothing wrong with my program
I’ll be appreciated for any suggestions!
Thank you!