#include #include #include #include #define WINDSIZEX 100 #define WINDSIZEY 100 static void RenderPointPolygon (GLfloat xoff, GLfloat yoff, GLfloat texScale) { glBegin (GL_POLYGON); glTexCoord2f (0.0, 0.0); glVertex2f (xoff + 0.0, yoff + 0.0); glTexCoord2f (texScale, 0.0); glVertex2f (xoff + 1.0, yoff + 0.0); glTexCoord2f (texScale, texScale); glVertex2f (xoff + 1.0, yoff + 1.0); glTexCoord2f (0.0, texScale); glVertex2f (xoff + 0.0, yoff + 1.0); glEnd (); } static void CreateTexture (GLint width, GLint height) { GLfloat *buf, *buf0; GLint i, j; buf = buf0 = malloc (3 * width * height * sizeof (GLfloat)); for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { *buf++ = 1.0; *buf++ = 1.0; *buf++ = 0.0; } } glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_FLOAT, buf0); free (buf0); } void init(void) { glDisable (GL_DITHER); glBindTexture (GL_TEXTURE_2D, 1); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glEnable(GL_TEXTURE_2D); } static void test(void) { GLint level = 6; GLfloat buf[18]; GLint width = 1; GLint height = 1; GLint i; glClear (GL_COLOR_BUFFER_BIT); CreateTexture (width, height); glClear (GL_COLOR_BUFFER_BIT); for (i = 1; i < level; i++) { RenderPointPolygon (i, 1.0, 1.0 / i); } glReadPixels(1, 1, level, 1, GL_RGB, GL_FLOAT, buf); for (i = 1; i <= level; i++) { if ( buf[3*i-3] != 1.0 || buf[3*i-2] != 1.0 || buf[3*i-1] != 0.0 ) { printf("draw pixel : %f %f %f\n", buf[3*i-3], buf[3*i-2], buf[3*i-1]); printf("draw pixel should be: 1.0, 1.0, 0.0 \n"); exit -1; } } } void display(void) { glViewport(0, 0, WINDSIZEX, WINDSIZEY); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, WINDSIZEX, 0, WINDSIZEY); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); test(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(WINDSIZEX, WINDSIZEY); glutInitWindowPosition(100, 100); glutCreateWindow(argv[0]); init(); glutDisplayFunc(display); glutMainLoop(); return 0; }