0

After creating a headless context with

ctx = create_context(standalone=True)
ctx.viewport = (0, 0, 500, 500)

and creating a vertex array, I wish to get the image data of vao.render(). Is there any way to do this?

Edit: Actually, I'm not sure how to even get the image data for a normal context without just screenshotting it. Is there a way to implement either version of the problem?

MichaelT572
  • 178
  • 1
  • 9

1 Answers1

0

Headless in moderngl doesn't provide a default framebuffer. You need to make one.

ctx = create_context(standalone=True)
# 100x100 RGBA8 texture attached to a framebuffer
fbo = ctx.framebuffer(
    color_attachments=[ctx.texture(size=(100, 100), components=4)],
)
fbo.use()
# Fake some rendering (clear with red)
fbo.clear(1.0, 0.0, 0.0, 1.0)
# Byte data of the framebuffer we can for example
# dump into a Pillow image and show/save
data = fbo.read(components=4, dtype="f1")
Grimmy
  • 3,992
  • 22
  • 25