Beispiel #1
0
    def unbind(self):
        """Unbind the FBO."""
        # Unbind the FBO
        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)

        # Restore the old viewport size
        gl.glViewport(*self._old_viewport_size)
Beispiel #2
0
    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()
Beispiel #3
0
def bind_texture_region(texture, x, y, w, h, premultiplied=False, flip=False):
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER,
                       gl.GL_NEAREST)
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER,
                       gl.GL_NEAREST)
    gl.glEnable(gl.GL_SCISSOR_TEST)

    gl.glEnable(gl.GL_BLEND)
    if premultiplied:
        gl.glBlendFunc(gl.GL_ONE, gl.GL_ONE_MINUS_SRC_ALPHA)

    else:
        gl.glBlendFuncSeparate(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA,
                               gl.GL_ONE, gl.GL_ONE_MINUS_SRC_ALPHA)

    gl.glMatrixMode(gl.GL_MODELVIEW)
    gl.glLoadIdentity()

    gl.glViewport(x, y, w, h)
    gl.glScissor(x, y, w, h)

    gl.glBindFramebufferEXT(gl.GL_DRAW_FRAMEBUFFER_EXT,
                            Manager.col_buffer.gl_buffer)

    gl.glFramebufferTexture2DEXT(gl.GL_DRAW_FRAMEBUFFER_EXT,
                                 gl.GL_COLOR_ATTACHMENT0_EXT, texture.target,
                                 texture.id, 0)

    gl.glMatrixMode(gl.GL_PROJECTION)
    gl.glLoadIdentity()
    if flip:
        gl.gluOrtho2D(0, w, h, 0)
    else:
        gl.gluOrtho2D(0, w, 0, h)
Beispiel #4
0
    def unbind(self):
        """Unbind the FBO."""
        # Unbind the FBO
        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)

        # Restore the old viewport size
        gl.glViewport(*self._old_viewport_size)
Beispiel #5
0
    def paint(self, func):
        gl.glEnable(gl.GL_BLEND)
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)

        gl.glMatrixMode(gl.GL_PROJECTION)
        gl.glLoadIdentity()

        gl.gluOrtho2D(
            0,
            self.width,
            0,
            self.height
        )

        gl.glMatrixMode(gl.GL_MODELVIEW)
        gl.glLoadIdentity()
        gl.glViewport(0, 0, self.width, self.height)

        gl.glBindFramebufferEXT(gl.GL_DRAW_FRAMEBUFFER_EXT,
                                self.col_buffer.gl_buffer)

        gl.glFramebufferTexture2DEXT(
            gl.GL_DRAW_FRAMEBUFFER_EXT,
            gl.GL_COLOR_ATTACHMENT0_EXT,
            self.texture.target, self.texture.id, 0)

        func()

        gl.glBindFramebufferEXT(gl.GL_DRAW_FRAMEBUFFER_EXT, 0)
Beispiel #6
0
    def __init__(self, width, height, background=[255, 255, 255]):
        self.width = width
        self.height = height
        self.triangles = []
        self.batch = Batch()
        self.bg_colour = background
        has_fbo = gl.gl_info.have_extension('GL_EXT_framebuffer_object')

        #setup a framebuffer
        self.fb = gl.GLuint()
        gl.glGenFramebuffersEXT(1, ctypes.byref(self.fb))
        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, self.fb)

        #allocate a texture for the fb to render to
        tex = image.Texture.create_for_size(gl.GL_TEXTURE_2D, width, height,
                                            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

        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)

        self.bg = self.batch.add(
            6, gl.GL_TRIANGLES, None,
            ("v2i/static",
             (0, 0, 0, height, width, height, width, height, width, 0, 0, 0)),
            ("c3B/static", background * 6))
Beispiel #7
0
    def on_draw():
        gl.glClearColor(1.0,1.0,1.0,1.0)
	window.clear()

        # Compute
        gl.glViewport(0, 0, width, height)
        gl.glMatrixMode(gl.GL_PROJECTION)
        gl.glLoadIdentity()
        gl.glOrtho(0, 1, 0, 1, -1, 1)
        gl.glMatrixMode(gl.GL_MODELVIEW)

        gl.glActiveTexture( gl.GL_TEXTURE1 )
	gl.glBindTexture(texture_s.target, texture_s.id)

        gl.glActiveTexture( gl.GL_TEXTURE0 )
	gl.glBindTexture(texture_uv.target, texture_uv.id)

        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, framebuffer)
	reaction_shader.bind()
        texture_uv.blit(x=0.0, y=0.0, width=1.0, height=1.0)
	reaction_shader.unbind()
        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)

        # Render
        gl.glViewport(0, 0, window.width, window.height)
        gl.glMatrixMode(gl.GL_PROJECTION)
        gl.glLoadIdentity()
        gl.glOrtho(0, 1, 0, 1, -1, 1)
        gl.glMatrixMode(gl.GL_MODELVIEW)

	color_shader.bind()
        texture_uv.blit(x=0.0, y=0.0, width=1.0, height=1.0)
	color_shader.bind()
