Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions arcade/gl/framebuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,10 @@ def _get_scissor(self) -> Optional[Tuple[int, int, int, int]]:
def _set_scissor(self, value):
self._scissor = value

if self._scissor is not None:
# If the framebuffer is bound we need to set the scissor box.
# Otherwise it will be set on use()
if self._scissor is None:
if self._ctx.active_framebuffer == self:
gl.glScissor(*self._viewport)
else:
if self._ctx.active_framebuffer == self:
gl.glScissor(*self._scissor)

Expand Down Expand Up @@ -351,11 +352,12 @@ def clear(
"""
Clears the framebuffer::

# Clear framebuffer using the color red in normalized form
fbo.clear(color=(1.0, 0.0, 0.0, 1.0), normalized=True)
# Clear the framebuffer using arcade's colors (not normalized)
fb.clear(color=arcade.color.WHITE)

# Clear framebuffer using the color red in normalized form
fbo.clear(color=(1.0, 0.0, 0.0, 1.0), normalized=True)

If the background color is an ``RGB`` value instead of ``RGBA```
we assume alpha value 255.

Expand All @@ -365,8 +367,12 @@ def clear(
:param Tuple[int, int, int, int] viewport: The viewport range to clear
"""
with self.activate():
scissor_values = self._scissor

if viewport:
gl.glScissor(*viewport)
self.scissor = viewport
else:
self.scissor = None

if normalized:
# If the colors are already normalized we can pass them right in
Expand All @@ -388,11 +394,7 @@ def clear(
else:
gl.glClear(gl.GL_COLOR_BUFFER_BIT)

if viewport:
if self._scissor is None:
gl.glScissor(*self._viewport)
else:
gl.glScissor(*self._scissor)
self.scissor = scissor_values

def read(
self, *, viewport=None, components=3, attachment=0, dtype="f1"
Expand Down
25 changes: 25 additions & 0 deletions tests/unit2/test_opengl_framebuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ def test_clear(ctx):
assert ctx.active_framebuffer == ctx.screen


def test_clear_viewport(ctx):
fb = create(ctx, 4, 4, components=1)
fb.clear(color=(64, 64, 64, 64))
assert fb.read(components=1) == b'\x40' * 16

# Clear only the center pixels and verify that the rest is unchanged
fb.clear()
fb.clear(color=(255, 255, 255, 255), viewport=(1, 1, 2, 2))
expected = (
b'\x00\x00\x00\x00'
b'\x00\xff\xff\x00'
b'\x00\xff\xff\x00'
b'\x00\x00\x00\x00'
)
assert bytes(fb.read(components=1)) == expected


def test_clear_with_scissor(ctx):
fb = create(ctx, 4, 4, components=1)
fb.clear()
fb.scissor = 1, 1, 2, 2
fb.clear(color=(255, 255, 255, 255))
assert bytes(fb.read(components=1)) == b'\xff' * 16


def test_multi_attachment(ctx):
"""Create framebuffers with multiple layers"""
for i in range(ctx.info.MAX_COLOR_ATTACHMENTS):
Expand Down