Skip to content

Commit b18fae4

Browse files
committed
Allow overriding texture parameters
1 parent 4e285db commit b18fae4

File tree

4 files changed

+68
-64
lines changed

4 files changed

+68
-64
lines changed

demosys/effects/effect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def get_shader(self, path):
5959
"""
6060
return resources.shaders.get(path, create=True)
6161

62-
def get_texture(self, path):
62+
def get_texture(self, path, **kwargs):
6363
"""
6464
Get a shader or schedule the texture for loading.
6565
If the resource is not loaded yet, an empty texture object
@@ -68,7 +68,7 @@ def get_texture(self, path):
6868
:param path: Path to the texture in the virtual texture directory
6969
:return: Texture object
7070
"""
71-
return resources.textures.get(path, create=True)
71+
return resources.textures.get(path, create=True, **kwargs)
7272

7373
def get_track(self, name):
7474
"""

demosys/opengl/fbo.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,27 +116,27 @@ def clear(self):
116116
self.release()
117117

118118
@classmethod
119-
def create(cls, width, height, depth=False, stencil=True,
119+
def create(cls, width, height, depth=False,
120120
internal_format=GL.GL_RGBA8, format=GL.GL_RGBA, type=GL.GL_UNSIGNED_BYTE):
121121
"""
122122
Convenient shortcut for creating single color attachment FBOs
123123
124124
:param width: Color buffer width
125125
:param height: Coller buffer height
126126
:param depth: (bool) Create a depth attachment
127-
:param stencil: (bool) Create a stencil attachment
128127
:param internal_format: The internalformat of the color buffer
129128
:param format: The format of the color buffer
130129
:param type: The type of the color buffer
131130
:return: A new FBO
132131
"""
133132
fbo = FBO()
134133
fbo.bind(stack=False)
135-
c = Texture.create_2d(width, height, internal_format=internal_format, format=format, type=type)
134+
c = Texture.create_2d(width=width, height=height, internal_format=internal_format, format=format, type=type,
135+
wrap_s=GL.GL_CLAMP_TO_EDGE, wrap_t=GL.GL_CLAMP_TO_EDGE, wrap_r=GL.GL_CLAMP_TO_EDGE)
136136
fbo.add_color_attachment(c)
137137
if depth:
138-
d = Texture.create_2d(width, height, internal_format=GL.GL_DEPTH24_STENCIL8,
139-
format=GL.GL_DEPTH_COMPONENT)
138+
d = Texture.create_2d(width=width, height=height, internal_format=GL.GL_DEPTH24_STENCIL8, format=GL.GL_DEPTH_COMPONENT,
139+
wrap_s=GL.GL_CLAMP_TO_EDGE, wrap_t=GL.GL_CLAMP_TO_EDGE, wrap_r=GL.GL_CLAMP_TO_EDGE)
140140
fbo.set_depth_attachment(d)
141141
fbo.check_status()
142142
fbo.release(stack=False)

demosys/opengl/texture.py

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,34 @@
55

66
class Texture:
77
"""Represents a texture"""
8-
def __init__(self):
8+
def __init__(self, name=None, path=None, width=0, height=0, depth=0, lod=0, target=GL.GL_TEXTURE_2D,
9+
internal_format=GL.GL_RGBA8, format=GL.GL_RGBA, type=GL.GL_UNSIGNED_BYTE,
10+
mipmap=False, anisotropy=0, min_filter=GL.GL_LINEAR, mag_filter=GL.GL_LINEAR,
11+
wrap_s=GL.GL_CLAMP_TO_EDGE, wrap_t=GL.GL_CLAMP_TO_EDGE, wrap_r=GL.GL_CLAMP_TO_EDGE,
12+
):
913
"""Initialize texture without allocating data using default values"""
1014
self.texture = GL.glGenTextures(1)
1115
# dimensions
12-
self.width = 0
13-
self.height = 0
14-
self.depth = 0
16+
self.width = width
17+
self.height = height
18+
self.depth = depth
1519
# format / type
16-
self.target = GL.GL_TEXTURE_2D
17-
self.lod = 0
18-
self.internal_format = GL.GL_RGBA8
19-
self.format = GL.GL_RGBA
20-
self.type = GL.GL_UNSIGNED_BYTE
20+
self.target = target
21+
self.lod = lod
22+
self.internal_format = internal_format
23+
self.format = format
24+
self.type = type
25+
self.wrap_s = wrap_s
26+
self.wrap_t = wrap_t
27+
self.wrap_r = wrap_r
2128
# filters
22-
self.min_filter = GL.GL_LINEAR
23-
self.mag_filter = GL.GL_LINEAR
29+
self.min_filter = min_filter
30+
self.mag_filter = mag_filter
31+
self.anisotropy = anisotropy
32+
self.mipmap = mipmap
2433
# For pre-loading files
25-
self.name = None
26-
self.path = None
34+
self.name = name
35+
self.path = path
2736

2837
@property
2938
def size(self):
@@ -35,36 +44,31 @@ def size(self):
3544
return self.width, self.height
3645

3746
@classmethod
38-
def from_image(cls, path, image=None):
47+
def from_image(cls, path, image=None, **kwargs):
3948
"""
40-
Creates and image from a image file using Pillow/PIL
49+
Creates and image from a image file using Pillow/PIL.
50+
Additional parameters is passed to the texture initializer.
4151
4252
:param path: The path to the file
43-
:param image: The PIL/Pillow image object
53+
:param image: The PIL/Pillow image object (Can be None)
4454
:return: Texture object
4555
"""
46-
t = Texture()
47-
t.path = path
48-
t.name = os.path.basename(path)
56+
t = Texture(path=path, name=os.path.basename(path), **kwargs)
4957
if image:
5058
t.set_image(image)
5159
return t
5260

5361
@classmethod
54-
def create_2d(cls, width, height, internal_format=GL.GL_RGBA8, format=GL.GL_RGBA, type=GL.GL_UNSIGNED_BYTE):
62+
def create_2d(cls, **kwargs):
5563
"""
56-
Creates a 2d texture
64+
Creates a 2d texture.
65+
All parameters are passed on the texture initializer.
5766
58-
:param width: Width of the texture
59-
:param height: height of the texture
60-
:param internal_format: Internal format
61-
:param format: Format
62-
:param type: Type
6367
:return: Texture object
6468
"""
65-
t = Texture()
66-
t._build(width, height, 0, target=GL.GL_TEXTURE_2D,
67-
internal_format=internal_format, format=format, type=type, data=None)
69+
kwargs['target'] = GL.GL_TEXTURE_2D
70+
t = Texture(**kwargs)
71+
t._build()
6872
return t
6973

7074
def bind(self):
@@ -73,24 +77,15 @@ def bind(self):
7377
"""
7478
GL.glBindTexture(self.target, self.texture)
7579

76-
def _build(self, width, height, depth, target=GL.GL_TEXTURE_2D, lod=0,
77-
internal_format=GL.GL_RGBA8, format=GL.GL_RGBA, type=GL.GL_UNSIGNED_BYTE, data=None):
80+
def _build(self, data=None):
7881
"""Internal method for building the texture"""
79-
# keep track of all states
80-
self.width = width
81-
self.height = height
82-
self.depth = depth
83-
self.target = target
84-
self. lod = lod
85-
self.internal_format = internal_format
86-
self.format = format
87-
self.type = type
88-
# set states
8982
self.bind()
90-
GL.glTexParameteri(self.target, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR)
91-
GL.glTexParameteri(self.target, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR)
92-
GL.glTexParameteri(self.target, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT)
93-
GL.glTexParameteri(self.target, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT)
83+
GL.glTexParameteri(self.target, GL.GL_TEXTURE_MIN_FILTER, self.min_filter)
84+
GL.glTexParameteri(self.target, GL.GL_TEXTURE_MAG_FILTER, self.mag_filter)
85+
86+
GL.glTexParameteri(self.target, GL.GL_TEXTURE_WRAP_S, self.wrap_s)
87+
GL.glTexParameteri(self.target, GL.GL_TEXTURE_WRAP_T, self.wrap_t)
88+
GL.glTexParameteri(self.target, GL.GL_TEXTURE_WRAP_R, self.wrap_r)
9489

9590
if self.target == GL.GL_TEXTURE_2D:
9691
GL.glTexImage2D(self.target, self.lod, self.internal_format,
@@ -110,32 +105,41 @@ def set_image(self, image):
110105
111106
:param image: The PIL/Pillow image object
112107
"""
113-
"""Set image data using a PIL/Pillow image"""
114108
image_flipped = image.transpose(Image.FLIP_TOP_BOTTOM)
115109
data = image_flipped.convert("RGBA").tobytes()
116110
self.width, self.height = image.size
117111
if self.width == 1 or self.height == 1:
118112
self.target = GL.GL_TEXTURE_1D
119113
else:
120114
self.target = GL.GL_TEXTURE_2D
121-
self._build(self.width, self.height, 0, data=data, target=self.target)
115+
self._build(data=data)
122116

123-
def set_texture_repeat(self, mode):
117+
def set_texture_repeat(self, wrap_s, wrap_t, wrap_r):
124118
"""
125119
Sets the texture repeat mode
126120
127-
:param mode: Repeat mode (gl enum)
121+
:param wrap_s: Repeat mode S (glenum)
122+
:param wrap_t: Repeat mode T (glenum)
123+
:param wrap_r: Repeat mode R (glenum)
128124
"""
125+
self.wrap_s = wrap_s
126+
self.wrap_t = wrap_t
127+
self.wrap_r = wrap_r
128+
129129
self.bind()
130-
GL.glTexParameteri(self.target, GL.GL_TEXTURE_WRAP_S, mode)
131-
GL.glTexParameteri(self.target, GL.GL_TEXTURE_WRAP_T, mode)
130+
GL.glTexParameteri(self.target, GL.GL_TEXTURE_WRAP_S, self.wrap_s)
131+
GL.glTexParameteri(self.target, GL.GL_TEXTURE_WRAP_T, self.wrap_t)
132+
GL.glTexParameteri(self.target, GL.GL_TEXTURE_WRAP_R, self.wrap_r)
132133

133-
def set_interpolation(self, mode):
134+
def set_interpolation(self, min_filter, mag_filter):
134135
"""
135136
Sets the texture interpolation mode
136137
137-
:param mode: Interpolation mode (gl enum)
138+
:param min_filter: Min filter mode (glenum)
139+
:param mag_filter: Max filter mode (glenum)
138140
"""
141+
self.min_filter = min_filter
142+
self.mag_filter = mag_filter
139143
self.bind()
140-
GL.glTexParameteri(self.target, GL.GL_TEXTURE_MIN_FILTER, mode)
141-
GL.glTexParameteri(self.target, GL.GL_TEXTURE_MAG_FILTER, mode)
144+
GL.glTexParameteri(self.target, GL.GL_TEXTURE_MIN_FILTER, self.min_filter)
145+
GL.glTexParameteri(self.target, GL.GL_TEXTURE_MAG_FILTER, self.mag_filter)

demosys/resources/textures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def count(self):
2020
"""
2121
return len(self.textures)
2222

23-
def get(self, path, create=False):
23+
def get(self, path, create=False, **kwargs):
2424
"""
2525
Get or create a texture object.
2626
This may return an empty object that will be filled during load
@@ -32,7 +32,7 @@ def get(self, path, create=False):
3232
"""
3333
texture = self.textures.get(path)
3434
if create and not texture:
35-
texture = Texture.from_image(path)
35+
texture = Texture.from_image(path, **kwargs)
3636
self.textures[path] = texture
3737
return texture
3838

0 commit comments

Comments
 (0)