aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/runtime/RuntimeGeomObj.js
diff options
context:
space:
mode:
authorhwc4872012-03-07 16:48:48 -0800
committerhwc4872012-03-07 16:48:48 -0800
commit818582d389f504c915be0c9052fafa33e3e76c92 (patch)
treeff686f52903d91f27f983b19e18c9909af5957d9 /js/lib/rdge/runtime/RuntimeGeomObj.js
parent855e8727b147771ff7b05e71bed481e65fe4b6b0 (diff)
downloadninja-818582d389f504c915be0c9052fafa33e3e76c92.tar.gz
File IO
Diffstat (limited to 'js/lib/rdge/runtime/RuntimeGeomObj.js')
-rw-r--r--js/lib/rdge/runtime/RuntimeGeomObj.js630
1 files changed, 630 insertions, 0 deletions
diff --git a/js/lib/rdge/runtime/RuntimeGeomObj.js b/js/lib/rdge/runtime/RuntimeGeomObj.js
new file mode 100644
index 00000000..5b212dce
--- /dev/null
+++ b/js/lib/rdge/runtime/RuntimeGeomObj.js
@@ -0,0 +1,630 @@
1
2/* <copyright>
3This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
4No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
5(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
6</copyright> */
7
8var RuntimeMaterial = require("js/lib/rdge/runtime/RuntimeMaterial");
9var RuntimeFlatMaterial = RuntimeMaterial.RuntimeFlatMaterial,
10 RuntimeRadialGradientMaterial = RuntimeMaterial.RuntimeRadialGradientMaterial,
11 RuntimeLinearGradientMaterial = RuntimeMaterial.RuntimeLinearGradientMaterial,
12 RuntimeBumpMetalMaterial = RuntimeMaterial.RuntimeBumpMetalMaterial,
13 RuntimeUberMaterial = RuntimeMaterial.RuntimeUberMaterial,
14 RuntimePulseMaterial = RuntimeMaterial.RuntimePulseMaterial;
15
16
17
18///////////////////////////////////////////////////////////////////////
19// Class RuntimeGeomObj
20// Super class for all geometry classes
21///////////////////////////////////////////////////////////////////////
22var RuntimeGeomObj = function RuntimeGeomObj()
23{
24 ///////////////////////////////////////////////////////////////////////
25 // Constants
26 ///////////////////////////////////////////////////////////////////////
27 this.GEOM_TYPE_RECTANGLE = 1;
28 this.GEOM_TYPE_CIRCLE = 2;
29 this.GEOM_TYPE_LINE = 3;
30 this.GEOM_TYPE_PATH = 4;
31 this.GEOM_TYPE_CUBIC_BEZIER = 5;
32 this.GEOM_TYPE_UNDEFINED = -1;
33
34 ///////////////////////////////////////////////////////////////////////
35 // Instance variables
36 ///////////////////////////////////////////////////////////////////////
37 this._children;
38
39 // stroke and fill colors
40 this._strokeColor = [0,0,0,0];
41 this._fillColor = [0,0,0,0];
42
43 // array of materials
44 this._materials = [];
45
46 ///////////////////////////////////////////////////////////////////////
47 // Property accessors
48 ///////////////////////////////////////////////////////////////////////
49
50 this.geomType = function() { return this.GEOM_TYPE_UNDEFINED; }
51
52 this.setWorld = function(w) { this._world = w; }
53 this.getWorld = function() { return this._world; }
54
55 ///////////////////////////////////////////////////////////////////////
56 // Methods
57 ///////////////////////////////////////////////////////////////////////
58 this.makeStrokeMaterial = function()
59 {
60 }
61
62 this.makeFillMaterial = function()
63 {
64 }
65
66
67 this.render = function()
68 {
69 }
70
71 this.addChild = function( child )
72 {
73 if (!this._children) this._children = [];
74 this._children.push( child );
75 }
76
77 this.import = function()
78 {
79 }
80
81 this.importMaterials = function(importStr)
82 {
83 var nMaterials = Number( getPropertyFromString( "nMaterials: ", importStr ) );
84 for (var i=0; i<nMaterials; i++)
85 {
86 var matNodeName = getPropertyFromString( "materialNodeName: ", importStr );
87
88 var mat;
89 var materialType = getPropertyFromString( "material: ", importStr );
90 switch (materialType)
91 {
92 case "flat": mat = new RuntimeFlatMaterial(); break;
93 case "radialGradient": mat = new RuntimeRadialGradientMaterial(); break;
94 case "linearGradient": mat = new RuntimeLinearGradientMaterial(); break;
95 case "bumpMetal": mat = new RuntimeBumpMetalMaterial(); break;
96 case "uber": mat = new RuntimeUberMaterial(); break;
97
98 case "deform":
99 case "water":
100 case "tunnel":
101 case "reliefTunnel":
102 case "squareTunnel":
103 case "twist":
104 case "fly":
105 case "julia":
106 case "mandel":
107 case "star":
108 case "zinvert":
109 case "keleidoscope":
110 case "pulse": mat = new RuntimePulseMaterial(); break;
111
112 default:
113 console.log( "material type: " + materialType + " is not supported" );
114 break;
115 }
116
117 if (mat)
118 {
119 mat.import( importStr );
120 mat._materialNodeName = matNodeName;
121 this._materials.push( mat );
122 }
123
124 var endIndex = importStr.indexOf( "endMaterial\n" );
125 if (endIndex < 0) break;
126 importStr = importStr.substr( endIndex );
127 }
128 }
129
130 ////////////////////////////////////////////////////////////////////
131 // vector function
132
133 this.vecAdd = function( dimen, a, b )
134 {
135 var rtnVec;
136 if ((a.length < dimen) || (b.length < dimen))
137 {
138 throw new Error( "dimension error in vecAdd" );
139 }
140
141 rtnVec = [];
142 for (var i=0; i<dimen; i++)
143 rtnVec[i] = a[i] + b[i];
144
145 return rtnVec;
146 }
147
148
149 this.vecSubtract = function( dimen, a, b )
150 {
151 var rtnVec;
152 if ((a.length < dimen) || (b.length < dimen))
153 {
154 throw new Error( "dimension error in vecSubtract" );
155 }
156
157 rtnVec = [];
158 for (var i=0; i<dimen; i++)
159 rtnVec[i] = a[i] - b[i];
160
161 return rtnVec;
162 }
163
164 this.vecDot = function( dimen, v0, v1 )
165 {
166 if ((v0.length < dimen) || (v1.length < dimen))
167 {
168 throw new Error( "dimension error in vecDot" );
169 }
170
171 var sum = 0.0;
172 for (var i=0; i<dimen; i++)
173 sum += v0[i] * v1[i];
174
175 return sum;
176 }
177
178 this.vecMag = function( dimen, vec )
179 {
180 var sum = 0.0;
181 for (var i=0; i<dimen; i++)
182 sum += vec[i]*vec[i];
183 return Math.sqrt( sum );
184 }
185
186 this.vecScale = function(dimen, vec, scale)
187 {
188 for (var i=0; i<dimen; i++)
189 vec[i] *= scale;
190
191 return vec;
192 }
193
194 this.vecNormalize = function(dimen, vec, len)
195 {
196 var rtnVec;
197 if (!len) len = 1.0;
198
199 var sum = 0.0;
200 for (var i=0; i<dimen; i++)
201 sum += vec[i]*vec[i];
202 sum = Math.sqrt( sum );
203
204 if (Math.abs(sum) >= 0.001)
205 {
206 var scale = len/sum;
207 rtnVec = [];
208 for (var i=0; i<dimen; i++)
209 rtnVec[i] = vec[i]*scale;
210 }
211
212 return rtnVec;
213 },
214
215 this.transformPoint = function( srcPt, mat )
216 {
217 var pt = srcPt.slice(0);
218 var x = this.vecDot(3, pt, [mat[0], mat[4], mat[ 8]] ) + mat[12],
219 y = this.vecDot(3, pt, [mat[1], mat[5], mat[ 9]] ) + mat[13],
220 z = this.vecDot(3, pt, [mat[2], mat[6], mat[10]] ) + mat[14];
221
222 return [x, y, z];
223 }
224}
225
226var getPropertyFromString = function getPropertyFromString( prop, str )