temp.js 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. // http://ejohn.org/blog/simple-javascript-inheritance/
  2. // Inspired by base2 and Prototype
  3. (function(){
  4. var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
  5. // The base Class implementation (does nothing)
  6. this.Class = function(){};
  7. // Create a new Class that inherits from this class
  8. Class.extend = function(prop) {
  9. var _super = this.prototype;
  10. // Instantiate a base class (but only create the instance,
  11. // don't run the init constructor)
  12. initializing = true;
  13. var prototype = new this();
  14. initializing = false;
  15. // Copy the properties over onto the new prototype
  16. for (var name in prop) {
  17. // Check if we're overwriting an existing function
  18. prototype[name] = typeof prop[name] == "function" &&
  19. typeof _super[name] == "function" && fnTest.test(prop[name]) ?
  20. (function(name, fn){
  21. return function() {
  22. var tmp = this._super;
  23. // Add a new ._super() method that is the same method
  24. // but on the super-class
  25. this._super = _super[name];
  26. // The method only need to be bound temporarily, so we
  27. // remove it when we're done executing
  28. var ret = fn.apply(this, arguments);
  29. this._super = tmp;
  30. return ret;
  31. };
  32. })(name, prop[name]) :
  33. prop[name];
  34. }
  35. // The dummy class constructor
  36. function Class() {
  37. // All construction is actually done in the init method
  38. if ( !initializing && this.init )
  39. this.init.apply(this, arguments);
  40. }
  41. // Populate our constructed prototype object
  42. Class.prototype = prototype;
  43. // Enforce the constructor to be what we expect
  44. Class.constructor = Class;
  45. // And make this class extendable
  46. Class.extend = arguments.callee;
  47. return Class;
  48. };
  49. })();
  50. var Color = Class.extend
  51. ({
  52. r: null, g: null, b: null, a: null,
  53. hex: null,
  54. styleString: null,
  55. init: function( hex )
  56. {
  57. this.setHex( hex ? hex : 0xff000000 );
  58. },
  59. setHex: function( hex )
  60. {
  61. this.hex = hex;
  62. this.updateRGBA();
  63. this.updateStyleString();
  64. },
  65. setRGBA: function( r, g, b, a )
  66. {
  67. this.r = r;
  68. this.g = g;
  69. this.b = b;
  70. this.a = a;
  71. this.updateHex();
  72. this.updateStyleString();
  73. },
  74. updateHex: function()
  75. {
  76. this.hex = this.a << 24 | this.r << 16 | this.g << 8 | this.b;
  77. },
  78. updateRGBA: function()
  79. {
  80. this.r = this.hex >> 16 & 0xff;
  81. this.g = this.hex >> 8 & 0xff;
  82. this.b = this.hex & 0xff;
  83. this.a = this.hex >> 24 & 0xff;
  84. },
  85. updateStyleString: function()
  86. {
  87. this.styleString = 'rgba(' + this.r + ',' + this.g + ',' + this.b + ',' + (this.a / 255) + ')';
  88. },
  89. toString: function()
  90. {
  91. return 'Color ( r: ' + this.r + ', g: ' + this.g + ', b: ' + this.b + ', a: ' + this.a + ', hex: ' + this.hex + ', style: ' + this.styleString + ' )';
  92. }
  93. });
  94. var Vector3 = Class.extend
  95. ({
  96. x: null, y: null, z: null,
  97. // sx: null, sy: null, sz: null,
  98. // userData: null,
  99. dx: null, dy: null, dz: null,
  100. tx: null, ty: null, tz: null,
  101. // oll: null,
  102. init: function(x, y, z)
  103. {
  104. this.x = x ? x : 0;
  105. this.y = y ? y : 0;
  106. this.z = z ? z : 0;
  107. },
  108. copy: function(v)
  109. {
  110. this.x = v.x;
  111. this.y = v.y;
  112. this.z = v.z;
  113. },
  114. addSelf: function(v)
  115. {
  116. this.x += v.x;
  117. this.y += v.y;
  118. this.z += v.z;
  119. },
  120. add: function(v1, v2)
  121. {
  122. this.x = v1.x + v2.x;
  123. this.y = v1.y + v2.y;
  124. this.z = v1.z + v2.z;
  125. },
  126. subSelf: function(v)
  127. {
  128. this.x -= v.x;
  129. this.y -= v.y;
  130. this.z -= v.z;
  131. },
  132. sub: function(v1, v2)
  133. {
  134. this.x = v1.x - v2.x;
  135. this.y = v1.y - v2.y;
  136. this.z = v1.z - v2.z;
  137. },
  138. cross: function(v)
  139. {
  140. this.tx = this.x;
  141. this.ty = this.y;
  142. this.tz = this.z;
  143. this.x = this.ty * v.z - this.tz * v.y;
  144. this.y = this.tz * v.x - this.tx * v.z;
  145. this.z = this.tx * v.y - this.ty * v.x;
  146. },
  147. multiply: function(s)
  148. {
  149. this.x *= s;
  150. this.y *= s;
  151. this.z *= s;
  152. },
  153. distanceTo: function(v)
  154. {
  155. this.dx = this.x - v.x;
  156. this.dy = this.y - v.y;
  157. this.dz = this.z - v.z;
  158. return Math.sqrt(this.dx * this.dx + this.dy * this.dy + this.dz * this.dz);
  159. },
  160. distanceToSquared: function(v)
  161. {
  162. this.dx = this.x - v.x;
  163. this.dy = this.y - v.y;
  164. this.dz = this.z - v.z;
  165. return this.dx * this.dx + this.dy * this.dy + this.dz * this.dz;
  166. },
  167. length: function()
  168. {
  169. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  170. },
  171. lengthSq: function()
  172. {
  173. return this.x * this.x + this.y * this.y + this.z * this.z;
  174. },
  175. negate: function()
  176. {
  177. this.x = -this.x;
  178. this.y = -this.y;
  179. this.z = -this.z;
  180. },
  181. normalize: function()
  182. {
  183. if (this.length() > 0)
  184. this.ool = 1.0 / this.length();
  185. else
  186. this.ool = 0;
  187. this.x *= this.ool;
  188. this.y *= this.ool;
  189. this.z *= this.ool;
  190. return this;
  191. },
  192. dot: function(v)
  193. {
  194. return this.x * v.x + this.y * v.y + this.z * v.z;
  195. },
  196. clone: function()
  197. {
  198. return new Vector3(this.x, this.y, this.z);
  199. },
  200. toVector4: function()
  201. {
  202. return new Vector4(this.x,this.y,this.z, 1.0);
  203. },
  204. toString: function()
  205. {
  206. return 'Vector3 (' + this.x + ', ' + this.y + ', ' + this.z + ')';
  207. }
  208. });
  209. Vector3.add = function(a, b)
  210. {
  211. return new Vector3( a.x + b.x, a.y + b.y, a.z + b.z );
  212. }
  213. Vector3.sub = function(a, b)
  214. {
  215. return new Vector3( a.x - b.x, a.y - b.y, a.z - b.z );
  216. }
  217. Vector3.multiply = function(a, s)
  218. {
  219. return new Vector3( a.x * s, a.y * s, a.z * s );
  220. }
  221. Vector3.cross = function(a, b)
  222. {
  223. return new Vector3( a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x );
  224. }
  225. Vector3.dot = function(a, b)
  226. {
  227. return a.x * b.x + a.y * b.y + a.z * b.z;
  228. }
  229. var Vector4 = Class.extend
  230. ({
  231. x: null, y: null, z: null, w: null,
  232. // sx: null, sy: null, sz: null,
  233. // userData: null,
  234. dx: null, dy: null, dz: null,
  235. tx: null, ty: null, tz: null,
  236. // oll: null,
  237. init: function(x, y, z, w)
  238. {
  239. this.x = x ? x : 0;
  240. this.y = y ? y : 0;
  241. this.z = z ? z : 0;
  242. this.w = w ? w : 1;
  243. },
  244. copy: function(v)
  245. {
  246. this.x = v.x;
  247. this.y = v.y;
  248. this.z = v.z;
  249. this.w = v.w;
  250. },
  251. addSelf: function(v)
  252. {
  253. this.x += v.x;
  254. this.y += v.y;
  255. this.z += v.z;
  256. this.w += v.w;
  257. },
  258. add: function(v1, v2)
  259. {
  260. this.x = v1.x + v2.x;
  261. this.y = v1.y + v2.y;
  262. this.z = v1.z + v2.z;
  263. this.w = v1.w + v2.w;
  264. },
  265. subSelf: function(v)
  266. {
  267. this.x -= v.x;
  268. this.y -= v.y;
  269. this.z -= v.z;
  270. this.w -= v.w;
  271. },
  272. sub: function(v1, v2)
  273. {
  274. this.x = v1.x - v2.x;
  275. this.y = v1.y - v2.y;
  276. this.z = v1.z - v2.z;
  277. this.w = v1.w - v2.w;
  278. },
  279. clone: function()
  280. {
  281. return new Vector4(this.x, this.y, this.z, this.w);
  282. },
  283. toString: function()
  284. {
  285. return 'Vector4 (' + this.x + ', ' + this.y + ', ' + this.z + ', ' + this.w + ')';
  286. },
  287. toVector3: function()
  288. {
  289. return new Vector3(this.x/this.w,this.y/this.w,this.z/this.w);
  290. }
  291. });
  292. Vector4.add = function(a, b)
  293. {
  294. return new Vector3( a.x + b.x, a.y + b.y, a.z + b.z , a.w + b.w );
  295. }
  296. Vector4.sub = function(a, b)
  297. {
  298. return new Vector3( a.x - b.x, a.y - b.y, a.z - b.z , a.w - b.w );
  299. }
  300. var Matrix4 = Class.extend
  301. ({
  302. n11: null, n12: null, n13: null, n14: null,
  303. n21: null, n22: null, n23: null, n24: null,
  304. n31: null, n32: null, n33: null, n34: null,
  305. n41: null, n42: null, n43: null, n44: null,
  306. x: null, y: null, z: null,
  307. init: function()
  308. {
  309. this.identity();
  310. },
  311. identity: function()
  312. {
  313. this.n11 = 1; this.n12 = 0; this.n13 = 0; this.n14 = 0;
  314. this.n21 = 0; this.n22 = 1; this.n23 = 0; this.n24 = 0;
  315. this.n31 = 0; this.n32 = 0; this.n33 = 1; this.n34 = 0;
  316. this.n41 = 0; this.n42 = 0; this.n43 = 0; this.n44 = 1;
  317. this.x = new Vector3(0,0,0);
  318. this.y = new Vector3(0,0,0);
  319. this.z = new Vector3(0,0,0);
  320. },
  321. lookAt: function(eye, center, up)
  322. {
  323. this.z.sub(center, eye);
  324. this.z.normalize();
  325. this.z.negate();
  326. this.x.copy(this.z);
  327. this.x.cross(up);
  328. this.x.normalize();
  329. this.x.negate();
  330. this.y.copy(this.x);
  331. this.y.cross(this.z);
  332. this.y.normalize();
  333. //this.y.negate(); //
  334. this.n11 = this.x.x;
  335. this.n12 = this.x.y;
  336. this.n13 = this.x.z;
  337. this.n14 = -this.x.dot(eye);
  338. this.n21 = this.y.x;
  339. this.n22 = this.y.y;
  340. this.n23 = this.y.z;
  341. this.n24 = -this.y.dot(eye);
  342. this.n31 = this.z.x;
  343. this.n32 = this.z.y;
  344. this.n33 = this.z.z;
  345. this.n34 = -this.z.dot(eye);
  346. },
  347. transform: function(v)
  348. {
  349. var vx = v.x, vy = v.y, vz = v.z, vw = (v.w ? v.w : 1.0);
  350. v.x = this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 * vw;
  351. v.y = this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 * vw;
  352. v.z = this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 * vw;
  353. vw = this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 * vw;
  354. if(v.w)
  355. {
  356. v.w = vw;
  357. }
  358. else
  359. {
  360. v.x = v.x/vw;
  361. v.y = v.y/vw;
  362. v.z = v.z/vw;
  363. }
  364. },
  365. crossVector: function(a)
  366. {
  367. v = new Vector4();
  368. v.x = this.n11*a.x+n12*a.y+n13*a.z+n14*a.w;
  369. v.y = this.n21*a.x+n22*a.y+n23*a.z+n24*a.w;
  370. v.z = this.n31*a.x+n32*a.y+n33*a.z+n34*a.w;
  371. if (a.w)
  372. {
  373. v.w = this.n41*a.x+n42*a.y+n43*a.z+n44*a.w;
  374. }
  375. else
  376. {
  377. v.w = 1.0;
  378. }
  379. return v;
  380. },
  381. multiply: function(a, b)
  382. {
  383. this.n11 = a.n11 * b.n11 + a.n12 * b.n21 + a.n13 * b.n31 + a.n14 * b.n41;
  384. this.n12 = a.n11 * b.n12 + a.n12 * b.n22 + a.n13 * b.n32 + a.n14 * b.n42;
  385. this.n13 = a.n11 * b.n13 + a.n12 * b.n23 + a.n13 * b.n33 + a.n14 * b.n43;
  386. this.n14 = a.n11 * b.n14 + a.n12 * b.n24 + a.n13 * b.n34 + a.n14 * b.n44;
  387. this.n21 = a.n21 * b.n11 + a.n22 * b.n21 + a.n23 * b.n31 + a.n24 * b.n41;
  388. this.n22 = a.n21 * b.n12 + a.n22 * b.n22 + a.n23 * b.n32 + a.n24 * b.n42;
  389. this.n23 = a.n21 * b.n13 + a.n22 * b.n23 + a.n23 * b.n33 + a.n24 * b.n34;
  390. this.n24 = a.n21 * b.n14 + a.n22 * b.n24 + a.n23 * b.n34 + a.n24 * b.n44;
  391. this.n31 = a.n31 * b.n11 + a.n32 * b.n21 + a.n33 * b.n31 + a.n34 * b.n41;
  392. this.n32 = a.n31 * b.n12 + a.n32 * b.n22 + a.n33 * b.n32 + a.n34 * b.n42;
  393. this.n33 = a.n31 * b.n13 + a.n32 * b.n23 + a.n33 * b.n33 + a.n34 * b.n43;
  394. this.n34 = a.n31 * b.n14 + a.n32 * b.n24 + a.n33 * b.n34 + a.n34 * b.n44;
  395. this.n41 = a.n41 * b.n11 + a.n42 * b.n21 + a.n43 * b.n31 + a.n44 * b.n41;
  396. this.n42 = a.n41 * b.n12 + a.n42 * b.n22 + a.n43 * b.n32 + a.n44 * b.n42;
  397. this.n43 = a.n41 * b.n13 + a.n42 * b.n23 + a.n43 * b.n33 + a.n44 * b.n43;
  398. this.n44 = a.n41 * b.n14 + a.n42 * b.n24 + a.n43 * b.n34 + a.n44 * b.n44;
  399. },
  400. multiplySelf: function(m)
  401. {
  402. var n11 = this.n11, n12 = this.n12, n13 = this.n13, n14 = this.n14;
  403. var n21 = this.n21, n22 = this.n22, n23 = this.n23, n24 = this.n24;
  404. var n31 = this.n31, n32 = this.n32, n33 = this.n33, n34 = this.n34;
  405. var n41 = this.n41, n42 = this.n42, n43 = this.n43, n44 = this.n44;
  406. this.n11 = n11 * m.n11 + n12 * m.n21 + n13 * m.n31 + n14 * m.n41;
  407. this.n12 = n11 * m.n12 + n12 * m.n22 + n13 * m.n32 + n14 * m.n42;
  408. this.n13 = n11 * m.n13 + n12 * m.n23 + n13 * m.n33 + n14 * m.n43;
  409. this.n14 = n11 * m.n14 + n12 * m.n24 + n13 * m.n34 + n14 * m.n44;
  410. this.n21 = n21 * m.n11 + n22 * m.n21 + n23 * m.n31 + n24 * m.n41;
  411. this.n22 = n21 * m.n12 + n22 * m.n22 + n23 * m.n32 + n24 * m.n42;
  412. this.n23 = n21 * m.n13 + n22 * m.n23 + n23 * m.n33 + n24 * m.n43;
  413. this.n24 = n21 * m.n14 + n22 * m.n24 + n23 * m.n34 + n24 * m.n44;
  414. this.n31 = n31 * m.n11 + n32 * m.n21 + n33 * m.n31 + n34 * m.n41;
  415. this.n32 = n31 * m.n12 + n32 * m.n22 + n33 * m.n32 + n34 * m.n42;
  416. this.n33 = n31 * m.n13 + n32 * m.n23 + n33 * m.n33 + n34 * m.n43;
  417. this.n34 = n31 * m.n14 + n32 * m.n24 + n33 * m.n34 + n34 * m.n44;
  418. this.n41 = n41 * m.n11 + n42 * m.n21 + n43 * m.n31 + n44 * m.n41;
  419. this.n42 = n41 * m.n12 + n42 * m.n22 + n43 * m.n32 + n44 * m.n42;
  420. this.n43 = n41 * m.n13 + n42 * m.n23 + n43 * m.n33 + n44 * m.n43;
  421. this.n44 = n41 * m.n14 + n42 * m.n24 + n43 * m.n34 + n44 * m.n44;
  422. },
  423. clone: function()
  424. {
  425. var m = new Matrix4();
  426. m.n11 = this.n11; m.n12 = this.n12; m.n13 = this.n13; m.n14 = this.n14;
  427. m.n21 = this.n21; m.n22 = this.n22; m.n23 = this.n23; m.n24 = this.n24;
  428. m.n31 = this.n31; m.n32 = this.n32; m.n33 = this.n33; m.n34 = this.n34;
  429. m.n41 = this.n41; m.n42 = this.n42; m.n43 = this.n43; m.n44 = this.n44;
  430. return m;
  431. },
  432. toString: function()
  433. {
  434. return "| " + this.n11 + " " + this.n12 + " " + this.n13 + " " + this.n14 + " |\n" +
  435. "| " + this.n21 + " " + this.n22 + " " + this.n23 + " " + this.n24 + " |\n" +
  436. "| " + this.n31 + " " + this.n32 + " " + this.n33 + " " + this.n34 + " |\n" +
  437. "| " + this.n41 + " " + this.n42 + " " + this.n43 + " " + this.n44 + " |";
  438. }
  439. });
  440. Matrix4.translationMatrix = function(x, y, z)
  441. {
  442. var m = new Matrix4();
  443. m.n14 = x;
  444. m.n24 = y;
  445. m.n34 = z;
  446. return m;
  447. }
  448. Matrix4.scaleMatrix = function(x, y, z)
  449. {
  450. var m = new Matrix4();
  451. m.n11 = x;
  452. m.n22 = y;
  453. m.n33 = z;
  454. return m;
  455. }
  456. Matrix4.rotationXMatrix = function(theta)
  457. {
  458. var rot = new Matrix4();
  459. rot.n22 = rot.n33 = Math.cos(theta);
  460. rot.n32 = Math.sin(theta);
  461. rot.n23 = -rot.n32;
  462. return rot;
  463. }
  464. Matrix4.rotationYMatrix = function(theta)
  465. {
  466. var rot = new Matrix4();
  467. rot.n11 = rot.n33 = Math.cos(theta);
  468. rot.n13 = Math.sin(theta);
  469. rot.n31 = -rot.n13;
  470. return rot;
  471. }
  472. Matrix4.rotationZMatrix = function(theta)
  473. {
  474. var rot = new Matrix4();
  475. rot.n11 = rot.n22 = Math.cos(theta);
  476. rot.n21 = Math.sin(theta);
  477. rot.n12 = -rot.n21;
  478. return rot;
  479. }
  480. Matrix4.makeFrustum = function(left,right,bottom,top,near,far)
  481. {
  482. var m = new Matrix4();
  483. var x = 2*near/(right-left);
  484. var y = 2*near/(top-bottom);
  485. var a = (right+left)/(right-left);
  486. var b = (top+bottom)/(top-bottom);
  487. var c = -(far+near)/(far-near);
  488. var d = -2*far*near/(far-near);
  489. m.n11=x;
  490. m.n13=a;
  491. m.n22=y;
  492. m.n23=b;
  493. m.n33=c;
  494. m.n34=d;
  495. m.n43=-1;
  496. m.n44=0;
  497. return m;
  498. }
  499. Matrix4.makePerspective = function(fovy, aspect, near, far)
  500. {
  501. var ymax = near * Math.tan(fovy * 0.00872664625972);
  502. var ymin = -ymax;
  503. var xmin = ymin * aspect;
  504. var xmax = ymax * aspect;
  505. return Matrix4.makeFrustum(xmin, xmax, ymin, ymax, near, far);
  506. }
  507. var Vertex = Vector3.extend
  508. ({
  509. u: null, v: null,
  510. screen: null,
  511. normal : null,
  512. visible: null,
  513. init: function(x, y, z)
  514. {
  515. this._super(x, y, z);
  516. this.screen = new Vector3();
  517. },
  518. toString: function()
  519. {
  520. return 'Vertex ( ' + this.x + ', ' + this.y + ', ' + this.z + ' )';
  521. }
  522. });
  523. var Face3 = Vector3.extend
  524. ({
  525. a: null, b: null, c: null,
  526. screen: null,
  527. uv: null,
  528. normal: null,
  529. color: null,
  530. init: function(a, b, c, uv, normal, color)
  531. {
  532. this._super((a.x + b.x + c.x) / 3, (a.y + b.y + c.y) / 3, (a.z + b.z + c.z) / 3);
  533. this.a = a;
  534. this.b = b;
  535. this.c = c;
  536. this.screen = new Vector3();
  537. this.uv = uv ? uv : [ [0, 0], [0, 0], [0, 0] ];
  538. this.normal = normal ? normal : new Vector3();
  539. this.color = color ? color : new Color();
  540. },
  541. toString: function()
  542. {
  543. return 'Face3 ( ' + this.a + ', ' + this.b + ', ' + this.c + ' )';
  544. }
  545. });
  546. var Face4 = Vector3.extend
  547. ({
  548. a: null, b: null, c: null, d: null,
  549. normal: null,
  550. screen: null,
  551. color: null,
  552. init: function(a, b, c, d, uv, normal, color)
  553. {
  554. this._super((a.x + b.x + c.x + d.x) / 4, (a.y + b.y + c.y + d.y) / 4, (a.z + b.z + c.z + d.z) / 4);
  555. this.a = a;
  556. this.b = b;
  557. this.c = c;
  558. this.d = d;
  559. this.screen = new Vector3();
  560. this.color = color ? color : new Color();
  561. },
  562. toString: function()
  563. {
  564. return 'Face4 ( ' + this.a + ', ' + this.b + ', ' + this.c + ', ' + this.d + ' )';
  565. }
  566. });
  567. var Geometry = Class.extend
  568. ({
  569. vertices: null,
  570. faces: null,
  571. init: function()
  572. {
  573. this.vertices = new Array();
  574. this.faces = new Array();
  575. }
  576. });
  577. var Camera = Vector3.extend
  578. ({
  579. up: null,
  580. target: null,
  581. zoom: null,
  582. focus: null,
  583. roll: null,
  584. matrix: null,
  585. projectionMatrix: null,
  586. init: function(x, y, z)
  587. {
  588. this._super(x, y, z);
  589. this.up = new Vector3( 0, 1, 0 );
  590. this.target = new Vector3( 0, 0, 0 );
  591. this.projectionMatrix = Matrix4.makePerspective(45, 1, 0.001, 1000);
  592. this.matrix = new Matrix4();
  593. this.updateMatrix();
  594. },
  595. updateMatrix: function()
  596. {
  597. this.matrix.lookAt( this, this.target, this.up );
  598. },
  599. setProjectionMatrix: function(matrix)
  600. {
  601. this.projectionMatrix=matrix;
  602. },
  603. toString: function()
  604. {
  605. return 'Camera ( ' + this.x + ', ' + this.y + ', ' + this.z + ' )';
  606. }
  607. });
  608. var Object3D = Class.extend
  609. ({
  610. position: null,
  611. rotation: null,
  612. scale: null,
  613. matrix: null,
  614. screen: null,
  615. init: function()
  616. {
  617. this.position = new Vector3(0, 0, 0);
  618. this.rotation = new Vector3(0, 0, 0);
  619. this.scale = new Vector3(1, 1, 1);
  620. this.matrix = new Matrix4();
  621. this.screen = new Vector3(0, 0, 0);
  622. },
  623. updateMatrix: function()
  624. {
  625. this.matrix.identity();
  626. this.matrix.multiplySelf( Matrix4.translationMatrix( this.position.x, this.position.y, this.position.z) );
  627. this.matrix.multiplySelf( Matrix4.rotationXMatrix( this.rotation.x ) );
  628. this.matrix.multiplySelf( Matrix4.rotationYMatrix( this.rotation.y ) );
  629. this.matrix.multiplySelf( Matrix4.rotationZMatrix( this.rotation.z ) );
  630. this.matrix.multiplySelf( Matrix4.scaleMatrix( this.scale.x, this.scale.y, this.scale.z ) );
  631. }
  632. });
  633. var Mesh = Object3D.extend
  634. ({
  635. geometry: null,
  636. material: null,
  637. doubleSide: null,
  638. init: function( geometry, material )
  639. {
  640. this._super();
  641. this.geometry = geometry;
  642. this.material = material;
  643. }
  644. });
  645. var Particle = Object3D.extend
  646. ({
  647. size: 1,
  648. material: null,
  649. init: function( material )
  650. {
  651. this._super();
  652. this.material = material;
  653. }
  654. });
  655. var Plane = Geometry.extend
  656. ({
  657. init: function( width, height )
  658. {
  659. this._super();
  660. var width_half = width / 2;
  661. var height_half = height / 2;
  662. this.v( -width_half, height_half, 0 );
  663. this.v( width_half, height_half, 0 );
  664. this.v( width_half, -height_half, 0 );
  665. this.v( -width_half, -height_half, 0 );
  666. this.f4( 0, 1, 2, 3 );
  667. },
  668. v: function( x, y, z )
  669. {
  670. this.vertices.push( new Vertex( x, y, z ) );
  671. },
  672. f4: function( a, b, c, d )
  673. {
  674. this.faces.push( new Face4( this.vertices[a], this.vertices[b], this.vertices[c], this.vertices[d] ) );
  675. }
  676. });
  677. var Cube = Geometry.extend
  678. ({
  679. init: function( width, height, depth )
  680. {
  681. this._super();
  682. var width_half = width / 2;
  683. var height_half = height / 2;
  684. var depth_half = depth / 2;
  685. this.v( width_half, height_half, -depth_half );
  686. this.v( width_half, -height_half, -depth_half );
  687. this.v( -width_half, -height_half, -depth_half );
  688. this.v( -width_half, height_half, -depth_half );
  689. this.v( width_half, height_half, depth_half );
  690. this.v( width_half, -height_half, depth_half );
  691. this.v( -width_half, -height_half, depth_half );
  692. this.v( -width_half, height_half, depth_half );
  693. this.f4( 0, 1, 2, 3 );
  694. this.f4( 4, 7, 6, 5 );
  695. this.f4( 0, 4, 5, 1 );
  696. this.f4( 1, 5, 6, 2 );
  697. this.f4( 2, 6, 7, 3 );
  698. this.f4( 4, 0, 3, 7 );
  699. },
  700. v: function( x, y, z )
  701. {
  702. this.vertices.push( new Vertex( x, y, z ) );
  703. },
  704. f4: function( a, b, c, d )
  705. {
  706. this.faces.push( new Face4( this.vertices[a], this.vertices[b], this.vertices[c], this.vertices[d] ) );
  707. }
  708. });
  709. var ColorMaterial = Class.extend
  710. ({
  711. color: null,
  712. init: function( hex, opacity )
  713. {
  714. this.color = new Color( (opacity ? (opacity * 0xff) << 24 : 0xff000000) | hex );
  715. }
  716. });
  717. var FaceColorMaterial = Class.extend
  718. ({
  719. });
  720. var Scene = Class.extend
  721. ({
  722. objects: null,
  723. init: function()
  724. {
  725. this.objects = new Array();
  726. },
  727. add: function( object )
  728. {
  729. this.objects.push( object );
  730. },
  731. remove: function( object )
  732. {
  733. for(var i = 0; i < this.objects.length; i++)
  734. if(object == this.objects[i])
  735. alert("yay");
  736. },
  737. toString: function()
  738. {
  739. return 'Scene ( ' + this.objects + ' )';
  740. }
  741. });
  742. var Renderer = Class.extend
  743. ({
  744. matrix: null,
  745. viewport: null,
  746. renderList: null,
  747. face3Pool: null,
  748. face4Pool: null,
  749. width: null,
  750. height: null,
  751. widthHalf: null,
  752. heightHalf: null,
  753. init: function()
  754. {
  755. this.matrix = new Matrix4();
  756. this.face3Pool = new Array();
  757. this.face4Pool = new Array();
  758. },
  759. setSize: function( width, height )
  760. {
  761. this.width = width;
  762. this.height = height;
  763. this.widthHalf = this.width / 2;
  764. this.heightHalf = this.height / 2;
  765. },
  766. sort: function(a, b)
  767. {
  768. return b.screen.z - a.screen.z;
  769. },
  770. render: function( scene, camera )
  771. {
  772. var vertex, face, object;
  773. var face3count = 0, face4count = 0;
  774. this.renderList = new Array();
  775. for (var i = 0; i < scene.objects.length; i++)
  776. {
  777. object = scene.objects[i];
  778. if (object instanceof Mesh)
  779. {
  780. this.matrix.multiply( camera.matrix, object.matrix );
  781. // vertices
  782. for (var j = 0; j < object.geometry.vertices.length; j++)
  783. {
  784. vertex = object.geometry.vertices[j];
  785. vertex.screen.copy( vertex );
  786. this.matrix.transform( vertex.screen );
  787. camera.projectionMatrix.transform( vertex.screen );
  788. vertex.visible = vertex.screen.z > 0 && object.screen.z < 1;
  789. //convert to screen coords
  790. vertex.screen.x *= this.widthHalf;
  791. vertex.screen.y *= this.heightHalf;
  792. }
  793. // faces
  794. for (j = 0; j < object.geometry.faces.length; j++)
  795. {
  796. face = object.geometry.faces[j];
  797. // TODO: Use normals for culling
  798. if (face instanceof Face3)
  799. {
  800. if (face.a.visible && face.b.visible && face.c.visible && (object.doubleSided ||
  801. (face.c.screen.x - face.a.screen.x) * (face.b.screen.y - face.a.screen.y) -
  802. (face.c.screen.y - face.a.screen.y) * (face.b.screen.x - face.a.screen.x) > 0) )
  803. {
  804. face.screen.z = (face.a.screen.z + face.b.screen.z + face.c.screen.z) * 0.3;
  805. if (this.face3Pool[face3count] == null)
  806. this.face3Pool[face3count] = new Face3(new Vertex(), new Vertex(), new Vertex());
  807. this.face3Pool[face3count].a.screen.copy(face.a.screen);
  808. this.face3Pool[face3count].b.screen.copy(face.b.screen);
  809. this.face3Pool[face3count].c.screen.copy(face.c.screen);
  810. this.face3Pool[face3count].screen.z = face.screen.z;
  811. this.face3Pool[face3count].color = face.color;
  812. this.face3Pool[face3count].material = object.material;
  813. this.renderList.push( this.face3Pool[face3count] );
  814. face3count++;
  815. }
  816. }
  817. else if (face instanceof Face4)
  818. {
  819. if (face.a.visible && face.b.visible && face.c.visible && (object.doubleSided ||
  820. ((face.d.screen.x - face.a.screen.x) * (face.b.screen.y - face.a.screen.y) -
  821. (face.d.screen.y - face.a.screen.y) * (face.b.screen.x - face.a.screen.x) > 0 ||
  822. (face.b.screen.x - face.c.screen.x) * (face.d.screen.y - face.c.screen.y) -
  823. (face.b.screen.y - face.c.screen.y) * (face.d.screen.x - face.c.screen.x) > 0)) )
  824. {
  825. face.screen.z = (face.a.screen.z + face.b.screen.z + face.c.screen.z + face.d.screen.z) * 0.25;
  826. if (this.face4Pool[face4count] == null)
  827. this.face4Pool[face4count] = new Face4(new Vertex(), new Vertex(), new Vertex(), new Vertex());
  828. this.face4Pool[face4count].a.screen.copy(face.a.screen);
  829. this.face4Pool[face4count].b.screen.copy(face.b.screen);
  830. this.face4Pool[face4count].c.screen.copy(face.c.screen);
  831. this.face4Pool[face4count].d.screen.copy(face.d.screen);
  832. this.face4Pool[face4count].screen.z = face.screen.z;
  833. this.face4Pool[face4count].color = face.color;
  834. this.face4Pool[face4count].material = object.material;
  835. this.renderList.push( this.face4Pool[face4count] );
  836. face4count++;
  837. }
  838. }
  839. }
  840. }
  841. else if (object instanceof Particle)
  842. {
  843. object.screen=object.position.toVector4();
  844. camera.matrix.transform( object.screen );
  845. camera.projectionMatrix.transform( object.screen );
  846. var size=object.screen.x/object.screen.w-(object.screen.x+camera.projectionMatrix.n11)/(object.screen.w+camera.projectionMatrix.n14);
  847. object.zsize=Math.abs(size)*object.size;
  848. object.screen=object.screen.toVector3();
  849. if (object.screen.z > 0 && object.screen.z < 1
  850. && object.screen.x+object.zsize > -1 && object.screen.x-object.zsize < 1
  851. && object.screen.y+object.zsize > -1 && object.screen.y-object.zsize < 1){
  852. object.zsize *= this.widthHalf;
  853. object.screen.x *= this.widthHalf;
  854. object.screen.y *= this.heightHalf;
  855. this.renderList.push( object );
  856. }
  857. }
  858. }
  859. this.renderList.sort(this.sort);
  860. }
  861. });
  862. var CanvasRenderer = Renderer.extend
  863. ({
  864. context: null,
  865. init: function()
  866. {
  867. this._super();
  868. this.viewport = document.createElement("canvas");
  869. this.viewport.style.position = "absolute";
  870. this.context = this.viewport.getContext("2d");
  871. },
  872. setSize: function( width, height )
  873. {
  874. this._super( width, height );
  875. this.viewport.width = this.width;
  876. this.viewport.height = this.height;
  877. this.context.setTransform(1, 0, 0, 1, this.widthHalf, this.heightHalf);
  878. },
  879. render: function( scene, camera )
  880. {
  881. this._super( scene, camera );
  882. var element , pi2 = Math.PI * 2;
  883. this.context.clearRect (-this.widthHalf, -this.heightHalf, this.width, this.height);
  884. for (j = 0; j < this.renderList.length; j++)
  885. {
  886. element = this.renderList[j];
  887. if (element.material instanceof ColorMaterial)
  888. {
  889. this.context.fillStyle = element.material.color.styleString;
  890. }
  891. else if (element.material instanceof FaceColorMaterial)
  892. {
  893. this.context.fillStyle = element.color.styleString;
  894. }
  895. if (element instanceof Face3)
  896. {
  897. this.context.beginPath();
  898. this.context.moveTo(element.a.screen.x, element.a.screen.y);
  899. this.context.lineTo(element.b.screen.x, element.b.screen.y);
  900. this.context.lineTo(element.c.screen.x, element.c.screen.y);
  901. this.context.fill();
  902. this.context.closePath();
  903. }
  904. else if (element instanceof Face4)
  905. {
  906. this.context.beginPath();
  907. this.context.moveTo(element.a.screen.x, element.a.screen.y);
  908. this.context.lineTo(element.b.screen.x, element.b.screen.y);
  909. this.context.lineTo(element.c.screen.x, element.c.screen.y);
  910. this.context.lineTo(element.d.screen.x, element.d.screen.y);
  911. this.context.fill();
  912. this.context.closePath();
  913. }
  914. else if (element instanceof Particle)
  915. {
  916. this.context.beginPath();
  917. this.context.arc(element.screen.x, element.screen.y, element.zsize, 0, pi2, true);
  918. this.context.fill();
  919. this.context.closePath();
  920. }
  921. }
  922. }
  923. });
  924. var SVGRenderer = Renderer.extend
  925. ({
  926. svgPathPool: null,
  927. svgCirclePool: null,
  928. init: function()
  929. {
  930. this._super();
  931. this.viewport = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  932. this.viewport.style.position = "absolute";
  933. this.svgPathPool = new Array();
  934. this.svgCirclePool = new Array();
  935. },
  936. setSize: function( width, height )
  937. {
  938. this.viewport.setAttribute('viewBox', (-width / 2) + ' ' + (-height / 2) + ' ' + width + ' ' + height );
  939. this.viewport.setAttribute('width', width);
  940. this.viewport.setAttribute('height', height);
  941. },
  942. render: function( scene, camera )
  943. {
  944. this._super( scene, camera );
  945. while (this.viewport.childNodes.length > 0)
  946. {
  947. this.viewport.removeChild(this.viewport.childNodes[0]);
  948. }
  949. var pathCount = 0, circleCount = 0, svgNode;
  950. for (j = 0; j < this.renderList.length; j++)
  951. {
  952. element = this.renderList[j];
  953. if (element instanceof Face3)
  954. {
  955. svgNode = this.getPathNode(pathCount++);
  956. svgNode.setAttribute('d', 'M ' + element.a.screen.x + ' ' + element.a.screen.y + ' L ' + element.b.screen.x + ' ' + element.b.screen.y + ' L ' + element.c.screen.x + ',' + element.c.screen.y + 'z');
  957. }
  958. else if (element instanceof Face4)
  959. {
  960. svgNode = this.getPathNode(pathCount++);
  961. svgNode.setAttribute('d', 'M ' + element.a.screen.x + ' ' + element.a.screen.y + ' L ' + element.b.screen.x + ' ' + element.b.screen.y + ' L ' + element.c.screen.x + ',' + element.c.screen.y + ' L ' + element.d.screen.x + ',' + element.d.screen.y + 'z');
  962. }
  963. else if (element instanceof Particle)
  964. {
  965. svgNode = this.getCircleNode(circleCount++);
  966. svgNode.setAttribute('cx', element.screen.x);
  967. svgNode.setAttribute('cy', element.screen.y);
  968. svgNode.setAttribute('r', element.zsize);
  969. }
  970. if (element.material instanceof ColorMaterial)
  971. {
  972. svgNode.setAttribute('style', 'fill: rgb(' + element.material.color.r + ',' + element.material.color.g + ',' + element.material.color.b + ')');
  973. }
  974. else if (element.material instanceof FaceColorMaterial)
  975. {
  976. svgNode.setAttribute('style', 'fill: rgb(' + element.color.r + ',' + element.color.g + ',' + element.color.b + ')');
  977. }
  978. this.viewport.appendChild(svgNode);
  979. }
  980. },
  981. getPathNode: function( id )
  982. {
  983. if (this.svgPathPool[id] == null)
  984. {
  985. this.svgPathPool[id] = document.createElementNS('http://www.w3.org/2000/svg', 'path');
  986. // this.svgPathPool[id].setAttribute('shape-rendering', 'crispEdges'); //optimizeSpeed
  987. return this.svgPathPool[id];
  988. }
  989. return this.svgPathPool[id];
  990. },
  991. getCircleNode: function( id )
  992. {
  993. if (this.svgCirclePool[id] == null)
  994. {
  995. this.svgCirclePool[id] = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
  996. // this.svgCirclePool[id].setAttribute('shape-rendering', 'crispEdges'); //optimizeSpeed
  997. this.svgCirclePool[id].setAttribute('fill', 'red');
  998. return this.svgCirclePool[id];
  999. }
  1000. return this.svgCirclePool[id];
  1001. }
  1002. });
  1003. var GLRenderer = Renderer.extend
  1004. ({
  1005. init: function()
  1006. {
  1007. this._super();
  1008. this.viewport = document.createElement("canvas");
  1009. this.viewport.style.position = "absolute";
  1010. try {
  1011. this.gl = this.viewport.getContext("experimental-webgl");
  1012. } catch(e) {}
  1013. if (!this.gl) {
  1014. alert("WebGL not supported");
  1015. throw "cannot create webgl context";
  1016. }
  1017. this.gl.enable(this.gl.DEPTH_TEST);
  1018. this.createProgram();
  1019. },
  1020. createProgram: function()
  1021. {
  1022. var gl=this.gl;
  1023. var vtxShader=[
  1024. "attribute vec3 position;",
  1025. "attribute vec4 color;",
  1026. "uniform mat4 mvMatrix;",
  1027. "uniform mat4 pMatrix;",
  1028. "varying vec4 vcolor;",
  1029. "void main(){",
  1030. "vcolor=color;",
  1031. "gl_Position = pMatrix*mvMatrix*vec4(position,1.0);",
  1032. "}"
  1033. ].join("");
  1034. frgShader=[
  1035. "varying vec4 vcolor;",
  1036. "void main(){",
  1037. "gl_FragColor=vcolor;",
  1038. "}"
  1039. ].join("");
  1040. var vertexShader=gl.createShader(gl.VERTEX_SHADER);
  1041. var fragmentShader=gl.createShader(gl.FRAGMENT_SHADER);
  1042. gl.shaderSource(vertexShader, vtxShader);
  1043. gl.shaderSource(fragmentShader,frgShader);
  1044. gl.compileShader(fragmentShader);
  1045. this.program = gl.createProgram();
  1046. gl.attachShader(this.program, vertexShader);
  1047. gl.attachShader(this.program, fragmentShader);
  1048. gl.linkProgram(this.program);
  1049. this.program.mvMatrix=gl.getUniformLocation(this.program, "mvMatrix");
  1050. this.program.pMatrix=gl.getUniformLocation(this.program, "pMatrix");
  1051. this.program.position=gl.getAttribLocation(this.program, "position");
  1052. this.program.color=gl.getAttribLocation(this.program, "color");
  1053. this.program.mvMatrixArray=new WebGLFloatArray(16);
  1054. this.program.pMatrixArray=new WebGLFloatArray(16);
  1055. },
  1056. matrix2Array: function( matrix, array )
  1057. {
  1058. array[0]=matrix.n11;
  1059. array[1]=matrix.n12;
  1060. array[3]=matrix.n13;
  1061. array[4]=matrix.n14;
  1062. array[5]=matrix.n21;
  1063. array[6]=matrix.n22;
  1064. array[7]=matrix.n23;
  1065. array[8]=matrix.n24;
  1066. array[9]=matrix.n31;
  1067. array[10]=matrix.n32;
  1068. array[11]=matrix.n33;
  1069. array[12]=matrix.n34;
  1070. array[13]=matrix.n41;
  1071. array[14]=matrix.n42;
  1072. array[15]=matrix.n43;
  1073. array[16]=matrix.n44;
  1074. },
  1075. render: function( scene, camera )
  1076. {
  1077. var gl=this.gl;
  1078. gl.viewport(0,0,this.width,this.height);
  1079. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  1080. for (var i = 0; i < scene.objects.length; i++)
  1081. {
  1082. object = scene.objects[i];
  1083. if (object instanceof Mesh)
  1084. {
  1085. // Very inefficient but easiest way initially
  1086. var vertexArray = [];
  1087. var faceArray = [];
  1088. var colorArray = [];
  1089. var vertexIndex = 0;
  1090. var color;
  1091. for (j = 0; j < object.geometry.faces.length; j++)
  1092. {
  1093. face = object.geometry.faces[j];
  1094. color = face.color;
  1095. if (face instanceof Face3){
  1096. vertexArray.push( face.a.x, face.a.y, face.a.z );
  1097. vertexArray.push( face.b.x, face.b.y, face.b.z );
  1098. vertexArray.push( face.c.x, face.c.y, face.c.z );
  1099. colorArray.push( color.r, color.g, color.b, color.a );
  1100. colorArray.push( color.r, color.g, color.b, color.a );
  1101. colorArray.push( color.r, color.g, color.b, color.a );
  1102. faceArray.push( vertexIndex, vertexIndex+1, vertexIndex+2 );
  1103. vertexIndex += 3;
  1104. }
  1105. else if (face instanceof Face4)
  1106. {
  1107. vertexArray.push( face.a.x, face.a.y, face.a.z );
  1108. vertexArray.push( face.b.x, face.b.y, face.b.z );
  1109. vertexArray.push( face.c.x, face.c.y, face.c.z );
  1110. vertexArray.push( face.d.x, face.d.y, face.d.z );
  1111. colorArray.push( color.r, color.g, color.b, color.a );
  1112. colorArray.push( color.r, color.g, color.b, color.a );
  1113. colorArray.push( color.r, color.g, color.b, color.a );
  1114. colorArray.push( color.r, color.g, color.b, color.a );
  1115. faceArray.push( vertexIndex, vertexIndex+1, vertexIndex+2 );
  1116. faceArray.push( vertexIndex, vertexIndex+3, vertexIndex+3 );
  1117. vertexIndex += 4;
  1118. }
  1119. }
  1120. var vertexArray = new WebGLFloatArray(vertexArray);
  1121. var colorArray = new WebGLFloatArray(colorArray);
  1122. var faceArray = new WebGLUnsignedShortArray(faceArray);
  1123. if (!object.WebGLVertexBuffer) object.WebGLVertexBuffer = gl.createBuffer();
  1124. gl.bindBuffer( gl.ARRAY_BUFFER, object.WebGLVertexBuffer );
  1125. gl.bufferData( gl.ARRAY_BUFFER, vertexArray, gl.DYNAMIC_DRAW );
  1126. if (!object.WebGLColorBuffer) object.WebGLColorBuffer = gl.createBuffer();
  1127. gl.bindBuffer( gl.ARRAY_BUFFER, object.WebGLColorBuffer );
  1128. gl.bufferData( gl.ARRAY_BUFFER, vertexArray, gl.DYNAMIC_DRAW );
  1129. if(!object.WebGLFaceBuffer) object.WebGLFaceBuffer = gl.createBuffer();
  1130. gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER, object.WebGLFaceBuffer );
  1131. gl.bufferData( gl.ELEMENT_ARRAY_BUFFER, faceArray, gl.DYNAMIC_DRAW );
  1132. gl.useProgram(this.program);
  1133. this.matrix.multiply( camera.matrix, object.matrix );
  1134. this.matrix2Array( this.matrix, this.program.mvMatrixArray );
  1135. this.matrix2Array( camera.projectionMatrix, this.program.pMatrixArray );
  1136. gl.uniformMatrix4fv(this.program.pMatrix, true, this.program.pMatrixArray);
  1137. gl.uniformMatrix4fv(this.program.mvMatrix, true, this.program.mvMatrixArray);
  1138. for(var i=0; i<8; i++) gl.disableVertexAttribArray(i);
  1139. gl.bindBuffer(gl.ARRAY_BUFFER, object.WebGLVertexBuffer);
  1140. gl.enableVertexAttribArray(this.program.position);
  1141. gl.vertexAttribPointer(this.program.position, 3, gl.FLOAT, false, 0, 0);
  1142. gl.bindBuffer(gl.ARRAY_BUFFER, object.WebGLColorBuffer);
  1143. gl.enableVertexAttribArray(this.program.color);
  1144. gl.vertexAttribPointer(this.program.color, 4, gl.FLOAT, false, 0, 0);
  1145. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, object.WebGLFaceBuffer);
  1146. alert(faceArray.length);
  1147. gl.drawElements(gl.TRIANGLES,faceArray.length, gl.UNSIGNED_SHORT, 0);
  1148. }
  1149. //TODO Particles!!
  1150. }
  1151. }
  1152. });