var LibraryGLUT = {
$GLUT__deps: ['$Browser', '$getFullscreenElement', 'glutPostRedisplay'],
$GLUT: {
initTime: null,
idleFunc: null,
displayFunc: null,
keyboardFunc: null,
keyboardUpFunc: null,
specialFunc: null,
specialUpFunc: null,
reshapeFunc: null,
motionFunc: null,
passiveMotionFunc: null,
mouseFunc: null,
buttons: 0,
modifiers: 0,
initWindowWidth: 256,
initWindowHeight: 256,
initDisplayMode: 0x0000 | 0x0002 | 0x0010 ,
windowX: 0,
windowY: 0,
windowWidth: 0,
windowHeight: 0,
requestedAnimationFrame: false,
saveModifiers: (event) => {
GLUT.modifiers = 0;
if (event['shiftKey'])
GLUT.modifiers += 1;
if (event['ctrlKey'])
GLUT.modifiers += 2;
if (event['altKey'])
GLUT.modifiers += 4;
},
onMousemove: (event) => {
var lastX = Browser.mouseX;
var lastY = Browser.mouseY;
Browser.calculateMouseEvent(event);
var newX = Browser.mouseX;
var newY = Browser.mouseY;
if (newX == lastX && newY == lastY) return;
if (GLUT.buttons == 0 && event.target == Browser.getCanvas() && GLUT.passiveMotionFunc) {
event.preventDefault();
GLUT.saveModifiers(event);
{{{ makeDynCall('vii', 'GLUT.passiveMotionFunc') }}}(lastX, lastY);
} else if (GLUT.buttons != 0 && GLUT.motionFunc) {
event.preventDefault();
GLUT.saveModifiers(event);
{{{ makeDynCall('vii', 'GLUT.motionFunc') }}}(lastX, lastY);
}
},
getSpecialKey: (keycode) => {
var key = null;
switch (keycode) {
case 8: key = 120 ; break;
case 46: key = 111 ; break;
case 0x70 : key = 1 ; break;
case 0x71 : key = 2 ; break;
case 0x72 : key = 3 ; break;
case 0x73 : key = 4 ; break;
case 0x74 : key = 5 ; break;
case 0x75 : key = 6 ; break;
case 0x76 : key = 7 ; break;
case 0x77 : key = 8 ; break;
case 0x78 : key = 9 ; break;
case 0x79 : key = 10 ; break;
case 0x7a : key = 11 ; break;
case 0x7b : key = 12 ; break;
case 0x25 : key = 100 ; break;
case 0x26 : key = 101 ; break;
case 0x27 : key = 102 ; break;
case 0x28 : key = 103 ; break;
case 0x21 : key = 104 ; break;
case 0x22 : key = 105 ; break;
case 0x24 : key = 106 ; break;
case 0x23 : key = 107 ; break;
case 0x2d : key = 108 ; break;
case 16 :
case 0x05 :
key = 112 ;
break;
case 0x06 :
key = 113 ;
break;
case 17 :
case 0x03 :
key = 114 ;
break;
case 0x04 :
key = 115 ;
break;
case 18 :
case 0x02 :
key = 116 ;
break;
case 0x01 :
key = 117 ;
break;
};
return key;
},
getASCIIKey: (event) => {
if (event['ctrlKey'] || event['altKey'] || event['metaKey']) return null;
var keycode = event['keyCode'];
if (48 <= keycode && keycode <= 57)
return keycode;
if (65 <= keycode && keycode <= 90)
return event['shiftKey'] ? keycode : keycode + 32;
if (96 <= keycode && keycode <= 105)
return keycode - 48;
if (106 <= keycode && keycode <= 111)
return keycode - 106 + 42;
switch (keycode) {
case 9:
case 13:
case 27:
case 32:
case 61:
return keycode;
}
var s = event['shiftKey'];
switch (keycode) {
case 186: return s ? 58 : 59;
case 187: return s ? 43 : 61;
case 188: return s ? 60 : 44;
case 189: return s ? 95 : 45;
case 190: return s ? 62 : 46;
case 191: return s ? 63 : 47;
case 219: return s ? 123 : 91;
case 220: return s ? 124 : 47;
case 221: return s ? 125 : 93;
case 222: return s ? 34 : 39;
}
return null;
},
onKeydown: (event) => {
if (GLUT.specialFunc || GLUT.keyboardFunc) {
var key = GLUT.getSpecialKey(event['keyCode']);
if (key !== null) {
if (GLUT.specialFunc) {
event.preventDefault();
GLUT.saveModifiers(event);
{{{ makeDynCall('viii', 'GLUT.specialFunc') }}}(key, Browser.mouseX, Browser.mouseY);
}
} else {
key = GLUT.getASCIIKey(event);
if (key !== null && GLUT.keyboardFunc) {
event.preventDefault();
GLUT.saveModifiers(event);
{{{ makeDynCall('viii', 'GLUT.keyboardFunc') }}}(key, Browser.mouseX, Browser.mouseY);
}
}
}
},
onKeyup: (event) => {
if (GLUT.specialUpFunc || GLUT.keyboardUpFunc) {
var key = GLUT.getSpecialKey(event['keyCode']);
if (key !== null) {
if (GLUT.specialUpFunc) {
event.preventDefault ();
GLUT.saveModifiers(event);
{{{ makeDynCall('viii', 'GLUT.specialUpFunc') }}}(key, Browser.mouseX, Browser.mouseY);
}
} else {
key = GLUT.getASCIIKey(event);
if (key !== null && GLUT.keyboardUpFunc) {
event.preventDefault ();
GLUT.saveModifiers(event);
{{{ makeDynCall('viii', 'GLUT.keyboardUpFunc') }}}(key, Browser.mouseX, Browser.mouseY);
}
}
}
},
touchHandler: (event) => {
if (event.target != Browser.getCanvas()) {
return;
}
var touches = event.changedTouches,
main = touches[0],
type = "";
switch (event.type) {
case "touchstart": type = "mousedown"; break;
case "touchmove": type = "mousemove"; break;
case "touchend": type = "mouseup"; break;
default: return;
}
var simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent(type, true, true, window, 1,
main.screenX, main.screenY,
main.clientX, main.clientY, false,
false, false, false, 0, null);
main.target.dispatchEvent(simulatedEvent);
event.preventDefault();
},
onMouseButtonDown: (event) => {
Browser.calculateMouseEvent(event);
GLUT.buttons |= (1 << event['button']);
if (event.target == Browser.getCanvas() && GLUT.mouseFunc) {
try {
event.target.setCapture();
} catch (e) {}
event.preventDefault();
GLUT.saveModifiers(event);
{{{ makeDynCall('viiii', 'GLUT.mouseFunc') }}}(event['button'], 0, Browser.mouseX, Browser.mouseY);
}
},
onMouseButtonUp: (event) => {
Browser.calculateMouseEvent(event);
GLUT.buttons &= ~(1 << event['button']);
if (GLUT.mouseFunc) {
event.preventDefault();
GLUT.saveModifiers(event);
{{{ makeDynCall('viiii', 'GLUT.mouseFunc') }}}(event['button'], 1, Browser.mouseX, Browser.mouseY);
}
},
onMouseWheel: (event) => {
Browser.calculateMouseEvent(event);
var e = window.event || event;
var delta = -Browser.getMouseWheelDelta(event);
delta = (delta == 0) ? 0 : (delta > 0 ? Math.max(delta, 1) : Math.min(delta, -1));
var button = 3;
if (delta < 0) {
button = 4;
}
if (GLUT.mouseFunc) {
event.preventDefault();
GLUT.saveModifiers(event);
{{{ makeDynCall('viiii', 'GLUT.mouseFunc') }}}(button, 0, Browser.mouseX, Browser.mouseY);
}
},
onFullscreenEventChange: (event) => {
var width;
var height;
if (getFullscreenElement()) {
width = screen["width"];
height = screen["height"];
} else {
width = GLUT.windowWidth;
height = GLUT.windowHeight;
document.removeEventListener('fullscreenchange', GLUT.onFullscreenEventChange, true);
document.removeEventListener('mozfullscreenchange', GLUT.onFullscreenEventChange, true);
document.removeEventListener('webkitfullscreenchange', GLUT.onFullscreenEventChange, true);
}
Browser.setCanvasSize(width, height, true);
if (GLUT.reshapeFunc) {
{{{ makeDynCall('vii', 'GLUT.reshapeFunc') }}}(width, height);
}
_glutPostRedisplay();
},
onResize: () => {
var canvas = Browser.getCanvas();
Browser.setCanvasSize(canvas.clientWidth, canvas.clientHeight, false);
}
},
glutGetModifiers__proxy: 'sync',
glutGetModifiers: () => GLUT.modifiers,
glutInit__deps: ['$Browser', '$addOnExit'],
glutInit__proxy: 'sync',
glutInit: (argcp, argv) => {
GLUT.initTime = Date.now();
var isTouchDevice = 'ontouchstart' in document.documentElement;
if (isTouchDevice) {
window.addEventListener('touchmove', GLUT.touchHandler, true);
window.addEventListener('touchstart', GLUT.touchHandler, true);
window.addEventListener('touchend', GLUT.touchHandler, true);
}
window.addEventListener('keydown', GLUT.onKeydown, true);
window.addEventListener('keyup', GLUT.onKeyup, true);
window.addEventListener('mousemove', GLUT.onMousemove, true);
window.addEventListener('mousedown', GLUT.onMouseButtonDown, true);
window.addEventListener('mouseup', GLUT.onMouseButtonUp, true);
window.addEventListener('mousewheel', GLUT.onMouseWheel, true);
window.addEventListener('DOMMouseScroll', GLUT.onMouseWheel, true);
window.addEventListener('resize', GLUT.onResize, true);
Browser.resizeListeners.push((width, height) => {
if (GLUT.reshapeFunc) {
{{{ makeDynCall('vii', 'GLUT.reshapeFunc') }}}(width, height);
}
});
addOnExit(() => {
if (isTouchDevice) {
window.removeEventListener('touchmove', GLUT.touchHandler, true);
window.removeEventListener('touchstart', GLUT.touchHandler, true);
window.removeEventListener('touchend', GLUT.touchHandler, true);
}
window.removeEventListener('keydown', GLUT.onKeydown, true);
window.removeEventListener('keyup', GLUT.onKeyup, true);
window.removeEventListener('mousemove', GLUT.onMousemove, true);
window.removeEventListener('mousedown', GLUT.onMouseButtonDown, true);
window.removeEventListener('mouseup', GLUT.onMouseButtonUp, true);
window.removeEventListener('mousewheel', GLUT.onMouseWheel, true);
window.removeEventListener('DOMMouseScroll', GLUT.onMouseWheel, true);
window.removeEventListener('resize', GLUT.onResize, true);
var canvas = Browser.getCanvas();
canvas.width = canvas.height = 1;
});
},
glutInitWindowSize__proxy: 'sync',
glutInitWindowSize: (width, height) => {
Browser.setCanvasSize( GLUT.initWindowWidth = width,
GLUT.initWindowHeight = height );
},
glutInitWindowPosition__proxy: 'sync',
glutInitWindowPosition: (x, y) => {},
glutGet: (type) => {
switch (type) {
case 100:
return 0;
case 101:
return 0;
case 102:
return Browser.getCanvas().width;
case 103:
return Browser.getCanvas().height;
case 200:
return Browser.getCanvas().width;
case 201:
return Browser.getCanvas().height;
case 500:
return 0;
case 501:
return 0;
case 502:
return GLUT.initWindowWidth;
case 503:
return GLUT.initWindowHeight;
case 700:
var now = Date.now();
return now - GLUT.initTime;
case 0x0069:
return GLctx.getContextAttributes().stencil ? 8 : 0;
case 0x006A:
return GLctx.getContextAttributes().depth ? 8 : 0;
case 0x006E:
return GLctx.getContextAttributes().alpha ? 8 : 0;
case 0x0078:
return GLctx.getContextAttributes().antialias ? 1 : 0;
default:
throw "glutGet(" + type + ") not implemented yet";
}
},
glutIdleFunc__proxy: 'sync',
glutIdleFunc__deps: ['$safeSetTimeout'],
glutIdleFunc: (func) => {
function callback() {
if (GLUT.idleFunc) {
{{{ makeDynCall('v', 'GLUT.idleFunc') }}}();
safeSetTimeout(callback, 4);
}
}
if (!GLUT.idleFunc) {
safeSetTimeout(callback, 0);
}
GLUT.idleFunc = func;
},
glutTimerFunc__proxy: 'sync',
glutTimerFunc__deps: ['$safeSetTimeout'],
glutTimerFunc: (msec, func, value) =>
safeSetTimeout(() => {{{ makeDynCall('vi', 'func') }}}(value), msec),
glutDisplayFunc__proxy: 'sync',
glutDisplayFunc: (func) => {
GLUT.displayFunc = func;
},
glutKeyboardFunc__proxy: 'sync',
glutKeyboardFunc: (func) => {
GLUT.keyboardFunc = func;
},
glutKeyboardUpFunc__proxy: 'sync',
glutKeyboardUpFunc: (func) => {
GLUT.keyboardUpFunc = func;
},
glutSpecialFunc__proxy: 'sync',
glutSpecialFunc: (func) => {
GLUT.specialFunc = func;
},
glutSpecialUpFunc__proxy: 'sync',
glutSpecialUpFunc: (func) => {
GLUT.specialUpFunc = func;
},
glutReshapeFunc__proxy: 'sync',
glutReshapeFunc: (func) => {
GLUT.reshapeFunc = func;
},
glutMotionFunc__proxy: 'sync',
glutMotionFunc: (func) => {
GLUT.motionFunc = func;
},
glutPassiveMotionFunc__proxy: 'sync',
glutPassiveMotionFunc: (func) => {
GLUT.passiveMotionFunc = func;
},
glutMouseFunc__proxy: 'sync',
glutMouseFunc: (func) => {
GLUT.mouseFunc = func;
},
glutSetCursor__proxy: 'sync',
glutSetCursor: (cursor) => {
var cursorStyle = 'auto';
switch (cursor) {
case 0x0000:
break;
case 0x0001:
break;
case 0x0002:
cursorStyle = 'pointer';
break;
case 0x0003:
break;
case 0x0004:
cursorStyle = 'help';
break;
case 0x0005:
break;
case 0x0006:
break;
case 0x0007:
cursorStyle = 'wait';
break;
case 0x0008:
cursorStyle = 'text';
break;
case 0x0009:
case 0x0066:
cursorStyle = 'crosshair';
break;
case 0x000A:
cursorStyle = 'ns-resize';
break;
case 0x000B:
cursorStyle = 'ew-resize';
break;
case 0x000C:
cursorStyle = 'n-resize';
break;
case 0x000D:
cursorStyle = 's-resize';
break;
case 0x000E:
cursorStyle = 'w-resize';
break;
case 0x000F:
cursorStyle = 'e-resize';
break;
case 0x0010:
cursorStyle = 'nw-resize';
break;
case 0x0011:
cursorStyle = 'ne-resize';
break;
case 0x0012:
cursorStyle = 'se-resize';
break;
case 0x0013:
cursorStyle = 'sw-resize';
break;
case 0x0064:
break;
case 0x0065:
cursorStyle = 'none';
break;
default:
throw "glutSetCursor: Unknown cursor type: " + cursor;
}
Browser.getCanvas().style.cursor = cursorStyle;
},
glutCreateWindow__proxy: 'sync',
glutCreateWindow__deps: ['$Browser'],
glutCreateWindow: (name) => {
var contextAttributes = {
antialias: ((GLUT.initDisplayMode & 0x0080 ) != 0),
depth: ((GLUT.initDisplayMode & 0x0010 ) != 0),
stencil: ((GLUT.initDisplayMode & 0x0020 ) != 0),
alpha: ((GLUT.initDisplayMode & 0x0008 ) != 0)
};
#if OFFSCREEN_FRAMEBUFFER
GL.enableOffscreenFramebufferAttributes(contextAttributes);
#endif
if (!Browser.createContext(Browser.getCanvas(), true, true, contextAttributes)) {
return 0;
}
return 1;
},
glutDestroyWindow__proxy: 'sync',
glutDestroyWindow__deps: ['$Browser'],
glutDestroyWindow: (name) => {
delete Module['ctx'];
return 1;
},
glutReshapeWindow__proxy: 'sync',
glutReshapeWindow__deps: ['$GLUT', 'glutPostRedisplay'],
glutReshapeWindow: (width, height) => {
Browser.exitFullscreen();
Browser.setCanvasSize(width, height, true);
if (GLUT.reshapeFunc) {
{{{ makeDynCall('vii', 'GLUT.reshapeFunc') }}}(width, height);
}
_glutPostRedisplay();
},
glutPositionWindow__proxy: 'sync',
glutPositionWindow__deps: ['$GLUT', 'glutPostRedisplay'],
glutPositionWindow: (x, y) => {
Browser.exitFullscreen();
_glutPostRedisplay();
},
glutFullScreen__proxy: 'sync',
glutFullScreen__deps: ['$GLUT'],
glutFullScreen: () => {
GLUT.windowX = 0;
GLUT.windowY = 0;
var canvas = Browser.getCanvas();
GLUT.windowWidth = canvas.width;
GLUT.windowHeight = canvas.height;
document.addEventListener('fullscreenchange', GLUT.onFullscreenEventChange, true);
document.addEventListener('mozfullscreenchange', GLUT.onFullscreenEventChange, true);
document.addEventListener('webkitfullscreenchange', GLUT.onFullscreenEventChange, true);
Browser.requestFullscreen(false, false);
},
glutInitDisplayMode__proxy: 'sync',
glutInitDisplayMode: (mode) => GLUT.initDisplayMode = mode,
glutSwapBuffers__proxy: 'sync',
glutSwapBuffers: () => {},
glutPostRedisplay__proxy: 'sync',
glutPostRedisplay__deps: ['$MainLoop'],
glutPostRedisplay: () => {
if (GLUT.displayFunc && !GLUT.requestedAnimationFrame) {
GLUT.requestedAnimationFrame = true;
MainLoop.requestAnimationFrame(() => {
GLUT.requestedAnimationFrame = false;
MainLoop.runIter(() => {{{ makeDynCall('v', 'GLUT.displayFunc') }}}());
});
}
},
glutMainLoop__proxy: 'sync',
glutMainLoop__deps: ['$GLUT', 'glutPostRedisplay'],
glutMainLoop: () => {
GLUT.onResize();
_glutPostRedisplay();
throw 'unwind';
},
};
autoAddDeps(LibraryGLUT, '$GLUT');
addToLibrary(LibraryGLUT);