-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathlucid.diff
142 lines (131 loc) · 5.59 KB
/
lucid.diff
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
diff -ruNZ lucid/misc/gl/glrenderer.py lucid.fixed/misc/gl/glrenderer.py
--- lucid/misc/gl/glrenderer.py 2019-12-19 15:24:16.326862130 -0800
+++ lucid.fixed/misc/gl/glrenderer.py 2019-12-20 14:08:34.970990828 -0800
@@ -124,9 +124,12 @@
def proj_matrix(self):
return perspective(self.fovy, self.aspect, self.znear, self.zfar)
- def render_mesh(self, position, uv, face=None,
+ def render_mesh(self, position, uv, faces=None,
clear_color=[0, 0, 0, 0],
modelview=np.eye(4)):
+ uvface = np.zeros((uv.shape[0], uv.shape[1]+1))
+ uvface[:, :2] = uv
+
MVP = modelview.T.dot(self.proj_matrix())
MVP = np.ascontiguousarray(MVP, np.float32)
position = np.ascontiguousarray(position, np.float32)
@@ -134,16 +137,15 @@
gl.glClearColor(*clear_color)
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
- with self.shader, self._bind_attrib(0, position), self._bind_attrib(1, uv):
- gl.glUniformMatrix4fv(self.shader['MVP'], 1, gl.GL_FALSE, MVP)
- gl.glEnable(gl.GL_DEPTH_TEST)
- if face is not None:
- face = np.ascontiguousarray(face, np.uint32)
+ for i, face in enumerate(faces):
+ face = np.ascontiguousarray(face, np.uint32)
+ uvface[:, 2] = i
+
+ with self.shader, self._bind_attrib(0, position), self._bind_attrib(1, uvface):
+ gl.glUniformMatrix4fv(self.shader['MVP'], 1, gl.GL_FALSE, MVP)
+ gl.glEnable(gl.GL_DEPTH_TEST)
gl.glDrawElements(gl.GL_TRIANGLES, face.size, gl.GL_UNSIGNED_INT, face)
- else:
- vert_n = position.size//position.shape[-1]
- gl.glDrawArrays(gl.GL_TRIANGLES, 0, vert_n)
- gl.glDisable(gl.GL_DEPTH_TEST)
+ gl.glDisable(gl.GL_DEPTH_TEST)
w, h = self.size
frame = gl.glReadPixels(0, 0, w, h, gl.GL_RGBA, gl.GL_FLOAT)
diff -ruNZ lucid/misc/gl/meshutil.py lucid.fixed/misc/gl/meshutil.py
--- lucid/misc/gl/meshutil.py 2019-12-19 15:24:16.326862130 -0800
+++ lucid.fixed/misc/gl/meshutil.py 2019-12-20 14:13:51.780006620 -0800
@@ -114,7 +114,10 @@
uv = [np.zeros(2, dtype=np.float32)]
tuple2idx = OrderedDict()
- trinagle_indices = []
+ triangle_indices = []
+ triangle_materials = []
+ materials = []
+ current_material_idx = -1
input_file = open(fn) if isinstance(fn, str) else fn
for line in input_file:
@@ -134,6 +137,9 @@
elif tag == 'vn':
normal.append(np.fromstring(line, sep=' '))
elif tag == 'f':
+ if current_material_idx == -1:
+ materials.append('__unknown__')
+ current_material_idx = materials.index('__unknown__')
output_face_indices = []
for chunk in line.split():
# tuple order: pos_idx, uv_idx, normal_idx
@@ -144,10 +150,23 @@
# generate face triangles
for i in range(1, len(output_face_indices)-1):
for vi in [0, i, i+1]:
- trinagle_indices.append(output_face_indices[vi])
+ triangle_indices.append(output_face_indices[vi])
+ triangle_materials.append(current_material_idx)
+
+ elif tag == 'usemtl':
+ # Create new material
+ if line not in materials:
+ materials.append(line)
+ current_material_idx = materials.index(line)
+
+ triangle_indices = np.int32(triangle_indices)
+ triangle_materials = np.int32(triangle_materials)
outputs = {}
- outputs['face'] = np.int32(trinagle_indices)
+ outputs['faces'] = []
+ outputs['materials'] = materials
+ for material_idx in np.unique(triangle_materials):
+ outputs['faces'].append(np.int32(triangle_indices)[triangle_materials == material_idx])
pos_idx, uv_idx, normal_idx = np.int32(list(tuple2idx)).T
if np.any(pos_idx):
outputs['position'] = _unify_rows(position)[pos_idx]
diff -ruNZ lucid/optvis/param/spatial.py lucid.fixed/optvis/param/spatial.py
--- lucid/optvis/param/spatial.py 2019-12-19 15:24:16.326862130 -0800
+++ lucid.fixed/optvis/param/spatial.py 2019-12-20 14:13:22.023520434 -0800
@@ -82,32 +82,33 @@
return pyramid
-def sample_bilinear(texture, uv):
+def sample_bilinear(textures, uvf):
"""Build bilinear texture sampling graph.
Coordinate transformation rules match OpenGL GL_REPEAT wrapping and GL_LINEAR
interpolation modes.
Args:
- texture: [tex_h, tex_w, channel_n] tensor.
- uv: [frame_h, frame_h, 2] tensor with per-pixel UV coordinates in range [0..1]
+ texture: [tex_c, tex_h, tex_w, channel_n] tensor.
+ uvf: [frame_h, frame_w, 3] tensor with per-pixel UV coordinates in range [0..1]
Returns:
- [frame_h, frame_h, channel_n] tensor with per-pixel sampled values.
+ [frame_h, frame_w, channel_n] tensor with per-pixel sampled values.
"""
- h, w = tf.unstack(tf.shape(texture)[:2])
- u, v = tf.split(uv, 2, axis=-1)
+ c, h, w = tf.unstack(tf.shape(textures)[:3])
+ u, v, f = tf.split(uvf, 3, axis=-1)
v = 1.0-v # vertical flip to match GL convention
u, v = u*tf.to_float(w)-0.5, v*tf.to_float(h)-0.5
u0, u1 = tf.floor(u), tf.ceil(u)
v0, v1 = tf.floor(v), tf.ceil(v)
uf, vf = u-u0, v-v0
- u0, u1, v0, v1 = map(tf.to_int32, [u0, u1, v0, v1])
- def sample(u, v):
- vu = tf.concat([v%h, u%w], axis=-1)
- return tf.gather_nd(texture, vu)
- s00, s01 = sample(u0, v0), sample(u0, v1)
- s10, s11 = sample(u1, v0), sample(u1, v1)
+ u0, u1, v0, v1, f = map(tf.to_int32, [u0, u1, v0, v1, f])
+ def sample(u, v, f):
+ fvu = tf.concat([f % c, v % h, u % w], axis=-1)
+ s = tf.gather_nd(textures, fvu)
+ return s
+ s00, s01 = sample(u0, v0, f), sample(u0, v1, f)
+ s10, s11 = sample(u1, v0, f), sample(u1, v1, f)
s0 = s00*(1.0-vf) + s01*vf
s1 = s10*(1.0-vf) + s11*vf
s = s0*(1.0-uf) + s1*uf