From fb0a659c9ca3479fd6799325498b11f074689936 Mon Sep 17 00:00:00 2001 From: John Mayhew Date: Mon, 2 Apr 2012 14:57:31 -0700 Subject: -Namespaced all RDGE javascript. -Removed the following unused files from the build script /core/script/fx/blur.js /core/script/fx/ssao.js /core/script/animation.js - Fully removed the following from the build and from source control as they are unused or no longer needed /core/script/util/dbgpanel.js /core/script/util/fpsTracker.js /core/script/util/statTracker.js /core/script/input.js /core/script/TextureManager.js /core/script/ubershader.js --- js/helper-classes/RDGE/src/core/script/renderer.js | 2299 ++++++++++---------- 1 file changed, 1121 insertions(+), 1178 deletions(-) (limited to 'js/helper-classes/RDGE/src/core/script/renderer.js') diff --git a/js/helper-classes/RDGE/src/core/script/renderer.js b/js/helper-classes/RDGE/src/core/script/renderer.js index a50b8a68..06e9885e 100755 --- a/js/helper-classes/RDGE/src/core/script/renderer.js +++ b/js/helper-classes/RDGE/src/core/script/renderer.js @@ -4,112 +4,81 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. */ -g_renderStats = {}; -g_renderStats.numDrawCalls = new stat("rendering", "numDrawCalls", 0, null, false); -g_renderStats.numTriangles = new stat("rendering", "numTriangles", 0, null, false); -g_renderStats.numVerts = new stat("rendering", "numVerts", 0, null, false); -g_renderStats.numPasses = new stat("rendering", "numPasses", 0, null, false); -g_renderStats.reset = function() { - g_renderStats.numTriangles.value = 0; - g_renderStats.numDrawCalls.value = 0; - g_renderStats.numVerts.value = 0; - g_renderStats.numPasses.value = 0; -}; - -rdgeConstants = {}; - -/* - * clear flags - */ -rdgeConstants.colorBuffer = 0x00004000; -rdgeConstants.depthBuffer = 0x00000100; -rdgeConstants.stencilBuffer = 0x00000400; - -/* -* buffer types -*/ -rdgeConstants.BUFFER_STATIC = 0; -rdgeConstants.BUFFER_DYNAMIC = 1; -rdgeConstants.BUFFER_STREAM = 2; - -/* -* primitive types -*/ -rdgeConstants.POINTS = 0; -rdgeConstants.LINES = 1; -rdgeConstants.LINE_LOOP = 2; -rdgeConstants.LINE_STRIP = 3; -rdgeConstants.TRIANGLES = 4; -rdgeConstants.TRIANGLE_STRIP = 5; -rdgeConstants.TRIANGLE_FAN = 6; - -/* - * primitive data types - */ -rdgeConstants.BYTE = 0x1400; -rdgeConstants.UNSIGNED_BYTE = 0x1401; -rdgeConstants.SHORT = 0x1402; -rdgeConstants.UNSIGNED_SHORT = 0x1403; -rdgeConstants.INT = 0x1404; -rdgeConstants.UNSIGNED_INT = 0x1405; -rdgeConstants.FLOAT = 0x1406; - -/* -* pre-defined vertex element type -*/ -rdgeConstants.VS_ELEMENT_FLOAT4 = 4; -rdgeConstants.VS_ELEMENT_POS = 3; -rdgeConstants.VS_ELEMENT_NORM = 3; -rdgeConstants.VS_ELEMENT_FLOAT3 = 3; -rdgeConstants.VS_ELEMENT_FLOAT2 = 2; -rdgeConstants.VS_ELEMENT_UV = 2; -rdgeConstants.VS_ELEMENT_FLOAT = 1; -rdgeConstants.MAX_ELEM_TYPES = 7; - -// GL Definition of buffer types -rdgeConstants.BUFFER_STATIC = 0x88E0; -rdgeConstants.BUFFER_DYNAMIC = 0x88E4; -rdgeConstants.BUFFER_STREAM = 0x88E8; - -// render constants -rdgeConstants.MAX_MATERIAL_LIGHTS = 4; - -/* -* Material categories determine sorting -* materials support the following categories -*/ -rdgeConstants.categoryEnumeration = -{ - 'BACKGROUND' : 0, - 'OPAQUE' : 1, - 'TRANSPARENT' : 2, - 'ADDITIVE' : 3, - 'TRANSLUCENT' : 4, - 'FOREGROUND' : 5, - 'MAX_CAT' : 6 -}; - -/* - * Node types supported by the scene graph - */ -rdgeConstants.nodeType = -{ - 'TRNODE' : 0, - 'MESHNODE' : 1, - 'MATNODE' : 2, - 'LIGHTNODE' : 3 -}; - -// generate an id for the renderer to map a render buffer to primitive -rdgeId = 0; -function getBufferID() -{ - return rdgeId++; -} - +var RDGE = RDGE || {}; + +// runtime globals +RDGE.rdgeConstants = (function () { + return { + // clear flags + colorBuffer: 0x00004000, + depthBuffer: 0x00000100, + stencilBuffer: 0x00000400, + + // buffer types + BUFFER_STATIC: 0, + BUFFER_DYNAMIC: 1, + BUFFER_STREAM: 2, + + // primitive types + POINTS: 0, + LINES: 1, + LINE_LOOP: 2, + LINE_STRIP: 3, + TRIANGLES: 4, + TRIANGLE_STRIP: 5, + TRIANGLE_FAN: 6, + + // primitive data types + BYTE: 0x1400, + UNSIGNED_BYTE: 0x1401, + SHORT: 0x1402, + UNSIGNED_SHORT: 0x1403, + INT: 0x1404, + UNSIGNED_INT: 0x1405, + FLOAT: 0x1406, + + // pre-defined vertex element type + VS_ELEMENT_FLOAT4: 4, + VS_ELEMENT_POS: 3, + VS_ELEMENT_NORM: 3, + VS_ELEMENT_FLOAT3: 3, + VS_ELEMENT_FLOAT2: 2, + VS_ELEMENT_UV: 2, + VS_ELEMENT_FLOAT: 1, + MAX_ELEM_TYPES: 7, + + // GL Definition of buffer types + BUFFER_STATIC: 0x88E0, + BUFFER_DYNAMIC: 0x88E4, + BUFFER_STREAM: 0x88E8, + + // render constants + MAX_MATERIAL_LIGHTS: 4, + + // Material categories determine sorting materials support the following categories + categoryEnumeration: + { + 'BACKGROUND': 0, + 'OPAQUE': 1, + 'TRANSPARENT': 2, + 'ADDITIVE': 3, + 'TRANSLUCENT': 4, + 'FOREGROUND': 5, + 'MAX_CAT': 6 + }, -_renderer = function(canvas) { + // Node types supported by the scene graph + nodeType: + { + 'TRNODE': 0, + 'MESHNODE': 1, + 'MATNODE': 2, + 'LIGHTNODE': 3 + } + }; +})(); +RDGE._renderer = function (canvas) { /* * Initialize the context associated with this canvas */ @@ -132,7 +101,7 @@ _renderer = function(canvas) { this.ctx.viewport(0, 0, canvas.width, canvas.height); // Add a console output to the renderer - this.console = ("console" in window) ? window.console : { log: function() { } }; + this.console = ("console" in window) ? window.console : { log: function () { } }; /* * Set the default clear color @@ -219,7 +188,7 @@ _renderer = function(canvas) { /* * the camera manager - contains the camera stack for this render context */ - this.cameraMan = new cameraManager(); + this.cameraMan = new RDGE.cameraManager(); /* * a list of device buffers that are owned by this render context @@ -230,88 +199,88 @@ _renderer = function(canvas) { /* * State wrappers */ - this.cullBackFace = function() { + this.cullBackFace = function () { this.ctx.cullFace(this.ctx.Back); - } + }; - this.cullFrontFace = function() { + this.cullFrontFace = function () { this.ctx.cullFace(this.ctx.FRONT); - } + }; - this.disableCulling = function() { + this.disableCulling = function () { this.ctx.disable(this.ctx.CULL_FACE); - } + }; - this.enableCulling = function() { + this.enableCulling = function () { this.ctx.enable(this.ctx.CULL_FACE); - } + }; - this.enablePolyOffsetFill = function() { + this.enablePolyOffsetFill = function () { this.ctx.enable(this.ctx.POLYGON_OFFSET_FILL); - } + }; - this.disablePolyOffsetFill = function() { + this.disablePolyOffsetFill = function () { this.ctx.enable(this.ctx.POLYGON_OFFSET_FILL); - } + }; - this.enablePointSprites = function() { -// this.ctx.enable(0x8642); - } + this.enablePointSprites = function () { + // this.ctx.enable(0x8642); + }; - this.disablePointSprites = function() { -// this.ctx.enable(0x8642); - } + this.disablePointSprites = function () { + // this.ctx.enable(0x8642); + }; - this.setClearColor = function(color) { + this.setClearColor = function (color) { this.clearColor = color.slice(); this.ctx.clearColor(color[0], color[1], color[2], color[3]); - } + }; /* * flags that specify how to clear the scene, can be OR'ed together */ - this.setClearFlags = function(flags) { + this.setClearFlags = function (flags) { this.clearFlags = flags; - } + }; /* * called by the system to clear the video buffer according to pre-set flags */ - this._clear = function() { + this._clear = function () { this.ctx.clear(this.clearFlags); - } + }; /* * clears the video buffer with flags provided */ - this.clear = function(flags) { + this.clear = function (flags) { this.ctx.clear(flags); - } + }; /* * flush the video buffer */ - this.flush = function() { + this.flush = function () { this.ctx.flush(); - } + }; /* * Sets the current viewport */ - this.setViewPort = function(x, y, width, height) { + this.setViewPort = function (x, y, width, height) { this.vpX = x; this.vpY = y; this.vpWidth = width; this.vpHeight = height; this.ctx.viewport(this.vpX, this.vpY, this.vpWidth, this.vpHeight); - } + }; /* * access the camera manager associated with the renderer */ - this.cameraManager = function() { + this.cameraManager = function () { return this.cameraMan; - } + }; /* * Sets of texture maps owned by the renderer @@ -325,7 +294,7 @@ _renderer = function(canvas) { * @param wrap - optional "CLAMP or "REPEAT", default is clamp * @param mips - optional true/false value to create mipmaps, the default is true */ - this.getTextureByName = function(name, wrap, mips) { + this.getTextureByName = function (name, wrap, mips) { var ext = name.split('.')[1]; if (!ext) @@ -335,22 +304,19 @@ _renderer = function(canvas) { var tex = this.textureMap[name]; - if (tex === undefined) - { + if (tex === undefined) { // load the texture tex = this.createTexture(name + ext, wrap, mips); this.textureMap[name] = tex; tex.lookUpName = name; - tex.previouslyReferenced = false; + tex.previouslyReferenced = false; + } + else { + //console.log( "texture already loaded: " + name ); + tex.previouslyReferenced = true; } - else - { - //console.log( "texture already loaded: " + name ); - tex.previouslyReferenced = true; - } return tex; - }; /* @@ -359,11 +325,13 @@ _renderer = function(canvas) { * @param wrap - optional "CLAMP or "REPEAT", default is clamp * @param mips - optional true/false value to create mipmaps, the default is true */ - this.unloadedTextureCount = 0; - _texparams = function(wrap, mips) { this.wrap = wrap, this.mips = mips }; - this.createTexture = function(url, wrap, mips) { + this.unloadedTextureCount = 0; + _texparams = function (wrap, mips) { + this.wrap = wrap, this.mips = mips + }; + this.createTexture = function (url, wrap, mips) { var texture = this.ctx.createTexture(); - this.unloadedTextureCount++; + this.unloadedTextureCount++; if (wrap === undefined) wrap = "CLAMP"; @@ -373,34 +341,33 @@ _renderer = function(canvas) { if (texture) { texture.image = new Image(); texture.image.src = url; - texture.image.context = g_Engine.getContext(); + texture.image.context = RDGE.globals.engine.getContext(); texture.texparams = new _texparams(wrap, mips); - texture.image.onload = function() { + texture.image.onload = function () { var stateMan = this.context.ctxStateManager; stateMan.RDGEInitState.loadTexture(texture); - this.context.renderer.unloadedTextureCount--; - //console.log( "loaded texture: " + texture.lookUpName + ",to: " + this.context.renderer._world._worldCount + ", textures remaining to load: " + this.context.renderer.unloadedTextureCount ); - if (texture.callback) texture.callback( texture ); - if (this.context.renderer.unloadedTextureCount < 0) - console.log( "more textures loaded then created..." ); + this.context.renderer.unloadedTextureCount--; + //console.log( "loaded texture: " + texture.lookUpName + ",to: " + this.context.renderer._world._worldCount + ", textures remaining to load: " + this.context.renderer.unloadedTextureCount ); + if (texture.callback) texture.callback(texture); + if (this.context.renderer.unloadedTextureCount < 0) + console.log("more textures loaded then created..."); + }; + texture.image.onerror = function () { + this.context.renderer.unloadedTextureCount--; + if (texture.callback) texture.callback(texture); + //console.log( "Error loading texture: " + texture.image.src ); + if (this.context.renderer.unloadedTextureCount < 0) + console.log("more textures loaded then created..."); }; - texture.image.onerror = function() { - this.context.renderer.unloadedTextureCount--; - if (texture.callback) texture.callback( texture ); - //console.log( "Error loading texture: " + texture.image.src ); - if (this.context.renderer.unloadedTextureCount < 0) - console.log( "more textures loaded then created..." ); - } - } return texture; - } + }; /* * commits a texture to video memory * @param - the texture object created by a call to create texture */ - this.commitTexture = function(texture) { + this.commitTexture = function (texture) { this.ctx.bindTexture(this.ctx.TEXTURE_2D, texture); this.ctx.texImage2D(this.ctx.TEXTURE_2D, 0, this.ctx.RGBA, this.ctx.RGBA, this.ctx.UNSIGNED_BYTE, texture.image); @@ -413,16 +380,16 @@ _renderer = function(canvas) { this.ctx.texParameteri(this.ctx.TEXTURE_2D, this.ctx.TEXTURE_WRAP_T, texture.texparams.wrap === "REPEAT" ? this.ctx.REPEAT : this.ctx.CLAMP_TO_EDGE); this.ctx.bindTexture(this.ctx.TEXTURE_2D, null); - } + }; - this.verify = function(label) { + this.verify = function (label) { var error = this.ctx.getError(); if (error != 0) { window.console.log("GLError ( " + label + ") : " + error); } - } + }; - this.createRenderTargetTexture = function(lookUpName, width, height, generateMips) { + this.createRenderTargetTexture = function (lookUpName, width, height, generateMips) { var ctx = this.ctx; // create framebuffer var renderTargetFrameBuffer = ctx.createFramebuffer(); @@ -501,7 +468,7 @@ _renderer = function(canvas) { ctx.bindRenderbuffer(ctx.RENDERBUFFER, null); ctx.bindFramebuffer(ctx.FRAMEBUFFER, null); - renderTarget.id = "RT_" + nodeIdGen.getId(); + renderTarget.id = "RT_" + RDGE.nodeIdGen.getId(); renderTarget.frameBuffer = renderTargetFrameBuffer; @@ -512,9 +479,7 @@ _renderer = function(canvas) { this.textureMap[lookUpName] = renderTarget; return renderTarget; - } - - + }; /* * The default shader setup @@ -538,7 +503,7 @@ _renderer = function(canvas) { // parameters 'params': { - }, + }, // render states 'states': @@ -548,562 +513,586 @@ _renderer = function(canvas) { 'culling': true, 'cullFace': "BACK" } -}] - } - }; - - + }] + } }; +}; /* - * Shader definitions - */ - +* Shader definitions +*/ + /* - * The default shader setup - */ -rdgeDefaultShaderDefintion = { - 'shaders': { - //'defaultVShader':"assets/shaders/test_vshader.glsl", - //'defaultFShader':"assets/shaders/test_fshader.glsl" - 'defaultVShader':"assets/shaders/Basic.vert.glsl", - 'defaultFShader':"assets/shaders/Basic.frag.glsl" - }, - 'techniques': { - 'defaultTechnique':[{ - 'vshader' : 'defaultVShader', - 'fshader' : 'defaultFShader', - // attributes - 'attributes' : +* The default shader setup +*/ +RDGE.rdgeDefaultShaderDefintion = { + 'shaders': { + //'defaultVShader':"assets/shaders/test_vshader.glsl", + //'defaultFShader':"assets/shaders/test_fshader.glsl" + 'defaultVShader': "assets/shaders/Basic.vert.glsl", + 'defaultFShader': "assets/shaders/Basic.frag.glsl" + }, + 'techniques': { + 'defaultTechnique': [{ + 'vshader': 'defaultVShader', + 'fshader': 'defaultFShader', + // attributes + 'attributes': { - 'vert' : { 'type' : 'vec3' }, - 'normal' : { 'type' : 'vec3' }, - 'texcoord' : { 'type' : 'vec2' } + 'vert': { 'type': 'vec3' }, + 'normal': { 'type': 'vec3' }, + 'texcoord': { 'type': 'vec2' } }, - // parameters - 'params' : + // parameters + 'params': { }, - // render states - 'states' : + // render states + 'states': { - 'depthEnable' : true, - 'blendEnable' : false, - 'culling' : true, - 'cullFace' : "BACK" + 'depthEnable': true, + 'blendEnable': false, + 'culling': true, + 'cullFace': "BACK" } - }]}}; - + }] + } +}; + /* - * The default depth map shader techniques - */ -rdgeDepthMapShaderDef = { -'shaders': { - 'depthMapVShader':"assets/shaders/depthMap_vshader.glsl", - 'depthMapFShader':"assets/shaders/depthMap_fshader.glsl" - }, -'techniques': -{ - 'shadowDepthMap': - [{ - 'vshader' : 'depthMapVShader', - 'fshader' : 'depthMapFShader', - // attributes - 'attributes' : - { 'vert' : { 'type' : 'vec3' }, - 'normal' : { 'type' : 'vec3' }, - 'texcoord' : { 'type' : 'vec2' } }, - // parameters - 'params' :{ }, - // render states - 'states' : - { 'depthEnable' : true, - 'blendEnable' : false, - 'culling' : true, - 'cullFace' : "BACK"} - }], - 'depthMap': - [{ - 'vshader' : 'depthMapVShader', - 'fshader' : 'depthMapFShader', - // attributes - 'attributes' : - { 'vert' : { 'type' : 'vec3' }, - 'normal' : { 'type' : 'vec3' }, - 'texcoord' : { 'type' : 'vec2' } }, - // parameters - 'params' :{ }, - // render states - 'states' : - { 'depthEnable' : true, - 'blendEnable' : false, - 'culling' : true, - 'cullFace' : "BACK"} - }] - -}}; +* The default depth map shader techniques +*/ +// currently not used in Ninja +// RDGE.rdgeDepthMapShaderDef = { +// 'shaders': { +// 'depthMapVShader': "assets/shaders/depthMap_vshader.glsl", +// 'depthMapFShader': "assets/shaders/depthMap_fshader.glsl" +// }, +// 'techniques': +// { +// 'shadowDepthMap': +// [{ +// 'vshader': 'depthMapVShader', +// 'fshader': 'depthMapFShader', +// // attributes +// 'attributes': +// { 'vert': { 'type': 'vec3' }, +// 'normal': { 'type': 'vec3' }, +// 'texcoord': { 'type': 'vec2' } +// }, +// // parameters +// 'params': {}, +// // render states +// 'states': +// { 'depthEnable': true, +// 'blendEnable': false, +// 'culling': true, +// 'cullFace': "BACK" +// } +// }], +// 'depthMap': +// [{ +// 'vshader': 'depthMapVShader', +// 'fshader': 'depthMapFShader', +// // attributes +// 'attributes': +// { 'vert': { 'type': 'vec3' }, +// 'normal': { 'type': 'vec3' }, +// 'texcoord': { 'type': 'vec2' } +// }, +// // parameters +// 'params': {}, +// // render states +// 'states': +// { 'depthEnable': true, +// 'blendEnable': false, +// 'culling': true, +// 'cullFace': "BACK" +// } +// }] +// +// } +// }; /* - * capture normals in view space - */ -rdgeViewSpaceNormalsShader = { -'shaders': { - 'normalsVShader':"assets/shaders/norm_depth_vshader.glsl", - 'normalsFShader':"assets/shaders/norm_depth_fshader.glsl" - }, -'techniques': -{ - 'depthMapWNormal': - [{ - 'vshader' : 'normalsVShader', - 'fshader' : 'normalsFShader', - // attributes - 'attributes' : - { 'vert' : { 'type' : 'vec3' }, - 'normal' : { 'type' : 'vec3' }}, - // parameters - 'params' :{ }, - // render states - 'states' : - { 'depthEnable' : true, - 'blendEnable' : false, - 'culling' : true, - 'cullFace' : "BACK"} - }] - -}}; +* capture normals in view space +*/ +// currently not used in Ninja +// RDGE.rdgeViewSpaceNormalsShader = { +// 'shaders': { +// 'normalsVShader': "assets/shaders/norm_depth_vshader.glsl", +// 'normalsFShader': "assets/shaders/norm_depth_fshader.glsl" +// }, +// 'techniques': +// { +// 'depthMapWNormal': +// [{ +// 'vshader': 'normalsVShader', +// 'fshader': 'normalsFShader', +// // attributes +// 'attributes': +// { 'vert': { 'type': 'vec3' }, +// 'normal': { 'type': 'vec3'} +// }, +// // parameters +// 'params': {}, +// // render states +// 'states': +// { 'depthEnable': true, +// 'blendEnable': false, +// 'culling': true, +// 'cullFace': "BACK" +// } +// }] +// +// } +// }; /* - * basic screen squad shader definition - */ -rdgeScreenQuadShaderDef = { - 'shaders': { - // Texture coordinates are setup so that the full texture - // is mapped completely onto the screen - 'screenQuadVShader': "\ - attribute vec3 a_pos;\ - attribute vec2 a_uv;\ - uniform float u_inv_viewport_width;\ - uniform float u_inv_viewport_height;\ - varying vec2 vTexcoord;\ - void main()\ - {\ - gl_Position = vec4(a_pos.xy, 0.0, 1.0);\ - \ - vTexcoord.x = 0.5 * (1.0 + a_pos.x + u_inv_viewport_width);\ - vTexcoord.y = 0.5 * (1.0 - a_pos.y + u_inv_viewport_height);\ - }", - 'screenQuadFShader': "\ - precision highp float;\ - uniform sampler2D u_mainRT;\ - uniform sampler2D u_glowFinal;\ - uniform sampler2D u_ssaoRT;\ - uniform sampler2D u_shadowMap;\ - varying vec2 vTexcoord;\ - void main()\ - {\ - vec2 tex = vec2(vTexcoord.x, 1.0 - vTexcoord.y);\ - vec4 glowTexel = texture2D(u_glowFinal, tex);\ - vec4 ssaoTexel = texture2D(u_ssaoRT, tex);\ - vec4 smapCoef = texture2D(u_shadowMap, tex);\ - ssaoTexel.a = 0.0;\ - vec4 texel = texture2D(u_mainRT, tex);\ - gl_FragColor = vec4(texel.r*((1.0 - glowTexel.r)*smapCoef.r), texel.g*((1.0 - glowTexel.g)*smapCoef.g), texel.b*((1.0 - glowTexel.b)*smapCoef.b), texel.a) + glowTexel - ssaoTexel;\ - }" - }, - 'techniques': { - // rendering control - 'screenQuad':[{ - 'vshader' : 'screenQuadVShader', - 'fshader' : 'screenQuadFShader', - - // attributes - 'attributes' :{ - 'a_pos' : { 'type' : 'vec3' }, - 'a_uv' : { 'type' : 'vec2' } }, - 'params' : { - 'u_mainRT' : {'type' : "tex2d"}, - 'u_glowFinal' : {'type' : "tex2d", 'data':"assets/images/black"}, - 'u_ssaoRT' : {'type' : "tex2d", 'data':"assets/images/black"}, - 'u_shadowMap' : {'type' : "tex2d", 'data':"assets/images/white"}}, - // render states - 'states' : { - 'blendEnabled' : true, - 'srcBlend' : 'SRC_ALPHA', - 'dstcBlend' : 'ONE_MINUS_SRC_ALPHA' } - }] - } -} +* basic screen squad shader definition +*/ +// currently not used in Ninja +// RDGE.rdgeScreenQuadShaderDef = { +// 'shaders': { +// // Texture coordinates are setup so that the full texture +// // is mapped completely onto the screen +// 'screenQuadVShader': "\ +// attribute vec3 a_pos;\ +// attribute vec2 a_uv;\ +// uniform float u_inv_viewport_width;\ +// uniform float u_inv_viewport_height;\ +// varying vec2 vTexcoord;\ +// void main()\ +// {\ +// gl_Position = vec4(a_pos.xy, 0.0, 1.0);\ +// \ +// vTexcoord.x = 0.5 * (1.0 + a_pos.x + u_inv_viewport_width);\ +// vTexcoord.y = 0.5 * (1.0 - a_pos.y + u_inv_viewport_height);\ +// }", +// 'screenQuadFShader': "\ +// precision highp float;\ +// uniform sampler2D u_mainRT;\ +// uniform sampler2D u_glowFinal;\ +// uniform sampler2D u_ssaoRT;\ +// uniform sampler2D u_shadowMap;\ +// varying vec2 vTexcoord;\ +// void main()\ +// {\ +// vec2 tex = vec2(vTexcoord.x, 1.0 - vTexcoord.y);\ +// vec4 glowTexel = texture2D(u_glowFinal, tex);\ +// vec4 ssaoTexel = texture2D(u_ssaoRT, tex);\ +// vec4 smapCoef = texture2D(u_shadowMap, tex);\ +// ssaoTexel.a = 0.0;\ +// vec4 texel = texture2D(u_mainRT, tex);\ +// gl_FragColor = vec4(texel.r*((1.0 - glowTexel.r)*smapCoef.r), texel.g*((1.0 - glowTexel.g)*smapCoef.g), texel.b*((1.0 - glowTexel.b)*smapCoef.b), texel.a) + glowTexel - ssaoTexel;\ +// }" +// }, +// 'techniques': { +// // rendering control +// 'screenQuad': [{ +// 'vshader': 'screenQuadVShader', +// 'fshader': 'screenQuadFShader', +// +// // attributes +// 'attributes': { +// 'a_pos': { 'type': 'vec3' }, +// 'a_uv': { 'type': 'vec2' } +// }, +// 'params': { +// 'u_mainRT': { 'type': "tex2d" }, +// 'u_glowFinal': { 'type': "tex2d", 'data': "assets/images/black" }, +// 'u_ssaoRT': { 'type': "tex2d", 'data': "assets/images/black" }, +// 'u_shadowMap': { 'type': "tex2d", 'data': "assets/images/white" } +// }, +// // render states +// 'states': { +// 'blendEnabled': true, +// 'srcBlend': 'SRC_ALPHA', +// 'dstcBlend': 'ONE_MINUS_SRC_ALPHA' +// } +// }] +// } +// }; /* - * creates the glow map - */ -rdgeGlowMapShader = { - 'shaders': { - 'createGlowVShader':"assets/shaders/glowMap_vshader.glsl", - 'createGlowFShader':"assets/shaders/glowMap_fshader.glsl" - }, - 'techniques': { - 'createGlowMap':[ - { - 'vshader' : 'createGlowVShader', - 'fshader' : 'createGlowFShader', - // attributes - 'attributes' : - { - 'vert' : { 'type' : 'vec3' }, - 'normal' : { 'type' : 'vec3' }, - 'texcoord' : { 'type' : 'vec2' } - }, - // parameters - 'params' : - { - }, - - // render states - 'states' : - { - 'depthEnable' : true, - 'blendEnable' : false, - 'culling' : true, - 'cullFace' : "BACK" - } - } - ]} -} +* creates the glow map +*/ +// currently not used in Ninja +// RDGE.rdgeGlowMapShader = { +// 'shaders': { +// 'createGlowVShader': "assets/shaders/glowMap_vshader.glsl", +// 'createGlowFShader': "assets/shaders/glowMap_fshader.glsl" +// }, +// 'techniques': { +// 'createGlowMap': [ +// { +// 'vshader': 'createGlowVShader', +// 'fshader': 'createGlowFShader', +// // attributes +// 'attributes': +// { +// 'vert': { 'type': 'vec3' }, +// 'normal': { 'type': 'vec3' }, +// 'texcoord': { 'type': 'vec2' } +// }, +// // parameters +// 'params': +// { +// }, +// +// // render states +// 'states': +// { +// 'depthEnable': true, +// 'blendEnable': false, +// 'culling': true, +// 'cullFace': "BACK" +// } +// } +// ] +// } +// }; /* * Gaussian blur shader */ -rdgeGaussianBlurShader = { - 'shaders': { - 'blurVShader':"assets/shaders/separableBlur_vshader.glsl", - 'blurFShader':"assets/shaders/separableBlur_fshader.glsl" - }, - 'techniques': { - 'gaussianBlur':[ - { - 'vshader' : 'blurVShader', - 'fshader' : 'blurFShader', - // attributes - 'attributes' : - { - 'vert' : { 'type' : 'vec3' }, - 'texcoord' : { 'type' : 'vec2' } - }, - // parameters - 'params' : - { - 'vCoeffs' : {'type' :'vec3', 'data': [ 5.0 / 16.0, 6.0 / 16.0, 5.0 / 16.0 ]}, - 'vOffset' : {'type' : 'vec2', 'data': [0.00617, 0.00617]}, - 'u_weight' : {'type' : 'float', 'data': [1.0]}, - 'sTexture': {'type' : "tex2d"} - }, - // render states - 'states' : - { - 'culling' : false - } - } - ] -}} +// currently not used in Ninja +// RDGE.rdgeGaussianBlurShader = { +// 'shaders': { +// 'blurVShader': "assets/shaders/separableBlur_vshader.glsl", +// 'blurFShader': "assets/shaders/separableBlur_fshader.glsl" +// }, +// 'techniques': { +// 'gaussianBlur': [ +// { +// 'vshader': 'blurVShader', +// 'fshader': 'blurFShader', +// // attributes +// 'attributes': +// { +// 'vert': { 'type': 'vec3' }, +// 'texcoord': { 'type': 'vec2' } +// }, +// // parameters +// 'params': +// { +// 'vCoeffs': { 'type': 'vec3', 'data': [5.0 / 16.0, 6.0 / 16.0, 5.0 / 16.0] }, +// 'vOffset': { 'type': 'vec2', 'data': [0.00617, 0.00617] }, +// 'u_weight': { 'type': 'float', 'data': [1.0] }, +// 'sTexture': { 'type': "tex2d" } +// }, +// // render states +// 'states': +// { +// 'culling': false +// } +// } +// ] +// } +// }; /* * Screen space ambient occlusion shader */ -rdgeSSAOShader = { - 'shaders': { - 'blurVShader':"assets/shaders/ssao_vshader.glsl", - 'blurFShader':"assets/shaders/ssaohr_fshader.glsl" - }, - 'techniques': { - 'ssao':[ - { - 'vshader' : 'blurVShader', - 'fshader' : 'blurFShader', - // attributes - 'attributes' : - { - 'vert' : { 'type' : 'vec3' }, - 'texcoord' : { 'type' : 'vec2' } - }, - // parameters - 'params' : - { - 'u_normalsRT' : {'type' : "tex2d"}, - 'u_depthMap' : {'type' : "tex2d"}, - 'sRandMap' : {'type' : "tex2d", 'data':"assets/images/random_normal.png", 'wrap':"REPEAT", 'mips':false}, - 'u_cameraFTR' : {'type' :'vec3'}, - 'u_artVals' : {'type' :'vec4', 'data':[0.36, 0.75, 0.60, 0.05]}, // sample radius, intensity, distScale, bias - 'u_randMapSize' : {'type' :'float', 'data':[64.0]}, - 'u_screenSize' : {'type' :'vec2', 'data':[1024, 1024]} - }, - // render states - 'states' : - { - 'culling' : false - } - } - ] -}} +// currently not used in Ninja +// RDGE.rdgeSSAOShader = { +// 'shaders': { +// 'blurVShader': "assets/shaders/ssao_vshader.glsl", +// 'blurFShader': "assets/shaders/ssaohr_fshader.glsl" +// }, +// 'techniques': { +// 'ssao': [ +// { +// 'vshader': 'blurVShader', +// 'fshader': 'blurFShader', +// // attributes +// 'attributes': +// { +// 'vert': { 'type': 'vec3' }, +// 'texcoord': { 'type': 'vec2' } +// }, +// // parameters +// 'params': +// { +// 'u_normalsRT': { 'type': "tex2d" }, +// 'u_depthMap': { 'type': "tex2d" }, +// 'sRandMap': { 'type': "tex2d", 'data': "assets/images/random_normal.png", 'wrap': "REPEAT", 'mips': false }, +// 'u_cameraFTR': { 'type': 'vec3' }, +// 'u_artVals': { 'type': 'vec4', 'data': [0.36, 0.75, 0.60, 0.05] }, // sample radius, intensity, distScale, bias +// 'u_randMapSize': { 'type': 'float', 'data': [64.0] }, +// 'u_screenSize': { 'type': 'vec2', 'data': [1024, 1024] } +// }, +// // render states +// 'states': +// { +// 'culling': false +// } +// } +// ] +// } +// }; /* - * Shadow map generation - */ -rdgeShadowMapShader = { - 'shaders': { - 'shadowMapVShader':"assets/shaders/shadowMap_vshader.glsl", - 'shadowMapFShader':"assets/shaders/shadowMap_fshader.glsl" - }, - 'techniques': { - 'shadowMap':[ - { - 'vshader' : 'shadowMapVShader', - 'fshader' : 'shadowMapFShader', - // attributes - 'attributes' : - { - 'a_pos' : { 'type' : 'vec3' }, - 'a_uv' : { 'type' : 'vec2' } - }, - // parameters - 'params' : - { - 'u_lightSize' : { 'type' : 'float', 'data' : [7.93]}, - // color is inverted, alpha represents intensity and is not inverted - 'u_shadowColor' : { 'type' : 'vec4', 'data' : [0.922, 0.7373, 0.4824, 0.5]} - }, - // render states - 'states' : - { - 'depthEnable' : true, - 'blendEnable' : false, - 'culling' : true, - 'cullFace' : "BACK" - } - } - ] -}} +* Shadow map generation +*/ +// currently not used in Ninja +// RDGE.rdgeShadowMapShader = { +// 'shaders': { +// 'shadowMapVShader': "assets/shaders/shadowMap_vshader.glsl", +// 'shadowMapFShader': "assets/shaders/shadowMap_fshader.glsl" +// }, +// 'techniques': { +// 'shadowMap': [ +// { +// 'vshader': 'shadowMapVShader', +// 'fshader': 'shadowMapFShader', +// // attributes +// 'attributes': +// { +// 'a_pos': { 'type': 'vec3' }, +// 'a_uv': { 'type': 'vec2' } +// }, +// // parameters +// 'params': +// { +// 'u_lightSize': { 'type': 'float', 'data': [7.93] }, +// // color is inverted, alpha represents intensity and is not inverted +// 'u_shadowColor': { 'type': 'vec4', 'data': [0.922, 0.7373, 0.4824, 0.5] } +// }, +// // render states +// 'states': +// { +// 'depthEnable': true, +// 'blendEnable': false, +// 'culling': true, +// 'cullFace': "BACK" +// } +// } +// ] +// } +// }; /* - * Noise blur filter - */ -rdgeNoiseBlurShader = { - 'shaders': { - 'blurVShader':"assets/shaders/noiseBlur_vshader.glsl", - 'blurFShader':"assets/shaders/noiseBlur_fshader.glsl" - }, - 'techniques': { - 'blur':[ - { - 'vshader' : 'blurVShader', - 'fshader' : 'blurFShader', - // attributes - 'attributes' : - { - 'a_pos' : { 'type' : 'vec3' }, - 'a_uv' : { 'type' : 'vec2' } - }, - // parameters - 'params' : - { - 'u_blurSourceMap': {'type' : "tex2d"} - }, - // render states - 'states' : - { - 'culling' : false - } - } - ] -}} +* Noise blur filter +*/ +// currently not used in Ninja +// RDGE.rdgeNoiseBlurShader = { +// 'shaders': { +// 'blurVShader': "assets/shaders/noiseBlur_vshader.glsl", +// 'blurFShader': "assets/shaders/noiseBlur_fshader.glsl" +// }, +// 'techniques': { +// 'blur': [ +// { +// 'vshader': 'blurVShader', +// 'fshader': 'blurFShader', +// // attributes +// 'attributes': +// { +// 'a_pos': { 'type': 'vec3' }, +// 'a_uv': { 'type': 'vec2' } +// }, +// // parameters +// 'params': +// { +// 'u_blurSourceMap': { 'type': "tex2d" } +// }, +// // render states +// 'states': +// { +// 'culling': false +// } +// } +// ] +// } +// }; /* * defines a primitive using java script native types */ -function rdgePrimitiveDefinition() { - - /* - * The type of primitive - * supported types are - * renderer.POINTS - * renderer.LINES - * renderer.LINE_LOOP - * renderer.LINE_STRIP - * renderer.TRIANGLES - * renderer.TRIANGLE_STRIP - * renderer.TRIANGLE_FAN - */ - this.type = rdgeConstants.TRIANGLE_STRIP; - - /* - * Define vertex elements size in bytes and order the element appears in the stream - * Predefined size types: - * renderer.VS_ELEMENT_FLOAT4 - * renderer.VS_ELEMENT_FLOAT3 - * renderer.VS_ELEMENT_FLOAT2 - * renderer.VS_ELEMENT_FLOAT - * renderer.VS_ELEMENT_UINT16 - * renderer.VS_ELEMENT_UINT8 - */ - this.vertexDefinition= +RDGE.rdgePrimitiveDefinition = function () { + /* + * The type of primitive + * supported types are + * renderer.POINTS + * renderer.LINES + * renderer.LINE_LOOP + * renderer.LINE_STRIP + * renderer.TRIANGLES + * renderer.TRIANGLE_STRIP + * renderer.TRIANGLE_FAN + */ + this.type = RDGE.rdgeConstants.TRIANGLE_STRIP; + + /* + * Define vertex elements size in bytes and order the element appears in the stream + * Predefined size types: + * renderer.VS_ELEMENT_FLOAT4 + * renderer.VS_ELEMENT_FLOAT3 + * renderer.VS_ELEMENT_FLOAT2 + * renderer.VS_ELEMENT_FLOAT + * renderer.VS_ELEMENT_UINT16 + * renderer.VS_ELEMENT_UINT8 + */ + this.vertexDefinition = { - // usage example: two ways of declaring a float 3 stream - //"vert" :{ 'type':renderer.VS_ELEMENT_POS, 'bufferIndex':indexIntoBufferStream, 'usage': renderer.BUFFER_STATIC } - //"a_pos" :{ 'type':renderer.VS_ELEMENT_POS, 'bufferIndex':indexIntoBufferStream, 'usage': renderer.BUFFER_STATIC } + // usage example: two ways of declaring a float 3 stream + //"vert" :{ 'type':renderer.VS_ELEMENT_POS, 'bufferIndex':indexIntoBufferStream, 'usage': renderer.BUFFER_STATIC } + //"a_pos" :{ 'type':renderer.VS_ELEMENT_POS, 'bufferIndex':indexIntoBufferStream, 'usage': renderer.BUFFER_STATIC } }; - /* - * Arrays of buffer data listed in the order given by the vertex definition - */ - this.bufferStreams= + /* + * Arrays of buffer data listed in the order given by the vertex definition + */ + this.bufferStreams = [ ]; - /* - * An array indicating the the stream usage listed in the order given by the vertex definition - * Valid usage values: renderer.BUFFER_STATIC The data store contents will be specified once by the application, and used many times - * renderer.BUFFER_DYNAMIC The data store contents will be respecified repeatedly by the application, and used many times - * renderer.BUFFER_STREAM The data store contents will be specified once by the application, and used at most a few times - */ - this.streamUsage= + /* + * An array indicating the the stream usage listed in the order given by the vertex definition + * Valid usage values: renderer.BUFFER_STATIC The data store contents will be specified once by the application, and used many times + * renderer.BUFFER_DYNAMIC The data store contents will be respecified repeatedly by the application, and used many times + * renderer.BUFFER_STREAM The data store contents will be specified once by the application, and used at most a few times + */ + this.streamUsage = [ - + ]; - - /* - * indicates the the stream usage of the index buffer - * Valid usage values: renderer.BUFFER_STATIC The data store contents will be specified once by the application, and used many times - * renderer.BUFFER_DYNAMIC The data store contents will be respecified repeatedly by the application, and used many times - * renderer.BUFFER_STREAM The data store contents will be specified once by the application, and used at most a few times - */ - this.indexUsage = rdgeConstants.BUFFER_STREAM; - - /* - * a references to an array of indices - */ - this.indexBuffer= + + /* + * indicates the the stream usage of the index buffer + * Valid usage values: renderer.BUFFER_STATIC The data store contents will be specified once by the application, and used many times + * renderer.BUFFER_DYNAMIC The data store contents will be respecified repeatedly by the application, and used many times + * renderer.BUFFER_STREAM The data store contents will be specified once by the application, and used at most a few times + */ + this.indexUsage = RDGE.rdgeConstants.BUFFER_STREAM; + + /* + * a references to an array of indices + */ + this.indexBuffer = [ ]; - - /* - * the number of sets of geometry in this primitive - */ - this.setCount = 0; - - /* - * Offset of a primitive in an index buffer - * if the primitive represents a set each set array has a corresponding index offset - */ - this.indexOffsets = + + /* + * the number of sets of geometry in this primitive + */ + this.setCount = 0; + + /* + * Offset of a primitive in an index buffer + * if the primitive represents a set each set array has a corresponding index offset + */ + this.indexOffsets = [ ]; - - ////////////////////////////////////////////////////////////// - // OUTPUT VALUES // - ////////////////////////////////////////////////////////////// - - /* - * the number of coordinates's that make this primitive - * auto populated with a call to renderer.createPrimitive - */ - this.posCount = 0; - - /* - * the number of triangle that make up this primitive - * auto populated with a call to renderer.createPrimitive - */ - this.triCount = 0; - - /* - * the number of indices in the primitive - * auto populated with a call to renderer.createPrimitive - */ - this.indexCount = 0; - - /* - * size of an index value in bytes - */ - this.indexElementSize = rdgeConstants.UNSIGNED_SHORT; - - /* - * bufferHanldes are created when passing a primitive to renderer.createPrimitive - * An array of handles to buffers in vram, listed in the order given by the vertex definition - */ - this.bufferHandles = + + ////////////////////////////////////////////////////////////// + // OUTPUT VALUES // + ////////////////////////////////////////////////////////////// + + /* + * the number of coordinates's that make this primitive + * auto populated with a call to renderer.createPrimitive + */ + this.posCount = 0; + + /* + * the number of triangle that make up this primitive + * auto populated with a call to renderer.createPrimitive + */ + this.triCount = 0; + + /* + * the number of indices in the primitive + * auto populated with a call to renderer.createPrimitive + */ + this.indexCount = 0; + + /* + * size of an index value in bytes + */ + this.indexElementSize = RDGE.rdgeConstants.UNSIGNED_SHORT; + + /* + * bufferHanldes are created when passing a primitive to renderer.createPrimitive + * An array of handles to buffers in vram, listed in the order given by the vertex definition + */ + this.bufferHandles = [ ]; - - /* - * An index into the buffers table for the render context that created the buffer - */ - this.buffersID = -1; - - /* - * Handle to index buffer object - */ - this.indexHandle = null; - - - /* - * ------------------- Double buffer Setup----------------------------- - */ - - /* - * user flag used to create a double buffered primitive - * Double buffer flag - when double buffered a .front() and .back() buffer - * are available as well as a call .flip() to flip the buffers - */ - this.useDoubleBuffer = false; - - /* - * The double buffer offset tells the renderer where in its 'bufferID' array for this primitive the 'doubled' buffers begin - */ - this.doubleBufferOffset = 0; - - /* - * Keeps track of which buffer is the front buffer or active buffer - */ - this.frontBufferIndex = 0; - - /* - * Helper function for retrieving the buffer for editing - successfully calling this makes the buffer dirty and will trigger a buffer swap - * @param bufIndex - buffer stream index - * @return buffer stream requested, or null if invalid index is given - */ - this.update = function( bufIndex ) - { - if(!this.bufferStreams[bufIndex]) - return null; - - this.bufferStreams[bufIndex].dirty = true; - return this.bufferStreams[bufIndex]; - } - - - /* - * Flips the front and back buffers - */ - this.flip = function( renderer ) - { - if(this.useDoubleBuffer === true) - { - // if a back buffer is dirty update it - for(var i = 0, len = this.bufferStreams.length; i < len; ++i) - { - if(this.bufferStreams[i].dirty) - { - this.bufferStreams[i].dirty = false; - renderer.updateBuffer(renderer.buffers[this.buffersID][this.frontBufferIndex*this.doubleBufferOffset + i], this.bufferStreams[i], this.streamUsage[i]); - this.frontBufferIndex = 1 - this.frontBufferIndex; - } - } - } - } -} + /* + * An index into the buffers table for the render context that created the buffer + */ + this.buffersID = -1; + + /* + * Handle to index buffer object + */ + this.indexHandle = null; + + + /* + * ------------------- Double buffer Setup----------------------------- + */ + + /* + * user flag used to create a double buffered primitive + * Double buffer flag - when double buffered a .front() and .back() buffer + * are available as well as a call .flip() to flip the buffers + */ + this.useDoubleBuffer = false; + + /* + * The double buffer offset tells the renderer where in its 'bufferID' array for this primitive the 'doubled' buffers begin + */ + this.doubleBufferOffset = 0; + + /* + * Keeps track of which buffer is the front buffer or active buffer + */ + this.frontBufferIndex = 0; + + /* + * Helper function for retrieving the buffer for editing - successfully calling this makes the buffer dirty and will trigger a buffer swap + * @param bufIndex - buffer stream index + * @return buffer stream requested, or null if invalid index is given + */ + this.update = function (bufIndex) { + if (!this.bufferStreams[bufIndex]) + return null; + + this.bufferStreams[bufIndex].dirty = true; + return this.bufferStreams[bufIndex]; + }; + + + /* + * Flips the front and back buffers + */ + this.flip = function (renderer) { + if (this.useDoubleBuffer === true) { + // if a back buffer is dirty update it + for (var i = 0, len = this.bufferStreams.length; i < len; ++i) { + if (this.bufferStreams[i].dirty) { + this.bufferStreams[i].dirty = false; + renderer.updateBuffer(renderer.buffers[this.buffersID][this.frontBufferIndex * this.doubleBufferOffset + i], this.bufferStreams[i], this.streamUsage[i]); + this.frontBufferIndex = 1 - this.frontBufferIndex; + } + } + } + }; +}; + + +// generate an id for the renderer to map a render buffer to primitive +RDGE._renderer.prototype._rendererID = 0; +RDGE._renderer.prototype.getBufferID = function () { + return RDGE._renderer.prototype._rendererID++; +}; /* * @param bufferSizeOrData: an array of indices, or the size in bytes to preallocate * @param bufferUsage: BUFFER_STATIC The data store contents will be specified once by the application, and used many times @@ -1111,16 +1100,15 @@ function rdgePrimitiveDefinition() { * BUFFER_STREAM The data store contents will be specified once by the application, and used at most a few times * @return an unsigned short index buffer object */ -_renderer.prototype.createIndexBufferUINT16 = function( bufferSizeOrData, bufferUsage ) -{ - var bufferObject = this.ctx.createBuffer(); - bufferObject.type = bufferSizeOrData.type; - this.ctx.bindBuffer(this.ctx.ELEMENT_ARRAY_BUFFER, bufferObject); - this.ctx.bufferData(this.ctx.ELEMENT_ARRAY_BUFFER, (typeof k == "number") +RDGE._renderer.prototype.createIndexBufferUINT16 = function (bufferSizeOrData, bufferUsage) { + var bufferObject = this.ctx.createBuffer(); + bufferObject.type = bufferSizeOrData.type; + this.ctx.bindBuffer(this.ctx.ELEMENT_ARRAY_BUFFER, bufferObject); + this.ctx.bufferData(this.ctx.ELEMENT_ARRAY_BUFFER, (typeof k == "number") ? bufferSizeOrData : new Uint16Array(bufferSizeOrData), bufferUsage); - return bufferObject; -} + return bufferObject; +}; /* * @param bufferSizeOrData: an array of indices, or the size in bytes to preallocate @@ -1129,16 +1117,15 @@ _renderer.prototype.createIndexBufferUINT16 = function( bufferSizeOrData, buffer * BUFFER_STREAM The data store contents will be specified once by the application, and used at most a few times * @return an unsigned byte index buffer object */ -_renderer.prototype.createIndexBufferUINT8 = function( bufferSizeOrData, bufferUsage ) -{ - var bufferObject = this.ctx.createBuffer(); - bufferObject.type = bufferSizeOrData.type; - this.ctx.bindBuffer(this.ctx.ELEMENT_ARRAY_BUFFER, bufferObject); - this.ctx.bufferData(this.ctx.ELEMENT_ARRAY_BUFFER, (typeof k == "number") +RDGE._renderer.prototype.createIndexBufferUINT8 = function (bufferSizeOrData, bufferUsage) { + var bufferObject = this.ctx.createBuffer(); + bufferObject.type = bufferSizeOrData.type; + this.ctx.bindBuffer(this.ctx.ELEMENT_ARRAY_BUFFER, bufferObject); + this.ctx.bufferData(this.ctx.ELEMENT_ARRAY_BUFFER, (typeof k == "number") ? bufferSizeOrData : new Uint8Array(bufferSizeOrData), bufferUsage); - return bufferObject; -} + return bufferObject; +}; /* * @param bufferSizeOrData: a buffer of data the represents a stream in a vertex, or the size in bytes to preallocate @@ -1147,16 +1134,15 @@ _renderer.prototype.createIndexBufferUINT8 = function( bufferSizeOrData, bufferU * BUFFER_STREAM The data store contents will be specified once by the application, and used at most a few times * @return an unsigned byte index buffer object */ -_renderer.prototype.createBufferFLOAT32 = function( bufferSizeOrData, bufferUsage ) -{ - var bufferObject = this.ctx.createBuffer(); - bufferObject.type = bufferSizeOrData.type; - this.ctx.bindBuffer(this.ctx.ARRAY_BUFFER, bufferObject); - this.ctx.bufferData(this.ctx.ARRAY_BUFFER, (typeof k == "number") +RDGE._renderer.prototype.createBufferFLOAT32 = function (bufferSizeOrData, bufferUsage) { + var bufferObject = this.ctx.createBuffer(); + bufferObject.type = bufferSizeOrData.type; + this.ctx.bindBuffer(this.ctx.ARRAY_BUFFER, bufferObject); + this.ctx.bufferData(this.ctx.ARRAY_BUFFER, (typeof k == "number") ? bufferSizeOrData : new Float32Array(bufferSizeOrData), bufferUsage); - return bufferObject; -} + return bufferObject; +}; /* * @param bufferSizeOrData: a buffer of data the represents a stream in a vertex, or the size in bytes to preallocate @@ -1165,15 +1151,15 @@ _renderer.prototype.createBufferFLOAT32 = function( bufferSizeOrData, bufferUsag * BUFFER_STREAM The data store contents will be specified once by the application, and used at most a few times * @return an unsigned byte index buffer object */ -_renderer.prototype.createBufferINT32 = function(bufferSizeOrData,bufferUsage) { - var bufferObject=this.ctx.createBuffer(); - bufferObject.type=bufferSizeOrData.type; - this.ctx.bindBuffer(this.ctx.ARRAY_BUFFER, bufferObject); - this.ctx.bufferData(this.ctx.ARRAY_BUFFER, (typeof k == "number") +RDGE._renderer.prototype.createBufferINT32 = function (bufferSizeOrData, bufferUsage) { + var bufferObject = this.ctx.createBuffer(); + bufferObject.type = bufferSizeOrData.type; + this.ctx.bindBuffer(this.ctx.ARRAY_BUFFER, bufferObject); + this.ctx.bufferData(this.ctx.ARRAY_BUFFER, (typeof k == "number") ? bufferSizeOrData : new Int32Array(bufferSizeOrData), bufferUsage); - return bufferObject; -} + return bufferObject; +}; /* * @param bufferSizeOrData: a buffer of data the represents a stream in a vertex, or the size in bytes to preallocate @@ -1182,225 +1168,189 @@ _renderer.prototype.createBufferINT32 = function(bufferSizeOrData,bufferUsage) { * BUFFER_STREAM The data store contents will be specified once by the application, and used at most a few times * @return an unsigned byte index buffer object */ -_renderer.prototype.createBufferINT16 = function(bufferSizeOrData,bufferUsage) { - var bufferObject=this.ctx.createBuffer(); - bufferObject.type=bufferSizeOrData.type; - this.ctx.bindBuffer(this.ctx.ARRAY_BUFFER, bufferObject); - this.ctx.bufferData(this.ctx.ARRAY_BUFFER, (typeof k == "number") +RDGE._renderer.prototype.createBufferINT16 = function (bufferSizeOrData, bufferUsage) { + var bufferObject = this.ctx.createBuffer(); + bufferObject.type = bufferSizeOrData.type; + this.ctx.bindBuffer(this.ctx.ARRAY_BUFFER, bufferObject); + this.ctx.bufferData(this.ctx.ARRAY_BUFFER, (typeof k == "number") ? bufferSizeOrData : new Int16Array(bufferSizeOrData), bufferUsage); - return bufferObject; -} + return bufferObject; +}; -_renderer.prototype.updateBuffer = function( dstBuffer, srcBuffer, bufferUsage, vertexOffset ) -{ - this.ctx.bindBuffer(this.ctx.ARRAY_BUFFER, dstBuffer); - if(bufferUsage === rdgeConstants.BUFFER_DYNAMIC) - { - // use bufferSubData - this.ctx.bufferSubData(this.ctx.ARRAY_BUFFER, vertexOffset || 0, new Float32Array(srcBuffer)); - } - else - { - // re-create the buffer +RDGE._renderer.prototype.updateBuffer = function (dstBuffer, srcBuffer, bufferUsage, vertexOffset) { + this.ctx.bindBuffer(this.ctx.ARRAY_BUFFER, dstBuffer); + if (bufferUsage === RDGE.rdgeConstants.BUFFER_DYNAMIC) { + // use bufferSubData + this.ctx.bufferSubData(this.ctx.ARRAY_BUFFER, vertexOffset || 0, new Float32Array(srcBuffer)); + } + else { + // re-create the buffer this.ctx.bufferData(this.ctx.ARRAY_BUFFER, new Float32Array(srcBuffer), bufferUsage); - } -} + } +}; /* - * @param primitiveDef defines a primitive using java script types, creates the video ram objects and attaches them to the primitive passed in - */ -_renderer.prototype.createPrimitive = function( primitiveDef ) -{ - if(!primitiveDef.built) - { - // store the buffer handle with the renderer as it is context specific, creating an ID to look up the buffer at render time - primitiveDef.buffersID = getBufferID(); - primitiveDef.built = true; - } - else if(this.buffers[primitiveDef.buffersID]) - { - // already created - return; - } - - // this mapping in the array holds an array of buffers - this.buffers[primitiveDef.buffersID] = []; - this.buffers[primitiveDef.buffersID].ctxId = this.id; - - // set up buffers - this.updatePrimitive(primitiveDef); -} +* @param primitiveDef defines a primitive using java script types, creates the video ram objects and attaches them to the primitive passed in +*/ +RDGE._renderer.prototype.createPrimitive = function (primitiveDef) { + if (!primitiveDef.built) { + // store the buffer handle with the renderer as it is context specific, creating an ID to look up the buffer at render time + primitiveDef.buffersID = this.getBufferID(); + primitiveDef.built = true; + } + else if (this.buffers[primitiveDef.buffersID]) { + // already created + return; + } + + // this mapping in the array holds an array of buffers + this.buffers[primitiveDef.buffersID] = []; + this.buffers[primitiveDef.buffersID].ctxId = this.id; + + // set up buffers + this.updatePrimitive(primitiveDef); +}; /* - * @param primitiveDef defines a primitive using java script types, creates the video ram objects and attaches them to the primitive passed in - */ - _renderer.prototype.updatePrimitive = function( prim ) -{ - if(!prim.built) - { - this.createPrimitive(prim); - return; - } +* @param primitiveDef defines a primitive using java script types, creates the video ram objects and attaches them to the primitive passed in +*/ +RDGE._renderer.prototype.updatePrimitive = function (prim) { + if (!prim.built) { + this.createPrimitive(prim); + return; + } - var bufIdxVisited = []; + var bufIdxVisited = []; - for(var e in prim.vertexDefinition) - { - var vert_element = prim.vertexDefinition[e]; - - if(bufIdxVisited.indexOf(vert_element.bufferIndex) > -1) - continue; - bufIdxVisited.push(vert_element.bufferIndex); - - vert_element.debugName = e + " buffer"; - - // flag the positional data - if(vert_element.type == this.VS_ELEMENT_POS) - { - prim.posCount = prim.bufferStreams[vert_element.bufferIndex].length; - - // save a reference to the coordinates for later - prim.positions = prim.bufferStreams[vert_element.bufferIndex]; - } - - // if we have not already created the buffer - do so now - if(this.buffers[prim.buffersID][vert_element.bufferIndex] == undefined) - { - prim.bufferStreams[vert_element.bufferIndex].type = e + " PrimaryBuffer"; - - if(prim.forceVertexCount) - { - this.buffers[prim.buffersID][vert_element.bufferIndex] = this.createBufferFLOAT32( 4 * prim.forceVertexCount, vert_element.bufferUsage ); - this.ctx.bufferSubData(this.ctx.ARRAY_BUFFER, 0, new Float32Array(prim.bufferStreams[vert_element.bufferIndex]), vert_element.bufferUsage ); - } - else - { - this.buffers[prim.buffersID][vert_element.bufferIndex] = this.createBufferFLOAT32( prim.bufferStreams[vert_element.bufferIndex], vert_element.bufferUsage ); - } - } - else - { - this.ctx.bindBuffer(this.ctx.ARRAY_BUFFER, this.buffers[prim.buffersID][vert_element.bufferIndex]); - this.ctx.bufferSubData(this.ctx.ARRAY_BUFFER, 0, new Float32Array(prim.bufferStreams[vert_element.bufferIndex]), vert_element.bufferUsage ); - } - - // double up the buffer - if(prim.useDoubleBuffer === true) - { - prim.doubleBufferOffset = prim.bufferStreams.length - - // debug data - prim.bufferStreams[vert_element.bufferIndex].type = e + " SecondaryBuffer"; - - // store double buffer at the doubleBuffer offset - if(this.buffers[prim.buffersID][prim.doubleBufferOffset + vert_element.bufferIndex] == undefined) - { - if(prim.forceVertexCount) - { - this.buffers[prim.buffersID][prim.doubleBufferOffset + vert_element.bufferIndex] = this.createBufferFLOAT32( 4 * prim.prim.forceVertexCount, vert_element.bufferUsage ); - this.ctx.bufferSubData(this.ctx.ARRAY_BUFFER, 0, new Float32Array(prim.bufferStreams[vert_element.bufferIndex]), vert_element.bufferUsage ); - } - else - { - this.buffers[prim.buffersID][prim.doubleBufferOffset + vert_element.bufferIndex] = this.createBufferFLOAT32( prim.bufferStreams[vert_element.bufferIndex], vert_element.bufferUsage ); - } - } - else - { - this.ctx.bindBuffer(this.ctx.ARRAY_BUFFER, this.buffers[prim.buffersID][prim.doubleBufferOffset + vert_element.bufferIndex]); - this.ctx.bufferSubData(this.ctx.ARRAY_BUFFER, 0, new Float32Array(prim.bufferStreams[vert_element.bufferIndex]), vert_element.bufferUsage ); - } - } - } - - if(prim.indexBuffer.length > 0) - { - var indexBufLength = prim.indexBuffer.length; - prim.indexBuffer.debugName="index Buffer"; - - if(this.buffers[prim.buffersID].indexHandle == undefined) - { - if(prim.forceIndexCount) - { - this.buffers[prim.buffersID].indexHandle = this.createIndexBufferUINT16( 2 * prim.forceIndexCount, prim.indexUsage ); - this.ctx.bufferSubData(this.ctx.ELEMENT_ARRAY_BUFFER, 0, new Float32Array(prim.indexBuffer), prim.indexUsage ); - } - else - { - this.buffers[prim.buffersID].indexHandle = this.createIndexBufferUINT16(prim.indexBuffer,prim.indexUsage); - } - } - else - { - this.ctx.bindBuffer(this.ctx.ELEMENT_ARRAY_BUFFER, this.buffers[prim.buffersID].indexHandle); - this.ctx.bufferSubData(this.ctx.ELEMENT_ARRAY_BUFFER, 0, new Float32Array(prim.indexBuffer), prim.indexUsage ); - } - - prim.indexCount=indexBufLength; - prim.triCount=indexBufLength/3; - } - else - { - prim.triCount=prim.posCount/3; - } -} + for (var e in prim.vertexDefinition) { + var vert_element = prim.vertexDefinition[e]; + + if (bufIdxVisited.indexOf(vert_element.bufferIndex) > -1) + continue; + bufIdxVisited.push(vert_element.bufferIndex); + + vert_element.debugName = e + " buffer"; + + // flag the positional data + if (vert_element.type == this.VS_ELEMENT_POS) { + prim.posCount = prim.bufferStreams[vert_element.bufferIndex].length; + + // save a reference to the coordinates for later + prim.positions = prim.bufferStreams[vert_element.bufferIndex]; + } + + // if we have not already created the buffer - do so now + if (this.buffers[prim.buffersID][vert_element.bufferIndex] == undefined) { + prim.bufferStreams[vert_element.bufferIndex].type = e + " PrimaryBuffer"; + + if (prim.forceVertexCount) { + this.buffers[prim.buffersID][vert_element.bufferIndex] = this.createBufferFLOAT32(4 * prim.forceVertexCount, vert_element.bufferUsage); + this.ctx.bufferSubData(this.ctx.ARRAY_BUFFER, 0, new Float32Array(prim.bufferStreams[vert_element.bufferIndex]), vert_element.bufferUsage); + } + else { + this.buffers[prim.buffersID][vert_element.bufferIndex] = this.createBufferFLOAT32(prim.bufferStreams[vert_element.bufferIndex], vert_element.bufferUsage); + } + } + else { + this.ctx.bindBuffer(this.ctx.ARRAY_BUFFER, this.buffers[prim.buffersID][vert_element.bufferIndex]); + this.ctx.bufferSubData(this.ctx.ARRAY_BUFFER, 0, new Float32Array(prim.bufferStreams[vert_element.bufferIndex]), vert_element.bufferUsage); + } + + // double up the buffer + if (prim.useDoubleBuffer === true) { + prim.doubleBufferOffset = prim.bufferStreams.length + + // debug data + prim.bufferStreams[vert_element.bufferIndex].type = e + " SecondaryBuffer"; + + // store double buffer at the doubleBuffer offset + if (this.buffers[prim.buffersID][prim.doubleBufferOffset + vert_element.bufferIndex] == undefined) { + if (prim.forceVertexCount) { + this.buffers[prim.buffersID][prim.doubleBufferOffset + vert_element.bufferIndex] = this.createBufferFLOAT32(4 * prim.prim.forceVertexCount, vert_element.bufferUsage); + this.ctx.bufferSubData(this.ctx.ARRAY_BUFFER, 0, new Float32Array(prim.bufferStreams[vert_element.bufferIndex]), vert_element.bufferUsage); + } + else { + this.buffers[prim.buffersID][prim.doubleBufferOffset + vert_element.bufferIndex] = this.createBufferFLOAT32(prim.bufferStreams[vert_element.bufferIndex], vert_element.bufferUsage); + } + } + else { + this.ctx.bindBuffer(this.ctx.ARRAY_BUFFER, this.buffers[prim.buffersID][prim.doubleBufferOffset + vert_element.bufferIndex]); + this.ctx.bufferSubData(this.ctx.ARRAY_BUFFER, 0, new Float32Array(prim.bufferStreams[vert_element.bufferIndex]), vert_element.bufferUsage); + } + } + } + + if (prim.indexBuffer.length > 0) { + var indexBufLength = prim.indexB