Skip to content

Commit 7d6481a

Browse files
committed
Screenshot feature should use ModernGL
1 parent db9aa6b commit 7d6481a

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

demosys/opengl/fbo.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ def create(size, components=4, depth=False, dtype='f1', layers=1):
7777
instance = FBO()
7878

7979
# Add N layers of color attachments
80-
for layer in range(layers):
81-
c = Texture2D.create(size, components, dtype=dtype)
82-
instance.color_buffers.append(c)
80+
for _ in range(layers):
81+
tex = Texture2D.create(size, components, dtype=dtype)
82+
instance.color_buffers.append(tex)
8383

8484
# Set depth attachment is specified
8585
if depth:
@@ -92,6 +92,24 @@ def create(size, components=4, depth=False, dtype='f1', layers=1):
9292

9393
return instance
9494

95+
def read(self, viewport=None, components=3, attachment=0, alignment=1, dtype='f1') -> bytes:
96+
"""
97+
Read the content of the framebuffer.
98+
99+
:param viewport: (tuple) The viewport
100+
:param components: The number of components to read.
101+
:param attachment: The color attachment
102+
:param alignment: The byte alignment of the pixels
103+
:param dtype: (str) dtype
104+
"""
105+
return self.fbo.read(
106+
viewport=viewport,
107+
components=components,
108+
attachment=attachment,
109+
alignment=alignment,
110+
dtype=dtype,
111+
)
112+
95113
@property
96114
def size(self):
97115
"""
@@ -102,7 +120,6 @@ def size(self):
102120
103121
:return: (w, h) tuple representing the size in pixels
104122
"""
105-
# FIXME: How do we deal with attachments of different sizes?
106123
if self.color_buffers:
107124
return self.color_buffers[0].size
108125

demosys/view/screenshot.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
from datetime import datetime
21
import os
2+
from datetime import datetime
33

4-
from OpenGL import GL
54
from PIL import Image
65

76
from demosys.conf import settings
87
from demosys import context
98

109

11-
def create(format='png', name=None):
10+
def create(file_format='png', name=None):
1211
"""
1312
Create a screenshot
14-
:param format: formats supported by PIL (png, jpeg etc)
13+
:param file_format: formats supported by PIL (png, jpeg etc)
1514
"""
1615
dest = ""
1716
if settings.SCREENSHOT_PATH:
@@ -22,17 +21,12 @@ def create(format='png', name=None):
2221
else:
2322
print("SCREENSHOT_PATH not defined in settings. Using cwd as fallback.")
2423

25-
# x, y, width, height = GL.glGetIntegerv(GL.GL_VIEWPORT)
26-
x, y, width, height = 0, 0, context.WINDOW.buffer_width, context.WINDOW.buffer_height
27-
28-
GL.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1)
29-
30-
data = GL.glReadPixels(x, y, width, height, GL.GL_RGB, GL.GL_UNSIGNED_BYTE)
31-
32-
image = Image.frombytes("RGB", (width, height), data)
24+
fbo = context.ctx().screen
25+
image = Image.frombytes("RGB", (fbo.width, fbo.height), fbo.read())
3326
image = image.transpose(Image.FLIP_TOP_BOTTOM)
27+
3428
if not name:
35-
name = "{}.{}".format(datetime.now().strftime("%Y-%m-%d-%H-%M-%S"), format)
29+
name = "{}.{}".format(datetime.now().strftime("%Y-%m-%d-%H-%M-%S-%f"), file_format)
3630

3731
print("Creating screenshot:", name)
38-
image.save(os.path.join(dest, name), format=format)
32+
image.save(os.path.join(dest, name), format=file_format)

0 commit comments

Comments
 (0)