-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture.py
More file actions
108 lines (86 loc) · 4.34 KB
/
Copy pathtexture.py
File metadata and controls
108 lines (86 loc) · 4.34 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
""" Taken from the TPs, but with additions/modifications """
import OpenGL.GL as GL # standard Python OpenGL wrapper
from PIL import Image # load texture maps
import numpy as np
# -------------- OpenGL Texture Wrapper ---------------------------------------
class Texture:
""" Helper class to create and automatically destroy textures
Modified to allow loading Texture from numpy array,
as well as textures of any internal format (e.g. single-channel textures)
"""
def __init__(self, tex_file, wrap_mode=GL.GL_REPEAT,
mag_filter=GL.GL_LINEAR, min_filter=GL.GL_LINEAR_MIPMAP_LINEAR,
tex_type=GL.GL_TEXTURE_2D, internal_format=GL.GL_RGBA, format=GL.GL_RGBA,
data_type=GL.GL_UNSIGNED_BYTE):
self.glid = GL.glGenTextures(1)
self.type = tex_type
if type(tex_file) is np.ndarray:
tex_bytes = tex_file.tobytes()
tex_string = str(type(tex_file))
height, width = tex_file.shape[:2]
else:
try:
# imports image as a numpy array in exactly right format
tex = Image.open(tex_file).convert('RGBA')
tex_bytes = tex.tobytes()
tex_string = tex_file
width, height = tex.width, tex.height
except FileNotFoundError:
print("ERROR: unable to load texture file %s" % tex_file)
GL.glBindTexture(tex_type, self.glid)
GL.glTexImage2D(tex_type, 0, internal_format, width, height,
0, format, data_type, tex_bytes)
GL.glTexParameteri(tex_type, GL.GL_TEXTURE_WRAP_S, wrap_mode)
GL.glTexParameteri(tex_type, GL.GL_TEXTURE_WRAP_T, wrap_mode)
GL.glTexParameteri(tex_type, GL.GL_TEXTURE_MIN_FILTER, min_filter)
GL.glTexParameteri(tex_type, GL.GL_TEXTURE_MAG_FILTER, mag_filter)
GL.glGenerateMipmap(tex_type)
print(f'Loaded texture {tex_string} ({width}x{height}'
f' wrap={str(wrap_mode).split()[0]}'
f' min={str(min_filter).split()[0]}'
f' mag={str(mag_filter).split()[0]})')
# added the ability to manually bind the texture to a texture unit
def bind(self, texture_unit):
GL.glActiveTexture(texture_unit)
GL.glBindTexture(self.type, self.glid)
def __del__(self): # delete GL texture from GPU when object dies
GL.glDeleteTextures(self.glid)
class CubeMap:
""" Similar to Texture, but for a cube map """
def __init__(self, cube):
""" cube should be tuple of np.ndarray (dtype=np.uint8) like
(right, left, top, bottom, back, front)
"""
self.glid = GL.glGenTextures(1)
tex_type = GL.GL_TEXTURE_CUBE_MAP
self.type = tex_type
GL.glBindTexture(tex_type, self.glid)
for i, face in enumerate(cube):
width, height = face.shape[:2]
GL.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL.GL_RGB, width, height, 0,
GL.GL_RGB, GL.GL_UNSIGNED_BYTE, face.tobytes())
GL.glTexParameteri(tex_type, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE)
GL.glTexParameteri(tex_type, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE)
GL.glTexParameteri(tex_type, GL.GL_TEXTURE_WRAP_R, GL.GL_CLAMP_TO_EDGE)
GL.glTexParameteri(tex_type, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR)
GL.glTexParameteri(tex_type, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR)
print("loaded a cubemap :)")
def bind(self, texture_unit):
""" Method to manually bind texture to the shaders
"""
GL.glActiveTexture(texture_unit)
GL.glBindTexture(self.type, self.glid)
def __del__(self):
GL.glDeleteTextures(self.glid)
# -------------- Textured mesh decorator --------------------------------------
class Textured:
""" Drawable mesh decorator that activates and binds OpenGL textures """
def __init__(self, drawable, **textures):
self.drawable = drawable
self.textures = textures
def draw(self, primitives=GL.GL_TRIANGLES, **uniforms):
for index, (name, texture) in enumerate(self.textures.items()):
GL.glActiveTexture(GL.GL_TEXTURE0 + index)
GL.glBindTexture(texture.type, texture.glid)
uniforms[name] = index
self.drawable.draw(primitives=primitives, **uniforms)