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.


In reply to Re: Capture opengl screen by Corion
in thread Capture opengl screen by iea

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.