-
Notifications
You must be signed in to change notification settings - Fork 68
/
textures.py
40 lines (34 loc) · 1.35 KB
/
textures.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import pygame as pg
import moderngl as mgl
class Textures:
def __init__(self, app):
self.app = app
self.ctx = app.ctx
# load textures
self.texture_0 = self.load('frame.png')
self.texture_1 = self.load('water.png')
self.texture_array_0 = self.load('tex_array_0.png', is_tex_array=True)
# assign texture unit
self.texture_0.use(location=0)
self.texture_array_0.use(location=1)
self.texture_1.use(location=2)
def load(self, file_name, is_tex_array=False):
texture = pg.image.load(f'assets/{file_name}')
texture = pg.transform.flip(texture, flip_x=True, flip_y=False)
if is_tex_array:
num_layers = 3 * texture.get_height() // texture.get_width() # 3 textures per layer
texture = self.app.ctx.texture_array(
size=(texture.get_width(), texture.get_height() // num_layers, num_layers),
components=4,
data=pg.image.tostring(texture, 'RGBA')
)
else:
texture = self.ctx.texture(
size=texture.get_size(),
components=4,
data=pg.image.tostring(texture, 'RGBA', False)
)
texture.anisotropy = 32.0
texture.build_mipmaps()
texture.filter = (mgl.NEAREST, mgl.NEAREST)
return texture