-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture_manager.py
More file actions
64 lines (53 loc) · 2.75 KB
/
texture_manager.py
File metadata and controls
64 lines (53 loc) · 2.75 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
import ctypes
import pyglet
import pyglet.gl as gl
#
# Texture_manager - Handles the texture encoding of all blocks. Class
# is instantiated in main.py, and one instance of the manager is used
# by all blocks. Formats textures for samping and generates mipmaps.
#
class Texture_manager:
#
# __init__ - Constructor, on instantiation of a Texture_manager.
#
def __init__(self, texture_width, texture_height, max_textures):
self.texture_width = texture_width
self.texture_height = texture_height
self.max_textures = max_textures
self.textures = [] # list of all added textures
##### create 3D texture array ###############################
self.texture_array = gl.GLuint(0)
gl.glGenTextures(1, self.texture_array)
gl.glBindTexture(gl.GL_TEXTURE_2D_ARRAY, self.texture_array)
# stop OpenGL from linear interpolation of magnified pixels, producing blurriness.
# instead, we tell OpenGL to select the nearest pixel's color when sampling.
gl.glTexParameteri(gl.GL_TEXTURE_2D_ARRAY, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST) # Magnification
# stop OpenGL from linear interpolation of far-away pixels, since it cannot handle partial transparency.
# instead, we tell OpenGL to select the nearest pixel's color when sampling.
gl.glTexParameteri(gl.GL_TEXTURE_2D_ARRAY, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST) # Minification
gl.glTexImage3D(
gl.GL_TEXTURE_2D_ARRAY, 0, gl.GL_RGBA, # target, level, internal format
self.texture_width, self.texture_height, self.max_textures, # width, height, depth
0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, None # border, format, type, pixels
)
#
# generate_mipmaps - Make smaller textures for further distances.
#
def generate_mipmaps(self):
gl.glGenerateMipmap(gl.GL_TEXTURE_2D_ARRAY)
#
# add_texture - Writes a new texture to the 3D array if not already handled.
#
def add_texture(self, texture):
if not texture in self.textures:
self.textures.append(texture) # add texture to list
texture_image = pyglet.image.load(f"textures/{texture}.png").get_image_data()
gl.glBindTexture(gl.GL_TEXTURE_2D_ARRAY, self.texture_array) # ensure texture array is bound
# copy texture data into array
gl.glTexSubImage3D(
gl.GL_TEXTURE_2D_ARRAY, 0, # target, level
0, 0, self.textures.index(texture), # x offset, y offset, z offset (layer)
self.texture_width, self.texture_height, 1, # width, height, depth
gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, # format, type
texture_image.get_data("RGBA", texture_image.width * 4) # pixels (data)
)