/
psapi.js
732 lines (594 loc) · 18.3 KB
/
psapi.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*
Copyright (c) 2010 Seneca College
MIT LICENSE
*/
function PointStream(){
const version = 0.2;
const XHR_DONE = 4;
// to calculate fps
var frames = 0;
var lastTime;
var renderCallback;
var bk = [1,1,1,1];
var VBOs;
// browser detection to handle differences such as mouse scrolling
var browser = -1 ;
const MINEFIELD = 0;
const CHROME = 1;
const CHROMIUM = 2;
const WEBKIT = 3;
// not used yet
const FIREFOX = 4;
const OPERA = 5;
const SAFARI = 6;
const IE = 7;
var verts = [];
var cols = [];
var norms = [];
var canvas;
var ctx;
var bufferIDCounter = 0;
// shader matrices
var projection;
var view;
var model;
var normalTransform;
var progObj;
// Vertex shader for boxes and spheres
var vertexShaderSource =
"attribute vec3 aVertex;" +
"attribute vec3 aNormal;" +
"attribute vec4 aColor;" +
"uniform bool usingMat;" +
"uniform vec3 specular;" +
"uniform vec3 mat_emissive;" +
"uniform vec3 mat_ambient;" +
"uniform vec3 mat_specular;" +
"uniform float shininess;" +
"uniform mat4 model;" +
"uniform mat4 view;" +
"uniform mat4 projection;" +
"uniform mat4 normalTransform;" +
"uniform int lightCount;" +
" uniform vec3 lposition;" +
" uniform vec3 lcolor;" +
"void DirectionalLight( inout vec3 col, in vec3 ecPos, in vec3 vertNormal ) {" +
" float nDotVP = max(0.0, dot( vertNormal, lposition ));" +
" float nDotVH = max(0.0, dot( vertNormal, normalize( lposition-ecPos )));" +
" col += lcolor * 2.0 * nDotVP;" +
"}" +
"void PointLight( inout vec3 col, in vec3 ecPos, in vec3 vertNormal, in vec3 eye ) {" +
// " float powerfactor;" +
// Get the vector from the light to the vertex
" vec3 VP = lposition - ecPos;" +
// Get the distance from the current vector to the light position
" float d = length( VP ); " +
// Normalize the light ray so it can be used in the dot product operation.
" VP = normalize( VP );" +
" float attenuation = 1.0 / ( 1.0 + ( d ) + ( d * d ));" +
" float nDotVP = max( 0.0, dot( vertNormal, VP ));" +
" vec3 halfVector = normalize( VP + eye );" +
" float nDotHV = max( 0.0, dot( vertNormal, halfVector ));" +
// " spec += specular * powerfactor * attenuation;" +
" col += lcolor * nDotVP * 2.0;" +
"}" +
"void main(void) {" +
" vec3 finalDiffuse = vec3( 0.0, 0.0, 0.0 );" +
" vec4 col = aColor;" +
" vec3 norm = vec3( normalTransform * vec4( aNormal, 0.0 ) );" +
" vec4 ecPos4 = view * model * vec4(aVertex,1.0);" +
" vec3 ecPos = (vec3(ecPos4))/ecPos4.w;" +
" vec3 eye = vec3( 0.0, 0.0, 1.0 );" +
// If there were no lights this draw call, just use the
// assigned fill color of the shape and the specular value
" if( lightCount == 0 ) {" +
" gl_FrontColor = vec4(col[0], col[1], col[2], 1.0);" +
" }" +
" else {" +
" PointLight(finalDiffuse, ecPos, norm, eye );" +
" gl_FrontColor = vec4(finalDiffuse[0] * col[0], finalDiffuse[1] * col[1], finalDiffuse[2] * col[2], 1.0);" +
" }" +
" gl_PointSize = 3.0;" +
" gl_Position = projection * view * model * vec4( aVertex, 1.0 );" +
"}";
var fragmentShaderSource =
"void main(void){" +
" gl_FragColor = gl_Color;" +
"}";
/**
*/
function uniformi(programObj, varName, varValue) {
var varLocation = ctx.getUniformLocation(programObj, varName);
// the variable won't be found if it was optimized out.
if (varLocation !== -1) {
if (varValue.length === 4) {
ctx.uniform4iv(varLocation, varValue);
} else if (varValue.length === 3) {
ctx.uniform3iv(varLocation, varValue);
} else if (varValue.length === 2) {
ctx.uniform2iv(varLocation, varValue);
} else {
ctx.uniform1i(varLocation, varValue);
}
}
}
/**
*/
function uniformf(programObj, varName, varValue) {
var varLocation = ctx.getUniformLocation(programObj, varName);
// the variable won't be found if it was optimized out.
if (varLocation !== -1) {
if (varValue.length === 4) {
ctx.uniform4fv(varLocation, varValue);
} else if (varValue.length === 3) {
ctx.uniform3fv(varLocation, varValue);
} else if (varValue.length === 2) {
ctx.uniform2fv(varLocation, varValue);
} else {
ctx.uniform1f(varLocation, varValue);
}
}
}
/**
*/
function vertexAttribPointer(programObj, varName, size, VBO) {
var varLocation = ctx.getAttribLocation(programObj, varName);
if (varLocation !== -1) {
ctx.bindBuffer(ctx.ARRAY_BUFFER, VBO);
ctx.vertexAttribPointer(varLocation, size, ctx.FLOAT, false, 0, 0);
ctx.enableVertexAttribArray(varLocation);
}
}
/**
*/
function getDataLayout(values){
var normalsPresent = false;
var colorsPresent = false;
// first check if there are 9 values, which would mean we have
// xyz rgb and normals
// We can do this by counting the number of whitespace on the first line
var i = 0;
var numSpaces = 0;
do{
i++;
if(values[i] == " "){
numSpaces++;
}
}while( values[i] != '\n');
// 1.916 -2.421 -4.0339 64 32 16 -0.3727 -0.2476 -0.8942
if(numSpaces === 8){
return 9;
}
// 1.916 -2.421 -4.0339
if(numSpaces == 2){
return 3;
}
var str = "";
for(i = 0; i < 500; i++){
str += values[i];
}
var str_split = str.split(/\s+/);
var data = [];
for(var i=3; i < str_split.length;){
data.push(str_split[i++]);
data.push(str_split[i++]);
data.push(str_split[i++]);
i+=3;
}
for(var i=0; i < data.length; i++){
if(data[i] < 0 || data[i] > 255){
normalsPresent = true;
return 1;
}
}
return 2;
}
/**
*/
function createVBOs(xyz, rgb, norm){
if(ctx){
var o = {};
var newBuffer = ctx.createBuffer();
ctx.bindBuffer(ctx.ARRAY_BUFFER, newBuffer);
ctx.bufferData(ctx.ARRAY_BUFFER, new WebGLFloatArray(xyz), ctx.STATIC_DRAW);
o.posBuffer = newBuffer;
o.id = bufferIDCounter;
o.size = xyz.length;
if(rgb.length > 0){
var newColBuffer = ctx.createBuffer();
ctx.bindBuffer(ctx.ARRAY_BUFFER, newColBuffer);
ctx.bufferData(ctx.ARRAY_BUFFER, new WebGLFloatArray(rgb), ctx.STATIC_DRAW);
o.colBuffer = newColBuffer;
}
if(norm.length > 0){
var newNormBuffer = ctx.createBuffer();
ctx.bindBuffer(ctx.ARRAY_BUFFER, newNormBuffer);
ctx.bufferData(ctx.ARRAY_BUFFER, new WebGLFloatArray(norm), ctx.STATIC_DRAW);
o.normBuffer = newNormBuffer;
}
bufferIDCounter++;
return o;
}
};
/**
*/
function disableVertexAttribPointer(programObj, varName){
var varLocation = ctx.getAttribLocation(programObj, varName);
if (varLocation !== -1) {
ctx.disableVertexAttribArray(varLocation);
}
}
/**
*/
function getUserAgent(userAgentString){
// keep in this order
if(userAgentString.match(/Chrome/)){
return CHROME;
}
if(userAgentString.match(/AppleWebKit/)){
return WEBKIT;
}
if(userAgentString.match(/Minefield/)){
return MINEFIELD;
}
}
/**
*/
function uniformMatrix(programObj, varName, transpose, matrix) {
var varLocation = ctx.getUniformLocation(programObj, varName);
// the variable won't be found if it was optimized out.
if (varLocation !== -1) {
if (matrix.length === 16) {
ctx.uniformMatrix4fv(varLocation, transpose, matrix);
} else if (matrix.length === 9) {
ctx.uniformMatrix3fv(varLocation, transpose, matrix);
} else {
ctx.uniformMatrix2fv(varLocation, transpose, matrix);
}
}
}
/**
*/
var createProgramObject = function(ctx, vetexShaderSource, fragmentShaderSource) {
var vertexShaderObject = ctx.createShader(ctx.VERTEX_SHADER);
ctx.shaderSource(vertexShaderObject, vetexShaderSource);
ctx.compileShader(vertexShaderObject);
if (!ctx.getShaderParameter(vertexShaderObject, ctx.COMPILE_STATUS)) {
throw ctx.getShaderInfoLog(vertexShaderObject);
}
var fragmentShaderObject = ctx.createShader(ctx.FRAGMENT_SHADER);
ctx.shaderSource(fragmentShaderObject, fragmentShaderSource);
ctx.compileShader(fragmentShaderObject);
if (!ctx.getShaderParameter(fragmentShaderObject, ctx.COMPILE_STATUS)) {
throw ctx.getShaderInfoLog(fragmentShaderObject);
}
var programObject = ctx.createProgram();
ctx.attachShader(programObject, vertexShaderObject);
ctx.attachShader(programObject, fragmentShaderObject);
ctx.linkProgram(programObject);
if (!ctx.getProgramParameter(programObject, ctx.LINK_STATUS)) {
throw "Error linking shaders.";
}
return programObject;
};
/**
*/
var xb = {
/**
*/
mouseX: 0,
mouseY: 0,
// Number of frames per seconds rendered in the last second.
frameRate: 0,
// Number of frames rendered since script started running
frameCount: 0,
/**
color
*/
background: function(color){
ctx.clearColor(color[0],color[1],color[2],color[3]);
},
/**
*/
clear: function(){
ctx.clear(ctx.COLOR_BUFFER_BIT | ctx.DEPTH_BUFFER_BIT);
},
/**
Get the version of the library.
*/
getVersion: function(){
return version;
},
/**
Resize the viewport.
This can be called after setup
width
height
*/
resize: function(width, height){
// delete old program object?
// delete old context?
canvas.setAttribute("width", width);
canvas.setAttribute("height", height);
// check if style exists? how? can't just query it...
canvas.style.width = width;
canvas.style.height = height;
ctx = canvas.getContext("experimental-webgl");
ctx.viewport(0, 0, width, height);
ctx.enable(ctx.DEPTH_TEST);
xb.background(bk);
progObj = createProgramObject(ctx, vertexShaderSource, fragmentShaderSource);
ctx.useProgram(progObj);
var fovy = 60;
var aspect = width/height;
var znear = 0.001;
var zfar = 1000;
var ymax = znear * Math.tan(fovy * Math.PI / 360.0);
var ymin = -ymax;
var xmin = ymin * aspect;
var xmax = ymax * aspect;
var left = xmin;
var right = xmax;
var top = ymax;
var bottom = ymin;
var X = 2 * znear / (right - left);
var Y = 2 * znear / (top - bottom);
var A = (right + left) / (right - left);
var B = (top + bottom) / (top - bottom);
var C = -(zfar + znear) / (zfar - znear);
var D = -2 * zfar * znear / (zfar - znear);
projection = M4x4.$(
X, 0, A, 0,
0, Y, B, 0,
0, 0, C, D,
0, 0, -1, 0);
view = M4x4.$(1,0,0,0,0,1,0,0,0,0,1, 0, 0,0,0,1);
model = M4x4.$(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1);
normalTransform = M4x4.$(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1);
// if VBOs already exist, recreate them
if(VBOs) {
VBOs = createVBOs(verts, cols, norms);
if(cols.length > 0){
uniformf(progObj, "lcolor", [1,1,1]);
uniformf(progObj, "lposition", [0,0,-1]);
uniformi(progObj, "lightCount", 1);
}
}
uniformMatrix(progObj, "view", false, M4x4.transpose(view));
uniformMatrix(progObj, "projection", false, M4x4.transpose(projection));
},
/**
*/
render: function(){
frames++;
xb.frameCount++;
var now = new Date();
if(ctx && VBOs){
vertexAttribPointer(progObj, "aVertex", 3, VBOs.posBuffer);
if(VBOs.colBuffer){
vertexAttribPointer(progObj, "aColor", 3, VBOs.colBuffer);
}
else{
disableVertexAttribPointer(progObj, "aColor");
}
if(VBOs.normBuffer){
vertexAttribPointer(progObj, "aNormal", 3, VBOs.normBuffer);
uniformf(progObj, "lcolor", [1,1,1]);
uniformf(progObj, "lposition", [0,0,-1]);
uniformi(progObj, "lightCount", 1);
}
else{
disableVertexAttribPointer(progObj, "aNormal");
}
var mvm = M4x4.mul(view, model);
normalTransform = M4x4.inverseOrthonormal(mvm);
uniformMatrix(progObj, "normalTransform", false, M4x4.transpose(normalTransform));
uniformMatrix(progObj, "model", false, model);
ctx.drawArrays(ctx.POINTS, 0, VBOs.size/3);
}
// if more than 1 second has elapsed, recalculate fps
if(now - lastTime > 1000){
xb.frameRate = frames/(now-lastTime)*1000;
frames = 0;
lastTime = now;
}
// clear state
model = M4x4.$(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1);
},
/**
Update the cursor position everytime the mouse moves
*/
mouseMove: function(e){
xb.mouseX = e.pageX;
xb.mouseY = e.pageY;
},
/**
element
type
func
*/
attach: function(element, type, func){
//
if(element.addEventListener){
element.addEventListener(type, func, false);
}
},
/*
returns -1 or 1
scrolling towards user is 1
scrolling towards screen is -1
should this return an object or a single value?
*/
_mouseScroll: function(evt){
var delta = 0;
// which check to use?
//if(browser === MINEFIELD){
if(evt.detail){
delta = evt.detail / 3;
}
else if(evt.wheelDelta){
delta = -evt.wheelDelta / 360;
}
if(xb.onMouseScroll){
xb.onMouseScroll(delta);
}
},
_mousePressed: function(evt){
if(xb.onMousePressed){
xb.onMousePressed();
}
},
_mouseReleased: function(){
if(xb.onMouseReleased){
xb.onMouseReleased();
}
},
/**
cvs
renderCB
*/
setup: function(cvs, renderCB){
canvas = cvs;
browser = getUserAgent(navigator.userAgent);
lastTime = new Date();
frames = 0;
xb.resize(canvas.getAttribute("width"), canvas.getAttribute("height"));
xb.renderCallback = renderCB;
setInterval(xb.renderCallback, 10);
xb.attach(cvs, "mouseup", xb._mouseReleased);
xb.attach(cvs, "mousedown", xb._mousePressed);
xb.attach(cvs, "mousemove", xb.mouseMove);
xb.attach(cvs, "DOMMouseScroll", xb._mouseScroll);
xb.attach(cvs, "mousewheel", xb._mouseScroll);
},
/**
1 arg = uniform scaling
3 args = independant scaling
*/
scale: function(sx, sy, sz){
// uniform scaling
if( !sy && !sz){
model = M4x4.scale1(sx, model);
}
else{
model = M4x4.scale3(sx, sy, sz, model);
}
},
/**
tx
ty
tz
*/
translate: function(tx, ty, tz){
model = M4x4.translate3(tx, ty, tz, model, model);
},
/**
radians
*/
rotateY: function(radians){
model = M4x4.rotate(radians,V3.$(0,1,0),model);
},
/**
radians
*/
rotateX: function(radians){
model = M4x4.rotate(radians,V3.$(1,0,0),model);
},
/**
radians
*/
rotateZ: function(radians){
model = M4x4.rotate(radians,V3.$(0,0,1),model);
},
/**
o - object such as {path:"acorn.asc", autoCenter: true}
*/
loadFile: function(o){
var path = o.path;
// need ||?
var autoCenter = o.autoCenter || false;
var AJAX = new XMLHttpRequest();
// this doesn't work
// AJAX.addEventListener("progress", f,false);
AJAX.open("GET", path, true);
AJAX.send(null);
var file = {
status: 0,
progress: 0,
numPoints: 0,
};
AJAX.onreadystatechange =
function(){
//??
if(AJAX.status === 200){
file.status = 1;
}
if(AJAX.readyState === XHR_DONE){
var code = getDataLayout(AJAX.responseText);
var normalsPresent = false;
var colorsPresent = false;
if(code == 1 ){
code = 6;
normalsPresent = true;
}
else if(code ==2 ){
code = 6;
colorsPresent = true;
}
if(code ==9){
normalsPresent = true;
colorsPresent = true;
}
var values = AJAX.responseText.split(/\s+/);
const numVerts = values.length/code;
var objCenter = [0,0,0];
// xyz rgb normals
for(var i = 0, len = values.length; i < len; i += code){
var currX = parseFloat(values[i]);
var currY = parseFloat(values[i+1]);
var currZ = parseFloat(values[i+2]);
verts.push(currX);
verts.push(currY);
verts.push(currZ);
// don't waste cycles if the user didn't want it centered.
if(autoCenter){
objCenter[0] += currX;
objCenter[1] += currY;
objCenter[2] += currZ;
}
if(colorsPresent){
cols.push(parseInt(values[i+3])/255);
cols.push(parseInt(values[i+4])/255);
cols.push(parseInt(values[i+5])/255);
}
if(normalsPresent){
norms.push(parseFloat(values[i+6]));
norms.push(parseFloat(values[i+7]));
norms.push(parseFloat(values[i+8]));
}
}
// if the user wants to center the point cloud
if(autoCenter){
objCenter[0] /= numVerts;
objCenter[1] /= numVerts;
objCenter[2] /= numVerts;
}
// if the user wanted to autocenter the point cloud,
// iterate over all the verts and subtract by the
// point cloud's current center.
if(autoCenter){
for(var i = 0; i < numVerts; i++){
verts[i*3] -= objCenter[0];
verts[i*3+1] -= objCenter[1];
verts[i*3+2] -= objCenter[2];
}
}
VBOs = createVBOs(verts, cols, norms);
file.status = 4;
}
}
return file;
}
}
return xb;
};