55
66class 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 )
0 commit comments