-
Notifications
You must be signed in to change notification settings - Fork 9
/
3Dviewer.py
203 lines (174 loc) · 6.2 KB
/
3Dviewer.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
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
199
200
201
202
203
import pygame
import numpy as np
import time
import transforms3d.euler as euler
import reader
from transfer import *
from vistool import *
from skeleton import *
from smpl_np import SMPLModel
from OpenGL.GL import *
from OpenGL.GLU import *
class Viewer:
def __init__(self, asf_joints, smpl_joints, motions):
self.asf_joints = asf_joints
self.smpl_joints = smpl_joints
align_smpl_asf(asf_joints, smpl_joints)
self.motions = motions
self.frame = 0
self.playing = False
self.fps = 120
self.rorate_dragging = False
self.translate_dragging = False
self.old_x = 0
self.old_y = 0
self.global_rx = 0
self.global_ry = 0
self.rotation_R = np.eye(3)
self.speed_rx = np.pi / 90
self.speed_ry = np.pi / 90
self.speed_trans = 0.25
self.speed_zoom = 0.5
self.done = False
self.default_translate = np.array([0, 0, -200], dtype=np.float32)
self.translate = np.copy(self.default_translate)
pygame.init()
self.screen_size = (1024, 768)
self.screen = pygame.display.set_mode(self.screen_size, pygame.DOUBLEBUF | pygame.OPENGL)
pygame.display.set_caption('AMC Parser - frame %d / %d' % (self.frame, len(self.motions)))
self.clock = pygame.time.Clock()
glClearColor(0, 0, 0, 0)
glShadeModel(GL_SMOOTH)
glMaterialfv(GL_FRONT, GL_SPECULAR, np.array([1, 1, 1, 1], dtype=np.float32))
glMaterialfv(GL_FRONT, GL_SHININESS, np.array([100.0], dtype=np.float32))
glMaterialfv(GL_FRONT, GL_AMBIENT, np.array([0.7, 0.7, 0.7, 0.7], dtype=np.float32))
glEnable(GL_POINT_SMOOTH)
glLightfv(GL_LIGHT0, GL_POSITION, np.array([1, 1, 1, 0], dtype=np.float32))
glEnable(GL_LIGHT0)
glEnable(GL_LIGHTING)
glEnable(GL_DEPTH_TEST)
gluPerspective(45, (self.screen_size[0]/self.screen_size[1]), 0.1, 500.0)
glPointSize(10)
glLineWidth(2.5)
def process_event(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
self.translate = self.default_translate
self.global_rx = 0
self.global_ry = 0
elif event.key == pygame.K_SPACE:
self.playing = not self.playing
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
self.rorate_dragging = True
else:
self.translate_dragging = True
self.old_x, self.old_y = event.pos
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
self.rorate_dragging = False
else:
self.translate_dragging = False
elif event.type == pygame.MOUSEMOTION:
if self.translate_dragging:
# haven't figure out best way to implement this
pass
elif self.rorate_dragging:
new_x, new_y = event.pos
self.global_ry -= (new_x - self.old_x) / \
self.screen_size[0] * np.pi
self.global_rx -= (new_y - self.old_y) / \
self.screen_size[1] * np.pi
self.old_x, self.old_y = new_x, new_y
pressed = pygame.key.get_pressed()
if pressed[pygame.K_DOWN]:
self.global_rx -= self.speed_rx
if pressed[pygame.K_UP]:
self. global_rx += self.speed_rx
if pressed[pygame.K_LEFT]:
self.global_ry += self.speed_ry
if pressed[pygame.K_RIGHT]:
self.global_ry -= self.speed_ry
if pressed[pygame.K_a]:
self.translate[0] -= self.speed_trans
if pressed[pygame.K_d]:
self.translate[0] += self.speed_trans
if pressed[pygame.K_w]:
self.translate[1] += self.speed_trans
if pressed[pygame.K_s]:
self.translate[1] -= self.speed_trans
if pressed[pygame.K_q]:
self.translate[2] += self.speed_zoom
if pressed[pygame.K_e]:
self.translate[2] -= self.speed_zoom
if pressed[pygame.K_COMMA]:
self.frame -= 1
if self.frame >= len(self.motions):
self.frame = 0
if pressed[pygame.K_PERIOD]:
self.frame += 1
if self.frame < 0:
self.frame = len(self.motions) - 1
grx = euler.euler2mat(self.global_rx, 0, 0)
gry = euler.euler2mat(0, self.global_ry, 0)
self.rotation_R = grx.dot(gry)
def set_asf_joints(self, asf_joints):
self.asf_joints = asf_joints
def set_smpl_joints(self, smpl_joints):
self.smpl_joints = smpl_joints
def set_motion(self, motions):
self.motions = motions
def draw(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glBegin(GL_POINTS)
self.draw_joints(self.asf_joints)
self.draw_joints(self.smpl_joints)
glEnd()
glBegin(GL_LINES)
self.draw_bones(self.asf_joints)
self.draw_bones(self.smpl_joints)
glEnd()
def draw_joints(self, joints):
for j in joints.values():
coord = np.array(np.squeeze(j.coordinate).dot(self.rotation_R)+self.translate, dtype=np.float32)
glVertex3f(*coord)
def draw_bones(self, joints):
for j in joints.values():
child = j
parent = j.parent
if parent is not None:
coord_x = np.array(np.squeeze(child.coordinate).dot(self.rotation_R)+self.translate, dtype=np.float32)
coord_y = np.array(np.squeeze(parent.coordinate).dot(self.rotation_R)+self.translate, dtype=np.float32)
glVertex3f(*coord_x)
glVertex3f(*coord_y)
def update_motion(self):
self.asf_joints['root'].set_motion(self.motions[self.frame])
R, offset = map_R_asf_smpl(asf_joints)
self.smpl_joints[0].coordinate = offset
self.smpl_joints[0].set_motion_R(R)
self.smpl_joints[0].update_coord()
move_skeleton(self.smpl_joints, [-40, 0, 0])
if self.playing:
self.frame += 1
if self.frame >= len(self.motions):
self.frame = 0
def run(self):
while not self.done:
self.process_event()
self.update_motion()
self.draw()
pygame.display.set_caption('AMC Parser - frame %d / %d' % (self.frame, len(self.motions)))
pygame.display.flip()
self.clock.tick(self.fps)
pygame.quit()
if __name__ == '__main__':
asf_joints = reader.parse_asf('./data/01/01.asf')
asf_joints['root'].reset_pose()
smpl = SMPLModel('./model.pkl')
smpl_joints = setup_smpl_joints(smpl)
motions = reader.parse_amc('./data/01/01_01.amc')
v = Viewer(asf_joints, smpl_joints, motions)
v.run()