@@ -47,16 +47,13 @@ def initialise( self ):
47
47
self .current_texture = 0
48
48
49
49
# 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 )
53
52
54
53
# generate some random ones using numpy
55
54
self .generate_random_textures ()
56
55
57
56
def load_texture_directory ( self , directory ):
58
- import pygly .pil_texture
59
-
60
57
print 'Loading images from' , directory
61
58
62
59
extensions = [
@@ -70,31 +67,61 @@ def load_texture_directory( self, directory ):
70
67
71
68
for filename in os .listdir ( directory ):
72
69
name , extension = os .path .splitext ( filename )
70
+
73
71
if extension not in extensions :
74
72
continue
75
73
76
74
try :
77
75
print filename ,
78
76
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 :
91
80
print 'Exception:' , e
92
81
# ensure we unbound our textures
93
82
GL .glBindTexture ( GL .GL_TEXTURE_2D , 0 )
94
83
95
84
def generate_random_textures ( self ):
96
85
import numpy
97
86
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
+ """
98
125
def random_rgb():
99
126
# create a random RGB texture
100
127
name = 'Red Shade RGB'
@@ -132,17 +159,22 @@ def random_luminance():
132
159
texture.unbind()
133
160
self.textures.append( (name, texture) )
134
161
random_luminance()
162
+ """
135
163
136
164
def on_key_pressed ( self , key ):
137
165
if key == "right" :
138
166
self .current_texture += 1
139
167
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
141
171
elif key == "left" :
142
172
self .current_texture -= 1
143
173
if self .current_texture < 0 :
144
174
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
146
178
147
179
def on_window_resized ( self , width , height ):
148
180
# update the viewport
0 commit comments