forked from pchalas1/libWall
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgl_quad.py
More file actions
120 lines (83 loc) · 2.66 KB
/
gl_quad.py
File metadata and controls
120 lines (83 loc) · 2.66 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
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
### OpenGL Imports ###
from OpenGL.GL import *
from OpenGL.GLU import *
from gl_utils import *
import threading
class Quad(object):
"""@author : Preetham Chalasani
@brief To create a quad, which basically is a rectangle, but this quad class helps to load an image onto it.
"""
def __init__(self, x, y, width, height, image = None, img_type = None):
super(Quad, self).__init__()
self.__x = x
self.__y = y
self.__width = width
self.__height = height
self.__texture = 0
if image != None and img_type != None:
self.__load_texture(image, img_type)
self.__obj = self.__make_commandlist()
pass
def __make_commandlist(self):
"""To make a commandlist which creates the quad with binded textur coordinates.
"""
gen_list = glGenLists(1)
glNewList(gen_list, GL_COMPILE)
glBegin(GL_QUADS)
glTexCoord3f(0.0, 0.0, 0.0)
glVertex3f(-1, -1, 0.0)
glTexCoord3f(1.0, 0.0, 0.0)
glVertex3f(1, -1,0.0)
glTexCoord3f(1.0, 1.0, 0.0)
glVertex3f(1, 1, 0.0)
glTexCoord3f(0.0, 1.0, 0.0)
glVertex3f(-1,1, 0.0)
glEnd()
glEndList()
return gen_list
def __load_texture(self, image, type):
"""Load a texture(image) on to the quad.
"""
if type == 'PIL':
self.__set_image_texture(image)
elif type == 'IPL':
self.__set_frame_texture(image)
pass
def __set_image_texture(self, image):
"""Set texture
"""
self.__texture_needed = True
img_x = image.size[0]
img_y = image.size[1]
raw_image = to_string(image)
glGenTextures(1, self.__texture)
glBindTexture(GL_TEXTURE_2D, self.__texture)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glTexImage2D(GL_TEXTURE_2D, 0, 3, img_x, img_y, 0, GL_RGBA, GL_UNSIGNED_BYTE, raw_image)
self.update()
pass
def __set_frame_texture(self, frame):
glGenTextures(1, self.__texture)
glBindTexture(GL_TEXTURE_2D, self.__texture)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, frame.shape[1], frame.shape[0], 0, GL_BGR, GL_UNSIGNED_BYTE, frame)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
self.update()
pass
def update(self):
"""Updates the commandlist
"""
self.__obj = self.__make_commandlist()
pass
def draw(self):
"""Call the generated commandlist.
"""
glCallList(self.__obj)
pass