深度缓冲区在Android上不起作用,但在Linux上有效(使用Kivy)

时间:2014-08-18 10:43:18

标签: android python opengl opengl-es kivy

我写了一个这个问题的简单例子。

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.resources import resource_find
from kivy.graphics.transformation import Matrix
from kivy.graphics import *
from kivy.graphics.opengl import *
import random

class Root(Widget):
    def __init__(self, *larg, **kw):
        super(Root, self).__init__(*larg, **kw)
        self.vertices = [[-1,-1, 0,    1,-1, 0,    1, 1, 0,   -1, 1, 0]]
        kw['shader_file'] = 'shaders.glsl'
        self.canvas = RenderContext(compute_normal_mat=True)
        shader_file = kw.pop('shader_file')
        self.canvas.shader.source = resource_find(shader_file)
        with self.canvas:
            self.cb = Callback(self.setup_gl_context)
            PushMatrix()
            Translate(0, 0, -5)
            for i in xrange(10):
                Translate(.5, 0, -.5)
                self.render(self.vertices, (random.random(), random.random(), random.random()))
            PopMatrix()
            self.cb = Callback(self.reset_gl_context)
        asp = float(Window.width) / Window.height / 2.0
        proj = Matrix().view_clip(-asp, asp, -0.5, 0.5, 1, 100, 1)
        self.canvas['projection_mat'] = proj

    def setup_gl_context(self, *args):
        glEnable(GL_DEPTH_TEST)

    def reset_gl_context(self, *args):
        glDisable(GL_DEPTH_TEST)

    def render(self, vertices, color):
        for i in xrange(len(vertices)):
            ChangeState(Kd=color, Ka=color, Ks=(.3, .3, .3), Tr=1., Ns=1., intensity=1.)
            Mesh(vertices=vertices[i], indices=[0, 1, 2, 3, 0, 2], fmt=[('v_pos', 3, 'float')], mode='triangles')

class TestApp(App): 
    def build(self):
        return Root()

if __name__ == '__main__':
    TestApp().run()

注意:我使用了this shaders.glsl

它渲染10个方块,它们具有相同的尺寸,但颜色随机。每个新方格都由Translate(.5, 0, -.5)相对翻译,所以最后一个方格是最远的方格。 由于存在glEnable(GL_DEPTH_TEST),因此当我在Linux上运行此时,它会正确呈现: enter image description here

但是在我使用Buildozer创建* .apk后(buildzer.spec我只更改了titlenameversion并将glsl添加到{{} 1}})并在Android(4.4)上运行,它被无法正确呈现(如果include_exts被禁用): enter image description here

问题可能是OpenGL ES和OpengGL之间的区别,但我认为GL_DEPTH_TEST应该同时适用于两者。包装也可能存在问题,但看起来并不像。 有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

正如Reto Koradi建议的那样,我再次深入研究了Kivy文档。我发现了Framebuffer(模块kivy.graphics.fbo),它是一个离屏窗口,就像一个Kivy Canvas。它(可能与Canvas不同)具有with_depthbuffer参数,默认情况下设置为False

所以解决方案是在Framebuffer中设置with_depthbuffer = True,然后在Canvas中使用Framebuffer的渲染纹理来显示它。

我不确定为什么这对Android来说是必要的,但对于Linux则不然。还有一个更好的选择,它不包括创建(有时无意义的)Framebuffer,但这种方法至少适用于两个平台。

我修改了示例代码:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.resources import resource_find
from kivy.graphics.transformation import Matrix
from kivy.graphics import *
from kivy.graphics.opengl import *
import random

class Root(Widget):
    def __init__(self, *larg, **kw):
        super(Root, self).__init__(*larg, **kw)     
        self.vertices = [[-1,-1, 0,    1,-1, 0,    1, 1, 0,   -1, 1, 0]]
        with self.canvas:
             self.fbo = Fbo(with_depthbuffer = True, size = Window.size)
             Rectangle(size=Window.size, texture=self.fbo.texture)

        kw['shader_file'] = 'shaders.glsl'
        shader_file = kw.pop('shader_file')   
        self.fbo.shader.source = resource_find(shader_file)
        with self.fbo:
            self.cb = Callback(self.setup_gl_context)
            PushMatrix()
            Translate(0, 0, -5)
            for i in xrange(10):
                Translate(.5, 0, -.5)
                self.render(self.vertices, (random.random(), random.random(), random.random()))
            PopMatrix()
            self.cb = Callback(self.reset_gl_context)

        asp = float(Window.width) / Window.height / 2.0
        proj = Matrix().view_clip(-asp, asp, -0.5, 0.5, 1, 100, 1)
        self.fbo['projection_mat'] = proj

    def setup_gl_context(self, *args):
        glEnable(GL_DEPTH_TEST)

    def reset_gl_context(self, *args):
        glDisable(GL_DEPTH_TEST)

    def render(self, vertices, color):
        for i in xrange(len(vertices)):
            ChangeState(Kd=color, Ka=color, Ks=(.3, .3, .3), Tr=1., Ns=1., intensity=1.)
            Mesh(vertices=vertices[i], indices=[0, 1, 2, 3, 0, 2], fmt=[('v_pos', 3, 'float')], mode='triangles')

class TestApp(App): 
    def build(self):
        return Root()

if __name__ == '__main__':
    TestApp().run()