2
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 设置的相同值?

4

1 回答 1

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]);
于 2012-12-17T08:36:05.000 回答