Beispiel #8
0
    def __init__(self, width, height, background=[255,255,255]):
        self.width = width
        self.height = height
        self.triangles = []
        self.batch = Batch()
        self.bg_colour = background
        has_fbo = gl.gl_info.have_extension('GL_EXT_framebuffer_object')

        #setup a framebuffer
        self.fb = gl.GLuint()
        gl.glGenFramebuffersEXT(1, ctypes.byref(self.fb))
        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, self.fb)

        #allocate a texture for the fb to render to
        tex = image.Texture.create_for_size(gl.GL_TEXTURE_2D, width, height, 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

        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)

        self.bg = self.batch.add( 6,
                        gl.GL_TRIANGLES,None,
                        ("v2i/static", (0,0,0,height,width,height,width,height,width,0,0,0)),
                        ("c3B/static",background*6)
                      )
Beispiel #9
0
    def bind(self):
        """Set the FBO as the active rendering buffer.

        """
        if self.id:
            gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, self.id)
        else:
            raise RuntimeError("Cannot bind to deleted framebuffer.")
Beispiel #10
0
    def activate(self):

        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, self._buffer)

        gl.glPushAttrib(gl.GL_VIEWPORT_BIT)
        gl.glViewport(0, 0, *self.render_target_size)

        gl.glClear(gl.GL_COLOR_BUFFER_BIT)
Beispiel #11
0
 def unbind(self):
     from pyglet import gl
     gl.glMatrixMode(gl.GL_MODELVIEW)
     gl.glPopMatrix()
     gl.glMatrixMode(gl.GL_PROJECTION)
     gl.glPopMatrix()
     gl.glPopAttrib()
     gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)
     Framebuffer.current_fbo = None
Beispiel #12
0
 def unbind(self):
     from pyglet import gl
     gl.glMatrixMode(gl.GL_MODELVIEW)
     gl.glPopMatrix()
     gl.glMatrixMode(gl.GL_PROJECTION)
     gl.glPopMatrix()
     gl.glPopAttrib()
     gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)
     Framebuffer.current_fbo = None
Beispiel #13
0
def bind_window(w, h):
    gl.glBindFramebufferEXT(gl.GL_DRAW_FRAMEBUFFER_EXT, 0)
    gl.glMatrixMode(gl.GL_MODELVIEW)
    gl.glLoadIdentity()
    gl.glViewport(0, 0, w, h)
    gl.glScissor(0, 0, w, h);
    gl.glMatrixMode(gl.GL_PROJECTION)
    gl.glLoadIdentity()
    gl.gluOrtho2D(0, w, h, 0)
Beispiel #14
0
def bind_window(w, h):
    gl.glBindFramebufferEXT(gl.GL_DRAW_FRAMEBUFFER_EXT, 0)
    gl.glMatrixMode(gl.GL_MODELVIEW)
    gl.glLoadIdentity()
    gl.glViewport(0, 0, w, h)
    gl.glScissor(0, 0, w, h)
    gl.glMatrixMode(gl.GL_PROJECTION)
    gl.glLoadIdentity()
    gl.gluOrtho2D(0, w, h, 0)
Beispiel #15
0
    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()
Beispiel #16
0
    def bind(self):
        """Bind the FBO.  Anything drawn afterward will be stored in the FBO's texture."""
        # This is called simply to deal with anything that might be currently bound (for example, Pyglet objects),
        gl.glBindTexture(gl.GL_TEXTURE_2D, 0)

        # Store current viewport size for later
        gl.glGetIntegerv(gl.GL_VIEWPORT, self._old_viewport_size)

        # Bind the FBO, and change the viewport to fit its texture.
        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, self.id)  # Rendering off-screen
        gl.glViewport(0, 0, self.texture.width, self.texture.height)
