From 867c03807c7849326ca2880c77f2752792ca7055 Mon Sep 17 00:00:00 2001 From: Thomas Hansen Date: Sun, 5 Dec 2010 16:53:13 -0600 Subject: [PATCH] start fbo --- kivy/c_ext/graphics/.gitignore | 1 + kivy/c_ext/graphics/fbo.pxd | 17 +++++++ kivy/c_ext/graphics/fbo.pyx | 87 ++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 kivy/c_ext/graphics/.gitignore create mode 100644 kivy/c_ext/graphics/fbo.pxd create mode 100644 kivy/c_ext/graphics/fbo.pyx diff --git a/kivy/c_ext/graphics/.gitignore b/kivy/c_ext/graphics/.gitignore new file mode 100644 index 000000000..064a8d8ef --- /dev/null +++ b/kivy/c_ext/graphics/.gitignore @@ -0,0 +1 @@ +*.c diff --git a/kivy/c_ext/graphics/fbo.pxd b/kivy/c_ext/graphics/fbo.pxd new file mode 100644 index 000000000..162fb56a6 --- /dev/null +++ b/kivy/c_ext/graphics/fbo.pxd @@ -0,0 +1,17 @@ +from c_opengl cimport * +from instructions cimport RenderContext, Canvas + +class Fbo(RenderContext): + cdef int width + cdef int height + cdef int depthbuffer_attached + cdef list clear_color + cdef GLuint buffer_id + cdef GLuint depthbuffer_id + cdef object texture + + cdef bind(self) + cdef release(self) + cdef clear(self) + + diff --git a/kivy/c_ext/graphics/fbo.pyx b/kivy/c_ext/graphics/fbo.pyx new file mode 100644 index 000000000..d4f8c7fc8 --- /dev/null +++ b/kivy/c_ext/graphics/fbo.pyx @@ -0,0 +1,87 @@ +__all__ = ('Fbo',) + + +import kivy +from kivy import Logger + +from c_opengl cimport * +from instructions cimport RenderContext, Canvas + +cdef class Fbo(RenderContext): + def __init__(self, *args, **kwargs): + RenderContext.__init__(self, *args, **kwargs) + + kwargs.setdefault('clear_color', (0, 0, 0, 0)) + kwargs.setdefault('size', (1024, 1024)) + kwargs.setdefault('push_viewport', False) + kwargs.setdefault('with_depthbuffer', True) + + self.size = kwargs.get('size') + self.clear_color = kwargs.get('clear_color') + self.depthbuffer_attached = kwargs.get('with_depthbuffer') + self.push_viewport = kwargs.get('push_viewport') + self._is_bound = False + + # create buffers and textures + self.texture = kivy.Texture.create(self.width, self.height) + + cdef GLuint id + glGenFramebuffers(1, &id) + self.buffer_id = id + glBindFramebuffer(GL_FRAMEBUFFER, self.buffer_id) + + if self.depthbuffer_attached: + glGenRenderbuffers(1, &id) + self.depthbuffer_id = id + glBindRenderbuffer(GL_RENDERBUFFER, self.depthbuffer_id) + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, + self.width, self.height) + glBindRenderbuffer(GL_RENDERBUFFER, 0) + glFramebufferRenderbuffer(GL_FRAMEBUFFER, + GL_DEPTH_ATTACHMENT, + GL_RENDERBUFFER, + self.depthbuffer_id) + + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, + GL_TEXTURE_2D, self.texture.id, 0) + + cdef int status = glCheckFramebufferStatus(GL_FRAMEBUFFER) + if status: + Logger.error("Error initializing FBO. %d" % status) + + + glBindFramebuffer(GL_FRAMEBUFFER, 0) + + + def bind(self): + self._is_bound = True + glBindFramebuffer(GL_FRAMEBUFFER, self.framebuffer) + glViewport(0, 0, self.width, self.height) + + def release(self): + self._is_bound = False + glBindFramebuffer(GL_FRAMEBUFFER, 0) + + def clear(self): + cdef float c[4] + c[0] = self.clear_color[0] + c[1] = self.clear_color[2] + c[2] = self.clear_color[2] + c[3] = self.clear_color[3] + glClearColor(c[0],c[1],c[2],c[3]) + if self.depthbuffer_attached: + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) + else: + glClear(GL_COLOR_BUFFER_BIT) + + + def __enter__(self): + Canvas.__enter__(self) + self.bind() + + def __exit__(self, type, value, traceback): + Canvas.__exit__(self, type, value, traceback) + self.release() + + +