-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmocap.js
More file actions
317 lines (270 loc) · 7.89 KB
/
mocap.js
File metadata and controls
317 lines (270 loc) · 7.89 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"use strict";
var canvas;
var gl;
var mvMatrix, pMatrix, nMatrix;
var matrixStack = new Array();
var lineProgram;
var sphereProgram;
var axis;
var floor;
var segment;
var sphere;
var root;
var pause;
var frameData;
var currentFrame;
var frameSpeed;
// Viewing
var aspect = 1.0;
// Data
var bvh;
var LineProgram = function() {
this.program = initShaders(gl, 'line-vshader', 'line-fshader');
gl.useProgram(this.program);
this.vertexLoc = gl.getAttribLocation(this.program, 'vPosition');
this.colorLoc = gl.getAttribLocation(this.program, 'vColor');
this.mvMatrixLoc = gl.getUniformLocation(this.program, 'mvMatrix');
this.pMatrixLoc = gl.getUniformLocation(this.program, 'pMatrix');
this.nMatrixLoc = gl.getUniformLocation(this.program, 'nMatrix');
};
var SphereProgram = function() {
this.program = initShaders(gl, 'sphere-vshader', 'sphere-fshader');
gl.useProgram(this.program);
this.vertexLoc = gl.getAttribLocation(this.program, 'vPosition');
this.normalLoc = gl.getAttribLocation(this.program, 'vNormal');
this.colorLoc = gl.getAttribLocation(this.program, 'vColor');
this.mvMatrixLoc = gl.getUniformLocation(this.program, 'mvMatrix');
this.pMatrixLoc = gl.getUniformLocation(this.program, 'pMatrix');
this.nMatrixLoc = gl.getUniformLocation(this.program, 'nMatrix');
};
var radius = 150.0;
var theta = radians(45);
var up = vec3(0.0, 1.0, 0.0);
var at = vec3(0.0, 0.0, 0.0);
var thetaY = radians(30.0);
function render() {
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
const fovy = 40.0;
const near = 0.01;
const far = 1000;
var eye = vec3(radius * Math.sin(theta),
radius * Math.sin(thetaY),
radius * Math.cos(theta));
at = vec3(frameData[0], frameData[1], frameData[2]);
pMatrix = perspective(fovy, aspect, near, far);
mvMatrix = lookAt(eye, at, up);
pushMatrix();
mvMatrix = mult(mvMatrix, scalem(50.0, 50.0, 50.0));
renderFloor();
popMatrix();
mvMatrix = mult(mvMatrix, translate(frameData[0], frameData[1], frameData[2]));
traverseBVHTree(vec3(0.0, 0.0, 0.0), root);
}
var reader = new FileReader();
reader.onload = function(e) {
var text = reader.result;
};
function tick() {
requestAnimFrame(tick);
render();
if (!pause) {
animate();
}
}
var lastTime = 0;
function animate() {
var timeNow = new Date().getTime();
if (lastTime != 0) {
var elapsed = timeNow - lastTime;
updateFrame(frameSpeed);
}
lastTime = timeNow;
}
/**
* Parses a BVH file and places the result in the bvh variable.
*/
function parse(input) {
var antlr4 = require('antlr4/index');
var BVHLexer = require('parser/BVHLexer');
var BVHListener = require('parser/BVHListener');
var BVHParser = require('parser/BVHParser');
require('./BVH');
var chars = new antlr4.InputStream(input);
var lexer = new BVHLexer.BVHLexer(chars);
var tokens = new antlr4.CommonTokenStream(lexer);
var parser = new BVHParser.BVHParser(tokens);
parser.buildParseTrees = true;
var tree = parser.mocap();
bvh = new BVH();
antlr4.tree.ParseTreeWalker.DEFAULT.walk(bvh, tree);
// Construct the hierarchical tree
root = Root();
currentFrame = 0;
frameData = bvh.frames[currentFrame];
}
function keyDown(e) {
switch (e.keyCode) {
case 37:
// left
theta = theta - radians(3);
break;
case 38:
// up
//thetaY += radians(5);
radius = radius - 1.5;
break;
case 39:
// right
theta = theta + radians(3);
break;
case 40:
// down
//thetaY -= radians(5);
radius = radius + 1.5;
break;
case 32:
// spacebar
pause = !pause;
break;
case 73:
// i
//radius = radius - 0.5;
frameSpeed++;
break;
case 79:
// o
//radius = radius + 0.5;
frameSpeed--;
if (frameSpeed < 0) {
frameSpeed = 0;
}
break;
case 'F'.charCodeAt(0):
// F or f
break;
default:
// To see what the code for a certain key is, uncomment this line,
// reload the page in the browser and press the key.
//console.log("Unrecognized key press: " + e.keyCode);
break;
}
requestAnimFrame(render);
}
/**
* Init function
*/
window.onload = function init() {
document.onkeydown = keyDown;
canvas = document.getElementById('gl-canvas');
gl = WebGLUtils.setupWebGL(canvas);
if (!gl) { alert("WebGL isn't available"); }
gl.viewport(0, 0, canvas.width, canvas.height);
aspect = canvas.width / canvas.height;
// Init some GL stuff
gl.clearColor(1.0, 1.0, 1.0, 1.0);
gl.enable(gl.DEPTH_TEST);
// Load shaders and initialize attribute buffers
lineProgram = new LineProgram();
sphereProgram = new SphereProgram();
floor = new Floor();
axis = new Axis();
segment = new Segment();
sphere = new Sphere(1, 20, 20);
// Listen for a file to be loaded and parse
var fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', function(e) {
var file = fileInput.files[0];
if (file && file.name) {
if (file.name.match(/.*\.bvh/)) {
var reader = new FileReader();
reader.onload = function(e) {
parse(reader.result);
};
reader.readAsText(file);
} else {
console.log('File not supported! ' + file.type);
}
}
});
pause = true;
frameSpeed = 2;
// Set up listeners
canvas.addEventListener('mousemove', mouse_move, false);
canvas.addEventListener('wheel', mouse_wheel, false);
// Parse a default file.
// TODO: change this to testData1 when you're ready to start rendering
// animation. These strings are defined in test1.bvh.js and test2.bvh.js.
parse(testData1);
tick();
};
function mouse_move(event) {
if (event.buttons & 1) {
theta = theta + (event.movementX * -0.005);
thetaY = thetaY + (event.movementY * 0.005);
}
}
function mouse_wheel(event) {
radius = radius + (0.5 * event.deltaY);
}
// Stack stuff
var matrixStack = new Array();
function pushMatrix() {
matrixStack.push(mat4(mvMatrix));
}
function popMatrix() {
mvMatrix = matrixStack.pop();
}
var Root = function() {
return bvh.roots[0];
};
var currentChannel = 0;
function traverseBVHTree(parentCoordinates, node) {
var currentCoordinates = vec3();
currentCoordinates[0] = parentCoordinates[0] + parseFloat(node.offsets[0]);
currentCoordinates[1] = parentCoordinates[1] + parseFloat(node.offsets[1]);
currentCoordinates[2] = parentCoordinates[2] + parseFloat(node.offsets[2]);
pushMatrix();
mvMatrix = mult(mvMatrix, translate(node.offsets));
var segmentVector = subtract(parentCoordinates, currentCoordinates);
var segmentLength = length(segmentVector);
if (segmentLength > 0) {
// calculate rotation angle and axis for drawing the line segment
var v1 = normalize(vec3(1.0, 0.0, 0.0));
var v2 = normalize(vec3(segmentVector));
var angle = Math.acos(dot(vec3(v1), vec3(v2)));
var axis = normalize(cross(vec3(v1), vec3(v2)));
pushMatrix();
mvMatrix = mult(mvMatrix, rotate(degrees(angle), axis));
mvMatrix = mult(mvMatrix, scalem(vec3(segmentLength, 0.0, 0.0)));
renderSegment();
popMatrix();
}
pushMatrix();
mvMatrix = mult(mvMatrix, scalem(0.5, 0.5, 0.5));
renderSphere();
popMatrix();
for (var i = 0; i < node.channels.length; i++) {
if (node.channels[i] == 'Zrotation') {
mvMatrix = mult(mvMatrix, rotateZ(frameData[node.channelOffset + i]));
}
else if (node.channels[i] == 'Yrotation') {
mvMatrix = mult(mvMatrix, rotateY(frameData[node.channelOffset + i]));
}
else if (node.channels[i] == 'Xrotation') {
mvMatrix = mult(mvMatrix, rotateX(frameData[node.channelOffset + i]));
}
}
// traverse this node's children
for (var i = 0; i < node.children.length; i++) {
traverseBVHTree(currentCoordinates, node.children[i]);
}
popMatrix();
}
function updateFrame(numFrames) {
currentFrame += numFrames;
if (currentFrame > bvh.frames.length - 1) {
currentFrame = 0;
pause = true;
}
frameData = bvh.frames[currentFrame];
}