Beispiel #17
0
    def unbind(self):
        """Unbind the FBO."""
        # Unbind the FBO
        if self.texture.mipmap:
            with self.texture:
                self.texture.generate_mipmap()

        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)

        # Restore the old viewport size
        gl.glViewport(*self._old_viewport_size)
Beispiel #18
0
    def bind(self):
        """Bind the FBO.  Anything drawn afterward will be stored in the FBO's texture."""
        # This is called simply to deal with anything that might be currently bound (for example, Pyglet objects),
        gl.glBindTexture(gl.GL_TEXTURE_2D, 0)

        # Store current viewport size for later
        gl.glGetIntegerv(gl.GL_VIEWPORT, self._old_viewport_size)

        # Bind the FBO, and change the viewport to fit its texture.
        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT,
                                self.id)  # Rendering off-screen
        gl.glViewport(0, 0, self.texture.width, self.texture.height)
Beispiel #19
0
    def on_mouse_drag(x, y, dx, dy, button, modifiers):
        sprite.x = int((x/float(window.width)) * width)-sprite.width//2
        sprite.y = int((y/float(window.height)) * height)-sprite.height//2

        # Compute
        gl.glViewport(0, 0, width, height)
        gl.glMatrixMode(gl.GL_PROJECTION)
        gl.glLoadIdentity()
        gl.glOrtho(0, width, 0, height, -1, 1)
        gl.glMatrixMode(gl.GL_MODELVIEW)
        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, framebuffer)
        sprite.draw()
        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)
Beispiel #20
0
    def _bound_context(self, target):
        """Nestable context for glBindFramebufferEXT with given target

        The value is the framebuffer in the outer context
        """
        assert self.initialized
        parent_fb = self._bind_stack[-1]
        gl.glBindFramebufferEXT(target, self.framebuffer_id)
        self._bind_stack.append(self)
        try:
            yield parent_fb
        finally:
            me = self._bind_stack.pop()
            assert me is self
            gl.glBindFramebufferEXT(target, parent_fb.framebuffer_id)
Beispiel #21
0
    def _blit_buffer_direct(self, framebuffer, parent_framebuffer,
            width, height, parent_width, parent_height,
            transformation, **kwargs):
        # XXX: Use this instead of blit_buffer when we don't need
        # colorizing/opacity
        transformation.reset()
        gl.glViewport(0, 0, width, height)
        gl.glBindFramebufferEXT(gl.GL_READ_FRAMEBUFFER_EXT,
            framebuffer.framebuffer_id)
        gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT0_EXT)

        gl.glBindFramebufferEXT(gl.GL_DRAW_FRAMEBUFFER_EXT,
            parent_framebuffer.framebuffer_id)
        gl.glBlitFramebufferEXT(0, 0, width, height, 0, 0,
            parent_width, parent_height,
            gl.GL_COLOR_BUFFER_BIT, gl.GL_NEAREST)
        gl.glDisable(gl.GL_TEXTURE_2D)
Beispiel #22
0
    def __init__(self, width, height):
        if not gl.gl_info.have_extension('GL_EXT_framebuffer_object'):
            raise Exception('framebuffer object extension not available')

        texture_id = buffer_texture(width, height)
        self.texture = Texture(width, height, gl.GL_TEXTURE_2D, texture_id)

        framebuffer_id = gl.GLuint()
        gl.glGenFramebuffersEXT(1, byref(framebuffer_id))
        self.id = framebuffer_id.value

        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, gl.GLuint(self.id))
        gl.glFramebufferTexture2DEXT(gl.GL_FRAMEBUFFER_EXT,
                                     gl.GL_COLOR_ATTACHMENT0_EXT + 0,
                                     gl.GL_TEXTURE_2D,
                                     texture_id,
                                     0)
        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)
Beispiel #23
0
    def __init__(self, width = 512, height = 512, screen_size=None ):
        self.render_target_size = width, height

        # create the framebuffer
        self._buffer = (c_uint * 1)() ; gl.glGenFramebuffersEXT(1,self._buffer)
        self._buffer = self._buffer[0]

        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, self._buffer)

        self.rgb_texture = self._create_texture(texture_size=self.render_target_size)

        gl.glFramebufferTexture2DEXT(gl.GL_FRAMEBUFFER_EXT, gl.GL_COLOR_ATTACHMENT0_EXT, gl.GL_TEXTURE_2D, self.rgb_texture, 0)

        status = gl.glCheckFramebufferStatusEXT(gl.GL_FRAMEBUFFER_EXT)
        if status != gl.GL_FRAMEBUFFER_COMPLETE_EXT:
            print("ERROR on FRAMEBUFFER")
            return
        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)
