I use the following code (using FrameBufferObjects) to save screenshots. I render into
texture $texture_id and then read that texture back into main memory:
sub tick {
my ($self,$texture_id) = @_;
# save current frame from input fbo
glBindTexture(GL_TEXTURE_2D, $texture_id);
my $frame;
for my $i (reverse (0..$self->height-1)) {
glReadPixels_s(0,$i,$self->width,1,GL_RGB,GL_UNSIGNED_BYTE,my
+$line);
$frame .= $line;
};
syswrite $self->stream, $frame, $self->width * $self->height * $se
+lf->depth
or die "Write failure";
};
... and here's the part that renders into an fbo:
sub allocate_fbo_texture{
my ($class,$width,$height) = @_;
(my $texture_id) = glGenTextures_p(1);
(my $fbo) = glGenFramebuffersEXT_p(1);
(my $render_buffer) = glGenRenderbuffersEXT_p(1);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, $fbo);
glBindTexture(GL_TEXTURE_2D, $texture_id);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, $render_buffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D_c(GL_TEXTURE_2D,0,GL_RGBA8,$width,$height,0,GL_RGBA,G
+L_UNSIGNED_BYTE,0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0
+_EXT, GL_TEXTURE_2D, $texture_id, 0);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24
+, $width,$height);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHME
+NT_EXT, GL_RENDERBUFFER_EXT, $render_buffer);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
return ($fbo,$texture_id, $render_buffer);
};
sub enable {
my $self = shift;
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0,0,$self->width, $self->height);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, $self->fbo);
my $res = $self->shader->Enable();
if ($self->config->{enable}) {
$self->config->{enable}->($self);
};
};
This is quite roughly ripped out of a larger framework, but it should give you at least a hint of how you could do it. I'm not sure why your approach of reading the memory doesn't work though. |