-
Notifications
You must be signed in to change notification settings - Fork 2
/
GLWidget.py
59 lines (48 loc) · 1.85 KB
/
GLWidget.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
59
from PyQt4.QtOpenGL import *
from OpenGL.GL import *
from OpenGL.GLU import *
class GLWidget(QGLWidget):
texture = None
def resizeGL(self, width, height):
glViewport(0, 0, width, height)
if self.texture is not None:
left = 0
right = self.texture.width
bottom = 0
top = self.texture.height
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspect = float(width) / float(height)
if aspect < 1.0:
# window taller than wide
top /= aspect
else:
right *= aspect
gluOrtho2D(left, right, bottom, top)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def setTexture(self, texture):
self.texture = texture
# (x,y) in tex coordinates [0,1]x[0,1] -> (x,y) image
self.texCoords = (GLfloat * 20)(
0., 0., 0., 0., 0.,
1., 0., float(texture.width), 0., 0.,
1., 1., float(texture.width), float(texture.height), 0.,
0., 1., 0., float(texture.height), 0.)
self.resizeGL(self.size().width(), self.size().height())
def paintGL(self):
if self.texture is not None:
self.texture.make_texture()
self.texture.dirty()
glPushAttrib(GL_ENABLE_BIT)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, self.texture.texture_id)
glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
glInterleavedArrays(GL_T2F_V3F, 0, self.texCoords)
glDrawArrays(GL_QUADS, 0, 4)
glPopClientAttrib()
glPopAttrib()
else:
glClearColor(0,0,0,0) # noir
glClear(GL_COLOR_BUFFER_BIT)
glFlush()