diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-06-14 17:49:43 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-06-14 17:49:43 -0700 |
commit | 76cdd45d6ade4bd8fbe5969e91923b0a3c2e3497 (patch) | |
tree | 48050d626ba09627e7ac191eca6aea6be22409b4 /src/library_gl.js | |
parent | 4729a321a9be339e33318b0852b1bab7592b1e48 (diff) |
basic fog support
Diffstat (limited to 'src/library_gl.js')
-rw-r--r-- | src/library_gl.js | 55 |
1 files changed, 44 insertions, 11 deletions
diff --git a/src/library_gl.js b/src/library_gl.js index 9dd29bb7..68716a39 100644 --- a/src/library_gl.js +++ b/src/library_gl.js @@ -906,7 +906,14 @@ var LibraryGL = { $GLEmulation__postset: 'GLEmulation.init();', $GLEmulation: { + // Fog support. Partial, we assume shaders are used that implement fog. We just pass them uniforms + fogStart: 0, + fogEnd: 1, + fogColor: null, + init: function() { + GLEmulation.fogColor = new Float32Array(4); + // Add some emulation workarounds Module.printErr('WARNING: using emscripten GL emulation. This is a collection of limited workarounds, do not expect it to work'); @@ -1071,16 +1078,16 @@ var LibraryGL = { source = 'varying vec4 v_color; \n' + source.replace(/gl_Color/g, 'v_color'); } if (source.indexOf('gl_Fog.color') >= 0) { - source = 'uniform vec4 a_fogColor; \n' + - source.replace(/gl_Fog.color/g, 'a_fogColor'); + source = 'uniform vec4 u_fogColor; \n' + + source.replace(/gl_Fog.color/g, 'u_fogColor'); } if (source.indexOf('gl_Fog.end') >= 0) { - source = 'uniform float a_fogEnd; \n' + - source.replace(/gl_Fog.end/g, 'a_fogEnd'); + source = 'uniform float u_fogEnd; \n' + + source.replace(/gl_Fog.end/g, 'u_fogEnd'); } if (source.indexOf('gl_Fog.scale') >= 0) { - source = 'uniform float a_fogScale; \n' + - source.replace(/gl_Fog.scale/g, 'a_fogScale'); + source = 'uniform float u_fogScale; \n' + + source.replace(/gl_Fog.scale/g, 'u_fogScale'); } if (source.indexOf('gl_FogFragCoord') >= 0) { source = 'varying float v_fogFragCoord; \n' + @@ -1170,11 +1177,11 @@ var LibraryGL = { } else if (pname == 0x0BA8) { // GL_TEXTURE_MATRIX HEAPF32.set(GL.immediate.matrix['t' + GL.immediate.clientActiveTexture], params >> 2); } else if (pname == 0x0B66) { // GL_FOG_COLOR - {{{ makeSetValue('params', '0', '0', 'float') }}}; + HEAPF32.set(GLEmulation.fogColor, params >> 2); } else if (pname == 0x0B63) { // GL_FOG_START - {{{ makeSetValue('params', '0', '0', 'float') }}}; + {{{ makeSetValue('params', '0', 'GLEmulation.fogStart', 'float') }}}; } else if (pname == 0x0B64) { // GL_FOG_END - {{{ makeSetValue('params', '0', '0', 'float') }}}; + {{{ makeSetValue('params', '0', 'GLEmulation.fogEnd', 'float') }}}; } else { glGetFloatv(pname, params); } @@ -1580,6 +1587,11 @@ var LibraryGL = { this.hasNormal = normalSize > 0 && this.normalLocation >= 0; this.floatType = Module.ctx.FLOAT; // minor optimization + + this.fogColorLocation = Module.ctx.getUniformLocation(this.program, 'u_fogColor'); + this.fogEndLocation = Module.ctx.getUniformLocation(this.program, 'u_fogEnd'); + this.fogScaleLocation = Module.ctx.getUniformLocation(this.program, 'u_fogScale'); + this.hasFog = !!(this.fogColorLocation || this.fogEndLocation || this.fogScaleLocation); }, prepare: function() { @@ -1661,6 +1673,11 @@ var LibraryGL = { Module.ctx.bindTexture(Module.ctx.TEXTURE_2D, texture); Module.ctx.uniform1i(this.textureLocation, 0); } + if (this.hasFog) { + if (this.fogColorLocation) Module.ctx.uniform4fv(this.fogColorLocation, GLEmulation.fogColor); + if (this.fogEndLocation) Module.ctx.uniform1f(this.fogEndLocation, GLEmulation.fogEnd); + if (this.fogScaleLocation) Module.ctx.uniform1f(this.fogScaleLocation, 1/(GLEmulation.fogEnd - GLEmulation.fogStart)); + } }, cleanup: function() { @@ -2018,10 +2035,26 @@ var LibraryGL = { _glColor4f({{{ makeGetValue('p', '0', 'float') }}}, {{{ makeGetValue('p', '4', 'float') }}}, {{{ makeGetValue('p', '8', 'float') }}}, {{{ makeGetValue('p', '12', 'float') }}}); }, - glFogf: function(){}, // TODO + glFogf: function(pname, param) { // partial support, TODO + switch(pname) { + case 0x0B63: // GL_FOG_START + GLEmulation.fogStart = param; break; + case 0x0B64: // GL_FOG_END + GLEmulation.fogEnd = param; break; + } + }, glFogi: function(){}, // TODO glFogx: function(){}, // TODO - glFogfv: function(){}, // TODO + glFogfv: function(pname, param) { // partial support, TODO + switch(pname) { + case 0x0B66: // GL_FOG_COLOR + GLEmulation.fogColor[0] = {{{ makeGetValue('param', '0', 'float') }}}; + GLEmulation.fogColor[1] = {{{ makeGetValue('param', '4', 'float') }}}; + GLEmulation.fogColor[2] = {{{ makeGetValue('param', '8', 'float') }}}; + GLEmulation.fogColor[3] = {{{ makeGetValue('param', '12', 'float') }}}; + break; + } + }, glPolygonMode: function(){}, // TODO |