渲染缓冲区大于窗口大小 - OpenGL

时间:2013-05-30 01:57:14

标签: opengl opengl-es fbo

我正在尝试绘制一个大于屏幕尺寸的渲染缓冲区(512x512)(即320x480)。

执行glReadPixels后,图像看起来正确,除非图像的尺寸超过屏幕尺寸 - 在此示例中,超过320水平和480垂直。造成这种异常的原因是什么?有什么我想念的吗? 当窗口大小>> =渲染缓冲区的大小时,此代码可以正常工作。

渲染到缓冲区的示例图像& glReadPixel'd: http://img593.imageshack.us/img593/3220/rendertobroke.png

unsigned int canvasFrameBuffer;
bglGenFramebuffers(1, &canvasFrameBuffer);
bglBindFramebuffer(BGL_RENDERBUFFER, canvasFrameBuffer);

// Attach renderbuffer
unsigned int canvasRenderBuffer;
bglGenRenderbuffers(1, &canvasRenderBuffer);
bglBindRenderbuffer(BGL_RENDERBUFFER, canvasRenderBuffer);
bglRenderbufferStorage(BGL_RENDERBUFFER, BGL_RGBA4, width, height);
bglFramebufferRenderbuffer(BGL_FRAMEBUFFER, BGL_COLOR_ATTACHMENT0, BGL_RENDERBUFFER, canvasRenderBuffer);

bglViewport(0, 0, width, height);

Matrix::matrix_t identity, colorMatrix;
Matrix::LoadIdentity(&identity);
Matrix::LoadIdentity(&colorMatrix);
bglClearColor(1.0f, 1.0f, 1.0f, 1.0f);
bglClear(BGL_COLOR_BUFFER_BIT);

Vector::vector_t oldPos, oldScale;
Vector::Copy(&oldPos, &pos);
Vector::Mul(&pos, 0.0f);
Vector::Copy(&oldScale, &scale);
Vector::Load(&scale, 1, 1, 1);
int oldHAlign = halignment;
int oldVAlign = valignment;
halignment = Font::HALIGN_LEFT;
valignment = Font::VALIGN_BOTTOM;
float oldXRatio = vid.xratio;
float oldYRatio = vid.yratio;
vid.xratio = 1;
vid.yratio = 1;
Drawing::Set2D(this->size.x, this->size.y); // glOrtho and setup projection/modelview matrices
Draw(&identity, &colorMatrix);
Vector::Copy(&pos, &oldPos);
Vector::Copy(&scale, &oldScale);
halignment = oldHAlign;
valignment = oldVAlign;
vid.xratio = oldXRatio;
vid.yratio = oldYRatio;

byte *buffer = (byte*)Z_Malloc(width * height * 3, ZT_STATIC);
bglPixelStorei(BGL_PACK_ALIGNMENT, 1);
bglReadPixels(0, 0, width, height, BGL_RGB, BGL_UNSIGNED_BYTE, buffer);

byte *final = RGBtoLuminance(buffer, width, height);

SaveTGA("canvas.tga", final, width, height, 1);

Z_Free(buffer);

// unbind frame buffer
bglBindRenderbuffer(BGL_RENDERBUFFER, 0);
bglBindFramebuffer(BGL_FRAMEBUFFER, 0);
bglDeleteRenderbuffers(1, &canvasRenderBuffer);
bglDeleteFramebuffers(1, &canvasFrameBuffer);

bglViewport(0, 0, vid.width, vid.height);

1 个答案:

答案 0 :(得分:1)

这是答案。

更改此行:     bglBindFramebuffer(BGL_RENDERBUFFER,canvasFrameBuffer);

到此:     bglBindFramebuffer(BGL_FRAMEBUFFER,canvasFrameBuffer);