Skip to content

Commit bceff33

Browse files
committed
Support loading 1D textures
1 parent 239ca1f commit bceff33

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

demosys/opengl/shader.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ def uniform_sampler_2d(self, unit, name, texture):
195195
texture.bind()
196196
GL.glUniform1i(uniform.location, unit)
197197

198+
def uniform_sampler_1d(self, unit, name, texture):
199+
uniform = self.uniform(name)
200+
GL.glActiveTexture(GL.GL_TEXTURE0 + unit)
201+
texture.bind()
202+
GL.glUniform1i(uniform.location, unit)
203+
198204

199205
class ShaderSource:
200206
def __init__(self, type, name, source):

demosys/opengl/texture.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,29 @@ def _build(self, width, height, depth, target=GL.GL_TEXTURE_2D, lod=0,
6464
GL.glTexParameteri(self.target, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR)
6565
GL.glTexParameteri(self.target, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT)
6666
GL.glTexParameteri(self.target, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT)
67-
GL.glTexImage2D(self.target, self.lod, self.internal_format,
68-
self.width, self.height, 0,
69-
self.format, self.type, data)
70-
# GL.glBindTexture(GL.GL_TEXTURE_2D, 0)
67+
68+
if self.target == GL.GL_TEXTURE_2D:
69+
GL.glTexImage2D(self.target, self.lod, self.internal_format,
70+
self.width, self.height, 0,
71+
self.format, self.type, data)
72+
elif self.target == GL.GL_TEXTURE_1D:
73+
if self.width > self.height:
74+
GL.glTexImage1D(self.target, self.lod, self.internal_format,
75+
self.width, 0, self.format, self.type, data)
76+
else:
77+
GL.glTexImage1D(self.target, self.lod, self.internal_format,
78+
self.height, 0, self.format, self.type, data)
7179

7280
def set_image(self, image):
7381
"""Set image data using a PIL/Pillow image"""
7482
image_flipped = image.transpose(Image.FLIP_TOP_BOTTOM)
7583
data = image_flipped.convert("RGBA").tobytes()
7684
self.width, self.height = image.size
77-
self._build(self.width, self.height, 0, data=data)
85+
if self.width == 1 or self.height == 1:
86+
self.target = GL.GL_TEXTURE_1D
87+
else:
88+
self.target = GL.GL_TEXTURE_2D
89+
self._build(self.width, self.height, 0, data=data, target=self.target)
7890

7991
def set_texture_repeat(self, mode):
8092
self.bind()

0 commit comments

Comments
 (0)