Beispiel #24
0
    def bind(self):
        assert Framebuffer.current_fbo is None
        from pyglet import gl
        t = self.texture
        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, self.fbo_id)
        gl.glPushAttrib(gl.GL_VIEWPORT_BIT | gl.GL_TRANSFORM_BIT)
        if t:
            gl.glViewport(0, 0, t.width, t.height)
        gl.glMatrixMode(gl.GL_PROJECTION)
        gl.glPushMatrix()
        if t:
            gl.glLoadIdentity()
            gl.gluOrtho2D(0, t.width, 0, t.height)
        gl.glMatrixMode(gl.GL_MODELVIEW)
        gl.glPushMatrix()
        if t:
            gl.glLoadIdentity()

        Framebuffer.current_fbo = self
Beispiel #25
0
    def __init__(self):
        """Framebuffer Object (FBO) for off-screen rendering.

        A framebuffer object contains one or more
        framebuffer-attachable images.  These can be either
        renderbuffers or texture images.

        For now the framebuffer object handles only textures.

        """
        ## Create a framebuffer object
        framebuffer = gl.GLuint()
        gl.glGenFramebuffersEXT(1, ctypes.byref(framebuffer))
        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, framebuffer)

        self.id = framebuffer
        self.MAX_COLOR_ATTACHMENTS = MAX_COLOR_ATTACHMENTS

        self._textures = []
Beispiel #26
0
    def bind(self):
        assert Framebuffer.current_fbo is None
        from pyglet import gl
        t = self.texture
        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, self.fbo_id)
        gl.glPushAttrib(gl.GL_VIEWPORT_BIT | gl.GL_TRANSFORM_BIT)
        if t:
            gl.glViewport(0, 0, t.width, t.height)
        gl.glMatrixMode(gl.GL_PROJECTION)
        gl.glPushMatrix()
        if t:
            gl.glLoadIdentity()
            gl.gluOrtho2D(0, t.width, 0, t.height)
        gl.glMatrixMode(gl.GL_MODELVIEW)
        gl.glPushMatrix()
        if t:
            gl.glLoadIdentity()

        Framebuffer.current_fbo = self
Beispiel #27
0
def bind_texture_region(texture, x, y, w, h, premultiplied=False, flip=False):
    gl.glTexParameteri(
        gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST)
    gl.glTexParameteri(
        gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)
    gl.glEnable(gl.GL_SCISSOR_TEST)

    gl.glEnable(gl.GL_BLEND)
    if premultiplied:
        gl.glBlendFunc(gl.GL_ONE, gl.GL_ONE_MINUS_SRC_ALPHA)

    else:
        gl.glBlendFuncSeparate(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA,
                               gl.GL_ONE, gl.GL_ONE_MINUS_SRC_ALPHA)

    gl.glMatrixMode(gl.GL_MODELVIEW)
    gl.glLoadIdentity()

    gl.glViewport(x, y, w, h)
    gl.glScissor(x, y, w, h)

    gl.glBindFramebufferEXT(
        gl.GL_DRAW_FRAMEBUFFER_EXT,
        Manager.col_buffer.gl_buffer
    )

    gl.glFramebufferTexture2DEXT(
        gl.GL_DRAW_FRAMEBUFFER_EXT,
        gl.GL_COLOR_ATTACHMENT0_EXT,
        texture.target,
        texture.id,
        0
    )

    gl.glMatrixMode(gl.GL_PROJECTION)
    gl.glLoadIdentity()
    if flip:
        gl.gluOrtho2D(0, w, h, 0)
    else:
        gl.gluOrtho2D(0, w, 0, h)
Beispiel #28
0
    def paint(self, func):
        gl.glEnable(gl.GL_BLEND)
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)

        gl.glMatrixMode(gl.GL_PROJECTION)
        gl.glLoadIdentity()

        gl.gluOrtho2D(0, self.width, 0, self.height)

        gl.glMatrixMode(gl.GL_MODELVIEW)
        gl.glLoadIdentity()
        gl.glViewport(0, 0, self.width, self.height)

        gl.glBindFramebufferEXT(gl.GL_DRAW_FRAMEBUFFER_EXT,
                                self.col_buffer.gl_buffer)

        gl.glFramebufferTexture2DEXT(gl.GL_DRAW_FRAMEBUFFER_EXT,
                                     gl.GL_COLOR_ATTACHMENT0_EXT,
                                     self.texture.target, self.texture.id, 0)

        func()

        gl.glBindFramebufferEXT(gl.GL_DRAW_FRAMEBUFFER_EXT, 0)
