diff options
author | Ehsan Akhgari <ehsan.akhgari@gmail.com> | 2012-04-13 10:32:45 -0400 |
---|---|---|
committer | Ehsan Akhgari <ehsan.akhgari@gmail.com> | 2012-04-13 10:32:45 -0400 |
commit | 74cfb3b59c714d2e0ef91d0ad3fe02bc096faf56 (patch) | |
tree | 4e743664d4d139c35b4e656f267cb2a0d4799114 /src | |
parent | 55d9a3bc975cb4e9fefb79aeca6d5b9ef5fcea1f (diff) | |
parent | ffae4e81b6eec71922b662eea9a75f5693994cb2 (diff) |
Merge remote-tracking branch 'upstream/incoming' into glmatrix
Conflicts:
src/library_gl.js
Diffstat (limited to 'src')
-rw-r--r-- | src/library_browser.js | 14 | ||||
-rw-r--r-- | src/library_gl.js | 166 | ||||
-rw-r--r-- | src/library_sdl.js | 1 |
3 files changed, 161 insertions, 20 deletions
diff --git a/src/library_browser.js b/src/library_browser.js index 17dff2eb..300f0b3b 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -47,13 +47,16 @@ mergeInto(LibraryManager.library, { case 'function': { wrapper[prop] = function() { var printArgs = Array.prototype.slice.call(arguments).map(function(arg) { + if (!arg) return arg; if (wrapper.objectMap[arg]) return '<' + arg + '|' + wrapper.objectMap[arg] + '>'; if (arg.toString() == '[object HTMLImageElement]') { return arg + '\n\n'; } if (arg.byteLength) { + return '{' + Array.prototype.slice.call(arg, 0, Math.min(arg.length, 40)) + '}'; // Useful for correct arrays, less so for compiled arrays, see the code below for that var buf = new ArrayBuffer(32); var i8buf = new Int8Array(buf); + var i16buf = new Int16Array(buf); var f32buf = new Float32Array(buf); switch(arg.toString()) { case '[object Uint8Array]': @@ -62,6 +65,9 @@ mergeInto(LibraryManager.library, { case '[object Float32Array]': f32buf.set(arg.subarray(0, 5)); break; + case '[object Uint16Array]': + i16buf.set(arg.subarray(0, 16)); + break; default: alert('unknown array for debugging: ' + arg); throw 'see alert'; @@ -75,25 +81,25 @@ mergeInto(LibraryManager.library, { } return arg; }); - Module.printErr('[gl_f:' + prop + ':' + printArgs + ']'); + console.log('[gl_f:' + prop + ':' + printArgs + ']'); var ret = tempCtx[prop].apply(tempCtx, arguments); var printRet = ret; if (typeof ret == 'object') { wrapper.objectMap[ret] = wrapper.objectCounter++; printRet = '<' + ret + '|' + wrapper.objectMap[ret] + '>'; } - Module.printErr('[ gl:' + prop + ':return:' + printRet + ']'); + if (typeof printRet != 'undefined') console.log('[ gl:' + prop + ':return:' + printRet + ']'); return ret; } break; } case 'number': case 'string': { wrapper.__defineGetter__(prop, function() { - //Module.printErr('[gl_g:' + prop + ':' + tempCtx[prop] + ']'); + //console.log('[gl_g:' + prop + ':' + tempCtx[prop] + ']'); return tempCtx[prop]; }); wrapper.__defineSetter__(prop, function(value) { - Module.printErr('[gl_s:' + prop + ':' + value + ']'); + console.log('[gl_s:' + prop + ':' + value + ']'); tempCtx[prop] = value; }); break; diff --git a/src/library_gl.js b/src/library_gl.js index d7bf31ea..f5235a3f 100644 --- a/src/library_gl.js +++ b/src/library_gl.js @@ -316,6 +316,10 @@ var LibraryGL = { Module.ctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels); }, + glReadPixels: function(x, y, width, height, format, type, pixels) { + Module.ctx.readPixels(x, y, width, height, format, type, HEAPU8.subarray(pixels)); + }, + glBindTexture: function(target, texture) { Module.ctx.bindTexture(target, GL.textures[texture]); }, @@ -963,6 +967,11 @@ var LibraryGL = { return 1 /* GL_TRUE */; }, + glEnable: function(cap) { + if (cap == 0x0DE1) return; // no GL_TEXTURE_2D in GLES or WebGL + Module.ctx.enable(cap); + }, + // GL emulation: provides misc. functionality not present in OpenGL ES 2.0 or WebGL $GLEmulation__deps: ['glCreateShader', 'glShaderSource', 'glCompileShader', 'glCreateProgram', 'glDeleteShader', 'glDeleteProgram', 'glAttachShader', 'glActiveTexture', 'glGetShaderiv', 'glGetProgramiv', 'glLinkProgram'], @@ -1017,20 +1026,140 @@ var LibraryGL = { } }, - glBegin__deps: ['$GL', function() { return 'GL.matrix.lib = ' + read('gl-matrix.js') + ';\n' + - 'GL.initMatrixLibrary();' - }], - glBegin: function() { - Module.print('TODO'); - } + // Immediate mode + + $GLImmediate: { + maxElements: 1024, + vertexData: null, + indexData: null, + vertexCounter: 0, + indexCounter: 0, + mode: 0, + + initted: false, + init: function() { + this.vertexShader = Module.ctx.createShader(Module.ctx.VERTEX_SHADER); + Module.ctx.shaderSource(this.vertexShader, 'attribute vec4 a_position; \n\ + attribute vec2 a_texCoord; \n\ + varying vec2 v_texCoord; \n\ + void main() \n\ + { \n\ + gl_Position = a_position; \n\ + v_texCoord = a_texCoord; \n\ + } \n'); + Module.ctx.compileShader(this.vertexShader); + + this.fragmentShader = Module.ctx.createShader(Module.ctx.FRAGMENT_SHADER); + Module.ctx.shaderSource(this.fragmentShader, 'precision mediump float; \n\ + varying vec2 v_texCoord; \n\ + uniform sampler2D s_texture; \n\ + void main() \n\ + { \n\ + gl_FragColor = texture2D( s_texture, v_texCoord );\n\ + } \n'); + Module.ctx.compileShader(this.fragmentShader); + + this.program = Module.ctx.createProgram(); + Module.ctx.attachShader(this.program, this.vertexShader); + Module.ctx.attachShader(this.program, this.fragmentShader); + Module.ctx.linkProgram(this.program); + + this.positionLocation = Module.ctx.getAttribLocation(this.program, 'a_position'); + this.texCoordLocation = Module.ctx.getAttribLocation(this.program, 'a_texCoord'); + this.textureLocation = Module.ctx.getUniformLocation(this.program, 's_texture'); + + // Buffer for data + this.vertexData = new Float32Array(5 * this.maxElements); + this.indexData = new Uint16Array(this.maxElements); + + this.vertexObject = Module.ctx.createBuffer(); + this.indexObject = Module.ctx.createBuffer(); + }, + flush: function() { + // Upload the data + Module.ctx.bindBuffer(Module.ctx.ARRAY_BUFFER, this.vertexObject); + Module.ctx.bufferData(Module.ctx.ARRAY_BUFFER, this.vertexData.subarray(0, 5*this.vertexCounter), Module.ctx.STATIC_DRAW); + Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, this.indexObject); + Module.ctx.bufferData(Module.ctx.ELEMENT_ARRAY_BUFFER, this.indexData.subarray(0, this.indexCounter), Module.ctx.STATIC_DRAW); + + // Render + //Module.ctx.viewport(0, 0, Module.canvas.width, Module.canvas.height); + //Module.ctx.clear(Module.ctx.COLOR_BUFFER_BIT); + + Module.ctx.useProgram(this.program); + + Module.ctx.bindBuffer(Module.ctx.ARRAY_BUFFER, this.vertexObject); + Module.ctx.vertexAttribPointer(this.positionLocation, 3, Module.ctx.FLOAT, + false, 5 * 4, 0); + Module.ctx.vertexAttribPointer(this.texCoordLocation, 2, Module.ctx.FLOAT, + false, 5 * 4, + 3 * 4); + + Module.ctx.enableVertexAttribArray(this.positionLoc); + Module.ctx.enableVertexAttribArray(this.texCoordLoc); + + Module.ctx.activeTexture(Module.ctx.TEXTURE0); + Module.ctx.bindTexture(Module.ctx.TEXTURE_2D, this.textureId); + + Module.ctx.uniform1i(this.textureLocation, 0); + + Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, this.indexObject); + Module.ctx.drawElements(Module.ctx.TRIANGLES, this.indexCounter, Module.ctx.UNSIGNED_SHORT, 0); + + this.vertexCounter = this.indexCounter = 0; + } + }, + + glBegin__deps: ['$GL', '$GLImmediate', function() { return 'GL.matrix.lib = ' + read('gl-matrix.js') + + ';\nGL.immediate = GLImmediate;\nGL.initMatrixLibrary();\n' }], + glBegin: function(mode) { + if (!GL.immediate.initted) GL.immediate.init(); + GL.immediate.mode = mode; + }, + + glEnd: function() { + GL.immediate.flush(); + }, + + glVertex3f: function(x, y, z) { + GL.immediate.vertexData[5*GL.immediate.vertexCounter ] = x; + GL.immediate.vertexData[5*GL.immediate.vertexCounter+1] = y; + GL.immediate.vertexData[5*GL.immediate.vertexCounter+2] = z; + GL.immediate.vertexCounter++; +#if ASSERTIONS + assert(GL.immediate.vertexCounter < GL.immediate.maxElements, 'too many immediate mode vertexes'); +#endif + if (GL.immediate.mode == 7) { // GL_QUADS + if (GL.immediate.vertexCounter % 4 == 0) { + var start = GL.immediate.vertexCounter % 4; + GL.immediate.indexData[GL.immediate.indexCounter ] = start; + GL.immediate.indexData[GL.immediate.indexCounter+1] = start+1; + GL.immediate.indexData[GL.immediate.indexCounter+2] = start+2; + GL.immediate.indexData[GL.immediate.indexCounter+3] = start; + GL.immediate.indexData[GL.immediate.indexCounter+4] = start+2; + GL.immediate.indexData[GL.immediate.indexCounter+5] = start+3; + GL.immediate.indexCounter += 6; + } + } else { + throw 'only GL_QUADS supported so far'; + } +#if ASSERTIONS + assert(GL.immediate.indexCounter < GL.immediate.maxElements, 'too many immediate mode indexes'); +#endif + }, + + glTexCoord2i: function(u, v) { + GL.immediate.vertexData[5*GL.immediate.vertexCounter+3] = u; + GL.immediate.vertexData[5*GL.immediate.vertexCounter+4] = v; + }, }; -// Simple pass-through functions. Starred ones have return values. +// Simple pass-through functions. Starred ones have return values. [X] ones have X in the C name but not in the JS name [[0, 'shadeModel fogi fogfv getError finish flush'], - [1, 'clearDepth depthFunc enable disable frontFace cullFace clear enableVertexAttribArray disableVertexAttribArray lineWidth clearStencil depthMask stencilMask stencilMaskSeparate checkFramebufferStatus* generateMipmap activeTexture blendEquation'], - [2, 'pixelStorei blendFunc blendEquationSeparate'], - [3, 'texParameteri texParameterf drawArrays vertexAttrib2f'], - [4, 'viewport clearColor scissor vertexAttrib3f colorMask drawElements renderbufferStorage blendFuncSeparate'], + [1, 'clearDepth clearDepth[f] depthFunc disable frontFace cullFace clear enableVertexAttribArray disableVertexAttribArray lineWidth clearStencil depthMask stencilMask stencilMaskSeparate checkFramebufferStatus* generateMipmap activeTexture blendEquation polygonOffset hint sampleCoverage'], + [2, 'pixelStorei blendFunc blendEquationSeparate depthRange depthRange[f]'], + [3, 'texParameteri texParameterf drawArrays vertexAttrib2f stencilFunc stencilOp'], + [4, 'viewport clearColor scissor vertexAttrib3f colorMask drawElements renderbufferStorage blendFuncSeparate blendColor'], [5, 'vertexAttrib4f'], [6, 'vertexAttribPointer'], [8, 'copyTexImage2D copyTexSubImage2D']].forEach(function(data) { @@ -1039,15 +1168,20 @@ var LibraryGL = { var args = range(num).map(function(i) { return 'x' + i }).join(', '); var plainStub = '(function(' + args + ') { ' + (num > 0 ? 'Module.ctx.NAME(' + args + ')' : '') + ' })'; var returnStub = '(function(' + args + ') { ' + (num > 0 ? 'return Module.ctx.NAME(' + args + ')' : '') + ' })'; - names.split(' ').forEach(function(name_) { + names.split(' ').forEach(function(name) { var stub = plainStub; - if (name_[name_.length-1] == '*') { - name_ = name_.substr(0, name_.length-1); + if (name[name.length-1] == '*') { + name = name.substr(0, name.length-1); stub = returnStub; } - var cName = 'gl' + name_[0].toUpperCase() + name_.substr(1); + var cName = name; + if (name.indexOf('[') >= 0) { + cName = name.replace('[', '').replace(']', ''); + name = cName.substr(0, cName.length-1); + } + var cName = 'gl' + cName[0].toUpperCase() + cName.substr(1); assert(!(cName in LibraryGL), "Cannot reimplement the existing function " + cName); - LibraryGL[cName] = eval(stub.replace('NAME', name_)); + LibraryGL[cName] = eval(stub.replace('NAME', name)); }); }); diff --git a/src/library_sdl.js b/src/library_sdl.js index b607bdb3..11431e4e 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -702,6 +702,7 @@ mergeInto(LibraryManager.library, { surfData.ctx.drawImage(raw, 0, 0, raw.width, raw.height, 0, 0, raw.width, raw.height); return surf; }, + SDL_LoadBMP: 'IMG_Load', // SDL_Audio |