19 var Viewer3d = Viewer3d || {};
27 Viewer3d.launch =
function() {
29 Debug.log(
"Viewer3d.launch");
31 Viewer3d.omni = document.getElementById(
"omni");
32 Viewer3d.omni.innerHTML =
"";
35 var w = Viewer3d.w = window.innerWidth;
36 var h = Viewer3d.h = window.innerHeight;
38 Viewer3d.omni.style.position =
"absolute";
39 Viewer3d.omni.style.left = 0 +
"px";
40 Viewer3d.omni.style.top = 0 +
"px";
41 Viewer3d.omni.style.width = w +
"px";
42 Viewer3d.omni.style.height = h +
"px";
43 Viewer3d.omni.style.backgroundColor =
"rgb(30,30,30)";
45 Viewer3d.omni.innerHTML =
"<center><div style='margin-top:" + (h/2-8) +
"px'>Loading 3-D...</div></center>";
58 Viewer3d.loadScripts();
65 Viewer3d.CANVAS_MIN_SIZE = 300;
66 Viewer3d.HUD_WIDTH = 200;
67 Viewer3d.HUD_MARGIN_RIGHT = 10;
68 Viewer3d.HUD_DROP_DOWN_SPEED = 10;
69 Viewer3d.WEBGL_NEAR_CLIPPING_DEPTH = 1000.0;
70 Viewer3d.WEBGL_FAR_CLIPPING_DEPTH = 10000000.0;
71 Viewer3d.WEBGL_RESET_CAMERA_POS = [-300000, 50000, -300000];
72 Viewer3d.WEBGL_RESET_CAMERA_FOCUS = [0, 50000, 0];
73 Viewer3d.WEBGL_RESET_CAMERA_UP = [0, 1, 0];
74 Viewer3d.WEBGL_AXES_LENGTH = 1000000;
75 Viewer3d.WEBGL_GRID_SPACING = 50000;
76 Viewer3d.WEBGL_GRID_EXPANSE = 1000000;
77 Viewer3d.WEBGL_GRID_YOFF = -100000;
78 Viewer3d.TICK_REFRESH_PERIOD = 30;
79 Viewer3d.KEY_ROT_SPEED_FAST = 0.06;
80 Viewer3d.KEY_ROT_SPEED_SLOW = 0.01;
81 Viewer3d.KEY_MOVE_SPEED_FAST = 10000;
82 Viewer3d.KEY_MOVE_SPEED_SLOW = 1000;
83 Viewer3d.MOUSE_NAV_SPEED = 0.005;
84 Viewer3d.MOUSE_WHEEL_SPEED_SLOW = 10000;
85 Viewer3d.MOUSE_WHEEL_SPEED_FAST = 100000;
86 Viewer3d.TOUCH_NAV_SPEED = 0.001;
87 Viewer3d.TOUCH_ZOOM_SPEED = 10000;
88 Viewer3d.DOUBLE_TAP_RESET_TIME = 600;
89 Viewer3d.DOUBLE_TAP_HUD_Y_ACCEPT = 300;
90 Viewer3d.Z_SQUASH_FACTOR = 10.0;
92 Viewer3d.FLY_BY_ROT_RATE = 0.003;
93 Viewer3d.FLY_BY_HEIGHT_MAX = 10000;
94 Viewer3d.FLY_BY_HEIGHT_RATE = 0.001;
95 Viewer3d.FLY_BY_RADIUS_RATE = 0.002;
96 Viewer3d.FLY_BY_RADIUS_SWING = 100000;
97 Viewer3d.FLY_BY_RADIUS_MIN = 10000;
98 Viewer3d.FLY_BY_FOCUS = [0,Viewer3d.FLY_BY_HEIGHT_MAX/10,0];
99 Viewer3d.FLY_BY_EVENT_TIME = 500;
100 Viewer3d.FLY_BY_ACCUM_TIME = 5000;
106 Viewer3d.textureCanvas;
107 Viewer3d.textureContext;
114 Viewer3d.lastMousePos = [-1,-1];
115 Viewer3d.lastTwoTouchMag = 0;
116 Viewer3d.lastTouchTime = 0;
117 Viewer3d.cameraAction =
false;
118 Viewer3d.keysDown = [];
120 Viewer3d.cameraFocus;
122 Viewer3d.cameraKeyNavFast =
false;
124 Viewer3d.drawAxesFlag =
true;
125 Viewer3d.drawGridFlag =
true;
126 Viewer3d.drawTracksFlag =
true;
127 Viewer3d.enableMouseNav =
true;
128 Viewer3d.enableFlyBy =
true;
129 Viewer3d.accumulateEvents =
true;
130 Viewer3d.enableFlyByParameter = 0;
131 Viewer3d.flyByEvent = 0;
132 Viewer3d.flyByModeAlarm = 0;
133 Viewer3d.flyByEventAlarm = 0;
138 Viewer3d.eventToDisplay = 0;
139 Viewer3d.runNumber = -1;
141 Viewer3d.scriptLoadedCount;
142 Viewer3d.SCRIPTS_TO_LOAD = 2;
144 Viewer3d.fragmentShader;
145 Viewer3d.vertexShader;
146 Viewer3d.shaderProgram;
147 Viewer3d.perspectiveMatrix;
148 Viewer3d.modelViewMatrix;
149 Viewer3d.modelViewMatrixStack = [];
193 Viewer3d.loadScripts =
function() {
195 Viewer3d.scriptLoadedCount = 0;
197 var head = document.getElementsByTagName(
'head')[0];
201 script = document.createElement(
'script');
202 script.type =
'text/javascript';
203 script.src =
'/WebPath/js/js_lib/glMatrix-0.9.5.min.js';
204 script.onload = Viewer3d.handleScriptLoaded;
205 head.appendChild(script);
207 script = document.createElement(
'script');
208 script.type =
'text/javascript';
209 script.src =
'/WebPath/js/js_lib/webgl-utils.js';
210 script.onload = Viewer3d.handleScriptLoaded;
211 head.appendChild(script);
214 Viewer3d.handleScriptLoaded =
function() {
215 ++Viewer3d.scriptLoadedCount;
216 Debug.log(
"Viewer3d.handleScriptLoaded " + Viewer3d.scriptLoadedCount);
217 if(Viewer3d.SCRIPTS_TO_LOAD == Viewer3d.scriptLoadedCount)
218 Viewer3d.initCanvas();
221 Viewer3d.initCanvas =
function() {
223 Viewer3d.canvas = document.createElement(
'canvas');
224 Viewer3d.canvas.style.position =
"absolute";
225 Viewer3d.canvas.style.left = 0 +
"px";
226 Viewer3d.canvas.style.top = 0 +
"px";
227 Viewer3d.canvas.style.zIndex = -1;
228 Viewer3d.omni.appendChild(Viewer3d.canvas);
230 Viewer3d.textureCanvas = document.createElement(
'canvas');
231 Viewer3d.textureContext = Viewer3d.textureCanvas.getContext(
'2d');
233 Viewer3d.textureCanvas.width = 128;
234 Viewer3d.textureCanvas.height = 128;
235 Viewer3d.textureContext.fillStyle =
"#FF33FF";
236 Viewer3d.textureContext.textAlign =
"center";
237 Viewer3d.textureContext.textBaseline =
"middle";
238 Viewer3d.textureContext.font =
"12px monospace";
239 Viewer3d.textureContext.fillText(
"3D Pixel Sensor",Viewer3d.textureCanvas.width/2, Viewer3d.textureCanvas.height/2);
241 Viewer3d.initWebGL();
245 Viewer3d.initTexture();
248 Viewer3d.hud =
new Viewer3d.createHud();
250 window.onresize = Viewer3d.handleWindowResize;
251 Viewer3d.handleWindowResize();
253 Viewer3d.getGeometry();
254 Viewer3d.getEvents();
256 if(Viewer3d.tickTimer) clearInterval(Viewer3d.tickTimer);
257 Viewer3d.tickTimer = setInterval(Viewer3d.tick, Viewer3d.TICK_REFRESH_PERIOD);
259 Viewer3d.canvas.onmousedown = Viewer3d.handleCanvasMouseDown;
260 Viewer3d.canvas.onmousemove = Viewer3d.handleCanvasMouseMove;
261 Viewer3d.canvas.onmousewheel = Viewer3d.handleCanvasMouseWheel;
262 Viewer3d.canvas.addEventListener(
'touchstart',Viewer3d.handleCanvasTouchStart);
263 Viewer3d.canvas.addEventListener(
'touchmove',Viewer3d.handleCanvasTouchMove);
264 window.onkeydown = Viewer3d.handleCanvasKeyDown;
265 window.onkeyup = Viewer3d.handleCanvasKeyUp;
269 function handleLoadedTexture(texture, textureCanvas) {
270 Debug.log(
"Viewer3d handleLoadedTexture");
271 var gl = Viewer3d.gl;
272 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL,
true);
274 gl.bindTexture(gl.TEXTURE_2D, texture);
275 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textureCanvas);
276 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
277 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
278 gl.generateMipmap(gl.TEXTURE_2D);
280 gl.bindTexture(gl.TEXTURE_2D, null);
286 Viewer3d.initTexture =
function() {
287 Debug.log(
"Viewer3d initTexture");
288 Viewer3d.cTexture = Viewer3d.gl.createTexture();
289 handleLoadedTexture(Viewer3d.cTexture, Viewer3d.textureCanvas);
296 Viewer3d.tick =
function() {
298 if(Viewer3d.enableFlyBy)
300 var radius = Math.sin(Viewer3d.enableFlyByParameter*Viewer3d.FLY_BY_RADIUS_RATE)*Viewer3d.FLY_BY_RADIUS_SWING/2 +
301 Viewer3d.FLY_BY_RADIUS_SWING/2 + Viewer3d.FLY_BY_RADIUS_MIN;
302 var height = Math.cos(Viewer3d.enableFlyByParameter*Viewer3d.FLY_BY_HEIGHT_RATE)*Viewer3d.FLY_BY_HEIGHT_MAX;
304 vec3.set([Math.cos(Viewer3d.enableFlyByParameter*Viewer3d.FLY_BY_ROT_RATE)*radius,
306 Math.sin(Viewer3d.enableFlyByParameter*Viewer3d.FLY_BY_ROT_RATE)*radius],
308 vec3.set(Viewer3d.FLY_BY_FOCUS, Viewer3d.cameraFocus);
313 if(!Viewer3d.flyByModeAlarm ||
314 (
new Date()).getTime() > Viewer3d.flyByModeAlarm)
316 if(Viewer3d.eventToDisplay < 0)
318 Viewer3d.eventToDisplay = Viewer3d.flyByEvent;
320 Viewer3d.flyByEventAlarm = (
new Date()).getTime() + Viewer3d.FLY_BY_EVENT_TIME;
322 Debug.log(
"Viewer3d tick Fly-by Mode: Individual");
326 Viewer3d.eventToDisplay = -1;
327 Viewer3d.drawTracksFlag = !Viewer3d.drawTracksFlag;
329 Debug.log(
"Viewer3d tick Fly-by Mode: Accum");
333 Viewer3d.flyByModeAlarm = (
new Date()).getTime() + Viewer3d.FLY_BY_ACCUM_TIME;
336 if(Viewer3d.eventToDisplay >= 0 &&
337 (
new Date()).getTime() > Viewer3d.flyByEventAlarm)
339 ++Viewer3d.flyByEvent;
340 if(!Viewer3d.events || Viewer3d.flyByEvent >= Viewer3d.events.length)
341 Viewer3d.flyByEvent = 0;
342 Viewer3d.eventToDisplay = Viewer3d.flyByEvent;
345 Viewer3d.flyByEventAlarm = (
new Date()).getTime() + Viewer3d.FLY_BY_EVENT_TIME;
347 Debug.log(
"Viewer3d tick Fly-by Event: " + Viewer3d.eventToDisplay);
358 ++Viewer3d.enableFlyByParameter;
362 if(Viewer3d.checkKeyAction() || Viewer3d.cameraAction)
367 Viewer3d.handleCanvasKeyDown =
function(event) {
368 var c = String.fromCharCode(event.keyCode);
371 c ==
"A" || c ==
"S" || c ==
"D" || c ==
"W" ||
372 c ==
"Q" || c ==
"E" || c ==
"R" || c ==
"F" ||
373 c ==
"%" || c ==
"(" || c ==
"'" || c ==
"&")
374 && !Viewer3d.keysDown[c])
375 Viewer3d.keysDown[c] = 1;
378 Viewer3d.handleCanvasKeyUp =
function(event) {
379 var c = String.fromCharCode(event.keyCode);
381 c ==
"A" || c ==
"S" || c ==
"D" || c ==
"W" ||
382 c ==
"Q" || c ==
"E" || c ==
"R" || c ==
"F" ||
383 c ==
"%" || c ==
"(" || c ==
"'" || c ==
"&")
384 Viewer3d.keysDown[c] = 0;
385 else if(c ==
" " || event.keyCode == 16)
387 Viewer3d.cameraKeyNavFast = !Viewer3d.cameraKeyNavFast;
388 Viewer3d.hud.update();
392 Viewer3d.resetCamera();
397 Viewer3d.checkKeyAction =
function(event) {
400 var actionTaken =
false;
402 if(Viewer3d.keysDown[
"Q"] || Viewer3d.keysDown[
"E"])
405 vec3.subtract(Viewer3d.cameraFocus,Viewer3d.cameraPos,dir);
408 vec3.cross(Viewer3d.cameraUp,dir,left);
409 vec3.normalize(left);
410 vec3.scale(left,Viewer3d.cameraKeyNavFast?Viewer3d.KEY_ROT_SPEED_FAST:Viewer3d.KEY_ROT_SPEED_SLOW);
412 if(Viewer3d.keysDown[
"Q"]) vec3.add(Viewer3d.cameraFocus, left, Viewer3d.cameraFocus);
413 else vec3.subtract(Viewer3d.cameraFocus, left, Viewer3d.cameraFocus);
415 vec3.subtract(Viewer3d.cameraFocus,Viewer3d.cameraPos,dir);
417 vec3.add(Viewer3d.cameraPos, dir, Viewer3d.cameraFocus);
422 if(Viewer3d.keysDown[
"R"] || Viewer3d.keysDown[
"F"])
425 vec3.subtract(Viewer3d.cameraFocus,Viewer3d.cameraPos,dir);
428 vec3.set(Viewer3d.cameraUp,up);
430 vec3.scale(up,Viewer3d.cameraKeyNavFast?Viewer3d.KEY_ROT_SPEED_FAST:Viewer3d.KEY_ROT_SPEED_SLOW);
432 if(Viewer3d.keysDown[
"R"]) vec3.add(Viewer3d.cameraFocus, up, Viewer3d.cameraFocus);
433 else vec3.subtract(Viewer3d.cameraFocus, up, Viewer3d.cameraFocus);
435 vec3.subtract(Viewer3d.cameraFocus,Viewer3d.cameraPos,dir);
437 vec3.add(Viewer3d.cameraPos, dir, Viewer3d.cameraFocus);
442 if(Viewer3d.keysDown[
"A"] || Viewer3d.keysDown[
"D"] ||
443 Viewer3d.keysDown[
"%"] || Viewer3d.keysDown[
"'"])
446 vec3.subtract(Viewer3d.cameraFocus,Viewer3d.cameraPos,dir);
449 vec3.cross(Viewer3d.cameraUp,dir,left);
450 vec3.normalize(left);
451 vec3.scale(left,Viewer3d.cameraKeyNavFast?Viewer3d.KEY_MOVE_SPEED_FAST:Viewer3d.KEY_MOVE_SPEED_SLOW);
453 if(Viewer3d.keysDown[
"%"] || Viewer3d.keysDown[
"A"]) vec3.add(Viewer3d.cameraPos, left);
454 else vec3.subtract(Viewer3d.cameraPos, left);
457 vec3.add(Viewer3d.cameraPos, dir, Viewer3d.cameraFocus);
461 if(Viewer3d.keysDown[
"("] || Viewer3d.keysDown[
"&"])
464 vec3.subtract(Viewer3d.cameraFocus,Viewer3d.cameraPos,dir);
467 vec3.set(Viewer3d.cameraUp,up);
469 vec3.scale(up,Viewer3d.cameraKeyNavFast?Viewer3d.KEY_MOVE_SPEED_FAST:Viewer3d.KEY_MOVE_SPEED_SLOW);
471 if(Viewer3d.keysDown[
"&"]) vec3.add(Viewer3d.cameraPos, up);
472 else vec3.subtract(Viewer3d.cameraPos, up);
475 vec3.add(Viewer3d.cameraPos, dir, Viewer3d.cameraFocus);
479 if(Viewer3d.keysDown[
"W"] || Viewer3d.keysDown[
"S"])
482 vec3.subtract(Viewer3d.cameraFocus,Viewer3d.cameraPos,dir);
484 vec3.scale(dir,Viewer3d.cameraKeyNavFast?Viewer3d.KEY_MOVE_SPEED_FAST:Viewer3d.KEY_MOVE_SPEED_SLOW);
486 if(Viewer3d.keysDown[
"W"]) vec3.add(Viewer3d.cameraPos, dir);
487 else vec3.subtract(Viewer3d.cameraPos, dir);
490 vec3.add(Viewer3d.cameraPos, dir, Viewer3d.cameraFocus);
494 if(Viewer3d.keysDown[
"W"] || Viewer3d.keysDown[
"S"])
502 Viewer3d.handleCanvasMouseWheel =
function(event) {
503 var delta =
event.wheelDelta/120;
504 Viewer3d.zoomCameraByDelta(delta,
505 Viewer3d.cameraKeyNavFast?Viewer3d.MOUSE_WHEEL_SPEED_FAST:Viewer3d.MOUSE_WHEEL_SPEED_SLOW);
508 Viewer3d.zoomCameraByDelta =
function(delta, speed) {
512 vec3.subtract(Viewer3d.cameraFocus,Viewer3d.cameraPos,dir);
514 vec3.scale(dir,speed);
516 if(delta > 0) vec3.add(Viewer3d.cameraPos, dir);
517 else vec3.subtract(Viewer3d.cameraPos, dir);
520 vec3.add(Viewer3d.cameraPos, dir, Viewer3d.cameraFocus);
522 Viewer3d.cameraAction =
true;
525 Viewer3d.handleCanvasTouchMove =
function(touchEvent) {
527 touchEvent.preventDefault();
528 touchEvent.cancelBubble=
true;
529 Viewer3d.lastTouchTime = 0;
531 var touch = touchEvent.targetTouches[0];
532 var newMousePos = [touch.pageX, touch.pageY];
533 var touch2 = touchEvent.targetTouches[1];
536 var mag = Math.sqrt((touch2.pageX-touch.pageX)*(touch2.pageX-touch.pageX) +
537 (touch2.pageY-touch.pageY)*(touch2.pageY-touch.pageY));
539 if(Viewer3d.lastTwoTouchMag)
540 Viewer3d.zoomCameraByDelta(mag-Viewer3d.lastTwoTouchMag, Viewer3d.TOUCH_ZOOM_SPEED);
541 Viewer3d.lastTwoTouchMag = mag;
542 Viewer3d.lastMousePos = [-1,-1];
546 var delta = [Viewer3d.lastMousePos[0] - newMousePos[0],
547 Viewer3d.lastMousePos[1] - newMousePos[1]];
548 if(Viewer3d.lastMousePos[0] > 0)
549 Viewer3d.panCameraByDelta(delta,Viewer3d.TOUCH_NAV_SPEED);
550 Viewer3d.lastMousePos = newMousePos;
553 Viewer3d.handleCanvasMouseMove =
function(event) {
555 if(Viewer3d.enableMouseNav && event.which == 1)
557 var newMousePos = [
event.clientX,
event.clientY];
559 var delta = [newMousePos[0] - Viewer3d.lastMousePos[0],
560 newMousePos[1] - Viewer3d.lastMousePos[1]];
562 Viewer3d.panCameraByDelta(delta,Viewer3d.MOUSE_NAV_SPEED);
563 Viewer3d.lastMousePos = newMousePos;
567 Viewer3d.handleCanvasTouchStart =
function(touchEvent) {
568 var touchTime =
new Date().getTime();
569 var touch = touchEvent.targetTouches[0];
570 Viewer3d.lastMousePos = [touch.pageX, touch.pageY];
571 if(Viewer3d.lastTouchTime && !touchEvent.targetTouches[1] &&
572 touchTime - Viewer3d.lastTouchTime < Viewer3d.DOUBLE_TAP_RESET_TIME)
575 touchEvent.preventDefault();
577 if(touch.pageY < Viewer3d.DOUBLE_TAP_HUD_Y_ACCEPT &&
578 touch.pageX > window.innerWidth - Viewer3d.hud.hudMouseOverDiv.offsetWidth - 2*Viewer3d.HUD_MARGIN_RIGHT)
580 Viewer3d.hud.mouseOverDropDown();
584 if(touch.pageY < Viewer3d.DOUBLE_TAP_HUD_Y_ACCEPT &&
585 touch.pageX < Viewer3d.hud.hudMouseOverDiv.offsetWidth + 2*Viewer3d.HUD_MARGIN_RIGHT)
592 Debug.log(
"Viewer3d handleCanvasTouchStart Double Tap Reset Camera! " + (touchTime - Viewer3d.lastTouchTime));
593 Viewer3d.resetCamera();
598 Viewer3d.lastTouchTime = touchTime;
599 Viewer3d.hud.mouseOutDropDown();
603 Viewer3d.handleCanvasMouseDown =
function(event) {
604 Debug.log(
"Viewer3d handleCanvasMouseDown ");
605 Viewer3d.lastMousePos = [
event.clientX,
event.clientY];
608 Viewer3d.hud.hudRunNumberInput.blur();
612 Viewer3d.panCameraByDelta =
function(delta, speed) {
613 if(!delta[0] && !delta[1])
return;
617 vec3.subtract(Viewer3d.cameraFocus,Viewer3d.cameraPos,dir);
620 vec3.cross(Viewer3d.cameraUp,dir,left);
621 vec3.normalize(left);
622 vec3.scale(left,-delta[0]*speed);
624 vec3.add(Viewer3d.cameraFocus, left, Viewer3d.cameraFocus);
626 vec3.subtract(Viewer3d.cameraFocus,Viewer3d.cameraPos,dir);
628 vec3.add(Viewer3d.cameraPos, dir, Viewer3d.cameraFocus);
632 vec3.set(Viewer3d.cameraUp,up);
634 vec3.scale(up,-delta[1]*speed);
636 vec3.add(Viewer3d.cameraFocus, up, Viewer3d.cameraFocus);
637 vec3.subtract(Viewer3d.cameraFocus,Viewer3d.cameraPos,dir);
639 vec3.add(Viewer3d.cameraPos, dir, Viewer3d.cameraFocus);
641 Viewer3d.cameraAction =
true;
644 Viewer3d.handleWindowResize =
function() {
646 var w = window.innerWidth < Viewer3d.CANVAS_MIN_SIZE? Viewer3d.CANVAS_MIN_SIZE:window.innerWidth;
647 var h = window.innerHeight < Viewer3d.CANVAS_MIN_SIZE? Viewer3d.CANVAS_MIN_SIZE:window.innerHeight;
649 Debug.log(
"Viewer3d.handleWindowResize " + w +
"-" + h);
651 Viewer3d.omni.style.width = w +
"px";
652 Viewer3d.omni.style.height = h +
"px";
653 Viewer3d.canvas.style.width = w +
"px";
654 Viewer3d.canvas.style.height = h +
"px";
655 Viewer3d.canvas.width = w;
656 Viewer3d.canvas.height = h;
659 var gl = Viewer3d.gl;
660 gl.viewportWidth = Viewer3d.canvas.width;
661 gl.viewportHeight = Viewer3d.canvas.height;
662 gl.clearColor(0.0, 0.0, 0.0, 1.0);
663 gl.enable(Viewer3d.gl.DEPTH_TEST);
664 gl.depthFunc(Viewer3d.gl.LEQUAL);
665 gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
666 mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, Viewer3d.WEBGL_NEAR_CLIPPING_DEPTH, Viewer3d.WEBGL_FAR_CLIPPING_DEPTH, Viewer3d.perspectiveMatrix);
669 Viewer3d.hud.handleWindowResize();
677 Viewer3d.initWebGL =
function() {
679 Viewer3d.gl = Viewer3d.canvas.getContext(
"webgl") || Viewer3d.canvas.getContext(
"experimental-webgl");
681 catch (e) { alert(
"Could not initialize WebGL, sorry :-(");
return; }
683 if (!Viewer3d.gl) { alert(
"Could not initialize WebGL! sorry :-(");
return; }
685 Viewer3d.initShaders();
687 Viewer3d.perspectiveMatrix = mat4.create();
688 Viewer3d.modelViewMatrix = mat4.create();
690 Viewer3d.resetCamera();
691 Viewer3d.initAxesAndGridBuffers();
694 Viewer3d.resetCamera =
function() {
695 Viewer3d.cameraPos = [];
696 Viewer3d.cameraUp = [];
697 Viewer3d.cameraFocus = [];
698 vec3.set(Viewer3d.WEBGL_RESET_CAMERA_POS, Viewer3d.cameraPos);
699 vec3.set(Viewer3d.WEBGL_RESET_CAMERA_UP, Viewer3d.cameraUp);
700 vec3.set(Viewer3d.WEBGL_RESET_CAMERA_FOCUS, Viewer3d.cameraFocus);
703 vec3.normalize(Viewer3d.cameraUp);
705 Debug.log(
"Viewer3d.resetCamera to pos: " + Viewer3d.cameraPos +
" focus: " + Viewer3d.cameraFocus +
" up: " + Viewer3d.cameraUp);
709 Viewer3d.initShaders =
function() {
712 var req =
new XMLHttpRequest();
713 req.open(
"GET",
'/WebPath/js/js_lib/webgl_shader-fs.shader',
false);
715 var fsCode = (req.status == 200) ? req.responseText : null;
716 if(!fsCode) { alert(
"Could not initialize WebGL fs shaders, sorry :-(");
return; }
717 Viewer3d.fragmentShader = Viewer3d.gl.createShader(Viewer3d.gl.FRAGMENT_SHADER);
719 Viewer3d.gl.shaderSource(Viewer3d.fragmentShader, fsCode);
720 Viewer3d.gl.compileShader(Viewer3d.fragmentShader);
721 if (!Viewer3d.gl.getShaderParameter(Viewer3d.fragmentShader, Viewer3d.gl.COMPILE_STATUS))
723 alert(
"An error occurred compiling the fragment shader: " + Viewer3d.gl.getShaderInfoLog(Viewer3d.fragmentShader));
728 req =
new XMLHttpRequest();
729 req.open(
"GET",
'/WebPath/js/js_lib/webgl_shader-vs.shader',
false);
731 var vsCode = (req.status == 200) ? req.responseText : null;
732 if(!vsCode) { alert(
"Could not initialize WebGL vs shaders, sorry :-(");
return; }
733 Viewer3d.vertexShader = Viewer3d.gl.createShader(Viewer3d.gl.VERTEX_SHADER);
735 Viewer3d.gl.shaderSource(Viewer3d.vertexShader, vsCode);
736 Viewer3d.gl.compileShader(Viewer3d.vertexShader);
737 if (!Viewer3d.gl.getShaderParameter(Viewer3d.vertexShader, Viewer3d.gl.COMPILE_STATUS))
739 alert(
"An error occurred compiling the vertex shader: " + Viewer3d.gl.getShaderInfoLog(Viewer3d.vertexShader));
744 Viewer3d.shaderProgram = Viewer3d.gl.createProgram();
745 Viewer3d.gl.attachShader(Viewer3d.shaderProgram, Viewer3d.vertexShader);
746 Viewer3d.gl.attachShader(Viewer3d.shaderProgram, Viewer3d.fragmentShader);
747 Viewer3d.gl.linkProgram(Viewer3d.shaderProgram);
749 if (!Viewer3d.gl.getProgramParameter(Viewer3d.shaderProgram, Viewer3d.gl.LINK_STATUS))
750 { alert(
"Could not initialize shader program");
return; }
752 Viewer3d.gl.useProgram(Viewer3d.shaderProgram);
754 Viewer3d.shaderProgram.vertexPositionAttribute = Viewer3d.gl.getAttribLocation(Viewer3d.shaderProgram,
"aVertexPosition");
755 Viewer3d.gl.enableVertexAttribArray(Viewer3d.shaderProgram.vertexPositionAttribute);
757 Viewer3d.shaderProgram.vertexColorAttribute = Viewer3d.gl.getAttribLocation(Viewer3d.shaderProgram,
"aVertexColor");
758 Viewer3d.gl.enableVertexAttribArray(Viewer3d.shaderProgram.vertexColorAttribute);
760 Viewer3d.shaderProgram.pMatrixUniform = Viewer3d.gl.getUniformLocation(Viewer3d.shaderProgram,
"uPMatrix");
761 Viewer3d.shaderProgram.mvMatrixUniform = Viewer3d.gl.getUniformLocation(Viewer3d.shaderProgram,
"uMVMatrix");
764 Viewer3d.createHud =
function() {
767 var animationTargetTop, isDropDownAnimating, isDropDownDown;
768 var controlMouseIsDown =
false;
769 var controlMouseTimeout = 0;
770 var getEventsTimeout = 0;
772 this.isInMotion =
function() {
return isDropDownAnimating; }
774 this.handleWindowResize =
function() {
775 Debug.log(
"Viewer3d Hud handleWindowResize");
776 this.hudMouseOverDiv.style.left = window.innerWidth - this.hudMouseOverDiv.offsetWidth - Viewer3d.HUD_MARGIN_RIGHT +
"px";
778 this.hudNavSpeedDiv.style.left = 5 +
"px";
779 this.hudNavSpeedDiv.style.top = window.innerHeight - 95 +
"px";
784 this.checkboxUpdate =
function(i) {
785 var chk = document.getElementById(
"hudCheckbox" + i);
786 Debug.log(
"Viewer3d Hud checkboxUpdate " + i +
"=" + chk.checked);
788 if(i==0) Viewer3d.drawAxesFlag = chk.checked;
789 else if(i==1) Viewer3d.drawGridFlag = chk.checked;
790 else if(i==2) Viewer3d.drawTracksFlag = chk.checked;
791 else if(i==3) Viewer3d.enableMouseNav = chk.checked;
792 else if(i==4) Viewer3d.enableFlyBy = chk.checked;
793 else if(i==5) Viewer3d.accumulateEvents = chk.checked;
799 this.update =
function() {
801 this.hudNavSpeedDiv.innerHTML =
"Tests of radiation-hard sensors for the SLHC<br>";
802 this.hudNavSpeedDiv.innerHTML +=
"at the Fermi Test Beam Facility.<br>";
803 this.hudNavSpeedDiv.innerHTML +=
"Devices under test for this run were 3D Pixel Sensors.<br>";
804 if(Viewer3d.enableFlyBy)
805 this.hudNavSpeedDiv.innerHTML +=
"Fly-by Mode";
807 this.hudNavSpeedDiv.innerHTML +=
"Nav Speed: " + (Viewer3d.cameraKeyNavFast?
"Fast":
"Slow");
809 var str =
" " + ((Viewer3d.accumulateEvents && Viewer3d.eventToDisplay<0)?
"Events Accumulated":(
"Event: " +
810 (Viewer3d.eventToDisplay<0?
"All":Viewer3d.eventToDisplay)));
811 str +=
" " +
"Run: " + (Viewer3d.runNumber < 0?
"Not found":Viewer3d.runNumber);
812 this.hudNavSpeedDiv.innerHTML += str;
814 this.hudEventNumber.innerHTML =
"Event #: " + (Viewer3d.eventToDisplay<0?
"All":Viewer3d.eventToDisplay);
816 if(Viewer3d.accumulateEvents)
817 this.hudEventNumberControls.style.display =
"none";
819 this.hudEventNumberControls.style.display =
"block";
823 var animateDropDown =
function() {
824 var dir = (animationTargetTop - hudMouseOverDiv.offsetTop > 0)? 1: -1;
826 var tmpTop = hudMouseOverDiv.offsetTop + dir*Viewer3d.HUD_DROP_DOWN_SPEED;
827 if(Math.abs(tmpTop - animationTargetTop) <= Viewer3d.HUD_DROP_DOWN_SPEED)
829 hudMouseOverDiv.style.top = animationTargetTop +
"px";
830 isDropDownAnimating =
false;
834 hudMouseOverDiv.style.top = tmpTop +
"px";
835 window.setTimeout(animateDropDown,30);
839 var mouseOverDropDown = this.mouseOverDropDown =
function() {
840 if(isDropDownAnimating)
return;
844 isDropDownDown =
true;
845 isDropDownAnimating =
true;
846 animationTargetTop = -15;
847 window.setTimeout(animateDropDown,30);
852 var mouseOutDropDown = this.mouseOutDropDown =
function(event) {
853 if(isDropDownAnimating)
return;
857 var e =
event.toElement ||
event.relatedTarget;
860 if(e ==
this)
return;
867 isDropDownDown =
false;
868 isDropDownAnimating =
true;
869 animationTargetTop = 15 - hudMouseOverDiv.offsetHeight;
870 window.setTimeout(animateDropDown,30);
875 this.handleControlDown =
function(i,evt) {
879 if(controlMouseIsDown)
885 controlMouseIsDown =
true;
887 if(controlMouseIsDown)
889 if(controlMouseTimeout) window.clearTimeout(controlMouseTimeout);
890 controlMouseTimeout = window.setTimeout(
function() {Viewer3d.hud.handleControlDown(i)},to);
895 this.handleControlUp =
function(i) {
896 if(!controlMouseIsDown)
return;
900 if(controlMouseTimeout) window.clearTimeout(controlMouseTimeout);
901 controlMouseIsDown =
false;
906 var doControlAction =
function(i) {
914 Viewer3d.eventToDisplay += (i==0?-1:1)*parseInt(Viewer3d.events.length/5+1);
916 Viewer3d.eventToDisplay += (i==1?-1:1);
918 if(Viewer3d.eventToDisplay < 0)
919 Viewer3d.eventToDisplay = 0;
920 if(Viewer3d.eventToDisplay >= Viewer3d.events.length)
921 Viewer3d.eventToDisplay = Viewer3d.events.length - 1;
925 Viewer3d.hud.hudRunNumberInput.value = parseInt(Viewer3d.hud.hudRunNumberInput.value) + (i*2-9);
926 if(Viewer3d.hud.hudRunNumberInput.value < 0) Viewer3d.hud.hudRunNumberInput.value = 0;
927 if(getEventsTimeout) window.clearTimeout(getEventsTimeout);
928 getEventsTimeout = window.setTimeout(Viewer3d.getEvents,1000);
931 Debug.log(
"hud doControlAction unknown action" + i);
935 Viewer3d.hud.update();
939 this.handleRunChange =
function() {
941 var s = this.hudRunNumberInput.value;
942 for(var i=0;i<s.length;++i)
944 if(!(parseInt(s[i])+1))
946 s = s.substr(0,i) + s.substr(i+1);
951 if(s+
"" != this.hudRunNumberInput.value)
953 this.hudRunNumberInput.value = s;
954 Debug.log(
"hud handleRunChange " + s);
959 this.hudNavSpeedDiv = document.createElement(
'div');
960 this.hudNavSpeedDiv.setAttribute(
"id",
"Viewer3d-hudNavSpeedOverlay");
961 this.hudNavSpeedDiv.style.position =
"absolute";
962 this.hudNavSpeedDiv.style.zIndex = 100;
963 Viewer3d.omni.appendChild(this.hudNavSpeedDiv);
966 hudMouseOverDiv = this.hudMouseOverDiv = document.createElement(
'div');
967 hudMouseOverDiv.setAttribute(
"id",
"Viewer3d-hudMouseOver");
968 hudMouseOverDiv.style.position =
"absolute";
969 hudMouseOverDiv.style.zIndex = 100;
971 this.hudDiv = document.createElement(
'div');
972 this.hudDiv.setAttribute(
"id",
"Viewer3d-hud");
973 var chkLabels = [
"Show Axes",
"Show Grid",
"Show Tracks",
"Mouse Nav",
"Fly-By",
"Accumulated"];
974 var chkDefaults = [
"checked",
"checked",
"checked",
"checked",Viewer3d.enableFlyBy?
"checked":
"",Viewer3d.accumulateEvents?
"checked":
""];
976 for(var i=0;i<chkLabels.length;++i)
977 str +=
"<input type='checkbox' id='hudCheckbox" + i +
"' onchange='Viewer3d.hud.checkboxUpdate(" + i +
978 ");' " + chkDefaults[i] +
"><label for='hudCheckbox" + i +
"'>" + chkLabels[i] +
"</label><br>";
981 str +=
"<center><div id='Viewer3d-hudEventNumberControls'><div id='Viewer3d-hudEventNumber'></div>";
982 var evtNumBtns = [
"<<",
"<",
">",
">>"];
983 for(var i=0;i<evtNumBtns.length;++i)
984 str +=
"<input type='button' onmousedown='Viewer3d.hud.handleControlDown(" + i +
",event);' " +
985 "onmouseup='Viewer3d.hud.handleControlUp(" + i +
");' " +
986 "onmouseout='Viewer3d.hud.handleControlUp(" + i +
");' " +
987 "value='" + evtNumBtns[i] +
"' />";
988 str +=
"</div></center>";
991 if(DesktopContent._theDesktopWindow)
993 str +=
"<div id='Viewer3d-hudRunNumberControls'>Run #: " +
994 "<input id='Viewer3d-hudRunNumberControls-textInput' oninput='Viewer3d.hud.handleRunChange();' type='text' value='40' > ";
995 evtNumBtns = [
"<",
">"];
996 for(var i=0;i<evtNumBtns.length;++i)
997 str +=
"<input type='button' onmousedown='Viewer3d.hud.handleControlDown(" + (i+4) +
",event);' " +
998 "onmouseup='Viewer3d.hud.handleControlUp(" + (i+4) +
");' " +
999 "onmouseout='Viewer3d.hud.handleControlUp(" + (i+4) +
");' " +
1000 "value='" + evtNumBtns[i] +
"' />";
1005 str +=
"<input id='Viewer3d-hudRunNumberControls-textInput' type='hidden' value='40' >";
1007 this.hudUrlOverlay = document.createElement(
'div');
1008 this.hudUrlOverlay.setAttribute(
"id",
"Viewer3d-hudUrlOverlay");
1009 this.hudUrlOverlay.style.position =
"absolute";
1010 this.hudUrlOverlay.style.zIndex = 100;
1011 this.hudUrlOverlay.style.left = 5 +
"px";
1012 this.hudUrlOverlay.style.top = 5 +
"px";
1013 this.hudUrlOverlay.innerHTML =
"Try on your own mobile device!<br>http://tinyurl.com/q6lhdrm";
1014 Viewer3d.omni.appendChild(this.hudUrlOverlay);
1017 this.hudDiv.innerHTML = str;
1024 hudMouseOverDiv.appendChild(this.hudDiv);
1026 hudMouseOverDiv.style.top = hudMouseOverDiv.offsetHeight - 15 +
"px";
1027 hudMouseOverDiv.style.width = Viewer3d.HUD_WIDTH +
"px";
1028 hudMouseOverDiv.onmouseover = mouseOverDropDown;
1029 hudMouseOverDiv.onmouseout = mouseOutDropDown;
1030 Viewer3d.omni.appendChild(hudMouseOverDiv);
1032 this.hudEventNumber = document.getElementById(
'Viewer3d-hudEventNumber');
1033 this.hudEventNumberControls = document.getElementById(
'Viewer3d-hudEventNumberControls');
1034 this.hudRunNumberInput = document.getElementById(
'Viewer3d-hudRunNumberControls-textInput');
1037 isDropDownDown =
false;
1038 isDropDownAnimating =
true;
1039 animationTargetTop = 15 - hudMouseOverDiv.offsetHeight;
1040 window.setTimeout(animateDropDown,30);
1041 this.handleWindowResize();
1045 Viewer3d.getEvents =
function() {
1046 Debug.log(
"Viewer3d getEvents for run " + Viewer3d.hud.hudRunNumberInput.value);
1047 DesktopContent.XMLHttpRequest(
"Request?RequestType=getEvents&run=" +
1048 parseInt(Viewer3d.hud.hudRunNumberInput.value),
"", Viewer3d.getEventsHandler);
1052 Viewer3d.getEventsHandler =
function (req) {
1054 Viewer3d.runNumber = Viewer3d.hud.hudRunNumberInput.value;
1055 var events = req.responseXML.getElementsByTagName(
"event");
1057 Viewer3d.events = [];
1059 var gl = Viewer3d.gl;
1060 var locPoints, xyzPoints, slopes, intercepts;
1064 var trackSz = 1000000;
1065 for(var i=0;i<events.length;++i)
1067 xyzPoints = events[i].getElementsByTagName(
"xyz_point");
1070 evt = Viewer3d.events.length;
1071 Viewer3d.events[evt] = Viewer3d.events[evt] || {};
1072 Viewer3d.events[evt].clusters = [];
1076 for(var j=0;j<xyzPoints.length;++j)
1080 locPoints[locPoints.length] = xyzPoints[j].getAttribute(
"value")/Viewer3d.Z_SQUASH_FACTOR;
1082 for(var k=0;k<3;++k)
1084 locPoints[locPoints.length] = locPoints[locPoints.length-3*(k+1)] + (k==0?pixelSz:(k==1?0:pixelSz));
1085 locPoints[locPoints.length] = locPoints[locPoints.length-3*(k+1)] + (k==0?0:(k==1?pixelSz:pixelSz));
1086 locPoints[locPoints.length] = locPoints[locPoints.length-3*(k+1)];
1091 cnt = Viewer3d.events[evt].clusters.length;
1092 Viewer3d.events[evt].clusters[cnt] = gl.createBuffer();
1093 gl.bindBuffer(gl.ARRAY_BUFFER, Viewer3d.events[evt].clusters[cnt]);
1094 gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array(locPoints), gl.STATIC_DRAW);
1095 Viewer3d.events[evt].clusters[cnt].itemSize = 3;
1096 Viewer3d.events[evt].clusters[cnt].numItems = locPoints.length/3;
1100 locPoints[locPoints.length] = xyzPoints[j].getAttribute(
"value") - pixelSz/2;
1104 slopes = events[i].getElementsByTagName(
"slope");
1105 intercepts = events[i].getElementsByTagName(
"intcpt");
1107 for(var j=0;j<slopes.length;j+=2)
1109 locSlope[0] = parseInt(slopes[j].getAttribute(
"value"))/Viewer3d.Z_SQUASH_FACTOR;
1110 locSlope[1] = parseInt(slopes[j+1].getAttribute(
"value"))/Viewer3d.Z_SQUASH_FACTOR;
1111 locIntcpt[0] = parseInt(intercepts[j].getAttribute(
"value"));
1112 locIntcpt[1] = parseInt(intercepts[j+1].getAttribute(
"value"));
1113 locPoints[locPoints.length] = locSlope[0]*-trackSz + locIntcpt[0];
1114 locPoints[locPoints.length] = locSlope[1]*-trackSz + locIntcpt[1];
1115 locPoints[locPoints.length] = -trackSz;
1116 locPoints[locPoints.length] = locSlope[0]*trackSz + locIntcpt[0];
1117 locPoints[locPoints.length] = locSlope[1]*trackSz + locIntcpt[1];
1118 locPoints[locPoints.length] = trackSz;
1122 Viewer3d.events[evt].tracks = gl.createBuffer();
1123 gl.bindBuffer(gl.ARRAY_BUFFER, Viewer3d.events[evt].tracks);
1124 gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array(locPoints), gl.STATIC_DRAW);
1125 Viewer3d.events[evt].tracks.itemSize = 3;
1126 Viewer3d.events[evt].tracks.numItems = locPoints.length/3;
1129 Debug.log(
"Viewer3d getEventsHandler event count " + Viewer3d.events.length);
1130 Viewer3d.cameraAction =
true;
1133 Viewer3d.getGeometry =
function() {
1134 Debug.log(
"Viewer3d.getGeometry ");
1135 DesktopContent.XMLHttpRequest(
"Request?RequestType=getGeometry",
"", Viewer3d.getGeometryHandler);
1139 Viewer3d.getGeometryHandler =
function (req) {
1142 var objects = req.responseXML.getElementsByTagName(
"object");
1144 Viewer3d.geometry = [];
1146 var gl = Viewer3d.gl;
1147 var locPoints, xyzPoints, objectType;
1148 for(var i=0;i<objects.length;++i)
1154 xyzPoints = objects[i].getElementsByTagName(
"xyz_point");
1157 for(var j=0;j<xyzPoints.length;++j)
1158 locPoints[locPoints.length] = xyzPoints[j].getAttribute(
"value")/(j%3==2?Viewer3d.Z_SQUASH_FACTOR:1.0);
1161 gi = Viewer3d.geometry.length;
1162 Viewer3d.geometry[gi] = gl.createBuffer();
1163 gl.bindBuffer(gl.ARRAY_BUFFER, Viewer3d.geometry[gi]);
1164 gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array(locPoints), gl.STATIC_DRAW);
1165 Viewer3d.geometry[gi].itemSize = 3;
1166 Viewer3d.geometry[gi].numItems = locPoints.length/3;
1169 Debug.log(
"Viewer3d getGeometryHandler geometry objects " + Viewer3d.geometry.length);
1170 Viewer3d.canvas.style.zIndex = 0;
1172 Viewer3d.cameraAction =
true;
1175 Viewer3d.mvPushMatrix =
function() {
1176 copy = mat4.create();
1177 mat4.set(Viewer3d.modelViewMatrix, copy);
1178 Viewer3d.modelViewMatrixStack.push(copy);
1181 Viewer3d.mvPopMatrix =
function() {
1182 if (Viewer3d.modelViewMatrixStack.length == 0) {
1183 throw "Invalid popMatrix!";
1185 Viewer3d.modelViewMatrix = Viewer3d.modelViewMatrixStack.pop();
1191 Viewer3d.setMatrixUniforms =
function() {
1192 Viewer3d.gl.uniformMatrix4fv(Viewer3d.shaderProgram.pMatrixUniform,
false, Viewer3d.perspectiveMatrix);
1193 Viewer3d.gl.uniformMatrix4fv(Viewer3d.shaderProgram.mvMatrixUniform,
false, Viewer3d.modelViewMatrix);
1199 Viewer3d.redraw =
function () {
1201 if(Viewer3d.hud.isInMotion())
return;
1203 Viewer3d.initForDraw();
1204 if(Viewer3d.drawAxesFlag) Viewer3d.drawAxes();
1205 if(Viewer3d.drawGridFlag) Viewer3d.drawGrid();
1206 Viewer3d.drawGeometry();
1210 Viewer3d.drawClusters((Viewer3d.accumulateEvents && !Viewer3d.enableFlyBy)?-1:Viewer3d.eventToDisplay);
1211 if(Viewer3d.drawTracksFlag) Viewer3d.drawTracks((Viewer3d.accumulateEvents && !Viewer3d.enableFlyBy)?-1:Viewer3d.eventToDisplay);
1212 Viewer3d.hud.update();
1215 Viewer3d.initForDraw =
function() {
1217 var gl = Viewer3d.gl;
1218 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
1219 mat4.identity(Viewer3d.modelViewMatrix);
1222 var tmpLookAtMatrix = [];
1223 mat4.lookAt(Viewer3d.cameraPos, Viewer3d.cameraFocus, Viewer3d.cameraUp, tmpLookAtMatrix);
1224 mat4.multiply(Viewer3d.modelViewMatrix,tmpLookAtMatrix);
1225 Viewer3d.setMatrixUniforms();
1228 Viewer3d.initAxesAndGridBuffers =
function() {
1229 var gl = Viewer3d.gl;
1234 Viewer3d.WEBGL_AXES_LENGTH,0,0,
1236 0,Viewer3d.WEBGL_AXES_LENGTH,0,
1238 0,0,Viewer3d.WEBGL_AXES_LENGTH ]
1240 Viewer3d.axes = gl.createBuffer();
1241 gl.bindBuffer(gl.ARRAY_BUFFER, Viewer3d.axes);
1242 gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array(tmpArray), gl.STATIC_DRAW);
1243 Viewer3d.axes.itemSize = 3;
1244 Viewer3d.axes.numItems = tmpArray.length/3;
1249 for(var x = 0; x <= 2; x+=2)
1250 for(var i = -Viewer3d.WEBGL_GRID_EXPANSE; i <= Viewer3d.WEBGL_GRID_EXPANSE; i += Viewer3d.WEBGL_GRID_SPACING)
1251 for(var j = -Viewer3d.WEBGL_GRID_EXPANSE; j <= Viewer3d.WEBGL_GRID_EXPANSE; j += Viewer3d.WEBGL_GRID_EXPANSE*2, ++ti)
1253 tmpArray[ti*3 + x] = j;
1254 tmpArray[ti*3 + 2 - x] = i;
1255 tmpArray[ti*3 + 1] = Viewer3d.WEBGL_GRID_YOFF;
1258 Viewer3d.grid = gl.createBuffer();
1259 gl.bindBuffer(gl.ARRAY_BUFFER, Viewer3d.grid);
1260 gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array(tmpArray), gl.STATIC_DRAW);
1261 Viewer3d.grid.itemSize = 3;
1262 Viewer3d.grid.numItems = tmpArray.length/3;
1267 Viewer3d.drawAxes =
function() {
1268 var gl = Viewer3d.gl;
1270 var geometryColorBuffer = gl.createBuffer();
1271 gl.bindBuffer(gl.ARRAY_BUFFER, geometryColorBuffer);
1273 for (var i=0; i < Viewer3d.axes.numItems; i++)
1274 colors = colors.concat([1.0, 1.0, 1.0, 1.0]);
1275 gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array(colors), gl.STATIC_DRAW);
1276 gl.bindBuffer(gl.ARRAY_BUFFER, geometryColorBuffer);
1277 gl.vertexAttribPointer(Viewer3d.shaderProgram.vertexColorAttribute, 4, gl.FLOAT,
false, 0, 0);
1280 gl.bindBuffer(gl.ARRAY_BUFFER, Viewer3d.axes);
1281 gl.vertexAttribPointer(Viewer3d.shaderProgram.vertexPositionAttribute, Viewer3d.axes.itemSize, gl.FLOAT,
false, 0, 0);
1282 gl.drawArrays(gl.LINES, 0, Viewer3d.axes.numItems);
1285 Viewer3d.drawGrid =
function() {
1286 var gl = Viewer3d.gl;
1289 var geometryColorBuffer = gl.createBuffer();
1290 gl.bindBuffer(gl.ARRAY_BUFFER, geometryColorBuffer);
1292 for (var i=0; i < Viewer3d.grid.numItems; i++)
1293 colors = colors.concat([0.5, 0.5, 0.5, 1.0]);
1294 gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array(colors), gl.STATIC_DRAW);
1295 gl.bindBuffer(gl.ARRAY_BUFFER, geometryColorBuffer);
1296 gl.vertexAttribPointer(Viewer3d.shaderProgram.vertexColorAttribute, 4, gl.FLOAT,
false, 0, 0);
1299 gl.bindBuffer(gl.ARRAY_BUFFER, Viewer3d.grid);
1300 gl.vertexAttribPointer(Viewer3d.shaderProgram.vertexPositionAttribute, Viewer3d.grid.itemSize, gl.FLOAT,
false, 0, 0);
1301 gl.drawArrays(gl.LINES, 0, Viewer3d.grid.numItems);
1304 Viewer3d.drawGeometry =
function() {
1306 if(!Viewer3d.geometry)
return;
1308 var gl = Viewer3d.gl;
1311 var geometryColorBuffer = gl.createBuffer();
1312 gl.bindBuffer(gl.ARRAY_BUFFER, geometryColorBuffer);
1314 for (var i=0; i < 4; i++)
1315 colors = colors.concat([0.0, 0.0, 1.0, 1.0]);
1316 gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array(colors), gl.STATIC_DRAW);
1317 gl.bindBuffer(gl.ARRAY_BUFFER, geometryColorBuffer);
1318 gl.vertexAttribPointer(Viewer3d.shaderProgram.vertexColorAttribute, 4, gl.FLOAT,
false, 0, 0);
1320 for(var i=0;i<Viewer3d.geometry.length;++i)
1322 gl.bindBuffer(gl.ARRAY_BUFFER, Viewer3d.geometry[i]);
1323 gl.vertexAttribPointer(Viewer3d.shaderProgram.vertexPositionAttribute, Viewer3d.geometry[i].itemSize, gl.FLOAT,
false, 0, 0);
1325 gl.activeTexture(gl.TEXTURE0);
1326 gl.bindTexture(gl.TEXTURE_2D, Viewer3d.cTexture);
1329 gl.drawArrays(gl.LINE_LOOP, 0, Viewer3d.geometry[i].numItems);
1335 Viewer3d.drawClusters =
function(evt) {
1337 if(!Viewer3d.events)
return;
1339 var gl = Viewer3d.gl;
1342 var clusterColorBuffer = gl.createBuffer();
1343 gl.bindBuffer(gl.ARRAY_BUFFER, clusterColorBuffer);
1345 for (var i=0; i < 4; i++)
1346 colors = colors.concat([1.0, 0.0, 0.0, 1.0]);
1347 gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array(colors), gl.STATIC_DRAW);
1348 gl.bindBuffer(gl.ARRAY_BUFFER, clusterColorBuffer);
1349 gl.vertexAttribPointer(Viewer3d.shaderProgram.vertexColorAttribute, 4, gl.FLOAT,
false, 0, 0);
1351 var lo = evt < 0?0:evt;
1352 var hi = evt < 0?Viewer3d.events.length:(evt+1);
1353 for(var i=lo;i<hi && i<Viewer3d.events.length;++i)
1355 for(var c=0;c<Viewer3d.events[i].clusters.length;++c)
1357 gl.bindBuffer(gl.ARRAY_BUFFER, Viewer3d.events[i].clusters[c]);
1358 gl.vertexAttribPointer(Viewer3d.shaderProgram.vertexPositionAttribute,
1359 Viewer3d.events[i].clusters[c].itemSize, gl.FLOAT,
false, 0, 0);
1360 gl.drawArrays(gl.TRIANGLE_STRIP, 0, Viewer3d.events[i].clusters[c].numItems);
1366 Viewer3d.drawTracks =
function(evt) {
1368 if(!Viewer3d.events)
return;
1370 var gl = Viewer3d.gl;
1373 var clusterColorBuffer = gl.createBuffer();
1374 gl.bindBuffer(gl.ARRAY_BUFFER, clusterColorBuffer);
1376 for (var i=0; i < 4; i++)
1377 colors = colors.concat([1%2, 1.0, 0.0, 1.0]);
1378 gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array(colors), gl.STATIC_DRAW);
1379 gl.bindBuffer(gl.ARRAY_BUFFER, clusterColorBuffer);
1380 gl.vertexAttribPointer(Viewer3d.shaderProgram.vertexColorAttribute, 2, gl.FLOAT,
false, 0, 0);
1382 var lo = evt < 0?0:evt;
1383 var hi = evt < 0?Viewer3d.events.length:(evt+1);
1384 for(var i=lo;i<hi && i<Viewer3d.events.length;++i)
1386 gl.bindBuffer(gl.ARRAY_BUFFER, Viewer3d.events[i].tracks);
1387 gl.vertexAttribPointer(Viewer3d.shaderProgram.vertexPositionAttribute,
1388 Viewer3d.events[i].tracks.itemSize, gl.FLOAT,
false, 0, 0);
1389 gl.drawArrays(gl.LINES, 0, Viewer3d.events[i].tracks.numItems);
1393 var cubeVertexPositionBuffer;
1394 var cubeVertexNormalBuffer;
1395 var cubeVertexTextureCoordBuffer;
1396 var cubeVertexIndexBuffer;
1398 function initBuffers() {
1399 var gl = Viewer3d.gl;
1400 cubeVertexPositionBuffer = gl.createBuffer();
1401 gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);
1441 gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array(vertices), gl.STATIC_DRAW);
1442 cubeVertexPositionBuffer.itemSize = 3;
1443 cubeVertexPositionBuffer.numItems = 24;
1445 cubeVertexNormalBuffer = gl.createBuffer();
1446 gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexNormalBuffer);
1447 var vertexNormals = [
1484 gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array(vertexNormals), gl.STATIC_DRAW);
1485 cubeVertexNormalBuffer.itemSize = 3;
1486 cubeVertexNormalBuffer.numItems = 24;
1488 cubeVertexTextureCoordBuffer = gl.createBuffer();
1489 gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexTextureCoordBuffer);
1490 var textureCoords = [
1527 gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array(textureCoords), gl.STATIC_DRAW);
1528 cubeVertexTextureCoordBuffer.itemSize = 2;
1529 cubeVertexTextureCoordBuffer.numItems = 24;
1531 cubeVertexIndexBuffer = gl.createBuffer();
1532 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
1533 var cubeVertexIndices = [
1536 8, 9, 10, 8, 10, 11,
1537 12, 13, 14, 12, 14, 15,
1538 16, 17, 18, 16, 18, 19,
1539 20, 21, 22, 20, 22, 23
1541 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,
new Uint16Array(cubeVertexIndices), gl.STATIC_DRAW);
1542 cubeVertexIndexBuffer.itemSize = 1;
1543 cubeVertexIndexBuffer.numItems = 36;
1547 function drawScene() {
1548 var gl = Viewer3d.gl;
1551 gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);
1552 gl.vertexAttribPointer(Viewer3d.shaderProgram.vertexPositionAttribute, cubeVertexPositionBuffer.itemSize, gl.FLOAT,
false, 0, 0);
1554 gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexNormalBuffer);
1555 gl.vertexAttribPointer(Viewer3d.shaderProgram.vertexNormalAttribute, cubeVertexNormalBuffer.itemSize, gl.FLOAT,
false, 0, 0);
1557 gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexTextureCoordBuffer);
1558 gl.vertexAttribPointer(Viewer3d.shaderProgram.textureCoordAttribute, cubeVertexTextureCoordBuffer.itemSize, gl.FLOAT,
false, 0, 0);
1560 gl.activeTexture(gl.TEXTURE0);
1561 gl.bindTexture(gl.TEXTURE_2D, Viewer3d.cTexture);
1562 gl.uniform1i(Viewer3d.shaderProgram.samplerUniform, 0);
1563 var lighting =
true;
1564 gl.uniform1i(Viewer3d.shaderProgram.useLightingUniform, lighting);
1566 gl.uniform3f(Viewer3d.shaderProgram.ambientColorUniform, 0.2, 0.2, 0.2);
1568 var lightingDirection = [ -0.25, -0.25, -1 ];
1569 var adjustedLD = vec3.create();
1570 vec3.normalize(lightingDirection, adjustedLD);
1571 vec3.scale(adjustedLD, -1);
1572 gl.uniform3fv(Viewer3d.shaderProgram.lightingDirectionUniform, adjustedLD);
1573 gl.uniform3f(Viewer3d.shaderProgram.directionalColorUniform, 0.8, 0.8, 0.8);
1576 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
1577 Viewer3d.setMatrixUniforms();
1578 gl.drawElements(gl.TRIANGLES, cubeVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);