On Mon, Dec 29, 2008 at 11:06 PM, Thomas Hansen <[email protected]>wrote:

>
> never mind figured it out.  The following did the trick of saving the
> texture bound to the FBO to a file:
>
> myFBO.bind()
> buffer =(GLubyte * 3 * width* height)()
> glReadPixels(0, 0, width, height, GL_RGB,GL_UNSIGNED_BYTE, byref
> (buffer))
> image = Image.fromstring(mode="RGB", size=(width, height),
> data=buffer)
> image.save('output.png')
> myFBO.release()
>


The recommended way to create a mutable buffer is with
ctypes.create_string_buffer().


--
> Thomas
>
>
> On Dec 29, 7:48 pm, Thomas  Hansen <[email protected]> wrote:
> > I'm using a framebuffer object to draw into a texture for a program I
> > am working on.  I only have an ID for the texture which resides on the
> > GPU, but would like to save that texture to a file (png or jpeg).
> > However I am having some trouble figuring out how to read back the
> > image from the gpu using pyglet.
> >
> > I have tried using glReadPixles as follows, but cant get it to work:
> > glReadPixels(0, 0, width, height, GL_RGB,  GL_UNSIGNED_BYTE, byref
> > (buffer))
> >
> > Does anyone know what type I have to make 'buffer' to make this work?
> > I tried buffer = c_uint(0) as well as c_ubyte(0), but that didnt seem
> > to work.  Do I have to allocate the size for the data as well?  if so
> > does anyone know how to declare a c array of a specific size in
> > python, or where I can read some more about the types?  Amybe I am
> > totally off as well.
> >
> > I'd like to try to save the image after getting the data from
> > glReadPixles the following way using PIL, unless there is a better
> > way:
> > image = Image.fromstring(mode="RGB", size=(width, height),
> > data=buffer)
> > image.save('test.png')
> >
> > Any help is greatly apretiated!
> >
> > --
> > Thomas
> >
> > Here is the FBO class I am using...I'm trying to write a saveTexture
> > method for it:
> >
> > class Fbo:
> >     def __init__(self, size=(1024,1024)):
> >         self.framebuffer = c_uint(0)
> >         self.depthbuffer = c_uint(0)
> >         self.texture = c_uint(0)
> >         self.size=size
> >
> >         glGenFramebuffersEXT(1,byref(self.framebuffer))
> >         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, self.framebuffer)
> >
> >         glGenRenderbuffersEXT(1, byref(self.depthbuffer));
> >         glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, self.depthbuffer)
> >         glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT,
> > GL_DEPTH_COMPONENT, size[0], size[1])
> >         glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
> > GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, self.depthbuffer)
> >
> >         glGenTextures (1, byref(self.texture))
> >         glBindTexture (GL_TEXTURE_2D, self.texture)
> >         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
> >         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
> > GL_LINEAR)
> >         glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, size[0], size[1],
> 0,GL_RGB,
> > GL_UNSIGNED_BYTE, 0)
> >         glFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT,
> > GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, self.texture, 0)
> >         glBindRenderbufferEXT (GL_RENDERBUFFER_EXT, 0)
> >         glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0)
> >
> >         status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
> >         if status != GL_FRAMEBUFFER_COMPLETE_EXT:
> >             print "Error in framebuffer activation"
> >
> >     def bind(self):
> >         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, self.framebuffer)
> >         glPushAttrib(GL_VIEWPORT_BIT)
> >         glViewport(0,0,self.size[0], self.size[1])
> >
> >     def release(self):
> >         glPopAttrib()
> >         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0)
> >
>


-- 
Tristam MacDonald
http://swiftcoder.wordpress.com/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to