-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMpacsWarp2DFixedFunction.py
More file actions
130 lines (98 loc) · 4.37 KB
/
MpacsWarp2DFixedFunction.py
File metadata and controls
130 lines (98 loc) · 4.37 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
121
122
123
124
125
126
127
128
129
130
from MpacsWarp2D import MpacsWarp2D
from BlendQuad import BlendQuad
from OpenGL.GL import *
import numpy
class MpacsWarp2DFixedFunction(MpacsWarp2D):
"""
Implements 2D warping via the OpenGL fixed-function pipeline.
This class creates a 2-d mesh out of the data in a pfm file,
and renders the input media on this mesh to compute the warping.
Each point in the pfm file becomes a vertex in the mesh.
"""
def __init__(self, mpcdi, region):
MpacsWarp2D.__init__(self, mpcdi, region)
self.blendCard = BlendQuad(self.alpha)
# We don't attempt to do beta-map processing in the
# fixed-function renderer, only alpha-map processing.
def initGL(self):
MpacsWarp2D.initGL(self)
self.blendCard.initGL()
xSize = self.pfm.xSize
ySize = self.pfm.ySize
# Discard every third element of the UV data, which is mostly
# NaN's and isn't really useful, and can confuse OpenGL into
# ignoring the first two.
uv_list = numpy.fromstring(self.pfm.data, dtype = 'float32')
uvs3 = numpy.reshape(uv_list, (-1, 3), 'C')
uvs = uvs3[:,0:2]
self.uvdata = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.uvdata)
glBufferData(GL_ARRAY_BUFFER, uvs, GL_STATIC_DRAW)
verts = []
xScale = 1.0 / float(xSize)
yScale = 1.0 / float(ySize)
for yi in range(ySize):
for xi in range(xSize):
verts.append((float(xi) + 0.5) * xScale)
verts.append((float(yi) + 0.5) * yScale)
self.numVertices = len(verts) / 2
verts = numpy.array(verts, dtype = 'float32')
self.vertdata = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.vertdata)
glBufferData(GL_ARRAY_BUFFER, verts, GL_STATIC_DRAW)
## def has_point(xi, yi):
## if xi < xSize and y < ySize and
tris = []
for yi in range(ySize - 1):
for xi in range(xSize - 1):
vi0 = ((xi) + (yi) * xSize);
vi1 = ((xi) + (yi + 1) * xSize);
vi2 = ((xi + 1) + (yi + 1) * xSize);
vi3 = ((xi + 1) + (yi) * xSize);
tris += [vi2, vi0, vi1]
tris += [vi3, vi0, vi2]
self.numIndices = len(tris)
tris = numpy.array(tris, dtype = 'int32')
self.idata = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.idata)
glBufferData(GL_ARRAY_BUFFER, tris, GL_STATIC_DRAW)
def draw(self):
glPushAttrib(GL_ENABLE_BIT)
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS)
# First, draw the mesh with the media texture applied.
glMatrixMode(GL_TEXTURE)
glPushMatrix()
glLoadIdentity()
# Flip the V axis to match OpenGL's texturing convention. (Or
# we could have loaded the media file in upside-down.)
glTranslatef(0.0, 1.0, 0.0)
glScalef(1.0, -1.0, 1.0)
# Scale the warping UV's into the correct range specified by
# the Region (as defined in the mpcdi.xml file).
glTranslatef(self.region.x, self.region.y, 0.0)
glScale(self.region.xsize, self.region.ysize, 1.0)
self.media.apply()
glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_TEXTURE_COORD_ARRAY)
glBindBuffer(GL_ARRAY_BUFFER, self.vertdata)
glVertexPointer(2, GL_FLOAT, 0, None)
glBindBuffer(GL_ARRAY_BUFFER, self.uvdata)
glTexCoordPointer(2, GL_FLOAT, 0, None)
glColor3f(1.0, 1.0, 1.0)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.idata)
glDrawElements(GL_TRIANGLES, self.numIndices, GL_UNSIGNED_INT, None)
glMatrixMode(GL_TEXTURE)
glPopMatrix()
glPopClientAttrib()
glPopAttrib()
if self.includeBlend:
# Now apply the blending map.
# We ought to apply the blending map in linear space, then
# re-apply the gamma curve; but this isn't really possible in
# the fixed-function pipeline. So we just naively apply the
# blending map to the warp by multiplying it as-is over the
# whole frame (assuming that it's been pre-scaled with the
# target gamma). This actually isn't a terrible approach, and
# looks fine as long as the media is sufficiently bright.
self.blendCard.draw()
self.saveOutputImage()