-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture.py
42 lines (34 loc) · 1.32 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
import sys
from scipy import misc
from OpenGL.GL import *
class Texture:
""" Simple wrapper over bytes array representing image.
Reads texture from file system and prepares it to work with OpenGL.
"""
def __init__(self, target, filename: str):
self.target = target
self.filename = filename
self.texture_obj = None
self.image = None
self.blob = None
@property
def ready(self):
return self.blob and self.texture_obj
def load(self):
try:
self.blob = misc.imread(self.filename)
except Exception as e:
print("Error occurred: " + str(e), file=sys.stderr)
return False
self.texture_obj = glGenTextures(1) # generate one texture
h, w, _ = self.blob.shape
glBindTexture(self.target, self.texture_obj)
glTexImage2D(self.target, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, self.blob.flatten())
glTexParameterf(self.target, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameterf(self.target, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glBindTexture(self.target, 0)
return True
def bind(self, texture_unit):
""" Enables specified texture unit and binds current texture """
glActiveTexture(texture_unit)
glBindTexture(self.target, self.texture_obj)