-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmocap.js
428 lines (340 loc) · 11.9 KB
/
mocap.js
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
'use strict';
var canvas;
var gl;
var floor;
var sphere;
var axis;
var segment;
var lineProgram;
var sphereProgram;
var go = false;
var hipFr = false;
var currFrame;
var view = 'A';
var aspect = 1.0;
var mvMatrix, pMatrix, nMatrix;
// Stack stuff
var matrixStack = new Array();
function pushMatrix() {
matrixStack.push(mat4(mvMatrix));
}
function popMatrix() {
mvMatrix = matrixStack.pop();
}
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');
};
// Viewing
var aspect = 1.0;
// Data
var bvh;
var reader = new FileReader();
reader.onload = function(e) {
var text = reader.result;
};
var theta = radians(45);
var radius = 5;
var at = vec3(0.0, 0.0, 0.0);
var hips = at;
function render() {
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
const fovy = 40.0;
const near = 0.01;
const far = 100;
var up = vec3(0.0, 1.0, 0.0);
var eye = vec3(radius * Math.sin(theta),
radius * Math.sin(radians(10.0)),
radius * Math.cos(theta));
if (view == 'X') {
eye = vec3(5, 0, 0);
} else if (view == 'Y') {
eye = vec3(0, 10, 0);
up = vec3(0.0, 0.0, -1.0);
} else if (view == 'Z') {
eye = vec3(0, 0, 5);
}
pMatrix = perspective(fovy, aspect, near, far);
mvMatrix = lookAt(eye, at, up);
renderFloor();
pushMatrix();
mvMatrix = mult(mvMatrix, scalem(0.05, 0.05, 0.05));
bvh.channelOffset = 0;
drawOffsets(bvh.roots[0]);
popMatrix();
}
function animate() {
}
var stop = false;
var frameCount;
var fps, fpsInterval, startTime, now, then, elapsed;
function tick() {
requestAnimationFrame(tick);
if (go) {
currFrame++;
//currFrame++; // Makes it go faster...
if (currFrame >= bvh.frames.length) {
currFrame = 0;
//console.log("reset");
}
}
render();
}
function initFrameRate(fps) {
frameCount = bvh.numFrames;
fpsInterval = 50 * fps;//10 * fps;
then = Date.now();
startTime = then;
}
function drawOffsets(cur) {
pushMatrix();
//-------Line segments---
var a = vec3(1, 0, 0);
var b = vec3(cur.offsets[0], cur.offsets[1], cur.offsets[2]);
var n = cross(a, b);
var curTheta = Math.acos((dot(a, b)) / length(b));
if (curTheta > 0) {
pushMatrix();
mvMatrix = mult(mvMatrix, rotate(degrees(curTheta), n));
mvMatrix = mult(mvMatrix, scalem(length(b), length(b), length(b)));
renderSegment();
popMatrix();
}
//-----------------------
mvMatrix = mult(mvMatrix,
translate(cur.offsets[0], cur.offsets[1], cur.offsets[2]));
var xpos, ypos, zpos;
for (var j = 0; j < cur.channels.length; j++) //cur.channels.length 0, 3, 6
{
if (cur.channels[j] == 'Xposition') {
xpos = bvh.frames[currFrame][cur.channelOffset + j];
mvMatrix = mult(mvMatrix,
translate(bvh.frames[currFrame][cur.channelOffset + j], 0, 0));
}
if (cur.channels[j] == 'Yposition') {
ypos = bvh.frames[currFrame][cur.channelOffset + j];
mvMatrix = mult(mvMatrix,
translate(0, bvh.frames[currFrame][cur.channelOffset + j], 0));
}
if (cur.channels[j] == 'Zposition') {
zpos = bvh.frames[currFrame][cur.channelOffset + j];
mvMatrix = mult(mvMatrix,
translate(0, 0, bvh.frames[currFrame][cur.channelOffset + j]));
}
if (cur.channels[j] == 'Xrotation') {
mvMatrix = mult(mvMatrix,
rotateX(bvh.frames[currFrame][cur.channelOffset + j]));
}
if (cur.channels[j] == 'Yrotation') {
mvMatrix = mult(mvMatrix,
rotateY(bvh.frames[currFrame][cur.channelOffset + j]));
}
if (cur.channels[j] == 'Zrotation') {
mvMatrix = mult(mvMatrix,
rotateZ(bvh.frames[currFrame][cur.channelOffset + j]));
}
}
pushMatrix();
mvMatrix = mult(mvMatrix, scalem(0.5, 0.5, 0.5));
renderSphere();
popMatrix();
for (var i = 0; i < cur.children.length; i++) {
drawOffsets(cur.children[i]);
}
popMatrix();
}
/**
* 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);
// TODO: add any initialization code upon loading a BVH file
fps = bvh.frameTime;
}
function keyDown(e) {
switch (e.keyCode) {
case 37:
// left
theta -= 0.1;
render();
break;
case 38:
// up
radius -= 0.1;
render();
break;
case 39:
// right
theta += 0.1;
render();
break;
case 40:
// down
radius += 0.1;
render();
break;
case 32:
// spacebar
go = !go;
tick();
break;
case 79:
//o look at origin
at = vec3(0, 0, 0);
render();
break;
case 80:
//p look at person's hips
at = hips;
hipFr = true;
//render();
tick();
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;
}
}
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;
// Awesome green background.
gl.clearColor(0.6, 0.7, 1.0, 1.0);
gl.enable(gl.DEPTH_TEST);
// Load shaders and initialize attribute buffers
lineProgram = new LineProgram();
sphereProgram = new SphereProgram();
// 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);
}
}
console.log('File uploaded');
});
// 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);
currFrame = 0;
axis = new Axis();
floor = new Floor();
segment = new LineSegment();
sphere = new Sphere(1, 200, 200);//0.05
initFrameRate(bvh.frameTime);
render();
};
function renderAxis() {
gl.useProgram(lineProgram.program);
gl.enableVertexAttribArray(lineProgram.vertexLoc);
gl.bindBuffer(gl.ARRAY_BUFFER, axis.vertexBuffer);
gl.vertexAttribPointer(lineProgram.vertexLoc, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(lineProgram.colorLoc);
gl.bindBuffer(gl.ARRAY_BUFFER, axis.colorBuffer);
gl.vertexAttribPointer(lineProgram.colorLoc, 4, gl.FLOAT, false, 0, 0);
gl.uniformMatrix4fv(lineProgram.mvMatrixLoc, false, flatten(mvMatrix));
gl.uniformMatrix4fv(lineProgram.pMatrixLoc, false, flatten(pMatrix));
gl.drawArrays(gl.LINES, 0, axis.numPoints);
}
function renderSegment() {
gl.useProgram(lineProgram.program);
gl.enableVertexAttribArray(lineProgram.vertexLoc);
gl.bindBuffer(gl.ARRAY_BUFFER, segment.vertexBuffer);
gl.vertexAttribPointer(lineProgram.vertexLoc, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(lineProgram.colorLoc);
gl.bindBuffer(gl.ARRAY_BUFFER, segment.colorBuffer);
gl.vertexAttribPointer(lineProgram.colorLoc, 4, gl.FLOAT, false, 0, 0);
gl.uniformMatrix4fv(lineProgram.mvMatrixLoc, false, flatten(mvMatrix));
gl.uniformMatrix4fv(lineProgram.pMatrixLoc, false, flatten(pMatrix));
gl.drawArrays(gl.LINES, 0, segment.numPoints);
}
function renderFloor() {
gl.useProgram(lineProgram.program);
gl.enableVertexAttribArray(lineProgram.vertexLoc);
gl.bindBuffer(gl.ARRAY_BUFFER, floor.vertexBuffer);
gl.vertexAttribPointer(lineProgram.vertexLoc, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(lineProgram.colorLoc);
gl.bindBuffer(gl.ARRAY_BUFFER, floor.colorBuffer);
gl.vertexAttribPointer(lineProgram.colorLoc, 4, gl.FLOAT, false, 0, 0);
gl.uniformMatrix4fv(lineProgram.mvMatrixLoc, false, flatten(mvMatrix));
gl.uniformMatrix4fv(lineProgram.pMatrixLoc, false, flatten(pMatrix));
gl.drawArrays(gl.LINES, 0, floor.numPoints);
}
function renderSphere() {
gl.useProgram(sphereProgram.program);
gl.enableVertexAttribArray(sphereProgram.vertexLoc);
gl.bindBuffer(gl.ARRAY_BUFFER, sphere.vertexBuffer);
gl.vertexAttribPointer(sphereProgram.vertexLoc, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(sphereProgram.normalLoc);
gl.bindBuffer(gl.ARRAY_BUFFER, sphere.normalBuffer);
gl.vertexAttribPointer(sphereProgram.normalLoc, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(sphereProgram.colorLoc);
gl.bindBuffer(gl.ARRAY_BUFFER, sphere.colorBuffer);
gl.vertexAttribPointer(sphereProgram.colorLoc, 4, gl.FLOAT, false, 0, 0);
nMatrix = normalMatrix(mvMatrix, false);
gl.uniformMatrix4fv(sphereProgram.mvMatrixLoc, false, flatten(mvMatrix));
gl.uniformMatrix4fv(sphereProgram.pMatrixLoc, false, flatten(pMatrix));
gl.uniformMatrix4fv(sphereProgram.nMatrixLoc, false, flatten(nMatrix));
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, sphere.indexBuffer);
gl.drawElements(gl.TRIANGLES, sphere.numIndices, gl.UNSIGNED_SHORT, 0);
}
var LineSegment = function() {
var pointsArray = [];
var colorsArray = [];
pointsArray.push(vec4(0.0, 0.0, 0.0, 1.0));
pointsArray.push(vec4(1.0, 0.0, 0.0, 1.0));
colorsArray.push(vec4(0.0, 0.0, 0.0, 1.0));
colorsArray.push(vec4(0.0, 0.0, 0.0, 1.0));
this.vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(pointsArray), gl.STATIC_DRAW);
this.colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(colorsArray), gl.STATIC_DRAW);
this.numPoints = pointsArray.length;
};