void render(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f( 0, 255, 0 ); //color it green
glVertex3f( -100, 0, 100 );
glVertex3f( -100, 0, -100 );
glVertex3f( 100, 0, -100 );
glVertex3f( 100, 0, 100 );
glEnd();
int tmpx, tmpy;
GLint pixel_color[3];
SDL_GetMouseState(&tmpx, &tmpy);
glReadPixels(tmpx, win_width - tmpy, 1, 1, GL_RGB, GL_INT , &pixel_color[0]);
printf("PIXEL: %d - %d - %d\n", pixel_color[0], pixel_color[1], pixel_color[2]);
SDL_GL_SwapBuffers();
}
使用上面的渲染代码,我试图获得鼠标指向的像素的rgb颜色。该平面的颜色为全绿色(255),但glReadPixels返回的值为2147483647而不是255.
我已经尝试过更改类型,但它从未返回我期望的值。如何让它返回glColor设置的相同值?
答案 0 :(得分:4)
更改
GLint pixel_color[3];
到
GLubyte pixel_color[3];
并更改
glReadPixels(tmpx, win_width - tmpy, 1, 1, GL_RGB, GL_INT , &pixel_color[0]);
到
glReadPixels(tmpx, win_width - tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_color[0]);