#include <windows.h>
#include <gl\GL.h>
#include <glut.h>
#include <gl\glu.h>
#include <stdio.h>
void DrawRegion();
void myInit(void);
void myDisplay(void);
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("test");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
DrawRegion();
//glFlush();
glutSwapBuffers();
}
void DrawRegion(void)
{
GLubyte Pixel = 0;
glColor3ub(0, 0, 0);
//for(GLint i = 500; i > 100; i--)
//{
// glBegin(GL_POINTS);
// glVertex2i(i, 50);
// glEnd();
// glReadPixels(i + 1, 50, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &Pixel);
// printf("%d ", Pixel);
//}
glBegin(GL_POINTS);
glVertex2i(50, 50);
glEnd();
glReadPixels(50, 50, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &Pixel);
printf("%d ", Pixel);
glReadPixels(49, 50, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &Pixel);
printf("%d ", Pixel);
glReadPixels(50, 49, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &Pixel);
printf("%d ", Pixel);
glReadPixels(49, 49, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &Pixel);
printf("%d ", Pixel);
glFlush();
}
This is my test code. I get the result “255 0 255 255” on my computer.In the past, I considered glReadPixels(50, 50, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &Pixel) get the value of pixel(50,50).
But, according to the result, glReadPixels(49, 50, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &Pixel) get the value of pixel(50,50).