Hi,
Let me explain what I am trying to do here. I am creating a quad over which I am mapping a texture image. After performing this task, I want to store the textured image back into the system memory. I am using glReadPixels for reading back the textured image from the framebuffer. But I am not able to read the appropriate values. When I read back the textured image, all the values are set to zero and nothing seems to work. I have attached the source code below.
Please help.
##################################################
#define YSCREEN 100
#define XSCREEN 100
#define WIDTH 2
#define HEIGHT 2
float Texture[HEIGHT][WIDTH][4];
GLuint Tex;
float RTexture[XSCREEN * YSCREEN * 4];
void GLUTInit (int*, char**);
void Init (void);
void Display (void);
void Draw (void);
void MakeTexture (void);
void MapTexture (void);
void ReadTexture (void);
int main (int argc, char** argv)
{
GLUTInit (&argc, argv);
Init ();
glutDisplayFunc (Display);
glutMainLoop ();
return 0;
}
void GLUTInit (int* argc, char** argv)
{
glutInit (argc, argv);
glutInitWindowSize (YSCREEN, XSCREEN);
glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA);
glutCreateWindow (“Texture Example”);
}
void Init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glClearDepth (1.0f);
MakeTexture ();
MapTexture ();
}
void Display (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable (GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, Tex);
Draw ();
}
void Draw (void)
{
glLoadIdentity ();
glViewport (0, 0, YSCREEN, XSCREEN);
glOrtho (0, YSCREEN, 0, XSCREEN, 0, 1);
glColor3f (0.0, 1.0, 1.0);
glBegin (GL_QUADS);
glTexCoord2f (0, 0);
glVertex2f (0, 0);
glTexCoord2f (1, 0);
glVertex2f (YSCREEN, 0);
glTexCoord2f (1, 1);
glVertex2f (YSCREEN, XSCREEN);
glTexCoord2f (0, 1);
glVertex2f (0, XSCREEN);
glEnd ();
glFinish ();
ReadTexture ();
glutSwapBuffers ();
}
void MakeTexture (void)
{
int i, j, c;
c = 0;
for # loop over i for HEIGHT
{
for # loop over j for WIDTH
{
Texture[i][j][0] = c;
Texture[i][j][1] = c;
Texture[i][j][2] = c;
Texture[i][j][3] = c;
c++;
}
}
}
void MapTexture (void)
{
glGenTextures (1, &Tex);
glBindTexture (GL_TEXTURE_2D, Tex);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, WIDTH, HEIGHT, 0, GL_RGBA, GL_FLOAT, Texture);
}
void ReadTexture (void)
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glReadBuffer (GL_BACK);
glReadPixels (0, 0, YSCREEN, XSCREEN, GL_RGBA, GL_UNSIGNED_BYTE, RTexture);
}
##################################################