Fork me on GitHub

API Documentation

  1 /*
  2 GLGE WebGL Graphics Engine
  3 Copyright (c) 2010, Paul Brunt
  4 All rights reserved.
  5 
  6 Redistribution and use in source and binary forms, with or without
  7 modification, are permitted provided that the following conditions are met:
  8     * Redistributions of source code must retain the above copyright
  9       notice, this list of conditions and the following disclaimer.
 10     * Redistributions in binary form must reproduce the above copyright
 11       notice, this list of conditions and the following disclaimer in the
 12       documentation and/or other materials provided with the distribution.
 13     * Neither the name of GLGE nor the
 14       names of its contributors may be used to endorse or promote products
 15       derived from this software without specific prior written permission.
 16 
 17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 20 DISCLAIMED. IN NO EVENT SHALL PAUL BRUNT BE LIABLE FOR ANY
 21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 27 */
 28 
 29 /**
 30  * @fileOverview
 31  * @name glge_physicsmesh.js
 32  * @author me@paulbrunt.co.uk
 33  */
 34 
 35 (function(GLGE){
 36 
 37 /**
 38 * @class A wrapping class for jiglib triangle mesh
 39 * @augments GLGE.PhysicsAbstract
 40 */
 41 GLGE.PhysicsMesh=function(uid){
 42 	this.jigLibObj=new jigLib.JTriangleMesh(null, 100, 0.1);
 43  44 	this.jigLibObj.GLGE=this;
 45 	this.jigLibObj.addEventListener(jigLib.JCollisionEvent.COLLISION, function(event){this.GLGE.fireEvent("collision",{obj:event.collisionBody.GLGE,impulse:event.collisionImpulse})});
 46 	this.dirty=true;
 47 	this.addEventListener("matrixUpdate",this.makeDirty);
 48 	this.addEventListener("childMatrixUpdate",this.makeDirty);
 49 	this.addEventListener("childAdded",this.makeDirty);
 50 	this.addEventListener("childRemoved",this.makeDirty);
 51 	
 52 	GLGE.PhysicsAbstract.call(this,uid);
 53 }
 54 GLGE.augment(GLGE.PhysicsAbstract,GLGE.PhysicsMesh);
 55 
 56 
 57 GLGE.PhysicsMesh.prototype.className="PhysicsMesh";
 58 /**
 59 * Forces and update of the triangle mesh
 60 */
 61 GLGE.PhysicsMesh.prototype.forceUpdate=function(){
 62 	this.dirty=true;
 63 	return this;
 64 }
 65 
 66 /**
 67 * flag to regenerate trimesh and redo octtree
 68 * @private
 69 */
 70 GLGE.PhysicsMesh.prototype.makeDirty=function(e){
 71 	this.dirty=true;
 72 }
 73 /**
 74 * called before a system intergrate
 75 * @private
 76 */
 77 GLGE.PhysicsMesh.prototype.preProcess=function(){
 78 	//recreate mesh and build octree
 79 	if(this.dirty){
 80 		var triangles=this.getTriangles();
 81 		this.jigLibObj.createMesh(triangles.verts, triangles.faces);
 82 		this.dirty=false;
 83 	}
 84 }
 85 /**
 86 * Creates the jiglib triangle arrays from the containing objects
 87 * @private
 88 */
 89 GLGE.PhysicsMesh.prototype.getTriangles=function(){
 90 	var objs=this.getObjects();
 91 	var verts=[];
 92 	var faces=[];
 93 	for(var i=0;i<objs.length;i++){
 94 		if(objs[i].multimaterials){
 95 			var matrix=objs[i].getModelMatrix();
 96 			for(var j=0;j<objs[i].multimaterials.length;j++){
 97 				var mesh=objs[i].multimaterials[j].getMesh();
 98 				var vertcnt=verts.length;
 99 				if(mesh){
100 					for(var k=0;k<mesh.positions.length;k=k+3){
101 						var vert=[mesh.positions[k],mesh.positions[k+1],mesh.positions[k+2],1];
102 						var v=GLGE.mulMat4Vec4(matrix,vert);
103 						verts.push([v[0],v[1],v[2],1]);
104 					}
105 					var mfaces=mesh.faces.data
106 					if(mfaces){
107 						var len=mfaces.length;
108 						len=((len/3)|0)*3;
109 						for(var k=0;k<len;k=k+3){
110 							faces.push([+mfaces[k]+vertcnt,+mfaces[k+1]+vertcnt,+mfaces[k+2]+vertcnt]);
111 						}
112 					}else{
113 						for(var k=0;k<mesh.positions.length/3;k=k+3){
114 							faces.push([k+vertcnt,k+1+vertcnt,k+2+vertcnt]);
115 						}
116 					}
117 				}
118 			}
119 		}
120 	}
121 	
122 	return {verts:verts,faces:faces};
123 }
124 
125 
126 })(GLGE);