From b89a7ee8b956c96a1dcee995ea840feddc5d4b27 Mon Sep 17 00:00:00 2001 From: Pierre Frisch Date: Thu, 22 Dec 2011 07:25:50 -0800 Subject: First commit of Ninja to ninja-internal Signed-off-by: Valerio Virgillito --- .../RDGE/src/core/script/MeshManager.js | 242 +++ .../RDGE/src/core/script/RenderInitProcs.js | 263 +++ .../RDGE/src/core/script/RenderProcs.js | 536 +++++++ .../RDGE/src/core/script/ScreenQuad.js | 31 + .../RDGE/src/core/script/ShaderManager.js | 96 ++ .../RDGE/src/core/script/TextureManager.js | 6 + .../RDGE/src/core/script/animation.js | 322 ++++ js/helper-classes/RDGE/src/core/script/box.js | 143 ++ js/helper-classes/RDGE/src/core/script/camera.js | 251 +++ js/helper-classes/RDGE/src/core/script/engine.js | 482 ++++++ js/helper-classes/RDGE/src/core/script/fx/blur.js | 196 +++ js/helper-classes/RDGE/src/core/script/fx/ssao.js | 116 ++ .../RDGE/src/core/script/init_state.js | 342 ++++ js/helper-classes/RDGE/src/core/script/input.js | 117 ++ js/helper-classes/RDGE/src/core/script/jpass.js | 710 ++++++++ js/helper-classes/RDGE/src/core/script/jshader.js | 750 +++++++++ .../RDGE/src/core/script/lightmanager.js | 97 ++ .../RDGE/src/core/script/math/mat4.js | 754 +++++++++ .../RDGE/src/core/script/math/quat.js | 225 +++ .../RDGE/src/core/script/math/vec2.js | 191 +++ .../RDGE/src/core/script/math/vec3.js | 351 ++++ .../RDGE/src/core/script/math/vec4.js | 258 +++ .../RDGE/src/core/script/objectManager.js | 72 + js/helper-classes/RDGE/src/core/script/particle.js | 842 ++++++++++ .../RDGE/src/core/script/precompiled.js | 69 + .../RDGE/src/core/script/renderUtils.js | 386 +++++ .../RDGE/src/core/script/rendercontext.js | 268 ++++ js/helper-classes/RDGE/src/core/script/renderer.js | 1696 ++++++++++++++++++++ .../RDGE/src/core/script/run_state.js | 456 ++++++ js/helper-classes/RDGE/src/core/script/runtime.js | 237 +++ .../RDGE/src/core/script/scenegraph.js | 1123 +++++++++++++ .../RDGE/src/core/script/scenegraphNodes.js | 649 ++++++++ .../RDGE/src/core/script/shadowLight.js | 54 + js/helper-classes/RDGE/src/core/script/sockets.js | 166 ++ .../RDGE/src/core/script/ubershader.js | 159 ++ .../RDGE/src/core/script/util/dbgpanel.js | 237 +++ .../RDGE/src/core/script/util/fpsTracker.js | 51 + .../RDGE/src/core/script/util/statTracker.js | 338 ++++ .../RDGE/src/core/script/utilities.js | 245 +++ 39 files changed, 13527 insertions(+) create mode 100644 js/helper-classes/RDGE/src/core/script/MeshManager.js create mode 100644 js/helper-classes/RDGE/src/core/script/RenderInitProcs.js create mode 100644 js/helper-classes/RDGE/src/core/script/RenderProcs.js create mode 100644 js/helper-classes/RDGE/src/core/script/ScreenQuad.js create mode 100644 js/helper-classes/RDGE/src/core/script/ShaderManager.js create mode 100644 js/helper-classes/RDGE/src/core/script/TextureManager.js create mode 100644 js/helper-classes/RDGE/src/core/script/animation.js create mode 100644 js/helper-classes/RDGE/src/core/script/box.js create mode 100644 js/helper-classes/RDGE/src/core/script/camera.js create mode 100644 js/helper-classes/RDGE/src/core/script/engine.js create mode 100644 js/helper-classes/RDGE/src/core/script/fx/blur.js create mode 100644 js/helper-classes/RDGE/src/core/script/fx/ssao.js create mode 100644 js/helper-classes/RDGE/src/core/script/init_state.js create mode 100644 js/helper-classes/RDGE/src/core/script/input.js create mode 100644 js/helper-classes/RDGE/src/core/script/jpass.js create mode 100644 js/helper-classes/RDGE/src/core/script/jshader.js create mode 100644 js/helper-classes/RDGE/src/core/script/lightmanager.js create mode 100644 js/helper-classes/RDGE/src/core/script/math/mat4.js create mode 100644 js/helper-classes/RDGE/src/core/script/math/quat.js create mode 100644 js/helper-classes/RDGE/src/core/script/math/vec2.js create mode 100644 js/helper-classes/RDGE/src/core/script/math/vec3.js create mode 100644 js/helper-classes/RDGE/src/core/script/math/vec4.js create mode 100644 js/helper-classes/RDGE/src/core/script/objectManager.js create mode 100644 js/helper-classes/RDGE/src/core/script/particle.js create mode 100644 js/helper-classes/RDGE/src/core/script/precompiled.js create mode 100644 js/helper-classes/RDGE/src/core/script/renderUtils.js create mode 100644 js/helper-classes/RDGE/src/core/script/rendercontext.js create mode 100644 js/helper-classes/RDGE/src/core/script/renderer.js create mode 100644 js/helper-classes/RDGE/src/core/script/run_state.js create mode 100644 js/helper-classes/RDGE/src/core/script/runtime.js create mode 100644 js/helper-classes/RDGE/src/core/script/scenegraph.js create mode 100644 js/helper-classes/RDGE/src/core/script/scenegraphNodes.js create mode 100644 js/helper-classes/RDGE/src/core/script/shadowLight.js create mode 100644 js/helper-classes/RDGE/src/core/script/sockets.js create mode 100644 js/helper-classes/RDGE/src/core/script/ubershader.js create mode 100644 js/helper-classes/RDGE/src/core/script/util/dbgpanel.js create mode 100644 js/helper-classes/RDGE/src/core/script/util/fpsTracker.js create mode 100644 js/helper-classes/RDGE/src/core/script/util/statTracker.js create mode 100644 js/helper-classes/RDGE/src/core/script/utilities.js (limited to 'js/helper-classes/RDGE/src/core/script') diff --git a/js/helper-classes/RDGE/src/core/script/MeshManager.js b/js/helper-classes/RDGE/src/core/script/MeshManager.js new file mode 100644 index 00000000..43813078 --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/MeshManager.js @@ -0,0 +1,242 @@ +/* +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + +function Model(name, mesh) +{ + this.name = name; + this.mesh = mesh; + this.camera = null; +} + +/* + * Maintains a list of meshes to allow instancing of data + */ +function MeshManager() { + this.contentUrl = "assets_web/mesh/"; + this.modelMap = {}; + this.readyList = []; // meshes that have data ready + this.meshesLoading = true; // indicates that no meshes have loaded or that they are still loading + this.postMeshLoadCallbackList = []; + this.tempSphere = null; + this.requestCounter = 0; +} + +/* + * Pass the scene meshNode stump, loads temp object while real mesh is downloading + */ +MeshManager.prototype.loadMesh = function (meshStump, tempMesh) +{ + // if it exists already, return the mesh requested + if ( this.modelMap[meshStump.name] !== undefined ) + return this.modelMap[meshStump.name]; + + meshStump.ready = false; + meshStump.addr = this.contentUrl + meshStump.name + "_mesh.json"; + meshStump.ctxID = g_Engine.getContext().renderer.id; + + // sets a temp mesh up in place of the final mesh to load + if (!tempMesh) + { + if (this.tempSphere == null) + { + this.tempSphere = makeSphere(g_Engine.getContext().renderer.ctx, 25, 5, 5); + } + + tempMesh = this.tempSphere; + } + + // add the temp mesh to the map of loaded meshes + this.modelMap[meshStump.name] = tempMesh; + + // update the request counter - we now have one more mesh to load + this.requestCounter++; + + requestMesh(meshStump); + + return null; +}; + +/* + * Deletes the passed mesh from the manager as well as all renderers + */ +MeshManager.prototype.deleteMesh = function (name) +{ + var model = this.modelMap[name]; + + if (model) + { + g_Engine.ctxMan.forEach(function(context) + { + context.renderer.deletePrimitive(model.primitive); + }); + + delete this.modelMap[name]; + } +}; + +MeshManager.prototype.getModelByName = function (name) +{ + return this.modelMap[name]; +}; + +MeshManager.prototype.getModelNames = function () +{ + var names = []; + for (var index in this.modelMap) { + names.push(this.modelList[index].name); + } + + return names; +}; + + +MeshManager.prototype.processMeshData = function () { + var renderer = g_Engine.getContext().renderer; + + // loop through meshes and load ready data + for (var index in this.readyList) { + // if item is ready load it + if (this.readyList[index] && this.readyList[index].ready && renderer.id === this.readyList[index].ctxID) { + + + // pop the item + var model = this.readyList[index]; + this.readyList.splice(index, 1); + + var primset = new rdgePrimitiveDefinition(); + + primset.vertexDefinition = + { + // this shows two ways to map this data to an attribute + "vert":{'type':rdgeConstants.VS_ELEMENT_POS, 'bufferIndex':0, 'bufferUsage': rdgeConstants.BUFFER_STATIC}, + "a_pos":{'type':rdgeConstants.VS_ELEMENT_POS, 'bufferIndex':0, 'bufferUsage': rdgeConstants.BUFFER_STATIC}, + "normal":{'type':rdgeConstants.VS_ELEMENT_FLOAT3, 'bufferIndex':1, 'bufferUsage': rdgeConstants.BUFFER_STATIC}, + "a_norm":{'type':rdgeConstants.VS_ELEMENT_FLOAT3, 'bufferIndex':1, 'bufferUsage': rdgeConstants.BUFFER_STATIC}, + "a_normal":{'type':rdgeConstants.VS_ELEMENT_FLOAT3, 'bufferIndex':1, 'bufferUsage': rdgeConstants.BUFFER_STATIC}, + "texcoord":{'type':rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex':2, 'bufferUsage': rdgeConstants.BUFFER_STATIC}, + "a_texcoord":{'type':rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex':2, 'bufferUsage': rdgeConstants.BUFFER_STATIC}, + "a_texcoords":{'type':rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex':2, 'bufferUsage': rdgeConstants.BUFFER_STATIC}, + "a_uv":{'type':rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex':2, 'bufferUsage': rdgeConstants.BUFFER_STATIC} + }; + + primset.bufferStreams = + [ + model.root.data.coords, + model.root.data.normals, + model.root.data.uvs + ]; + + primset.streamUsage = + [ + rdgeConstants.BUFFER_STATIC, + rdgeConstants.BUFFER_STATIC, + rdgeConstants.BUFFER_STATIC + ]; + + primset.indexUsage = rdgeConstants.BUFFER_STREAM; + + primset.indexBuffer = model.root.data.indices; + + renderer.createPrimitive( primset ); + + model.root.primitive = primset; + + // generate a bounding box for this mesh + model.root.bbox = new box(); + + var numCoords = model.root.data.coords.length; var idx = 0; + while (idx < numCoords - 2) + { + var thisCoord = [model.root.data.coords[idx+0], model.root.data.coords[idx+1], model.root.data.coords[idx+2]]; + model.root.bbox.addVec3(thisCoord); + idx += 3; + } + + this.modelMap[model.root.attribs.name] = model.root; + + // now that the model is load reduce the request count + this.requestCounter--; + + this.onLoaded(model.root.attribs.name); + //break; + } + + } +} + +MeshManager.prototype.isReady = function() +{ + return this.readyList.length == 0; +} + +MeshManager.prototype.addOnLoadedCallback = function (callback) +{ + this.postMeshLoadCallbackList.push(callback) +} + +MeshManager.prototype.onLoaded = function ( meshName ) +{ + for (var index = 0 in this.postMeshLoadCallbackList) + { + // call the functions + this.postMeshLoadCallbackList[index].onMeshLoaded(meshName); + } +} + +MeshManager.prototype.exportJSON = function () +{ + for(var m in this.modelMap) + { + this.modelMap[m].primitive.built = false; + } + + return JSON.stringify(this.modelMap); +} + +MeshManager.prototype.importJSON = function ( jsonMeshExport ) +{ + try + { + var tempModelMap = JSON.parse(jsonMeshExport); + + for(var m in tempModelMap) + { + if(!this.modelMap[m]) + { + this.modelMap[m] = tempModelMap[m]; + } + } + window.console.log("meshes imported"); + }catch( e ) + { + window.console.error("error importing meshes: " + e.description ); + } +} + +/* + * global function for the mesh manager to make mesh file requests + */ +function requestMesh(mesh) +{ + var request = new XMLHttpRequest(); + request.mesh = mesh; + request.onreadystatechange = function () { + if (request.readyState == 4) { + if (request.status == 200 || window.location.href.indexOf("http") == -1) { + var mesh = eval("(" + request.responseText + ")"); //retrieve result as an JavaScript object + mesh.ready = true; + mesh.ctxID = request.mesh.ctxID; + g_meshMan.readyList.push(mesh); + } + else { + alert("An error has occured making the request"); + } + } + } + + request.open("GET", mesh.addr, true); + request.send(null); +} diff --git a/js/helper-classes/RDGE/src/core/script/RenderInitProcs.js b/js/helper-classes/RDGE/src/core/script/RenderInitProcs.js new file mode 100644 index 00000000..9d4e0d63 --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/RenderInitProcs.js @@ -0,0 +1,263 @@ +/* +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + +renderInitProcDefault = __renderInitProcDefault; +renderInitScreenQuad = __renderInitScreenQuad; +renderInitProcDepthMap = __renderInitProcDepthMap; +renderInitShadowReceiver= __renderInitShadowReceiver; +renderInitShadowProjection = __renderInitShadowProjection; + +function __renderInitProcDefault(primSet, vertexData) +{ + var material = primSet.material; + + //push envMap tex + material.tex.env.push(arrayPeek(material.shader).envMap); + material.tex.envDiff.push(arrayPeek(material.shader).envDiff); + + gl.useProgram(arrayPeek(material.shader).shaderHandle); + + arrayPeek(material.renderObj).addTexture("layerMap1", 0, UNIFORMTYPE.TEXTURE2D); + arrayPeek(material.renderObj).addTexture("layerMap2", 1, UNIFORMTYPE.TEXTURE2D); + arrayPeek(material.renderObj).addTexture("colorMeMap1", 2, UNIFORMTYPE.TEXTURE2D); + arrayPeek(material.renderObj).addTexture("colorMeMap2", 3, UNIFORMTYPE.TEXTURE2D); + arrayPeek(material.renderObj).addTexture("envMap", 4, UNIFORMTYPE.TEXTURE2D); + arrayPeek(material.renderObj).addTexture("envDiff", 5, UNIFORMTYPE.TEXTURE2D); + + arrayPeek(material.renderObj).addTexture("normalMap1", 15, UNIFORMTYPE.TEXTURE2D); + arrayPeek(material.renderObj).addTexture("normalMap2", 6, UNIFORMTYPE.TEXTURE2D); + + arrayPeek(material.renderObj).addTexture("stickerMap0", 7, UNIFORMTYPE.TEXTURE2D); + arrayPeek(material.renderObj).addTexture("stickerMap1", 8, UNIFORMTYPE.TEXTURE2D); + arrayPeek(material.renderObj).addTexture("stickerMap2", 9, UNIFORMTYPE.TEXTURE2D); + arrayPeek(material.renderObj).addTexture("stickerMap3", 10, UNIFORMTYPE.TEXTURE2D); + arrayPeek(material.renderObj).addTexture("stickerMap4", 11, UNIFORMTYPE.TEXTURE2D); + arrayPeek(material.renderObj).addTexture("stickerMap5", 12, UNIFORMTYPE.TEXTURE2D); + arrayPeek(material.renderObj).addTexture("stickerMap6", 13, UNIFORMTYPE.TEXTURE2D); + arrayPeek(material.renderObj).addTexture("stickerMap7", 14, UNIFORMTYPE.TEXTURE2D); + + arrayPeek(material.renderObj).addUniform("u_normalMatrix", gl.normalMatrix, UNIFORMTYPE.MATRIX4); + arrayPeek(material.renderObj).addUniform("u_mvMatrix", gl.mvMatrix, UNIFORMTYPE.MATRIX4); + arrayPeek(material.renderObj).addUniform("u_invMvMatrix", gl.invMvMatrix, UNIFORMTYPE.MATRIX4); + + arrayPeek(material.renderObj).addUniform("u_stickerMatrix0", primSet.parentMesh.stickers[0], UNIFORMTYPE.MATRIX4); + arrayPeek(material.renderObj).addUniform("u_stickerMatrix1", primSet.parentMesh.stickers[1], UNIFORMTYPE.MATRIX4); + arrayPeek(material.renderObj).addUniform("u_stickerMatrix2", primSet.parentMesh.stickers[2], UNIFORMTYPE.MATRIX4); + arrayPeek(material.renderObj).addUniform("u_stickerMatrix3", primSet.parentMesh.stickers[3], UNIFORMTYPE.MATRIX4); + arrayPeek(material.renderObj).addUniform("u_stickerMatrix4", primSet.parentMesh.stickers[4], UNIFORMTYPE.MATRIX4); + arrayPeek(material.renderObj).addUniform("u_stickerMatrix5", primSet.parentMesh.stickers[5], UNIFORMTYPE.MATRIX4); + arrayPeek(material.renderObj).addUniform("u_stickerMatrix6", primSet.parentMesh.stickers[6], UNIFORMTYPE.MATRIX4); + arrayPeek(material.renderObj).addUniform("u_stickerMatrix7", primSet.parentMesh.stickers[7], UNIFORMTYPE.MATRIX4); + + arrayPeek(material.renderObj).addUniform("u_stickerPos0", primSet.parentMesh.stickersPos[0], UNIFORMTYPE.FLOAT3); + arrayPeek(material.renderObj).addUniform("u_stickerPos1", primSet.parentMesh.stickersPos[1], UNIFORMTYPE.FLOAT3); + arrayPeek(material.renderObj).addUniform("u_stickerPos2", primSet.parentMesh.stickersPos[2], UNIFORMTYPE.FLOAT3); + arrayPeek(material.renderObj).addUniform("u_stickerPos3", primSet.parentMesh.stickersPos[3], UNIFORMTYPE.FLOAT3); + arrayPeek(material.renderObj).addUniform("u_stickerPos4", primSet.parentMesh.stickersPos[4], UNIFORMTYPE.FLOAT3); + arrayPeek(material.renderObj).addUniform("u_stickerPos5", primSet.parentMesh.stickersPos[5], UNIFORMTYPE.FLOAT3); + arrayPeek(material.renderObj).addUniform("u_stickerPos6", primSet.parentMesh.stickersPos[6], UNIFORMTYPE.FLOAT3); + arrayPeek(material.renderObj).addUniform("u_stickerPos7", primSet.parentMesh.stickersPos[7], UNIFORMTYPE.FLOAT3); + + + arrayPeek(material.renderObj).addUniform("u_projMatrix", gl.perspectiveMatrix, UNIFORMTYPE.MATRIX4); + + arrayPeek(material.renderObj).addUniform("u_fillColor1", material.fillColor[0], UNIFORMTYPE.FLOAT4); + arrayPeek(material.renderObj).addUniform("u_fillColor2", material.fillColor[1], UNIFORMTYPE.FLOAT4); + arrayPeek(material.renderObj).addUniform("u_skinColor", material.fillColor[2], UNIFORMTYPE.FLOAT4); + + + // debug--- + vertexData.vertexObject.name = "vertexObject"; + vertexData.normalObject.name = "normalObject"; + vertexData.texCoordObject.name = "texCoordObject"; + vertexData.indexObject.name = "indexObject"; + //---------- + + arrayPeek(material.renderObj).addBuffers(vertexData.vertexObject, gl.ARRAY_BUFFER, 3, 0, gl.FLOAT); + arrayPeek(material.renderObj).addBuffers(vertexData.normalObject, gl.ARRAY_BUFFER, 3, 1, gl.FLOAT); + arrayPeek(material.renderObj).addBuffers(vertexData.texCoordObject, gl.ARRAY_BUFFER, 2, 2, gl.FLOAT); + arrayPeek(material.renderObj).addBuffers(vertexData.indexObject, gl.ELEMENT_ARRAY_BUFFER); + + + //} + + gl.useProgram(null); +// gl.console.log("Mesh Init done!"); + +} + +function __renderInitScreenQuad(quad, shader) +{ + if(shader == undefined) + { + quad.shader = createShader(gl, 'screenQuad_vShader', 'screenQuad_fShader', [ "vert", "texcoord"]); + } + else + { + quad.shader = shader; + } + + quad.renderObj = new RenderObject(quad.shader); + + quadBuf = getScreenAlignedQuad(); + + quad.vertBuffer = quadBuf.vertexObject; + quad.uvBuffer = quadBuf.texCoordObject; + + + quad.renderObj.addTexture("basemap", 0, UNIFORMTYPE.TEXTURE2D); + + var invWidth = (g_width == 0.0) ? 0.0 : 1.0/g_width, invHeight = (g_height == 0.0) ? 0.0 : 1.0/g_height; + quad.renderObj.addUniform("u_inv_viewport_width", invWidth, UNIFORMTYPE.FLOAT); + quad.renderObj.addUniform("u_inv_viewport_height", invHeight, UNIFORMTYPE.FLOAT); + + quad.renderObj.addBuffers(quad.vertBuffer, gl.ARRAY_BUFFER, 3, 0, gl.FLOAT); + quad.renderObj.addBuffers(quad.uvBuffer, gl.ARRAY_BUFFER, 2, 2, gl.FLOAT); +} + +function __renderInitProcDepthMap(renderObj) +{ + renderObj.shader = g_depthShader.shaderHandle;//createShader(gl, depthMapVShader, depthMapFShader, [ "vert", "normal", "texcoord"]); + + gl.useProgram(renderObj.shader); + + renderObj.addUniform("u_mvpLightMatrix", g_mainLight.mvpMatrix, UNIFORMTYPE.MATRIX4); + //renderObj.addUniform("u_mvpLightMatrixWTF", g_mainLight.mvpMatrix, UNIFORMTYPE.MATRIX4); + // var uni = gl.getUniformLocation(renderObj.shader, "u_mvpLightMatrixWTF"); +// renderObj.addUniform("u_WTF1", g_lightMat[0], UNIFORMTYPE.FLOAT4); +// renderObj.addUniform("u_WTF2", g_lightMat[1], UNIFORMTYPE.FLOAT4); +// renderObj.addUniform("u_WTF3", g_lightMat[2], UNIFORMTYPE.FLOAT4); +// renderObj.addUniform("u_WTF4", g_lightMat[3], UNIFORMTYPE.FLOAT4); +// + // since the uniform data references should not change we can just bind one time + renderObj.bindUniforms(); + + gl.useProgram(null); +} + +function __renderInitShadowReceiver(primSet, vertexData) +{ + // setup passes + primSet.shadowTarget = g_texMan.loadRenderTarget("shadowTarget", 256, 256); + primSet.shadowTargetFinal = g_texMan.loadRenderTarget("shadowTargetFinal", 256, 256); + primSet.screenQuad = new ScreenQuad(primSet.shadowTargetFinal); + primSet.screenQuad.initialize(__renderInitRadialBlur); + + // set the target as the shadow to get projcetd + primSet.parentMesh.shadowToProject = primSet.shadowTarget; + + //mainSceneQuad = new ScreenQuad(primSet.shadowTarget); + //mainSceneQuad.initialize(renderInitScreenQuad); + + var material = primSet.material; + + //push envMap tex + material.tex.env.push(arrayPeek(material.shader).envMap); + material.tex.envDiff.push(arrayPeek(material.shader).envDiff); + + gl.useProgram(arrayPeek(material.shader).shaderHandle); + + arrayPeek(material.renderObj).addTexture("shadowMap", 0, UNIFORMTYPE.TEXTURE2D); + arrayPeek(material.renderObj).addUniform("u_mvMatrix", gl.mvMatrix, UNIFORMTYPE.MATRIX4); + + arrayPeek(material.renderObj).addUniform("u_projMatrix", gl.perspectiveMatrix, UNIFORMTYPE.MATRIX4); + + arrayPeek(material.renderObj).addUniform("u_shadowBiasMatrix", g_mainLight.shadowMatrix, UNIFORMTYPE.MATRIX4); + arrayPeek(material.renderObj).addUniform("u_vShadowLight", g_mainLight.view, UNIFORMTYPE.MATRIX4); + + + // debug--- + vertexData.vertexObject.name = "vertexObject"; + vertexData.normalObject.name = "normalObject"; + vertexData.texCoordObject.name = "texCoordObject"; + vertexData.indexObject.name = "indexObject"; + //---------- + + arrayPeek(material.renderObj).addBuffers(vertexData.vertexObject, gl.ARRAY_BUFFER, 3, 0, gl.FLOAT); + arrayPeek(material.renderObj).addBuffers(vertexData.normalObject, gl.ARRAY_BUFFER, 3, 1, gl.FLOAT); + arrayPeek(material.renderObj).addBuffers(vertexData.texCoordObject, gl.ARRAY_BUFFER, 2, 2, gl.FLOAT); + arrayPeek(material.renderObj).addBuffers(vertexData.indexObject, gl.ELEMENT_ARRAY_BUFFER); + + + //} + + gl.useProgram(null); +// gl.console.log("Mesh Init done!"); + + //__renderInitShadowProjection(primSet, vertexData); + +} + + +function __renderInitShadowProjection(primSet, vertexData) +{ + var material = primSet.material; + + //push envMap tex + //material.tex.env.push(arrayPeek(material.shader).envMap); + //material.tex.envDiff.push(arrayPeek(material.shader).envDiff); + + gl.useProgram(arrayPeek(material.shader).shaderHandle); + + arrayPeek(material.renderObj).addTexture("shadowMap", 0, UNIFORMTYPE.TEXTURE2D); + arrayPeek(material.renderObj).addUniform("u_mvMatrix", gl.mvMatrix, UNIFORMTYPE.MATRIX4); + + arrayPeek(material.renderObj).addUniform("u_projMatrix", gl.perspectiveMatrix, UNIFORMTYPE.MATRIX4); + + arrayPeek(material.renderObj).addUniform("u_shadowBiasMatrix", g_mainLight.shadowMatrix, UNIFORMTYPE.MATRIX4); + arrayPeek(material.renderObj).addUniform("u_vShadowLight", g_mainLight.view, UNIFORMTYPE.MATRIX4); + + + // debug--- + vertexData.vertexObject.name = "vertexObject"; + vertexData.normalObject.name = "normalObject"; + vertexData.texCoordObject.name = "texCoordObject"; + vertexData.indexObject.name = "indexObject"; + //---------- + + arrayPeek(material.renderObj).addBuffers(vertexData.vertexObject, gl.ARRAY_BUFFER, 3, 0, gl.FLOAT); + arrayPeek(material.renderObj).addBuffers(vertexData.normalObject, gl.ARRAY_BUFFER, 3, 1, gl.FLOAT); + arrayPeek(material.renderObj).addBuffers(vertexData.texCoordObject, gl.ARRAY_BUFFER, 2, 2, gl.FLOAT); + arrayPeek(material.renderObj).addBuffers(vertexData.indexObject, gl.ELEMENT_ARRAY_BUFFER); + + + //} + + gl.useProgram(null); +// gl.console.log("Mesh Init done!"); + +} + +function __renderInitRadialBlur(quad, shader) +{ + if(shader == undefined) + { + quad.shader = createShader(gl, 'radialBlur_vshader', 'radialBlur_fshader', [ "vert", "texcoord"]); + } + else + { + quad.shader = shader; + } + + quad.renderObj = new RenderObject(quad.shader); + + quadBuf = getScreenAlignedQuad(); + + quad.vertBuffer = quadBuf.vertexObject; + quad.uvBuffer = quadBuf.texCoordObject; + + + quad.renderObj.addTexture("basemap", 0, UNIFORMTYPE.TEXTURE2D); + + quad.renderObj.addUniform("u_inv_viewport_width", 1.0/g_width, UNIFORMTYPE.FLOAT); + quad.renderObj.addUniform("u_inv_viewport_height", 1.0/g_height, UNIFORMTYPE.FLOAT); + quad.renderObj.addUniform("u_sampRadius", 5.0, UNIFORMTYPE.FLOAT); + quad.renderObj.addUniform("u_numSamples", 16, UNIFORMTYPE.INT); + quad.renderObj.addUniform("u_mapSize", 256.0, UNIFORMTYPE.FLOAT); + + quad.renderObj.addBuffers(quad.vertBuffer, gl.ARRAY_BUFFER, 3, 0, gl.FLOAT); + quad.renderObj.addBuffers(quad.uvBuffer, gl.ARRAY_BUFFER, 2, 2, gl.FLOAT); +} diff --git a/js/helper-classes/RDGE/src/core/script/RenderProcs.js b/js/helper-classes/RDGE/src/core/script/RenderProcs.js new file mode 100644 index 00000000..6d3b02df --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/RenderProcs.js @@ -0,0 +1,536 @@ +/* +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + +/** + * supported uniform types + */ +function __UNIFORMTYPE() +{ + this.INT = 0x3F0; + this.FLOAT = 0x3E8; + this.FLOAT2 = 0x3E9; + this.FLOAT3 = 0x3EA; + this.FLOAT4 = 0x3EB; + this.MATRIX3 = 0x3EC; + this.MATRIX4 = 0x3ED; + this.TEXTURE2D = 0x3EE; + this.TEXTURECUBE = 0x3EF; +} +UNIFORMTYPE = new __UNIFORMTYPE(); + +/** + * RenderObject - contains references to all the data need to render, including vertex buffers, uniform handles, and matrices + * @param shaderHandle + */ +function RenderObject(shaderHandle) +{ + this.shader = shaderHandle; + this.world = null; + this.bindings = new ShaderData(); + this.initRenderProc = null; + this.renderProc = null; + this.postRenderProc = null; +} + +/** + * Adds a uniform to the render object to bound during render + * @param name - name of the uniform + * @param value - reference to value that will get bound (will be referenced from now on, don't delete the ref) + * @param type - type of uniform, use UNIFORMTYPE + */ +RenderObject.prototype.addUniform = function(name, value, type) +{ + var uniform = gl.getUniformLocation(this.shader, name); + if(uniform) + { + uniform.debugName = name; + this.bindings.uniforms.push( new UniformPair(uniform, value, type)); + } +/* + else + { + gl.console.log("ERROR: uniform - " + name + " not found!"); + } +*/ +}; + +/** +* Adds a uniform to the render object to bound during render +* @param name - name of the uniform +* @param value - reference to value that will get bound (will be referenced from now on, don't delete the ref) +* @param type - type of uniform, use UNIFORMTYPE +*/ +RenderObject.prototype.addUniformArray = function(name, value, type, size) +{ + var uniform = gl.getUniformLocation(this.shader, name); + if (uniform) + { + for (var index=0; index +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. + */ + + +function ScreenQuad(texture) +{ + this.vertBuffer = null; + this.uvBuffer = null; + this.texture = texture; + this.shader = null; + this.renderObj = null; +} + +ScreenQuad.prototype.initialize = function(initProc, shaderOpt) +{ + initProc(this, shaderOpt); +}; + +ScreenQuad.prototype.setTexture = function(texture) +{ + this.texture = texture; +}; + + +ScreenQuad.prototype.render = function(renderProc) +{ + renderProc(this); +}; \ No newline at end of file diff --git a/js/helper-classes/RDGE/src/core/script/ShaderManager.js b/js/helper-classes/RDGE/src/core/script/ShaderManager.js new file mode 100644 index 00000000..f253d3c7 --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/ShaderManager.js @@ -0,0 +1,96 @@ +/* +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + +function Shader(name, shaderHandle, renderProc, initRenderProc, envMap) +{ + this.name = name; + this.shaderHandle = shaderHandle; + this.initRenderProc = initRenderProc; + this.renderProc = renderProc; + this.postRenderProc = postRenderProcDefault; + this.envMap = envMap; + this.envDiff = envMap; +} + +function ShaderManager() +{ + this.shaderMap = []; +} + +ShaderManager.prototype.addShader = function(name, vShader, fShader, attribs, renderProc, initRenderProc, envMapName, envDiffMapName) +{ + var shader = this.shaderMap[name]; + if(shader == undefined) + { + + var handle = createShader(g_Engine.getContext().renderer.ctx, vShader, fShader, attribs); + if (envMapName != undefined || envDiffMapName != undefined) + { + var envMap = g_texMan.loadMaterial(envMapName); + var envDiff = g_texMan.loadMaterial(envDiffMapName); + this.shaderMap[name] = new Shader(name, handle, renderProc, initRenderProc, envMap); + this.shaderMap[name].envDiff = envDiff; + } + else + { + this.shaderMap[name] = new Shader(name, handle, renderProc, initRenderProc, null); + this.shaderMap[name].envDiff = null; + } + + this.shaderMap[name].name = name; + + return this.shaderMap[name]; + } + + return shader; +} + +ShaderManager.prototype.getShaderNames = function() +{ + var names = []; + for(var index in this.shaderMap) + { + names.push(this.shaderMap[index].name); + } + + return names; +} + +ShaderManager.prototype.getShaderByName = function(name) +{ + var shader = this.shaderMap[name]; + + if(shader != undefined && shader != null) + return shader; + +// gl.console.log("No shader by the name of: \"" + name + "\""); + + return null; +} + + +/** +* Setup shader names +**/ +ShaderManager.prototype.init = function() +{ + // create shaders for each look + this.addShader( "default", vortexVShader, vortexFShader, ["vert", "normal", "texcoord"], renderProcDefault, renderInitProcDefault, "material_DullPlastic.png", "material_DullPlastic.png" ); + this.addShader( "barlights",vortexVShader, vortexFShader, ["vert", "normal", "texcoord"], renderProcDefault, renderInitProcDefault, "material_barlights.png", "material_barlightsDull.png"); + this.addShader( "gloss", vortexVShader, vortexFShader, ["vert", "normal", "texcoord"], renderProcDefault, renderInitProcDefault, "material_gloss.png", "material_glossDull.png" ); + this.addShader( "inGlass", vortexVShader, vortexFShader, ["vert", "normal", "texcoord"], renderProcDefault, renderInitProcDefault, "material_inGlass.png", "material_inGlassDull.png"); + this.addShader( "normals", vortexVShader, vortexFShader, ["vert", "normal", "texcoord"], renderProcDefault, renderInitProcDefault, "material_normal.png", "material_normalDull.png" ); + this.addShader( "paint", vortexVShader, vortexFShader, ["vert", "normal", "texcoord"], renderProcDefault, renderInitProcDefault, "material_paint.png", "material_paintDull.png" ); + this.addShader( "plastic", vortexVShader, vortexFShader, ["vert", "normal", "texcoord"], renderProcDefault, renderInitProcDefault, "material_plastic.png", "material_plasticDull.png" ); + this.addShader( "shadows", vortexVShader, vortexFShader, ["vert", "normal", "texcoord"], renderProcDefault, renderInitProcDefault, "material_shadows.png", "material_shadowsDull.png" ); + this.addShader( "skin", vortexVShader, vortexFShader, ["vert", "normal", "texcoord"], renderProcDefault, renderInitProcDefault, "material_skin.png", "material_skinDull.png" ); + this.addShader( "wax", vortexVShader, vortexFShader, ["vert", "normal", "texcoord"], renderProcDefault, renderInitProcDefault, "material_wax.png", "material_waxDull.png" ); + + // used by backdrop + this.addShader("shadowReceiver",shadow_vshader, shadow_fshader, ["vert", "normal", "texcoord"], renderProcShadowReceiver, renderInitShadowReceiver); + this.addShader("shadowProj", shadowProj_vshader, shadowProj_fshader, [ "vert", "normal", "texcoord"], renderProcShadowProjection, renderInitShadowProjection); + +} diff --git a/js/helper-classes/RDGE/src/core/script/TextureManager.js b/js/helper-classes/RDGE/src/core/script/TextureManager.js new file mode 100644 index 00000000..cb674ddf --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/TextureManager.js @@ -0,0 +1,6 @@ +/* +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + diff --git a/js/helper-classes/RDGE/src/core/script/animation.js b/js/helper-classes/RDGE/src/core/script/animation.js new file mode 100644 index 00000000..63eca0a2 --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/animation.js @@ -0,0 +1,322 @@ +/* +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + +var g_numChannels = new stat( "animation", "numChannels", 0, null, false ); +var g_numTracks = new stat( "animation", "numTracks", 0, null, false ); + +/** + * channelController + * The channel controller is really the workhorse of the RDGE animation system. It handles timing, + * interpolation, and sampling of attributes over the lifetime of an animation. Each channel controller + * is responsible for animating a single attribute. The current implementation supports animating vector, + * boolean, or quaternion attributes. This class is used internally by the animation system. + * + * @param _animation - the animation resource + * @param _channel - the channel id + * + */ +channelController = function(_animation, _channel) { + /** + * this.interpolate - Enable/Disable interpolation between animation frames. + * Typically this should be enabled for smoother looking animation. However, + * there may be applications where interpolation is undesireable. + */ + this.interpolate = false; + + /** + * this.animation - the animation resource. + * This is where the keyframes for the channel are stored. + */ + this.animation = _animation; + + /** + * this.channel - the channel id. This is used to look up the keyframe data for this channel. + */ + this.channel = _channel; + + /** + * this.localTime - the current time, relative to the start time. + */ + this.localTime = 0.0; + + /** + * this.startTime - the start time of the animation clip window. + */ + this.startTime = this.animation.clipStart / this.animation.framesPerSec; + + /** + * this.endTime - the end time of the animation clip window. + */ + this.endTime = this.animation.clipEnd / this.animation.framesPerSec; + + /** + * this.cachedFrame - cached frame index, this optimizes best case scenario computeFrame calls. + */ + this.cachedFrame = -1; + + /** + * oneFrameInSecs - stores the interval of a single frame in seconds. This is used for internal calculations. + */ + oneFrameInSecs = 1.0 / _animation.framesPerSec; + + /** + * this.channel.timeline - stores the animation timeline. + * Currently this is calculated based on the framePerSec settings of the animation. + * Eventually the timeline should be exported with the animation. Individual channels + * may have different timelines depending on which frames are keyed. + */ + this.channel.timeline = new Array(this.channel.numKeys + 1); + for (i = 0; i <= this.channel.numKeys; ++i) { + this.channel.timeline[i] = i / this.animation.framesPerSec; + } + + /** this.computeFrame + * Calculates the current frame index of the animation at the current time. + * In the worst case, this function will perform a binary search for the frame + * whose time is closest to and less than the current time. In the best case, + * the current frame is near the most recently cached frame, or it remains unchanged. + */ + this.computeFrame = function() { + var absTime = this.localTime + this.startTime; + var start = this.animation.clipStart; + var end = this.animation.clipEnd; + + if (this.cachedFrame != -1) { + // if the current time is reasonably close to the last frame processed try searching + // forward or backward. best case it is the next frame. + if (Math.abs(absTime - this.channel.timeline[this.cachedFrame]) < 5 * oneFrameInSecs) { + if (this.channel.timeline[this.cachedFrame] < absTime) { + while (this.channel.timeline[this.cachedFrame + 1] <= absTime) { + this.cachedFrame++; + if (this.animation.looping) { + if (this.cachedFrame > this.numFrames - 1) { + this.cachedFrame -= this.numFrames - 1; + } + } else { + if (this.cachedFrame > this.numFrames - 1) { + this.cachedFrame = this.numFrames - 1; + } + } + } + return this.cachedFrame; + + } else { + while (this.channel.timeline[this.cachedFrame] > absTime) { + this.cachedFrame--; + if (this.animation.looping) { + if (this.cachedFrame < 0) { + this.cachedFrame += this.numFrames - 1; + } + } else { + if (this.cachedFrame > this.numFrames - 1) { + this.cachedFrame = this.numFrames - 1; + } + } + } + return this.cachedFrame; + } + } + } + + // binary search... + while (start + 1 < end) { + var mid = Math.floor((start + end) / 2); + if (absTime > this.channel.timeline[mid]) { + start = mid + 1; + } else { + end = mid - 1; + } + } + + this.cachedFrame = start; + + return start; + } + + /* this.sampleBool - Sample a boolean at the current frame, booleans are not interpolated. + * This function is used internally. + */ + this.sampleBool = function() { + // no interpolation on flags... + var index = this.computeFrame(); + return this.channel.keys[index]; + } + + /* this.sampleQuat - Sample a quaternion at the current frame. + * if this.interpolate == true, quaternions are interpolated inbetween frames using spherical linear interpolation (SLERP). + */ + this.sampleQuat = function() { + var frame0 = this.computeFrame(); + var frame1 = frame0 + 1; + + var k0 = this.channel.timeline[frame0]; + var k1 = this.channel.timeline[frame1]; + var index0 = frame0 * 4; + var index1 = frame1 * 4; + + if (this.interpolate) { + var absTime = this.localTime + this.startTime; + var t = (absTime - k0) / (k1 - k0); + var a = [this.channel.keys[index0 + 0], this.channel.keys[index0 + 1], this.channel.keys[index0 + 2], this.channel.keys[index0 + 3]]; + var b = [this.channel.keys[index1 + 0], this.channel.keys[index1 + 1], this.channel.keys[index1 + 2], this.channel.keys[index1 + 3]]; + return quat.slerp(a, b, t); + } + + return [this.channel.keys[index0 + 0], this.channel.keys[index0 + 1], this.channel.keys[index0 + 2], this.channel.keys[index0 + 3]]; + } + + /* this.sampleVec3 - Sample a vector3 at the current frame. + * if this.interpolate == true, vectors are interpolated inbetween frames using linear interpolation (LERP). + */ + this.sampleVec3 = function() { + var frame0 = this.computeFrame(); + var frame1 = frame0 + 1; + + var k0 = this.channel.timeline[frame0]; + var k1 = this.channel.timeline[frame1]; + var index0 = frame0 * 3; + var index1 = frame1 * 3; + + if (this.interpolate) { + var absTime = this.localTime + this.startTime; + var t = (absTime - k0) / (k1 - k0); + var a = [this.channel.keys[index0 + 0], this.channel.keys[index0 + 1], this.channel.keys[index0 + 2]]; + var b = [this.channel.keys[index1 + 0], this.channel.keys[index1 + 1], this.channel.keys[index1 + 2]]; + + return vec3.lerp(a, b, t); + } + return [this.channel.keys[index0 + 0], this.channel.keys[index0 + 1], this.channel.keys[index0 + 2]]; + } + + /* this.setTime - set the current time. + */ + this.setTime = function(t) { + this.localTime = t; + if (this.localTime < 0.0) { + this.localTime = 0.0; + } + if (this.localTime > this.animation.duration - oneFrameInSecs) { + this.localTime = this.animation.duration - oneFrameInSecs; + } + } + + /* this.setProgress - set the current time as a percentage of the duration. + */ + this.setProgress = function(f) { + this.setTime(f * this.animation.duration); + } + + /* this.setFrame - set the current time by frame number. + */ + this.setFrame = function(f) { + this.setTime(f / this.animation.framesPerSec); + } + + /* this.step - advance time by the given timestep and wrap if looping. + */ + this.step = function(_dt) { + this.localTime += _dt; + if (this.animation.looping) { + while (this.localTime < 0.0) { + this.localTime += this.animation.duration - oneFrameInSecs; + } + while (this.localTime >= this.animation.duration - oneFrameInSecs) { + this.localTime -= this.animation.duration - oneFrameInSecs; + } + } else { + if (this.localTime < 0.0) { + this.localTime = 0.0; + } + if (this.localTime > this.animation.duration) { + this.localTime = this.animation.duration; + } + } + } +} + +/** + * track + * Each track advances and samples from a list of channel controllers, and is assigned to a scene graph node. + * + * @param _animation - the animation resource + * @param _track - the track id + * @param _node - the scene node + * + */ +track = function(_animation, _track, _node) { + this.track = _track; + this.node = _node; + + this.channelControllers = new Array(); + for (ch in _track) { + this.channelControllers[ch] = new channelController(_animation, _track[ch]); + g_numChannels.value++; + } + + this.step = function(_dt) { + for (cc in this.channelControllers) { + this.channelControllers[cc].step(_dt); + } + var rotate = this.channelControllers["rotate"].sampleQuat(); + var scale = this.channelControllers["scale"].sampleVec3(); + var translate = this.channelControllers["translate"].sampleVec3(); + if (this.channelControllers["vis"] != null) { + var vis = this.channelControllers["vis"].sampleBool(); + this.node.hide = !vis; + } + + var m = mat4.identity(); + m = mat4.scale(m, scale); + m = mat4.mul(m, quat.toMatrix(rotate)); + m = mat4.translate(m, translate); + this.node.local = m; + } +} + +animation = function(_scene, _clipStart, _clipEnd, _loop) { + this.animation = _scene.scene.animation; + this.clipStart = _clipStart; + // this is a little hacky, but it works for now. + if (_clipEnd == -1) { + for (tr in this.animation) { + for (ch in this.animation[tr]) { + _clipEnd = this.animation[tr][ch].numKeys - 1; + break; + } + break; + } + } + this.clipEnd = _clipEnd; + this.numFrames = _clipEnd - _clipStart; + this.framesPerSec = 30.0; + this.duration = this.numFrames / this.framesPerSec; + this.rate = 1.0; + this.looping = _loop; + this.tracks = new Array(); + + // creating a mapping here to make binding tracks a little more + // straightforward. + mapping = new Array(); + mapping.process = function(trNode, parent) { + mapping[trNode.name] = trNode; + } + + g_Engine.getContext().getScene().Traverse(mapping); + //g_sg.Traverse(mapping); + + for (tr in this.animation) { + if (mapping[tr] !== undefined) { + this.tracks.push(new track(this, this.animation[tr], mapping[tr])); + g_numTracks.value++; + } + } + + this.step = function(_dt) { + for (tr in this.tracks) { + this.tracks[tr].step(g_animationRate * this.rate * _dt); + } + } +} \ No newline at end of file diff --git a/js/helper-classes/RDGE/src/core/script/box.js b/js/helper-classes/RDGE/src/core/script/box.js new file mode 100644 index 00000000..8272d952 --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/box.js @@ -0,0 +1,143 @@ +/* +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + +MAX_VAL = 1e+38; + +function box() +{ + this.min = [MAX_VAL, MAX_VAL, MAX_VAL]; + this.max = [-MAX_VAL, -MAX_VAL, -MAX_VAL]; +} + +box.prototype.addBox = function(a) +{ + this.min = vec3.min( this.min, a.min ); + this.max = vec3.