#include <windows.h>
#include <gl\glew.h>
#include <gl\glut.h>
#include <gl\freeglut_ext.h>
#include <iostream>
#include <stdio.h>
//helpers
void renderScene(void)
{
glClear (GL_COLOR_BUFFER_BIT );
glBegin(GL_QUADS);
glColor3f(1,0,0);
glVertex2f(10 , 10);
glVertex2f(10, 512 +10);
glVertex2f(512 + 10 , 512 +10 );
/** glReadPixels will work if a solid color is used on this quad,
* so commenting out folowing line will give correct behaviour */
glColor3f(0,1,0);
glVertex2f(512 + 10 , 10);
glEnd();
glFlush();
}
void changeSize(GLsizei w, GLsizei h)
{
glViewport(0,0,w,h);
//Reset the coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w , 0, h , 1,-1);
glMatrixMode(GL_MODELVIEW );
glLoadIdentity();
}
bool writeTGA(const std::string & filename, unsigned char * texture, unsigned int width, unsigned int height, unsigned int bpp)
{
FILE *file = fopen(filename.c_str(), "w");
if( file == NULL)
return false;
GLubyte header[] = {
00,00,02, 00,00,00, 00,00,00, 00,00,00,
0xff & width, 0xff & width >> 8,
0xff & height, 0xff & height >> 8,
bpp, 0x20
};
fwrite( header, sizeof(header), 1, file);
fwrite(texture, width * height * bpp / 8, 1, file);
fclose(file);
return true;
}
//main
int main(int argc, char ** argv)
{
unsigned int windowW = 800;
unsigned int windowH = 600;
int windowId = 0;
std::cout << "ReadPixels Test" << std::endl;
std::cout << "-----------------------------\n" << std::endl;
//Setup FreeGlut
int tmpArg = 0;
glutInit(&tmpArg, NULL);
// Note: glutSetOption is only available with freeglut
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(windowW, windowH);
windowId = glutCreateWindow("glReadPixels Test");
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutReshapeFunc(changeSize);
//setup some opengl states
glClearColor (0.5, 0.5, 0.5, 1.0);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
//adjust window size
changeSize(windowW, windowH);
//main loop
int counter = 200;
while(counter--)
{
glutPostRedisplay();
glutMainLoopEvent();
glutSwapBuffers();
if(50 == counter )
{
unsigned char * texture = (unsigned char *)malloc(windowW * windowH * 3);
memset(texture, 0x00, windowW * windowH * 3);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, windowW , windowH, GL_BGR, GL_UNSIGNED_BYTE, texture);
//write pixels to file
writeTGA("testout.tga", texture, windowW, windowH, 24);
free(texture);
}
Sleep(25);
}
return 0;
}