如何在Moderngl EGL后端中添加深度缓冲区? [英] How to add a depth buffer in Moderngl EGL backend?

查看:175
本文介绍了如何在Moderngl EGL后端中添加深度缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当注释深度缓冲线depth_attachment=ctx.depth_texture((512, 512), samples=8)时,此代码将呈现一个带有抗锯齿(samples=8)的彩色三角形.

This code renders a colored triangle with anti-aliasing (samples=8) when a depth buffer line depth_attachment=ctx.depth_texture((512, 512), samples=8) is commented.

但是,当我添加深度缓冲区时,它会在将fbo_msaa帧缓冲区绑定到GL_READ_FRAMEBUFFER时返回GL错误.您知道如何解决此错误吗?

But when I add a depth buffer it returns a GL error at the binding fbo_msaa framebuffer to GL_READ_FRAMEBUFFER. Do you know how to fix this error?

import numpy as np
from PIL import Image
import moderngl
import OpenGL.GL as gl

ctx = moderngl.create_standalone_context(backend='egl')

fbo = ctx.framebuffer(
        color_attachments=ctx.texture((512, 512), 4, samples=0)
    )

fbo_msaa = ctx.framebuffer(
        color_attachments=ctx.texture((512, 512), 4, samples=8),
        depth_attachment=ctx.depth_texture((512, 512), samples=8)
    )
fbo_msaa.use()

vertices = np.array([
    -1.0,  -1.0,   1.0, 0.0, 0.0,
     1.0,  -1.0,   0.0, 1.0, 0.0,
     0.0,   1.0,   0.0, 0.0, 1.0],
    dtype='f4',
)

prog = ctx.program(vertex_shader="""
#version 330
in vec2 in_vert;
in vec3 in_color;
out vec3 color;
void main() {
    gl_Position = vec4(in_vert, 0.0, 1.0);
    color = in_color;
}
""",
fragment_shader="""
#version 330
out vec4 fragColor;
in vec3 color;
void main() {
    fragColor = vec4(color, 1.0);
}
""",
)
vao = ctx.simple_vertex_array(prog, ctx.buffer(vertices), 'in_vert', 'in_color')
vao.render(mode=moderngl.TRIANGLES)

gl.glBindFramebuffer(gl.GL_READ_FRAMEBUFFER, fbo_msaa.glo)
gl.glBindFramebuffer(gl.GL_DRAW_FRAMEBUFFER, fbo.glo)
gl.glBlitFramebuffer(0, 0, 512, 512, 0, 0, 512, 512, gl.GL_COLOR_BUFFER_BIT, gl.GL_LINEAR)

image = Image.frombytes('RGBA', (512, 512), fbo.read(components=4))
image = image.transpose(Image.FLIP_TOP_BOTTOM)
image.save('triangle.png', format='png')

GLError                                   Traceback (most recent call last)
<ipython-input-3-b59e4f46148e> in <module>()
     46 vao.render(mode=moderngl.TRIANGLES)
     47 
---> 48 gl.glBindFramebuffer(gl.GL_READ_FRAMEBUFFER, fbo_msaa.glo)
     49 gl.glBindFramebuffer(gl.GL_DRAW_FRAMEBUFFER, fbo.glo)
     50 gl.glBlitFramebuffer(0, 0, 512, 512, 0, 0, 512, 512, gl.GL_COLOR_BUFFER_BIT, gl.GL_LINEAR)

1 frames
/usr/local/lib/python3.6/dist-packages/OpenGL/platform/baseplatform.py in __call__(self, *args, **named)
    413     def __call__( self, *args, **named ):
    414         if self.load():
--> 415             return self( *args, **named )
    416         else:
    417             try:

/usr/local/lib/python3.6/dist-packages/OpenGL/error.py in glCheckError(self, result, baseOperation, cArguments, *args)
    232                         result,
    233                         cArguments = cArguments,
--> 234                         baseOperation = baseOperation,
    235                     )
    236                 return result

GLError: GLError(
    err = 1280,
    description = b'invalid enumerant',
    baseOperation = glBindFramebuffer,
    cArguments = (GL_READ_FRAMEBUFFER, 2)
)

推荐答案

当使用纹理多样本深度缓冲区:

It works when using a Renderbuffer rather than a Texture for the multisample depth buffer:

代替

fbo_msaa = ctx.framebuffer(
       color_attachments=ctx.texture((512, 512), 4, samples=8),
       depth_attachment=ctx.depth_texture((512, 512), samples=8)
   )

fbo_msaa = ctx.framebuffer(
        color_attachments=ctx.texture((512, 512), 4, samples=8),
        depth_attachment=ctx.depth_renderbuffer((512, 512), samples=8)
    )

这可能是 ModernGL 中的错误多样本纹理深度缓冲附件.
而且错误1280(GL_INVALID_ENUM)没有任何意义,GL_READ_FRAMEBUFFER

This seams to be a bug in ModernGL, related to multisample texture depth buffer attachment.
Furthermore error 1280 (GL_INVALID_ENUM) does not make any sense, GL_READ_FRAMEBUFFER is a valid argument for glBindFramebuffer.

这篇关于如何在Moderngl EGL后端中添加深度缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