Skip to content

Commit c05e294

Browse files
Re-write texture module.
Remove pil_texture, rolled into texture.py. Make texture more like vertex_buffer. Update texture example.
1 parent 99bd75d commit c05e294

File tree

3 files changed

+509
-442
lines changed

3 files changed

+509
-442
lines changed

pygly/examples/scene_texture.py

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,13 @@ def initialise( self ):
4747
self.current_texture = 0
4848

4949
# load some from a directory using PIL
50-
self.load_texture_directory(
51-
os.path.join( os.path.dirname( __file__ ), 'data/textures' )
52-
)
50+
image_directory = os.path.join( os.path.dirname( __file__ ), 'data/textures' )
51+
self.load_texture_directory(image_directory)
5352

5453
# generate some random ones using numpy
5554
self.generate_random_textures()
5655

5756
def load_texture_directory( self, directory ):
58-
import pygly.pil_texture
59-
6057
print 'Loading images from', directory
6158

6259
extensions = [
@@ -70,31 +67,61 @@ def load_texture_directory( self, directory ):
7067

7168
for filename in os.listdir( directory ):
7269
name, extension = os.path.splitext( filename )
70+
7371
if extension not in extensions:
7472
continue
7573

7674
try:
7775
print filename,
7876
full_path = '%s/%s' % (directory, filename)
79-
80-
image = Image.open( full_path )
81-
print image.format, image.mode, image.getbands()
82-
83-
texture = Texture2D()
84-
texture.bind()
85-
texture.set_min_mag_filter( GL.GL_NEAREST, GL.GL_NEAREST )
86-
pygly.pil_texture.set_pil_image( texture, image )
87-
texture.unbind()
88-
89-
self.textures.append( (filename, texture) )
90-
except IOError as e:
77+
texture = Texture2D.from_file(full_path)
78+
self.textures.append((filename, texture))
79+
except Exception as e:
9180
print 'Exception:', e
9281
# ensure we unbound our textures
9382
GL.glBindTexture( GL.GL_TEXTURE_2D, 0 )
9483

9584
def generate_random_textures( self ):
9685
import numpy
9786

87+
def float32_rgb():
88+
data = numpy.linspace(0.0, 1.0, 32 * 32 * 3).astype('float32')
89+
data.shape = (32,32,-1)
90+
texture = Texture2D(data=data)
91+
return texture
92+
93+
self.textures.append(('Gradient RGB (float32)', float32_rgb()))
94+
95+
def uint8_rgb():
96+
data = numpy.linspace(0, 255, 32 * 32 * 3).astype('uint8')
97+
data.shape = (32,32,-1)
98+
texture = Texture2D(data=data)
99+
return texture
100+
101+
self.textures.append(('Gradient RGB (uint8)', uint8_rgb()))
102+
103+
def random_rgb():
104+
data = numpy.random.random_integers(0, 255, 32 * 32 * 3).astype('uint8')
105+
data.shape = (32,32,-1)
106+
texture = Texture2D(data=data)
107+
return texture
108+
109+
self.textures.append(('Random RGB (uint8)', random_rgb()))
110+
111+
def random_luminance():
112+
data = numpy.random.random_integers(0, 255, 32 * 32).astype('uint8')
113+
data.shape = (32,32,-1)
114+
115+
from pygly import gl
116+
if gl.is_legacy():
117+
texture = Texture2D(data=data, internal_format=GL.GL_LUMINANCE)
118+
else:
119+
texture = Texture2D(data=data, swizzle='rrr', internal_format=GL.GL_RGB)
120+
return texture
121+
122+
self.textures.append(('Random Luminance (uint32)', random_luminance()))
123+
124+
"""
98125
def random_rgb():
99126
# create a random RGB texture
100127
name = 'Red Shade RGB'
@@ -132,17 +159,22 @@ def random_luminance():
132159
texture.unbind()
133160
self.textures.append( (name, texture) )
134161
random_luminance()
162+
"""
135163

136164
def on_key_pressed( self, key ):
137165
if key == "right":
138166
self.current_texture += 1
139167
self.current_texture %= len( self.textures )
140-
print self.textures[ self.current_texture ][ 0 ]
168+
169+
name, texture = self.textures[ self.current_texture ]
170+
print name, texture.internal_format
141171
elif key == "left":
142172
self.current_texture -= 1
143173
if self.current_texture < 0:
144174
self.current_texture = len( self.textures ) - 1
145-
print self.textures[ self.current_texture ][ 0 ]
175+
176+
name, texture = self.textures[ self.current_texture ]
177+
print name, texture.internal_format
146178

147179
def on_window_resized( self, width, height ):
148180
# update the viewport

pygly/pil_texture.py

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)