how to read back RenderBuffer back to CPU;

227 views
Skip to first unread message

will mi

unread,
May 14, 2010, 3:55:35 AM5/14/10
to G3D Users
I try to use renderbuffer instead of Texture to do off-screen
rendering.
While use Texture get the right result, but renderbuffer get a error
when use glReadPixels to read data of renderbuffer back to CPU.
The error is that "access violation writing location" .

I have malloc enough memory on CPU, and glGetTexImage works fine while
using Texture.
The funciotn of Texture, toImage3() gives me right Image, too.

So, how to use renderbuffer?( as I use rendering result on CPU, so
it's better to use renderbuffer, rather than Texture for me)

I use G3D-8.00-b1, Windows7, and GPU is NVIDIA GT240M.

following is main code of framebuffer.

My define:
FrameBuffer::Ref framebuffer;
Texture::Ref outColorTex;
Texture::Ref outColorTex2;
Renderbuffer::Ref renb;
Renderbuffer::Ref renb2;

init:
framebuffer = Framebuffer::create("framebuffer");
outColorTex = Texture::createEmpty("outColorTex", 1024, 1024,
ImageFormat::RGBA16());
outColorTex2 = Texture::createEmpty("outColorTex", 1024, 1024,
ImageFormat::RGBA16());
renb = RenderBuffer::createEmpty("renderbuffer", 1024, 1024,
ImageFormat::RGBA16F());
renb2 = RenderBuffer::createEmpty("renderbuffer2", 1024, 1024,
ImageFormat::RGBA16F());
framebuffer->set(FrameBuffer::COLOR_ATTACHMENT0, outColorTex);
framebuffer->set(Framebuffer::COLOR_ATTACHMENT1, outColorTex2);
//framebuffer->set(FrameBuffer::COLOR_ATTACHMENT0, renb);
//framebuffer->set(Framebuffer::COLOR_ATTACHMENT1, renb2);

render:
rd->pushState(framebuffer);
rd->push2D();
// Enable the shader
configureShaderArgs(localLighting);
//link a shader here and it will write gl_FragData[0]
and gl_FragData[1]
rd->setShader(testShader);

// Send model geometry to the graphics card
Draw::rect2D( framebuffer->rect2DBounds(), rd, Color3::green() );

float* cpuImage1 = new float[framebuffer->width() * framebuffer-
>height()];
float* cpuImage2 = new float[framebuffer->width() * framebuffer-
>height()];
glReadBuffer( GL_COLOR_ATTACHMENT0_EXT );
glReadPixels(0, 0, framebuffer->width(), framebuffer->height(),
GL_RGBA, GL_FLOAT, (GLvoid*)cpuImage1);
//glGetTexImage(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, GL_FLOAT,
cpuImage1);
delete[] cpuImage1;
delete[] cpuImage2;

rd->pop2D();
rd->popState();

will mi

unread,
May 14, 2010, 4:42:01 AM5/14/10
to G3D Users
I find the stupid mistake I make;
float* cpuImage1 = new float[framebuffer->width() *
framebuffer->height()];

I forget to multiply it by 4, where each color has RGBA, 4 components;
float* cpuImage1 = new float[framebuffer->width() *
framebuffer->height() * 4];

Sorry for all!

Corey Taylor

unread,
May 14, 2010, 2:11:01 PM5/14/10
to g3d-...@googlegroups.com
You can hard-code it to 4-bytes per pixel, or you could try to use ImageFormat with the appropriate RGBA format.  Then, you should have access to the bits per pixel values.
 
corey

Reply all
Reply to author
Forward
0 new messages