#include "GL/glut.h" #include #include #include #include #define WINDSIZEX 100 #define WINDSIZEY 100 #define TEXSIZE 64 static GLfloat *buf1; // Buffers for reading screen static GLfloat *rgbFourTexture; static GLuint texImages; static GLfloat mMap[] = { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, }; static GLfloat mScale[] = { 1.0/64.0, 0.0, 0.0, 0.0, 0.0, 1.0/64.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, }; typedef struct { GLfloat s1; GLfloat t1; GLfloat s2; GLfloat t2; GLfloat r; GLfloat q; } COORD_RECT; static COORD_RECT pcrect = { -4.0, -4.0, 124.0, 124.0, 4.0, 2.0}; //static COORD_RECT pcrect = { 0.0, 0.0, 64.0, 64.0, 0, 0}; GLfloat colorMap[][3] = { { 0.0, 0.0, 0.0 }, { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } }; static void CreateFourColorTexture(void) { GLint x, y, colorIndex; GLint xmappos, ymappos; GLfloat *prgb = rgbFourTexture; for (y = 0; y < TEXSIZE; y++) { for (x = 0; x < TEXSIZE; x++) { xmappos = x / 4; ymappos = y / 4; colorIndex = (xmappos + ymappos + 1) % 4; *prgb++ = colorMap[colorIndex][0]; *prgb++ = colorMap[colorIndex][1]; *prgb++ = colorMap[colorIndex][2]; } } } void init() { glDisable(GL_DITHER); glClearColor (0.0, 0.0, 0.0, 0.0); buf1 = (GLfloat *)malloc(WINDSIZEX*WINDSIZEY*sizeof(GLfloat)*3); rgbFourTexture = (GLfloat *)malloc(TEXSIZE*TEXSIZE*3*sizeof(GLfloat)); glGenTextures(1, &texImages); glBindTexture (GL_TEXTURE_2D, texImages); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); CreateFourColorTexture (); glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, TEXSIZE, TEXSIZE, 0, GL_RGB, GL_FLOAT, rgbFourTexture); glEnable (GL_TEXTURE_2D); glMatrixMode (GL_TEXTURE); glLoadIdentity (); glLoadMatrixf (mScale); glMultMatrixf (mMap); } #define X1 4 #define Y1 4 #define X2 68 #define Y2 68 long Test () { GLint x; glReadPixels(X1, Y1, X2, Y2, GL_RGB, GL_FLOAT, buf1); for (x = 0; x < 64; x++) printf("pixel color: %f %f %f\n", buf1[x*3], buf1[x*3+1], buf1[x*3+2]); return 0; } void test(){ glClear (GL_COLOR_BUFFER_BIT); glBindTexture (GL_TEXTURE_2D, texImages); glBegin (GL_QUADS); glTexCoord4d (pcrect.s1, pcrect.t1, pcrect.r, pcrect.q); glVertex2f (X1, Y1); glTexCoord4d (pcrect.s2, pcrect.t1, pcrect.r, pcrect.q); glVertex2f (X2, Y1); glTexCoord4d (pcrect.s2, pcrect.t2, pcrect.r, pcrect.q); glVertex2f (X2, Y2); glTexCoord4d (pcrect.s1, pcrect.t2, pcrect.r, pcrect.q); glVertex2f (X1, Y2); glEnd (); /* glBegin (GL_QUADS); glTexCoord2d (pcrect.s1, pcrect.t1); glVertex2f (X1, Y1); glTexCoord2d (pcrect.s2, pcrect.t1); glVertex2f (X2, Y1); glTexCoord2d (pcrect.s2, pcrect.t2); glVertex2f (X2, Y2); glTexCoord2d (pcrect.s1, pcrect.t2); glVertex2f (X1, Y2); glEnd (); */ Test(); // exit(0); } void display() { glViewport(0, 0, WINDSIZEX, WINDSIZEY); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, WINDSIZEY, 0, WINDSIZEX); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); test(); } void main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA); glutInitWindowSize (WINDSIZEX, WINDSIZEY); glutInitWindowPosition (100, 100); glutCreateWindow ("hello"); init(); glutDisplayFunc(display); glutMainLoop(); }