Beispiel #29
0
 def bind(self):
     """Set FBO as current rendering target"""
     gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, self._id)
Beispiel #30
0
 def unbind(self):
     gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)
Beispiel #31
0
 def bind(self):
     gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, self.buff_id)
Beispiel #32
0
 def bind(self):
     """Set FBO as current rendering target"""
     gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, self._id)
Beispiel #33
0
    def deactivate(self, *args):

        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)
        gl.glPopAttrib()
Beispiel #34
0
def draw_parent(parent, width):
    gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)
    parent.blit(width,0)
Beispiel #35
0
 def unbind(self):
     gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)
Beispiel #36
0
 def draw(self):
     gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, self.fb)
     self.batch.draw()
     gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)
Beispiel #37
0
 def __enter__(self):
     gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, gl.GLuint(self.id))
Beispiel #38
0
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)
Beispiel #39
0
 def __exit__(self, *args, **kwargs):
     gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)
Beispiel #40
0
    # Color shader
    # ------------
    vertex_shader   = open('./color.vert').read()
    fragment_shader = open('./color.frag').read()
    color_shader    = Shader(vertex_shader, fragment_shader)

    color_shader.bind()
    color_shader.uniformi('texture', 0)
    color_shader.unbind()

    # Framebuffer
    # -----------
    framebuffer = gl.GLuint(0)
    gl.glGenFramebuffersEXT(1, ctypes.byref(framebuffer))
    gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, framebuffer)
    gl.glFramebufferTexture2DEXT(gl.GL_FRAMEBUFFER_EXT, gl.GL_COLOR_ATTACHMENT0_EXT,
                                 gl.GL_TEXTURE_2D, texture_uv.id, 0); 
    gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)



    @window.event
    def on_mouse_drag(x, y, dx, dy, button, modifiers):
        sprite.x = int((x/float(window.width)) * width)-sprite.width//2
        sprite.y = int((y/float(window.height)) * height)-sprite.height//2

        # Compute
        gl.glViewport(0, 0, width, height)
        gl.glMatrixMode(gl.GL_PROJECTION)
        gl.glLoadIdentity()
Beispiel #41
0
 def draw(self):
     gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, self.fb)
     gl.glClear(gl.GL_COLOR_BUFFER_BIT)
     self.batch.draw()
Beispiel #42
0
 def unbind(self):
     """Set default framebuffer as current rendering target"""
     gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)
Beispiel #43
0
 def bind(self):
     gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, self.buff_id)
Beispiel #44
0
    def unbind(self):
        """Set the window as the active rendering buffer.

        """
        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)
Beispiel #45
0
 def unbind(self):
     """Set default framebuffer as current rendering target"""
     gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)
Beispiel #46
0
def main(image_file, num_polygons=250, resume=False):
    global image_pixels
    global keeps
    global newdrawing, olddrawing
    global blitted

    pic = image.load(image_file)
    width = pic.width
    height = pic.height
    size = width*height

    #setup the framebuffer
    # 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, width, height, 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

    newdrawing = Drawing(width,height,fbo)

    try:
        os.path.isfile(resume)
    except TypeError:
        svg_file = image_file + '.svg'
        if resume == True:
            newdrawing.svg_import(svg_file)
        else:
            newdrawing.generate(num_polygons)
    else:
        newdrawing.svg_import(resume)
        svg_file = resume

    w = window.Window(width*2,height,"cows", vsync = False)
    w.set_visible(True)
    gl.glEnable(gl.GL_BLEND)
    gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
    fps = pyglet.clock.Clock()

    #use this for pixel dumps
    a = (gl.GLubyte * (4*size))(0)

    @w.event
    def on_close():
        gl.glDeleteFramebuffersEXT(1, ctypes.byref(olddrawing.fb))
        olddrawing.svg_export(image_file, svg_file)

    @w.event
    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.
            """
            gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)
            gl.glClear(gl.GL_COLOR_BUFFER_BIT)
            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.glReadPixels(0,
                        0,
                        newdrawing.width,
                        newdrawing.height,
                        gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, a)
        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
            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()

    # Isolate and mutate the child on each clock tick
    pyglet.clock.schedule(update)
    # Set it running
    pyglet.app.run()