How to add a depth buffer in Moderngl EGL backend?

时光怂恿深爱的人放手 提交于 2020-05-15 08:08:57

问题


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.

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)
)

回答1:


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

Instead of

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

do

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

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.



来源:https://stackoverflow.com/questions/61304886/how-to-add-a-depth-buffer-in-moderngl-egl-backend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!