#include #include #include #include #define WINDSIZEX 100 #define WINDSIZEY 100 void init(void) { GLfloat blackColor[4] = {0.0, 0.0, 0.0, 1.0}; GLfloat whiteColor[4] = {1.0, 1.0, 1.0, 1.0}; glDisable (GL_DITHER); glEnable (GL_LIGHT0); //LIGHT0 diffuse = WHITE glEnable (GL_LIGHTING); glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT, blackColor); glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, blackColor); glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, whiteColor); } static void test(void) { GLfloat buf[4]; GLfloat *allbuf; GLfloat vbuf[] = {5.5, 6.5, 5.5, 12.5, 10.5, 6.5, 10.5, 12.5}; GLfloat position[4] = {0, 0.6, 0.8, 0}; GLfloat nbuf[][3] = {{0.3, 0.4, 0.7}, {0.3, 0.4, 0.7}, {0.3, 0.4, 0.7}, {0.3, 0.4, 0.7}}; glPolygonMode (GL_FRONT_AND_BACK, GL_POINT); glVertexPointer (2, GL_FLOAT, 0, vbuf); glEnableClientState (GL_VERTEX_ARRAY); glEnableClientState (GL_NORMAL_ARRAY); glNormalPointer (GL_FLOAT, 0, nbuf); glLightfv (GL_LIGHT0, GL_POSITION, position); glClear (GL_COLOR_BUFFER_BIT); glDrawArrays (GL_QUAD_STRIP, 0, 4); allbuf = malloc(WINDSIZEX * WINDSIZEY * sizeof(GLfloat) * 3); glReadPixels(0, 0, WINDSIZEX, WINDSIZEY, GL_RGB, GL_FLOAT, allbuf); glReadPixels(5.5, 6.5, 1, 1, GL_RGBA, GL_FLOAT, buf); // pixel should be 0*0.3+0.6*0.4+0.8*0.7=0.8 printf("draw pixel at (5.5, 6.5): %f %f %f, it should be 0.8\n", buf[0], buf[1], buf[2]); } 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; }