From 0aa376776f5bac0f54cc7da3bf016a40e920ea8e Mon Sep 17 00:00:00 2001 From: Einar Forselv Date: Sat, 20 Jun 2020 20:15:15 +0200 Subject: [PATCH] Add missing context flags + point size --- arcade/gl/context.py | 68 +++++++++++++++++++++++++----- tests/unit2/test_opengl_context.py | 17 +++++++- 2 files changed, 74 insertions(+), 11 deletions(-) diff --git a/arcade/gl/context.py b/arcade/gl/context.py index ac086f8b8..d9dc37e47 100644 --- a/arcade/gl/context.py +++ b/arcade/gl/context.py @@ -3,7 +3,7 @@ import logging import weakref from pathlib import Path -from typing import Any, Dict, List, Tuple, Union, Sequence +from typing import Any, Dict, List, Tuple, Union, Sequence, Set # import pyglet from pyglet.window import Window @@ -42,12 +42,10 @@ class Context: MIRRORED_REPEAT = gl.GL_MIRRORED_REPEAT # Flags - # NOTHING BLEND = gl.GL_BLEND - # DEPTH_TEST - # CULL_FACE - # RASTERIZER_DISCARD - # PROGRAM_POINT_SIZE + DEPTH_TEST = gl.GL_DEPTH_TEST + CULL_FACE = gl.GL_CULL_FACE + PROGRAM_POINT_SIZE = gl.GL_PROGRAM_POINT_SIZE # Blend functions ZERO = 0x0000 @@ -76,6 +74,7 @@ class Context: BLEND_DEFAULT = 0x0302, 0x0303 BLEND_ADDITIVE = 0x0001, 0x0001 BLEND_PREMULTIPLIED_ALPHA = 0x0302, 0x0001 + # VertexArray: Primitives POINTS = gl.GL_POINTS # 0 LINES = gl.GL_LINES # 1 @@ -112,6 +111,8 @@ def __init__(self, window): # States self._blend_func = self.BLEND_DEFAULT + self._point_size = 1.0 + self._flags = set() @property def window(self) -> Window: @@ -147,13 +148,47 @@ def activate(cls, ctx: 'Context'): """Mark this context as the currently active one""" cls.active = ctx - def enable(self, flag: int): + def enable(self, *args): """Enables a context flag""" - gl.glEnable(flag) + self._flags.update(args) + + for flag in args: + gl.glEnable(flag) + + def enable_only(self, *args): + """Enable only some flags. This will disable all other flags""" + self._flags = set(args) + + if self.BLEND in self._flags: + gl.glEnable(self.BLEND) + else: + gl.glDisable(self.BLEND) + + if self.DEPTH_TEST in self._flags: + gl.glEnable(self.DEPTH_TEST) + else: + gl.glDisable(self.DEPTH_TEST) + + if self.CULL_FACE in self._flags: + gl.glEnable(self.CULL_FACE) + else: + gl.glDisable(self.CULL_FACE) + + if self.PROGRAM_POINT_SIZE in self._flags: + gl.glEnable(self.PROGRAM_POINT_SIZE) + else: + gl.glDisable(self.PROGRAM_POINT_SIZE) - def disable(self, flag: int): + def disable(self, *args): """Disable a context flag""" - gl.glDisable(flag) + self._flags -= set(args) + + for flag in args: + gl.glDisable(flag) + + def is_enabled(self, flag) -> bool: + """Check if a context flag is enabled""" + return flag in self._flags @property def blend_func(self) -> Tuple[int, int]: @@ -165,6 +200,19 @@ def blend_func(self, value: Tuple[int, int]): self._blend_func = value gl.glBlendFunc(value[0], value[1]) + @property + def point_size(self) -> float: + """float: Get or set the point size. + + For this to take effect the ``PROGRAM_POINT_SIZE`` context flag has to be enabled. + """ + gl.glPointSize(self._point_size) + return self._point_size + + @point_size.setter + def point_size(self, value: float): + self._point_size = value + def buffer(self, *, data: Optional[Any] = None, reserve: int = 0, usage: str = 'static') -> Buffer: """Create a new OpenGL Buffer object. diff --git a/tests/unit2/test_opengl_context.py b/tests/unit2/test_opengl_context.py index ff9e4e643..e3119bc12 100644 --- a/tests/unit2/test_opengl_context.py +++ b/tests/unit2/test_opengl_context.py @@ -24,7 +24,22 @@ def test_ctx(ctx): assert ctx.limits.MAX_ARRAY_TEXTURE_LAYERS >= 256 assert ctx.blend_func == ctx.BLEND_DEFAULT - ctx.enable(ctx.BLEND) ctx.blend_func = ctx.BLEND_PREMULTIPLIED_ALPHA assert ctx.blend_func == ctx.BLEND_PREMULTIPLIED_ALPHA + + +def test_enable_disable(ctx): + # Blend is enabled by default + assert ctx.is_enabled(ctx.BLEND) + ctx.enable_only() + assert len(ctx._flags) == 0 + + ctx.enable(ctx.BLEND) + ctx.enable(ctx.BLEND, ctx.DEPTH_TEST, ctx.CULL_FACE) + assert ctx.is_enabled(ctx.BLEND) + assert ctx.is_enabled(ctx.DEPTH_TEST) + assert ctx.is_enabled(ctx.CULL_FACE) + ctx.disable(ctx.BLEND) + assert ctx.is_enabled(ctx.BLEND) is False + assert len(ctx._flags) == 2