-
-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathpvm_projection.py
More file actions
198 lines (146 loc) · 5.31 KB
/
pvm_projection.py
File metadata and controls
198 lines (146 loc) · 5.31 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# -----------------------------------------------------------------------------
# Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved.
# Distributed under the (new) BSD License.
# -----------------------------------------------------------------------------
import numpy as np
from . transform import Transform
from glumpy import gl, glm, library
class PVMProjection(Transform):
"""
Perspective projection is an approximate representation, on a flat surface
of an image as it is seen by the eye. The two most characteristic features
of perspective are that objects are smaller as their distance from the
observer increases; and that they are subject to foreshortening, meaning
that an object's dimensions along the line of sight are shorter than its
dimensions across the line of sight.
:param float distance:
Distance of the camera to the origin. Default is 5.
:param bool fovy:
Field of view along y axis. Default is 40.
:param int znear:
Z near clipping place. Default is 2.
:param int zfar:
Z far clipping place. Default is 100.
The transform is connected to the following events:
* ``on_attach``: Transform initialization
* ``on_resize``: Recompute projection matrix
**Usage example**
.. code:: python
vertex = '''
attribute vec3 position;
attribute vec4 color;
void main()
{
v_color = color;
gl_Position = <transform>;
} '''
fragment = '''
varying vec4 v_color;
void main()
{
gl_FragColor = v_color;
} '''
window = app.Window()
@window.event
def on_draw(dt):
global phi, theta
window.clear()
cube.draw(gl.GL_TRIANGLES, I)
theta += 0.5 # degrees
phi += 0.5 # degrees
model = np.eye(4, dtype=np.float32)
glm.rotate(model, theta, 0, 0, 1)
glm.rotate(model, phi, 0, 1, 0)
cube['transform']['model'] = model
V, I, O = colorcube()
cube = gloo.Program(vertex, fragment)
cube.bind(V)
cube['transform'] = PVMProjection(Position("position"))
window.attach(cube['transform'])
phi, theta = 0, 0
app.run()
"""
def __init__(self, *args, **kwargs):
code = library.get("transforms/pvm.glsl")
Transform.__init__(self, code, *args, **kwargs)
self._width = None
self._height = None
self._distance = Transform._get_kwarg("distance", kwargs) or 5
self._fovy = Transform._get_kwarg("fovy", kwargs) or 40
self._znear = Transform._get_kwarg("znear", kwargs) or 2.0
self._zfar = Transform._get_kwarg("zfar", kwargs) or 100.0
self._view = np.eye(4, dtype=np.float32)
self._model = np.eye(4, dtype=np.float32)
self._projection = np.eye(4, dtype=np.float32)
glm.translate(self._view, 0, 0, -self._distance)
@property
def view(self):
""" View matrix """
return self._view
@property
def model(self):
""" Model matrix """
return self._model
@property
def projection(self):
""" Projection matrix """
return self._projection
@property
def distance(self):
""" Distance of the camera to the origin """
return self._distance
@distance.setter
def distance(self, value):
""" Distance of the camera to the origin """
if value > 1:
self._distance = value
self._view = np.eye(4, dtype=np.float32)
glm.translate(self._view, 0, 0, -self._distance)
self["view"] = self._view
@property
def fovy(self):
""" Field of view along y axis """
return self._fovy
@fovy.setter
def fovy(self, value):
""" Field of view along y axis """
if 0 < fovy < 90:
self._fovy = value
self._build_projection()
@property
def znear(self):
""" Z near clipping place """
return self._znear
@znear.setter
def znear(self, value):
""" Z near clipping place """
if value < self._zfar:
self._znear = value
self._build_projection()
@property
def zfar(self):
""" Z far clipping place """
return self._zfar
@zfar.setter
def zfar(self, value):
""" Z far clipping place """
if value > self._zfar:
self._zfar = value
self._build_projection()
def on_attach(self, program):
self["view"] = self._view
self["model"] = self._model
self["projection"] = self._projection
def on_resize(self, width, height):
self._width = width
self._height = height
# Build projection matrix
self._build_projection()
# Propagate event to children
Transform.on_resize(self, width, height)
def _build_projection(self):
# We need to have caught at least one resize event
if self._width is None: return
aspect = self._width / float(self._height)
self['projection'] = glm.perspective(self.fovy, aspect,
self._znear, self._zfar)