def on_draw(): #w.clear() global parent global parentdiff global olddrawing,newdrawing global blitted global image_pixels global keeps global i if not blitted: """ At the start we've not seen the target before, so draw it and store the pixel data. """ pic.blit(0,0) blitted = 1 image_pixels = (gl.GLubyte * (4*size))(0) gl.glReadPixels(0, 0, newdrawing.width, newdrawing.height, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, image_pixels ) image_pixels = np.frombuffer(image_pixels, dtype=np.uint8).astype(np.int32) # Draw the new child newdrawing.draw() # Read the pixel data for the child and find out if its any good gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, newdrawing.fb) gl.glReadPixels(0, 0, newdrawing.width, newdrawing.height, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, a) gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0) diff = compute_diff(a) if parent == None or diff < parentdiff: # The new drawing is better. # Redraw the parent as this child. # Set this child's diff as the new one to beat. parent = image.ImageData(newdrawing.width,newdrawing.height,"RGBA",a) parentdiff = diff draw_parent(parent, newdrawing.width) else: # The new drawing sucks. Replace it # dump it's framebuffer first though!!!! gl.glDeleteFramebuffersEXT(1, ctypes.byref(newdrawing.fb)) newdrawing = olddrawing i += 1 if (i % 20 == 0): # Use the window title to let the user know how we're doing w.set_caption(str(fps.get_fps())+" "+str(parentdiff) + " " + str(log(parentdiff,10))+ " " + str(i)) fps.tick()
def delete(self): ''' Destroy this framebuffer and free allocated resources. ''' gl.glDeleteFramebuffersEXT(1, [self._buffer]) gl.glDeleteTextures(1, [self.rgb_texture]) self._buffer = None
def __del__(self): """Delete the framebuffer from the graphics card's memory. """ self.unbind() if self.id: gl.glDeleteFramebuffersEXT(1, self.id) self.id = None
def on_draw(): #w.clear() global parent global parentdiff global olddrawing, newdrawing global blitted global image_pixels global keeps global i if not blitted: """ At the start we've not seen the target before, so draw it and store the pixel data. """ pic.blit(0, 0) blitted = 1 image_pixels = (gl.GLubyte * (4 * size))(0) gl.glReadPixels(0, 0, newdrawing.width, newdrawing.height, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, image_pixels) image_pixels = np.frombuffer(image_pixels, dtype=np.uint8).astype(np.int32) # Draw the new child newdrawing.draw() # Read the pixel data for the child and find out if its any good gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, newdrawing.fb) gl.glReadPixels(0, 0, newdrawing.width, newdrawing.height, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, a) gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0) diff = compute_diff(a) if parent == None or diff < parentdiff: # The new drawing is better. # Redraw the parent as this child. # Set this child's diff as the new one to beat. parent = image.ImageData(newdrawing.width, newdrawing.height, "RGBA", a) parentdiff = diff draw_parent(parent, newdrawing.width) else: # The new drawing sucks. Replace it # dump it's framebuffer first though!!!! gl.glDeleteFramebuffersEXT(1, ctypes.byref(newdrawing.fb)) newdrawing = olddrawing i += 1 if (i % 20 == 0): # Use the window title to let the user know how we're doing w.set_caption( str(fps.get_fps()) + " " + str(parentdiff) + " " + str(log(parentdiff, 10)) + " " + str(i)) fps.tick()
def destroy(self): """Free memory""" if self.initialized: byref = ctypes.byref if self.framebuffer_id: gl.glDeleteFramebuffersEXT(1, byref(self.framebuffer_id)) if self.depthbuffer_id: gl.glDeleteRenderbuffersEXT(1, byref(self.depthbuffer_id)) if self.texture_id: gl.glDeleteTextures(1, byref(self.texture_id)) self.initialized = False
def __del__(self): """Delete the framebuffer from the GPU memory""" id = gl.GLuint(self._id) gl.glDeleteFramebuffersEXT(1, ct.byref(id))
def renderToTexture(w, h, function): import ctypes from pyglet import gl from pyglet import image from pyglet.gl import gl_info global has_fbo if has_fbo is None: has_fbo = gl_info.have_extension('GL_EXT_framebuffer_object') # enforce dimensions are ints w, h = int(w), int(h) # set up viewport gl.glPushAttrib(gl.GL_VIEWPORT_BIT | gl.GL_TRANSFORM_BIT) gl.glViewport(0, 0, w, h) gl.glMatrixMode(gl.GL_PROJECTION) gl.glPushMatrix() gl.glLoadIdentity() gl.glOrtho(0, w, 0, h, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) if has_fbo: # render directly to texture # create our frame buffer fbo = gl.GLuint() gl.glGenFramebuffersEXT(1, ctypes.byref(fbo)) gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, fbo) # allocate a texture and add to the frame buffer tex = image.Texture.create_for_size(gl.GL_TEXTURE_2D, w, h, gl.GL_RGBA) gl.glBindTexture(gl.GL_TEXTURE_2D, tex.id) gl.glFramebufferTexture2DEXT(gl.GL_FRAMEBUFFER_EXT, gl.GL_COLOR_ATTACHMENT0_EXT, gl.GL_TEXTURE_2D, tex.id, 0) status = gl.glCheckFramebufferStatusEXT(gl.GL_FRAMEBUFFER_EXT) assert status == gl.GL_FRAMEBUFFER_COMPLETE_EXT # now render gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, fbo) function() gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0) # clean up gl.glDeleteFramebuffersEXT(1, ctypes.byref(fbo)) else: # render and copy to texture # render function() # grab the buffer and copy contents to the texture buffer = image.get_buffer_manager().get_color_buffer() tex = image.Texture.create_for_size(gl.GL_TEXTURE_2D, w, h, gl.GL_RGBA) tex.blit_into(buffer.get_region(0, 0, w, h), 0, 0, 0) gl.glMatrixMode(gl.GL_PROJECTION) gl.glPopMatrix() gl.glPopAttrib() # return the region (the whole texture will most likely be larger) return tex.get_region(0, 0, w, h)
def __del__(self): from pyglet import gl try: gl.glDeleteFramebuffersEXT(1, self.fbo_id) except: pass
def on_close(): gl.glDeleteFramebuffersEXT(1, ctypes.byref(olddrawing.fb)) olddrawing.svg_export(image_file, svg_file)
def renderToTexture(w, h, function): import ctypes from pyglet import gl from pyglet import image from pyglet.gl import gl_info global has_fbo if has_fbo is None: has_fbo = gl_info.have_extension("GL_EXT_framebuffer_object") # enforce dimensions are ints w, h = int(w), int(h) # set up viewport gl.glPushAttrib(gl.GL_VIEWPORT_BIT | gl.GL_TRANSFORM_BIT) gl.glViewport(0, 0, w, h) gl.glMatrixMode(gl.GL_PROJECTION) gl.glPushMatrix() gl.glLoadIdentity() gl.glOrtho(0, w, 0, h, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) if has_fbo: # render directly to texture # create our frame buffer fbo = gl.GLuint() gl.glGenFramebuffersEXT(1, ctypes.byref(fbo)) gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, fbo) # allocate a texture and add to the frame buffer tex = image.Texture.create_for_size(gl.GL_TEXTURE_2D, w, h, gl.GL_RGBA) gl.glBindTexture(gl.GL_TEXTURE_2D, tex.id) gl.glFramebufferTexture2DEXT(gl.GL_FRAMEBUFFER_EXT, gl.GL_COLOR_ATTACHMENT0_EXT, gl.GL_TEXTURE_2D, tex.id, 0) status = gl.glCheckFramebufferStatusEXT(gl.GL_FRAMEBUFFER_EXT) assert status == gl.GL_FRAMEBUFFER_COMPLETE_EXT # now render gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, fbo) function() gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0) # clean up gl.glDeleteFramebuffersEXT(1, ctypes.byref(fbo)) else: # render and copy to texture # render function() # grab the buffer and copy contents to the texture buffer = image.get_buffer_manager().get_color_buffer() tex = image.Texture.create_for_size(gl.GL_TEXTURE_2D, w, h, gl.GL_RGBA) tex.blit_into(buffer.get_region(0, 0, w, h), 0, 0, 0) gl.glMatrixMode(gl.GL_PROJECTION) gl.glPopMatrix() gl.glPopAttrib() # return the region (the whole texture will most likely be larger) return tex.get_region(0, 0, w, h)