/*
* GL support. See https://github.com/kripken/emscripten/wiki/OpenGL-support
* for current status.
*/
var LibraryGL = {
$GL: {
counter: 1,
buffers: {},
programs: {},
framebuffers: {},
renderbuffers: {},
textures: {},
uniforms: {},
shaders: {},
// The folowing data structures are used for OpenGL Immediate Mode matrix routines.
matrix: {
'm': null, // modelview
'p': null // projection
},
matrixStack: {
'm': [], // modelview
'p': [] // projection
},
currentMatrix: 'm', // default is modelview
tempMatrix: null,
initMatrixLibrary: function() {
GL.matrix['m'] = GL.matrix.lib.mat4.create();
GL.matrix['p'] = GL.matrix.lib.mat4.create();
GL.tempMatrix = GL.matrix.lib.mat4.create();
},
// Linear lookup in one of the tables (buffers, programs, etc.). TODO: consider using a weakmap to make this faster, if it matters
scan: function(table, object) {
for (var item in table) {
if (table[item] == object) return item;
}
return 0;
},
// Find a token in a shader source string
findToken: function(source, token) {
function isIdentChar(ch) {
if (ch >= 48 && ch <= 57) // 0-9
return true;
if (ch >= 65 && ch <= 90) // A-Z
return true;
if (ch >= 97 && ch <= 122) // a-z
return true;
return false;
}
var i = -1;
do {
i = source.indexOf(token, i + 1);
if (i < 0) {
break;
}
if (i > 0 && isIdentChar(source[i - 1])) {
continue;
}
i += token.length;
if (i < source.length - 1 && isIdentChar(source[i + 1])) {
continue;
}
return true;
} while (true);
return false;
}
},
glGetString: function(name_) {
switch(name_) {
case 0x1F00 /* GL_VENDOR */:
case 0x1F01 /* GL_RENDERER */:
case 0x1F02 /* GL_VERSION */:
return allocate(intArrayFromString(Module.ctx.getParameter(name_)), 'i8', ALLOC_NORMAL);
case 0x1F03 /* GL_EXTENSIONS */:
return allocate(intArrayFromString(Module.ctx.getSupportedExtensions().join(' ')), 'i8', ALLOC_NORMAL);
default:
throw 'Failure: Invalid glGetString value: ' + name_;
}
},
glGetIntegerv: function(name_, p) {
var result = Module.ctx.getParameter(name_);
switch (typeof(result)) {
case "number":
{{{ makeSetValue('p', '0', 'result', 'i32') }}};
break;
case "boolean":
{{{ makeSetValue('p', '0', 'result ? 1 : 0', 'i8') }}};
break;
case "string":
throw 'Native code calling glGetIntegerv(' + name_ + ') on a name which returns a string!';
case "object":
if (result === null) {
{{{ makeSetValue('p', '0', '0', 'i32') }}};
} else if (result instanceof Float32Array ||
result instanceof Uint32Array ||
result instanceof Int32Array ||
result instanceof Arra