Skip to content

Commit 7b651d5

Browse files
committed
Store context ref in TextureBase
1 parent bbbc0a0 commit 7b651d5

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

demosys/opengl/texture.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ class BaseTexture:
99
"""
1010
Wraps the basic functionality of the ModernGL methods
1111
"""
12+
ctx = None # type: mgl.Context
1213

1314
def __init__(self):
1415
self._texture = None
16+
BaseTexture.ctx = context.ctx()
1517

1618
def use(self, location=0):
1719
"""
@@ -44,7 +46,11 @@ def dtype(self) -> str:
4446
@property
4547
def depth(self) -> bool:
4648
"""Is this a depth texture?"""
47-
return self.depth
49+
return self._texture.depth
50+
51+
@property
52+
def swizzle(self):
53+
return self._texture.swizzle
4854

4955
@property
5056
def mgl_instance(self):
@@ -114,9 +120,9 @@ def create(cls, size, components, data=None, samples=0, alignment=1, dtype='f1',
114120
115121
:return: Texture object
116122
"""
117-
t = Texture2D(path="dynamic", mipmap=mipmap)
123+
texture = Texture2D(path="dynamic", mipmap=mipmap)
118124

119-
t._texture = context.ctx().texture(
125+
texture._texture = cls.ctx.texture(
120126
size,
121127
components,
122128
data=data,
@@ -126,9 +132,9 @@ def create(cls, size, components, data=None, samples=0, alignment=1, dtype='f1',
126132
)
127133

128134
if mipmap:
129-
t.build_mipmaps()
135+
texture.build_mipmaps()
130136

131-
return t
137+
return texture
132138

133139
def build_mipmaps(self, base=0, max_level=1000):
134140
"""
@@ -150,10 +156,10 @@ def from_image(cls, path, image=None, **kwargs):
150156
:param image: The PIL/Pillow image object (Can be None)
151157
:return: Texture object
152158
"""
153-
t = Texture2D(path=path, **kwargs)
159+
texture = Texture2D(path=path, **kwargs)
154160
if image:
155-
t.set_image(image)
156-
return t
161+
texture.set_image(image)
162+
return texture
157163

158164
def set_image(self, image, flip=True):
159165
"""
@@ -165,7 +171,7 @@ def set_image(self, image, flip=True):
165171
if flip:
166172
image = image.transpose(Image.FLIP_TOP_BOTTOM)
167173

168-
self._texture = context.ctx().texture(
174+
self._texture = self.ctx.texture(
169175
image.size,
170176
4,
171177
image.convert("RGBA").tobytes(),
@@ -195,7 +201,7 @@ class DepthTexture(BaseTexture):
195201
quad = None
196202
shader = None
197203

198-
def __init__(self, size, data=None, samples=0, alignment=4):
204+
def __init__(self, size, data=None, samples=0, alignment=8):
199205
"""
200206
Create a depth texture
201207
@@ -205,7 +211,7 @@ def __init__(self, size, data=None, samples=0, alignment=4):
205211
:param alignment: The byte alignment 1, 2, 4 or 8.
206212
"""
207213
super().__init__()
208-
self._texture = context.ctx().depth_texture(size, data=data, samples=samples, alignment=alignment)
214+
self._texture = self.ctx.depth_texture(size, data=data, samples=samples, alignment=alignment)
209215
_init_depth_texture_draw()
210216

211217
def draw(self, near, far, pos=(0.0, 0.0), scale=(1.0, 1.0)):

0 commit comments

Comments
 (0)