我正在使用屏幕外帧缓冲在Mac OS X上对OpenGL进行单元测试。 当我表演时
glClearColor(1, 0, 0.5, 0.5);
glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
,它为每个像素提供0xFF008080
而不是0xFF007F7F
。令人困惑的是为什么这个结果出来了。我唯一可以弄清楚的是颜色校准,但在这种情况下,我不知道如何处理这个以期望正确的结果。 (甚至,正确的结果是什么?校正颜色?或原始颜色?)
这是我的问题。
以下是完整的源代码供参考。
- (void)test_1000_createContext
{
NSOpenGLPixelFormatAttribute pfa[1] = { 0 };
NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:pfa];
@autoreleasepool
{
NSOpenGLContext* GL = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
[GL makeCurrentContext];
{
ASS (GL != nil);
GLuint fb;
GLuint rbc;
glGenFramebuffers(1, &fb);
glGenRenderbuffers(1, &rbc);
ASS (fb !=0 );
ASS (rbc != 0);
GLint w, h;
w = 2;
h = 2;
glBindRenderbuffer(GL_RENDERBUFFER, rbc);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, w, h);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbc);
glClearColor(1, 0, 0.5, 0.5);
glClear(GL_COLOR_BUFFER_BIT);
glFinish();
uint8_t* buffer = malloc(4*w*h);
glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
NSData* bdat = [[NSData alloc] initWithBytesNoCopy:buffer length:4*w*h freeWhenDone:YES];
NSLog(@"%@", bdat);
uint8_t answer[] =
{
0xFF, 0x00, 0x7F, 0x7F,
0xFF, 0x00, 0x7F, 0x7F,
0xFF, 0x00, 0x7F, 0x7F,
0xFF, 0x00, 0x7F, 0x7F,
};
ASS (memcmp(answer, buffer, 4*4) == 0 );
}
[NSOpenGLContext clearCurrentContext];
}
}