-
Notifications
You must be signed in to change notification settings - Fork 36
/
texture.py
58 lines (49 loc) · 2.28 KB
/
texture.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import pygame as pg
import moderngl as mgl
import glm
class Texture:
def __init__(self, app):
self.app = app
self.ctx = app.ctx
self.textures = {}
self.textures[0] = self.get_texture(path='textures/img.png')
self.textures[1] = self.get_texture(path='textures/img_1.png')
self.textures[2] = self.get_texture(path='textures/img_2.png')
self.textures['cat'] = self.get_texture(path='objects/cat/20430_cat_diff_v1.jpg')
self.textures['skybox'] = self.get_texture_cube(dir_path='textures/skybox1/', ext='png')
self.textures['depth_texture'] = self.get_depth_texture()
def get_depth_texture(self):
depth_texture = self.ctx.depth_texture(self.app.WIN_SIZE)
depth_texture.repeat_x = False
depth_texture.repeat_y = False
return depth_texture
def get_texture_cube(self, dir_path, ext='png'):
faces = ['right', 'left', 'top', 'bottom'] + ['front', 'back'][::-1]
# textures = [pg.image.load(dir_path + f'{face}.{ext}').convert() for face in faces]
textures = []
for face in faces:
texture = pg.image.load(dir_path + f'{face}.{ext}').convert()
if face in ['right', 'left', 'front', 'back']:
texture = pg.transform.flip(texture, flip_x=True, flip_y=False)
else:
texture = pg.transform.flip(texture, flip_x=False, flip_y=True)
textures.append(texture)
size = textures[0].get_size()
texture_cube = self.ctx.texture_cube(size=size, components=3, data=None)
for i in range(6):
texture_data = pg.image.tostring(textures[i], 'RGB')
texture_cube.write(face=i, data=texture_data)
return texture_cube
def get_texture(self, path):
texture = pg.image.load(path).convert()
texture = pg.transform.flip(texture, flip_x=False, flip_y=True)
texture = self.ctx.texture(size=texture.get_size(), components=3,
data=pg.image.tostring(texture, 'RGB'))
# mipmaps
texture.filter = (mgl.LINEAR_MIPMAP_LINEAR, mgl.LINEAR)
texture.build_mipmaps()
# AF
texture.anisotropy = 32.0
return texture
def destroy(self):
[tex.release() for tex in self.textures.values()]