Pas2JS_WebGL_Terrain.js 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252
  1. var pas = {};
  2. var rtl = {
  3. quiet: false,
  4. debug_load_units: false,
  5. debug_rtti: false,
  6. debug: function(){
  7. if (rtl.quiet || !console || !console.log) return;
  8. console.log(arguments);
  9. },
  10. error: function(s){
  11. rtl.debug('Error: ',s);
  12. throw s;
  13. },
  14. warn: function(s){
  15. rtl.debug('Warn: ',s);
  16. },
  17. hasString: function(s){
  18. return rtl.isString(s) && (s.length>0);
  19. },
  20. isArray: function(a) {
  21. return Array.isArray(a);
  22. },
  23. isFunction: function(f){
  24. return typeof(f)==="function";
  25. },
  26. isModule: function(m){
  27. return rtl.isObject(m) && rtl.hasString(m.$name) && (pas[m.$name]===m);
  28. },
  29. isImplementation: function(m){
  30. return rtl.isObject(m) && rtl.isModule(m.$module) && (m.$module.$impl===m);
  31. },
  32. isNumber: function(n){
  33. return typeof(n)==="number";
  34. },
  35. isObject: function(o){
  36. var s=typeof(o);
  37. return (typeof(o)==="object") && (o!=null);
  38. },
  39. isString: function(s){
  40. return typeof(s)==="string";
  41. },
  42. getNumber: function(n){
  43. return typeof(n)==="number"?n:NaN;
  44. },
  45. getChar: function(c){
  46. return ((typeof(c)==="string") && (c.length===1)) ? c : "";
  47. },
  48. getObject: function(o){
  49. return ((typeof(o)==="object") || (typeof(o)==='function')) ? o : null;
  50. },
  51. isPasClass: function(type){
  52. return (rtl.isObject(type) && type.hasOwnProperty('$classname') && rtl.isObject(type.$module));
  53. },
  54. isPasClassInstance: function(type){
  55. return (rtl.isObject(type) && rtl.isPasClass(type.$class));
  56. },
  57. hexStr: function(n,digits){
  58. return ("000000000000000"+n.toString(16).toUpperCase()).slice(-digits);
  59. },
  60. m_loading: 0,
  61. m_loading_intf: 1,
  62. m_intf_loaded: 2,
  63. m_loading_impl: 3, // loading all used unit
  64. m_initializing: 4, // running initialization
  65. m_initialized: 5,
  66. module: function(module_name, intfuseslist, intfcode, impluseslist, implcode){
  67. if (rtl.debug_load_units) rtl.debug('rtl.module name="'+module_name+'" intfuses='+intfuseslist+' impluses='+impluseslist+' hasimplcode='+rtl.isFunction(implcode));
  68. if (!rtl.hasString(module_name)) rtl.error('invalid module name "'+module_name+'"');
  69. if (!rtl.isArray(intfuseslist)) rtl.error('invalid interface useslist of "'+module_name+'"');
  70. if (!rtl.isFunction(intfcode)) rtl.error('invalid interface code of "'+module_name+'"');
  71. if (!(impluseslist==undefined) && !rtl.isArray(impluseslist)) rtl.error('invalid implementation useslist of "'+module_name+'"');
  72. if (!(implcode==undefined) && !rtl.isFunction(implcode)) rtl.error('invalid implementation code of "'+module_name+'"');
  73. if (pas[module_name])
  74. rtl.error('module "'+module_name+'" is already registered');
  75. var module = pas[module_name] = {
  76. $name: module_name,
  77. $intfuseslist: intfuseslist,
  78. $impluseslist: impluseslist,
  79. $state: rtl.m_loading,
  80. $intfcode: intfcode,
  81. $implcode: implcode,
  82. $impl: null,
  83. $rtti: Object.create(rtl.tSectionRTTI)
  84. };
  85. module.$rtti.$module = module;
  86. if (implcode) module.$impl = {
  87. $module: module,
  88. $rtti: module.$rtti
  89. };
  90. },
  91. exitcode: 0,
  92. run: function(module_name){
  93. function doRun(){
  94. if (!rtl.hasString(module_name)) module_name='program';
  95. if (rtl.debug_load_units) rtl.debug('rtl.run module="'+module_name+'"');
  96. rtl.initRTTI();
  97. var module = pas[module_name];
  98. if (!module) rtl.error('rtl.run module "'+module_name+'" missing');
  99. rtl.loadintf(module);
  100. rtl.loadimpl(module);
  101. if (module_name=='program'){
  102. if (rtl.debug_load_units) rtl.debug('running $main');
  103. var r = pas.program.$main();
  104. if (rtl.isNumber(r)) rtl.exitcode = r;
  105. }
  106. }
  107. if (rtl.showUncaughtExceptions) {
  108. try{
  109. doRun();
  110. } catch(re) {
  111. var errMsg = re.hasOwnProperty('$class') ? re.$class.$classname : '';
  112. errMsg += ((errMsg) ? ': ' : '') + (re.hasOwnProperty('fMessage') ? re.fMessage : re);
  113. alert('Uncaught Exception : '+errMsg);
  114. rtl.exitCode = 216;
  115. }
  116. } else {
  117. doRun();
  118. }
  119. return rtl.exitcode;
  120. },
  121. loadintf: function(module){
  122. if (module.$state>rtl.m_loading_intf) return; // already finished
  123. if (rtl.debug_load_units) rtl.debug('loadintf: "'+module.$name+'"');
  124. if (module.$state===rtl.m_loading_intf)
  125. rtl.error('unit cycle detected "'+module.$name+'"');
  126. module.$state=rtl.m_loading_intf;
  127. // load interfaces of interface useslist
  128. rtl.loaduseslist(module,module.$intfuseslist,rtl.loadintf);
  129. // run interface
  130. if (rtl.debug_load_units) rtl.debug('loadintf: run intf of "'+module.$name+'"');
  131. module.$intfcode(module.$intfuseslist);
  132. // success
  133. module.$state=rtl.m_intf_loaded;
  134. // Note: units only used in implementations are not yet loaded (not even their interfaces)
  135. },
  136. loaduseslist: function(module,useslist,f){
  137. if (useslist==undefined) return;
  138. for (var i in useslist){
  139. var unitname=useslist[i];
  140. if (rtl.debug_load_units) rtl.debug('loaduseslist of "'+module.$name+'" uses="'+unitname+'"');
  141. if (pas[unitname]==undefined)
  142. rtl.error('module "'+module.$name+'" misses "'+unitname+'"');
  143. f(pas[unitname]);
  144. }
  145. },
  146. loadimpl: function(module){
  147. if (module.$state>=rtl.m_loading_impl) return; // already processing
  148. if (module.$state<rtl.m_intf_loaded) rtl.error('loadimpl: interface not loaded of "'+module.$name+'"');
  149. if (rtl.debug_load_units) rtl.debug('loadimpl: load uses of "'+module.$name+'"');
  150. module.$state=rtl.m_loading_impl;
  151. // load interfaces of implementation useslist
  152. rtl.loaduseslist(module,module.$impluseslist,rtl.loadintf);
  153. // load implementation of interfaces useslist
  154. rtl.loaduseslist(module,module.$intfuseslist,rtl.loadimpl);
  155. // load implementation of implementation useslist
  156. rtl.loaduseslist(module,module.$impluseslist,rtl.loadimpl);
  157. // Note: At this point all interfaces used by this unit are loaded. If
  158. // there are implementation uses cycles some used units might not yet be
  159. // initialized. This is by design.
  160. // run implementation
  161. if (rtl.debug_load_units) rtl.debug('loadimpl: run impl of "'+module.$name+'"');
  162. if (rtl.isFunction(module.$implcode)) module.$implcode(module.$impluseslist);
  163. // run initialization
  164. if (rtl.debug_load_units) rtl.debug('loadimpl: run init of "'+module.$name+'"');
  165. module.$state=rtl.m_initializing;
  166. if (rtl.isFunction(module.$init)) module.$init();
  167. // unit initialized
  168. module.$state=rtl.m_initialized;
  169. },
  170. createCallback: function(scope, fn){
  171. var cb;
  172. if (typeof(fn)==='string'){
  173. cb = function(){
  174. return scope[fn].apply(scope,arguments);
  175. };
  176. } else {
  177. cb = function(){
  178. return fn.apply(scope,arguments);
  179. };
  180. };
  181. cb.scope = scope;
  182. cb.fn = fn;
  183. return cb;
  184. },
  185. cloneCallback: function(cb){
  186. return rtl.createCallback(cb.scope,cb.fn);
  187. },
  188. eqCallback: function(a,b){
  189. // can be a function or a function wrapper
  190. if (a==b){
  191. return true;
  192. } else {
  193. return (a!=null) && (b!=null) && (a.fn) && (a.scope===b.scope) && (a.fn==b.fn);
  194. }
  195. },
  196. initClass: function(c,parent,name,initfn){
  197. parent[name] = c;
  198. c.$classname = name;
  199. if ((parent.$module) && (parent.$module.$impl===parent)) parent=parent.$module;
  200. c.$parent = parent;
  201. c.$fullname = parent.$name+'.'+name;
  202. if (rtl.isModule(parent)){
  203. c.$module = parent;
  204. c.$name = name;
  205. } else {
  206. c.$module = parent.$module;
  207. c.$name = parent.name+'.'+name;
  208. };
  209. // rtti
  210. if (rtl.debug_rtti) rtl.debug('initClass '+c.$fullname);
  211. var t = c.$module.$rtti.$Class(c.$name,{ "class": c, module: parent });
  212. c.$rtti = t;
  213. if (rtl.isObject(c.$ancestor)) t.ancestor = c.$ancestor.$rtti;
  214. if (!t.ancestor) t.ancestor = null;
  215. // init members
  216. initfn.call(c);
  217. },
  218. createClass: function(parent,name,ancestor,initfn){
  219. // create a normal class,
  220. // ancestor must be null or a normal class,
  221. // the root ancestor can be an external class
  222. var c = null;
  223. if (ancestor != null){
  224. c = Object.create(ancestor);
  225. c.$ancestor = ancestor;
  226. // Note:
  227. // if root is an "object" then c.$ancestor === Object.getPrototypeOf(c)
  228. // if root is a "function" then c.$ancestor === c.__proto__, Object.getPrototypeOf(c) returns the root
  229. } else {
  230. c = {};
  231. c.$create = function(fnname,args){
  232. if (args == undefined) args = [];
  233. var o = Object.create(this);
  234. o.$class = this; // Note: o.$class === Object.getPrototypeOf(o)
  235. o.$init();
  236. try{
  237. o[fnname].apply(o,args);
  238. o.AfterConstruction();
  239. } catch($e){
  240. o.$destroy;
  241. throw $e;
  242. }
  243. return o;
  244. };
  245. c.$destroy = function(fnname){
  246. this.BeforeDestruction();
  247. this[fnname]();
  248. this.$final;
  249. };
  250. };
  251. rtl.initClass(c,parent,name,initfn);
  252. },
  253. createClassExt: function(parent,name,ancestor,newinstancefnname,initfn){
  254. // Create a class using an external ancestor.
  255. // If newinstancefnname is given, use that function to create the new object.
  256. // If exist call BeforeDestruction and AfterConstruction.
  257. var c = null;
  258. c = Object.create(ancestor);
  259. c.$create = function(fnname,args){
  260. if (args == undefined) args = [];
  261. var o = null;
  262. if (newinstancefnname.length>0){
  263. o = this[newinstancefnname](fnname,args);
  264. } else {
  265. o = Object.create(this);
  266. }
  267. o.$class = this; // Note: o.$class === Object.getPrototypeOf(o)
  268. o.$init();
  269. try{
  270. o[fnname].apply(o,args);
  271. if (o.AfterConstruction) o.AfterConstruction();
  272. } catch($e){
  273. o.$destroy;
  274. throw $e;
  275. }
  276. return o;
  277. };
  278. c.$destroy = function(fnname){
  279. if (this.BeforeDestruction) this.BeforeDestruction();
  280. this[fnname]();
  281. this.$final;
  282. };
  283. rtl.initClass(c,parent,name,initfn);
  284. },
  285. tObjectDestroy: "Destroy",
  286. free: function(obj,name){
  287. if (obj[name]==null) return;
  288. obj[name].$destroy(rtl.tObjectDestroy);
  289. obj[name]=null;
  290. },
  291. freeLoc: function(obj){
  292. if (obj==null) return;
  293. obj.$destroy(rtl.tObjectDestroy);
  294. return null;
  295. },
  296. is: function(instance,type){
  297. return type.isPrototypeOf(instance) || (instance===type);
  298. },
  299. isExt: function(instance,type,mode){
  300. // mode===1 means instance must be a Pascal class instance
  301. // mode===2 means instance must be a Pascal class
  302. // Notes:
  303. // isPrototypeOf and instanceof return false on equal
  304. // isPrototypeOf does not work for Date.isPrototypeOf(new Date())
  305. // so if isPrototypeOf is false test with instanceof
  306. // instanceof needs a function on right side
  307. if (instance == null) return false; // Note: ==null checks for undefined too
  308. if ((typeof(type) !== 'object') && (typeof(type) !== 'function')) return false;
  309. if (instance === type){
  310. if (mode===1) return false;
  311. if (mode===2) return rtl.isPasClass(instance);
  312. return true;
  313. }
  314. if (type.isPrototypeOf && type.isPrototypeOf(instance)){
  315. if (mode===1) return rtl.isPasClassInstance(instance);
  316. if (mode===2) return rtl.isPasClass(instance);
  317. return true;
  318. }
  319. if ((typeof type == 'function') && (instance instanceof type)) return true;
  320. return false;
  321. },
  322. Exception: null,
  323. EInvalidCast: null,
  324. EAbstractError: null,
  325. ERangeError: null,
  326. raiseE: function(typename){
  327. var t = rtl[typename];
  328. if (t==null){
  329. var mod = pas.SysUtils;
  330. if (!mod) mod = pas.sysutils;
  331. if (mod){
  332. t = mod[typename];
  333. if (!t) t = mod[typename.toLowerCase()];
  334. if (!t) t = mod['Exception'];
  335. if (!t) t = mod['exception'];
  336. }
  337. }
  338. if (t){
  339. if (t.Create){
  340. throw t.$create("Create");
  341. } else if (t.create){
  342. throw t.$create("create");
  343. }
  344. }
  345. if (typename === "EInvalidCast") throw "invalid type cast";
  346. if (typename === "EAbstractError") throw "Abstract method called";
  347. if (typename === "ERangeError") throw "range error";
  348. throw typename;
  349. },
  350. as: function(instance,type){
  351. if((instance === null) || rtl.is(instance,type)) return instance;
  352. rtl.raiseE("EInvalidCast");
  353. },
  354. asExt: function(instance,type,mode){
  355. if((instance === null) || rtl.isExt(instance,type,mode)) return instance;
  356. rtl.raiseE("EInvalidCast");
  357. },
  358. createInterface: function(module, name, guid, fnnames, ancestor, initfn){
  359. //console.log('createInterface name="'+name+'" guid="'+guid+'" names='+fnnames);
  360. var i = ancestor?Object.create(ancestor):{};
  361. module[name] = i;
  362. i.$module = module;
  363. i.$name = name;
  364. i.$fullname = module.$name+'.'+name;
  365. i.$guid = guid;
  366. i.$guidr = null;
  367. i.$names = fnnames?fnnames:[];
  368. if (rtl.isFunction(initfn)){
  369. // rtti
  370. if (rtl.debug_rtti) rtl.debug('createInterface '+i.$fullname);
  371. var t = i.$module.$rtti.$Interface(name,{ "interface": i, module: module });
  372. i.$rtti = t;
  373. if (ancestor) t.ancestor = ancestor.$rtti;
  374. if (!t.ancestor) t.ancestor = null;
  375. initfn.call(i);
  376. }
  377. return i;
  378. },
  379. strToGUIDR: function(s,g){
  380. var p = 0;
  381. function n(l){
  382. var h = s.substr(p,l);
  383. p+=l;
  384. return parseInt(h,16);
  385. }
  386. p+=1; // skip {
  387. g.D1 = n(8);
  388. p+=1; // skip -
  389. g.D2 = n(4);
  390. p+=1; // skip -
  391. g.D3 = n(4);
  392. p+=1; // skip -
  393. if (!g.D4) g.D4=[];
  394. g.D4[0] = n(2);
  395. g.D4[1] = n(2);
  396. p+=1; // skip -
  397. for(var i=2; i<8; i++) g.D4[i] = n(2);
  398. return g;
  399. },
  400. guidrToStr: function(g){
  401. if (g.$intf) return g.$intf.$guid;
  402. var h = rtl.hexStr;
  403. var s='{'+h(g.D1,8)+'-'+h(g.D2,4)+'-'+h(g.D3,4)+'-'+h(g.D4[0],2)+h(g.D4[1],2)+'-';
  404. for (var i=2; i<8; i++) s+=h(g.D4[i],2);
  405. s+='}';
  406. return s;
  407. },
  408. createTGUID: function(guid){
  409. var TGuid = (pas.System)?pas.System.TGuid:pas.system.tguid;
  410. var g = rtl.strToGUIDR(guid,new TGuid());
  411. return g;
  412. },
  413. getIntfGUIDR: function(intfTypeOrVar){
  414. if (!intfTypeOrVar) return null;
  415. if (!intfTypeOrVar.$guidr){
  416. var g = rtl.createTGUID(intfTypeOrVar.$guid);
  417. if (!intfTypeOrVar.hasOwnProperty('$guid')) intfTypeOrVar = Object.getPrototypeOf(intfTypeOrVar);
  418. g.$intf = intfTypeOrVar;
  419. intfTypeOrVar.$guidr = g;
  420. }
  421. return intfTypeOrVar.$guidr;
  422. },
  423. addIntf: function (aclass, intf, map){
  424. function jmp(fn){
  425. if (typeof(fn)==="function"){
  426. return function(){ return fn.apply(this.$o,arguments); };
  427. } else {
  428. return function(){ rtl.raiseE('EAbstractError'); };
  429. }
  430. }
  431. if(!map) map = {};
  432. var t = intf;
  433. var item = Object.create(t);
  434. aclass.$intfmaps[intf.$guid] = item;
  435. do{
  436. var names = t.$names;
  437. if (!names) break;
  438. for (var i=0; i<names.length; i++){
  439. var intfname = names[i];
  440. var fnname = map[intfname];
  441. if (!fnname) fnname = intfname;
  442. //console.log('addIntf: intftype='+t.$name+' index='+i+' intfname="'+intfname+'" fnname="'+fnname+'" proc='+typeof(fn));
  443. item[intfname] = jmp(aclass[fnname]);
  444. }
  445. t = Object.getPrototypeOf(t);
  446. }while(t!=null);
  447. },
  448. getIntfG: function (obj, guid, query){
  449. if (!obj) return null;
  450. //console.log('getIntfG: obj='+obj.$classname+' guid='+guid+' query='+query);
  451. // search
  452. var maps = obj.$intfmaps;
  453. if (!maps) return null;
  454. var item = maps[guid];
  455. if (!item) return null;
  456. // check delegation
  457. //console.log('getIntfG: obj='+obj.$classname+' guid='+guid+' query='+query+' item='+typeof(item));
  458. if (typeof item === 'function') return item.call(obj); // COM: contains _AddRef
  459. // check cache
  460. var intf = null;
  461. if (obj.$interfaces){
  462. intf = obj.$interfaces[guid];
  463. //console.log('getIntfG: obj='+obj.$classname+' guid='+guid+' cache='+typeof(intf));
  464. }
  465. if (!intf){ // intf can be undefined!
  466. intf = Object.create(item);
  467. intf.$o = obj;
  468. if (!obj.$interfaces) obj.$interfaces = {};
  469. obj.$interfaces[guid] = intf;
  470. }
  471. if (typeof(query)==='object'){
  472. // called by queryIntfT
  473. var o = null;
  474. if (intf.QueryInterface(rtl.getIntfGUIDR(query),
  475. {get:function(){ return o; }, set:function(v){ o=v; }}) === 0){
  476. return o;
  477. } else {
  478. return null;
  479. }
  480. } else if(query===2){
  481. // called by TObject.GetInterfaceByStr
  482. if (intf.$kind === 'com') intf._AddRef();
  483. }
  484. return intf;
  485. },
  486. getIntfT: function(obj,intftype){
  487. return rtl.getIntfG(obj,intftype.$guid);
  488. },
  489. queryIntfT: function(obj,intftype){
  490. return rtl.getIntfG(obj,intftype.$guid,intftype);
  491. },
  492. queryIntfIsT: function(obj,intftype){
  493. var i = rtl.queryIntfG(obj,intftype.$guid);
  494. if (!i) return false;
  495. if (i.$kind === 'com') i._Release();
  496. return true;
  497. },
  498. asIntfT: function (obj,intftype){
  499. var i = rtl.getIntfG(obj,intftype.$guid);
  500. if (i!==null) return i;
  501. rtl.raiseEInvalidCast();
  502. },
  503. intfIsClass: function(intf,classtype){
  504. return (intf!=null) && (rtl.is(intf.$o,classtype));
  505. },
  506. intfAsClass: function(intf,classtype){
  507. if (intf==null) return null;
  508. return rtl.as(intf.$o,classtype);
  509. },
  510. intfToClass: function(intf,classtype){
  511. if ((intf!==null) && rtl.is(intf.$o,classtype)) return intf.$o;
  512. return null;
  513. },
  514. // interface reference counting
  515. intfRefs: { // base object for temporary interface variables
  516. ref: function(id,intf){
  517. // called for temporary interface references needing delayed release
  518. var old = this[id];
  519. //console.log('rtl.intfRefs.ref: id='+id+' old="'+(old?old.$name:'null')+'" intf="'+(intf?intf.$name:'null'));
  520. if (old){
  521. // called again, e.g. in a loop
  522. delete this[id];
  523. old._Release(); // may fail
  524. }
  525. this[id]=intf;
  526. return intf;
  527. },
  528. free: function(){
  529. //console.log('rtl.intfRefs.free...');
  530. for (var id in this){
  531. if (this.hasOwnProperty(id)) this[id]._Release;
  532. }
  533. }
  534. },
  535. createIntfRefs: function(){
  536. //console.log('rtl.createIntfRefs');
  537. return Object.create(rtl.intfRefs);
  538. },
  539. setIntfP: function(path,name,value,skipAddRef){
  540. var old = path[name];
  541. //console.log('rtl.setIntfP path='+path+' name='+name+' old="'+(old?old.$name:'null')+'" value="'+(value?value.$name:'null')+'"');
  542. if (old === value) return;
  543. if (old !== null){
  544. path[name]=null;
  545. old._Release();
  546. }
  547. if (value !== null){
  548. if (!skipAddRef) value._AddRef();
  549. path[name]=value;
  550. }
  551. },
  552. setIntfL: function(old,value,skipAddRef){
  553. //console.log('rtl.setIntfL old="'+(old?old.$name:'null')+'" value="'+(value?value.$name:'null')+'"');
  554. if (old !== value){
  555. if (value!==null){
  556. if (!skipAddRef) value._AddRef();
  557. }
  558. if (old!==null){
  559. old._Release(); // Release after AddRef, to avoid double Release if Release creates an exception
  560. }
  561. } else if (skipAddRef){
  562. if (old!==null){
  563. old._Release(); // value has an AddRef
  564. }
  565. }
  566. return value;
  567. },
  568. _AddRef: function(intf){
  569. //if (intf) console.log('rtl._AddRef intf="'+(intf?intf.$name:'null')+'"');
  570. if (intf) intf._AddRef();
  571. return intf;
  572. },
  573. _Release: function(intf){
  574. //if (intf) console.log('rtl._Release intf="'+(intf?intf.$name:'null')+'"');
  575. if (intf) intf._Release();
  576. return intf;
  577. },
  578. checkMethodCall: function(obj,type){
  579. if (rtl.isObject(obj) && rtl.is(obj,type)) return;
  580. rtl.raiseE("EInvalidCast");
  581. },
  582. rc: function(i,minval,maxval){
  583. // range check integer
  584. if ((Math.floor(i)===i) && (i>=minval) && (i<=maxval)) return i;
  585. rtl.raiseE('ERangeError');
  586. },
  587. rcc: function(c,minval,maxval){
  588. // range check char
  589. if ((typeof(c)==='string') && (c.length===1)){
  590. var i = c.charCodeAt(0);
  591. if ((i>=minval) && (i<=maxval)) return c;
  592. }
  593. rtl.raiseE('ERangeError');
  594. },
  595. rcSetCharAt: function(s,index,c){
  596. // range check setCharAt
  597. if ((typeof(s)!=='string') || (index<0) || (index>=s.length)) rtl.raiseE('ERangeError');
  598. return rtl.setCharAt(s,index,c);
  599. },
  600. rcCharAt: function(s,index){
  601. // range check charAt
  602. if ((typeof(s)!=='string') || (index<0) || (index>=s.length)) rtl.raiseE('ERangeError');
  603. return s.charAt(index);
  604. },
  605. rcArrR: function(arr,index){
  606. // range check read array
  607. if (Array.isArray(arr) && (typeof(index)==='number') && (index>=0) && (index<arr.length)){
  608. if (arguments.length>2){
  609. // arr,index1,index2,...
  610. arr=arr[index];
  611. for (var i=2; i<arguments.length; i++) arr=rtl.rcArrR(arr,arguments[i]);
  612. return arr;
  613. }
  614. return arr[index];
  615. }
  616. rtl.raiseE('ERangeError');
  617. },
  618. rcArrW: function(arr,index,value){
  619. // range check write array
  620. // arr,index1,index2,...,value
  621. for (var i=3; i<arguments.length; i++){
  622. arr=rtl.rcArrR(arr,index);
  623. index=arguments[i-1];
  624. value=arguments[i];
  625. }
  626. if (Array.isArray(arr) && (typeof(index)==='number') && (index>=0) && (index<arr.length)){
  627. return arr[index]=value;
  628. }
  629. rtl.raiseE('ERangeError');
  630. },
  631. length: function(arr){
  632. return (arr == null) ? 0 : arr.length;
  633. },
  634. arraySetLength: function(arr,defaultvalue,newlength){
  635. // multi dim: (arr,defaultvalue,dim1,dim2,...)
  636. if (arr == null) arr = [];
  637. var p = arguments;
  638. function setLength(a,argNo){
  639. var oldlen = a.length;
  640. var newlen = p[argNo];
  641. if (oldlen!==newlength){
  642. a.length = newlength;
  643. if (argNo === p.length-1){
  644. if (rtl.isArray(defaultvalue)){
  645. for (var i=oldlen; i<newlen; i++) a[i]=[]; // nested array
  646. } else if (rtl.isFunction(defaultvalue)){
  647. for (var i=oldlen; i<newlen; i++) a[i]=new defaultvalue(); // e.g. record
  648. } else if (rtl.isObject(defaultvalue)) {
  649. for (var i=oldlen; i<newlen; i++) a[i]={}; // e.g. set
  650. } else {
  651. for (var i=oldlen; i<newlen; i++) a[i]=defaultvalue;
  652. }
  653. } else {
  654. for (var i=oldlen; i<newlen; i++) a[i]=[]; // nested array
  655. }
  656. }
  657. if (argNo < p.length-1){
  658. // multi argNo
  659. for (var i=0; i<newlen; i++) a[i]=setLength(a[i],argNo+1);
  660. }
  661. return a;
  662. }
  663. return setLength(arr,2);
  664. },
  665. arrayEq: function(a,b){
  666. if (a===null) return b===null;
  667. if (b===null) return false;
  668. if (a.length!==b.length) return false;
  669. for (var i=0; i<a.length; i++) if (a[i]!==b[i]) return false;
  670. return true;
  671. },
  672. arrayClone: function(type,src,srcpos,end,dst,dstpos){
  673. // type: 0 for references, "refset" for calling refSet(), a function for new type()
  674. // src must not be null
  675. // This function does not range check.
  676. if (rtl.isFunction(type)){
  677. for (; srcpos<end; srcpos++) dst[dstpos++] = new type(src[srcpos]); // clone record
  678. } else if((typeof(type)==="string") && (type === 'refSet')) {
  679. for (; srcpos<end; srcpos++) dst[dstpos++] = rtl.refSet(src[srcpos]); // ref set
  680. } else {
  681. for (; srcpos<end; srcpos++) dst[dstpos++] = src[srcpos]; // reference
  682. };
  683. },
  684. arrayConcat: function(type){
  685. // type: see rtl.arrayClone
  686. var a = [];
  687. var l = 0;
  688. for (var i=1; i<arguments.length; i++) l+=arguments[i].length;
  689. a.length = l;
  690. l=0;
  691. for (var i=1; i<arguments.length; i++){
  692. var src = arguments[i];
  693. if (src == null) continue;
  694. rtl.arrayClone(type,src,0,src.length,a,l);
  695. l+=src.length;
  696. };
  697. return a;
  698. },
  699. arrayCopy: function(type, srcarray, index, count){
  700. // type: see rtl.arrayClone
  701. // if count is missing, use srcarray.length
  702. if (srcarray == null) return [];
  703. if (index < 0) index = 0;
  704. if (count === undefined) count=srcarray.length;
  705. var end = index+count;
  706. if (end>srcarray.length) end = srcarray.length;
  707. if (index>=end) return [];
  708. if (type===0){
  709. return srcarray.slice(index,end);
  710. } else {
  711. var a = [];
  712. a.length = end-index;
  713. rtl.arrayClone(type,srcarray,index,end,a,0);
  714. return a;
  715. }
  716. },
  717. setCharAt: function(s,index,c){
  718. return s.substr(0,index)+c+s.substr(index+1);
  719. },
  720. getResStr: function(mod,name){
  721. var rs = mod.$resourcestrings[name];
  722. return rs.current?rs.current:rs.org;
  723. },
  724. createSet: function(){
  725. var s = {};
  726. for (var i=0; i<arguments.length; i++){
  727. if (arguments[i]!=null){
  728. s[arguments[i]]=true;
  729. } else {
  730. var first=arguments[i+=1];
  731. var last=arguments[i+=1];
  732. for(var j=first; j<=last; j++) s[j]=true;
  733. }
  734. }
  735. return s;
  736. },
  737. cloneSet: function(s){
  738. var r = {};
  739. for (var key in s) r[key]=true;
  740. return r;
  741. },
  742. refSet: function(s){
  743. s.$shared = true;
  744. return s;
  745. },
  746. includeSet: function(s,enumvalue){
  747. if (s.$shared) s = rtl.cloneSet(s);
  748. s[enumvalue] = true;
  749. return s;
  750. },
  751. excludeSet: function(s,enumvalue){
  752. if (s.$shared) s = rtl.cloneSet(s);
  753. delete s[enumvalue];
  754. return s;
  755. },
  756. diffSet: function(s,t){
  757. var r = {};
  758. for (var key in s) if (!t[key]) r[key]=true;
  759. delete r.$shared;
  760. return r;
  761. },
  762. unionSet: function(s,t){
  763. var r = {};
  764. for (var key in s) r[key]=true;
  765. for (var key in t) r[key]=true;
  766. delete r.$shared;
  767. return r;
  768. },
  769. intersectSet: function(s,t){
  770. var r = {};
  771. for (var key in s) if (t[key]) r[key]=true;
  772. delete r.$shared;
  773. return r;
  774. },
  775. symDiffSet: function(s,t){
  776. var r = {};
  777. for (var key in s) if (!t[key]) r[key]=true;
  778. for (var key in t) if (!s[key]) r[key]=true;
  779. delete r.$shared;
  780. return r;
  781. },
  782. eqSet: function(s,t){
  783. for (var key in s) if (!t[key] && (key!='$shared')) return false;
  784. for (var key in t) if (!s[key] && (key!='$shared')) return false;
  785. return true;
  786. },
  787. neSet: function(s,t){
  788. return !rtl.eqSet(s,t);
  789. },
  790. leSet: function(s,t){
  791. for (var key in s) if (!t[key] && (key!='$shared')) return false;
  792. return true;
  793. },
  794. geSet: function(s,t){
  795. for (var key in t) if (!s[key] && (key!='$shared')) return false;
  796. return true;
  797. },
  798. strSetLength: function(s,newlen){
  799. var oldlen = s.length;
  800. if (oldlen > newlen){
  801. return s.substring(0,newlen);
  802. } else if (s.repeat){
  803. // Note: repeat needs ECMAScript6!
  804. return s+' '.repeat(newlen-oldlen);
  805. } else {
  806. while (oldlen<newlen){
  807. s+=' ';
  808. oldlen++;
  809. };
  810. return s;
  811. }
  812. },
  813. spaceLeft: function(s,width){
  814. var l=s.length;
  815. if (l>=width) return s;
  816. if (s.repeat){
  817. // Note: repeat needs ECMAScript6!
  818. return ' '.repeat(width-l) + s;
  819. } else {
  820. while (l<width){
  821. s=' '+s;
  822. l++;
  823. };
  824. };
  825. },
  826. floatToStr : function(d,w,p){
  827. // input 1-3 arguments: double, width, precision
  828. if (arguments.length>2){
  829. return rtl.spaceLeft(d.toFixed(p),w);
  830. } else {
  831. // exponent width
  832. var pad = "";
  833. var ad = Math.abs(d);
  834. if (ad<1.0e+10) {
  835. pad='00';
  836. } else if (ad<1.0e+100) {
  837. pad='0';
  838. }
  839. if (arguments.length<2) {
  840. w=9;
  841. } else if (w<9) {
  842. w=9;
  843. }
  844. var p = w-8;
  845. var s=(d>0 ? " " : "" ) + d.toExponential(p);
  846. s=s.replace(/e(.)/,'E$1'+pad);
  847. return rtl.spaceLeft(s,w);
  848. }
  849. },
  850. initRTTI: function(){
  851. if (rtl.debug_rtti) rtl.debug('initRTTI');
  852. // base types
  853. rtl.tTypeInfo = { name: "tTypeInfo" };
  854. function newBaseTI(name,kind,ancestor){
  855. if (!ancestor) ancestor = rtl.tTypeInfo;
  856. if (rtl.debug_rtti) rtl.debug('initRTTI.newBaseTI "'+name+'" '+kind+' ("'+ancestor.name+'")');
  857. var t = Object.create(ancestor);
  858. t.name = name;
  859. t.kind = kind;
  860. rtl[name] = t;
  861. return t;
  862. };
  863. function newBaseInt(name,minvalue,maxvalue,ordtype){
  864. var t = newBaseTI(name,1 /* tkInteger */,rtl.tTypeInfoInteger);
  865. t.minvalue = minvalue;
  866. t.maxvalue = maxvalue;
  867. t.ordtype = ordtype;
  868. return t;
  869. };
  870. newBaseTI("tTypeInfoInteger",1 /* tkInteger */);
  871. newBaseInt("shortint",-0x80,0x7f,0);
  872. newBaseInt("byte",0,0xff,1);
  873. newBaseInt("smallint",-0x8000,0x7fff,2);
  874. newBaseInt("word",0,0xffff,3);
  875. newBaseInt("longint",-0x80000000,0x7fffffff,4);
  876. newBaseInt("longword",0,0xffffffff,5);
  877. newBaseInt("nativeint",-0x10000000000000,0xfffffffffffff,6);
  878. newBaseInt("nativeuint",0,0xfffffffffffff,7);
  879. newBaseTI("char",2 /* tkChar */);
  880. newBaseTI("string",3 /* tkString */);
  881. newBaseTI("tTypeInfoEnum",4 /* tkEnumeration */,rtl.tTypeInfoInteger);
  882. newBaseTI("tTypeInfoSet",5 /* tkSet */);
  883. newBaseTI("double",6 /* tkDouble */);
  884. newBaseTI("boolean",7 /* tkBool */);
  885. newBaseTI("tTypeInfoProcVar",8 /* tkProcVar */);
  886. newBaseTI("tTypeInfoMethodVar",9 /* tkMethod */,rtl.tTypeInfoProcVar);
  887. newBaseTI("tTypeInfoArray",10 /* tkArray */);
  888. newBaseTI("tTypeInfoDynArray",11 /* tkDynArray */);
  889. newBaseTI("tTypeInfoPointer",15 /* tkPointer */);
  890. var t = newBaseTI("pointer",15 /* tkPointer */,rtl.tTypeInfoPointer);
  891. t.reftype = null;
  892. newBaseTI("jsvalue",16 /* tkJSValue */);
  893. newBaseTI("tTypeInfoRefToProcVar",17 /* tkRefToProcVar */,rtl.tTypeInfoProcVar);
  894. // member kinds
  895. rtl.tTypeMember = {};
  896. function newMember(name,kind){
  897. var m = Object.create(rtl.tTypeMember);
  898. m.name = name;
  899. m.kind = kind;
  900. rtl[name] = m;
  901. };
  902. newMember("tTypeMemberField",1); // tmkField
  903. newMember("tTypeMemberMethod",2); // tmkMethod
  904. newMember("tTypeMemberProperty",3); // tmkProperty
  905. // base object for storing members: a simple object
  906. rtl.tTypeMembers = {};
  907. // tTypeInfoStruct - base object for tTypeInfoClass, tTypeInfoRecord, tTypeInfoInterface
  908. var tis = newBaseTI("tTypeInfoStruct",0);
  909. tis.$addMember = function(name,ancestor,options){
  910. if (rtl.debug_rtti){
  911. if (!rtl.hasString(name) || (name.charAt()==='$')) throw 'invalid member "'+name+'", this="'+this.name+'"';
  912. if (!rtl.is(ancestor,rtl.tTypeMember)) throw 'invalid ancestor "'+ancestor+':'+ancestor.name+'", "'+this.name+'.'+name+'"';
  913. if ((options!=undefined) && (typeof(options)!='object')) throw 'invalid options "'+options+'", "'+this.name+'.'+name+'"';
  914. };
  915. var t = Object.create(ancestor);
  916. t.name = name;
  917. this.members[name] = t;
  918. this.names.push(name);
  919. if (rtl.isObject(options)){
  920. for (var key in options) if (options.hasOwnProperty(key)) t[key] = options[key];
  921. };
  922. return t;
  923. };
  924. tis.addField = function(name,type,options){
  925. var t = this.$addMember(name,rtl.tTypeMemberField,options);
  926. if (rtl.debug_rtti){
  927. if (!rtl.is(type,rtl.tTypeInfo)) throw 'invalid type "'+type+'", "'+this.name+'.'+name+'"';
  928. };
  929. t.typeinfo = type;
  930. this.fields.push(name);
  931. return t;
  932. };
  933. tis.addFields = function(){
  934. var i=0;
  935. while(i<arguments.length){
  936. var name = arguments[i++];
  937. var type = arguments[i++];
  938. if ((i<arguments.length) && (typeof(arguments[i])==='object')){
  939. this.addField(name,type,arguments[i++]);
  940. } else {
  941. this.addField(name,type);
  942. };
  943. };
  944. };
  945. tis.addMethod = function(name,methodkind,params,result,options){
  946. var t = this.$addMember(name,rtl.tTypeMemberMethod,options);
  947. t.methodkind = methodkind;
  948. t.procsig = rtl.newTIProcSig(params);
  949. t.procsig.resulttype = result?result:null;
  950. this.methods.push(name);
  951. return t;
  952. };
  953. tis.addProperty = function(name,flags,result,getter,setter,options){
  954. var t = this.$addMember(name,rtl.tTypeMemberProperty,options);
  955. t.flags = flags;
  956. t.typeinfo = result;
  957. t.getter = getter;
  958. t.setter = setter;
  959. // Note: in options: params, stored, defaultvalue
  960. if (rtl.isArray(t.params)) t.params = rtl.newTIParams(t.params);
  961. this.properties.push(name);
  962. if (!rtl.isString(t.stored)) t.stored = "";
  963. return t;
  964. };
  965. tis.getField = function(index){
  966. return this.members[this.fields[index]];
  967. };
  968. tis.getMethod = function(index){
  969. return this.members[this.methods[index]];
  970. };
  971. tis.getProperty = function(index){
  972. return this.members[this.properties[index]];
  973. };
  974. newBaseTI("tTypeInfoRecord",12 /* tkRecord */,rtl.tTypeInfoStruct);
  975. newBaseTI("tTypeInfoClass",13 /* tkClass */,rtl.tTypeInfoStruct);
  976. newBaseTI("tTypeInfoClassRef",14 /* tkClassRef */);
  977. newBaseTI("tTypeInfoInterface",15 /* tkInterface */,rtl.tTypeInfoStruct);
  978. },
  979. tSectionRTTI: {
  980. $module: null,
  981. $inherited: function(name,ancestor,o){
  982. if (rtl.debug_rtti){
  983. rtl.debug('tSectionRTTI.newTI "'+(this.$module?this.$module.$name:"(no module)")
  984. +'"."'+name+'" ('+ancestor.name+') '+(o?'init':'forward'));
  985. };
  986. var t = this[name];
  987. if (t){
  988. if (!t.$forward) throw 'duplicate type "'+name+'"';
  989. if (!ancestor.isPrototypeOf(t)) throw 'typeinfo ancestor mismatch "'+name+'" ancestor="'+ancestor.name+'" t.name="'+t.name+'"';
  990. } else {
  991. t = Object.create(ancestor);
  992. t.name = name;
  993. t.$module = this.$module;
  994. this[name] = t;
  995. }
  996. if (o){
  997. delete t.$forward;
  998. for (var key in o) if (o.hasOwnProperty(key)) t[key]=o[key];
  999. } else {
  1000. t.$forward = true;
  1001. }
  1002. return t;
  1003. },
  1004. $Scope: function(name,ancestor,o){
  1005. var t=this.$inherited(name,ancestor,o);
  1006. t.members = {};
  1007. t.names = [];
  1008. t.fields = [];
  1009. t.methods = [];
  1010. t.properties = [];
  1011. return t;
  1012. },
  1013. $TI: function(name,kind,o){ var t=this.$inherited(name,rtl.tTypeInfo,o); t.kind = kind; return t; },
  1014. $Int: function(name,o){ return this.$inherited(name,rtl.tTypeInfoInteger,o); },
  1015. $Enum: function(name,o){ return this.$inherited(name,rtl.tTypeInfoEnum,o); },
  1016. $Set: function(name,o){ return this.$inherited(name,rtl.tTypeInfoSet,o); },
  1017. $StaticArray: function(name,o){ return this.$inherited(name,rtl.tTypeInfoArray,o); },
  1018. $DynArray: function(name,o){ return this.$inherited(name,rtl.tTypeInfoDynArray,o); },
  1019. $ProcVar: function(name,o){ return this.$inherited(name,rtl.tTypeInfoProcVar,o); },
  1020. $RefToProcVar: function(name,o){ return this.$inherited(name,rtl.tTypeInfoRefToProcVar,o); },
  1021. $MethodVar: function(name,o){ return this.$inherited(name,rtl.tTypeInfoMethodVar,o); },
  1022. $Record: function(name,o){ return this.$Scope(name,rtl.tTypeInfoRecord,o); },
  1023. $Class: function(name,o){ return this.$Scope(name,rtl.tTypeInfoClass,o); },
  1024. $ClassRef: function(name,o){ return this.$inherited(name,rtl.tTypeInfoClassRef,o); },
  1025. $Pointer: function(name,o){ return this.$inherited(name,rtl.tTypeInfoPointer,o); },
  1026. $Interface: function(name,o){ return this.$Scope(name,rtl.tTypeInfoInterface,o); }
  1027. },
  1028. newTIParam: function(param){
  1029. // param is an array, 0=name, 1=type, 2=optional flags
  1030. var t = {
  1031. name: param[0],
  1032. typeinfo: param[1],
  1033. flags: (rtl.isNumber(param[2]) ? param[2] : 0)
  1034. };
  1035. return t;
  1036. },
  1037. newTIParams: function(list){
  1038. // list: optional array of [paramname,typeinfo,optional flags]
  1039. var params = [];
  1040. if (rtl.isArray(list)){
  1041. for (var i=0; i<list.length; i++) params.push(rtl.newTIParam(list[i]));
  1042. };
  1043. return params;
  1044. },
  1045. newTIProcSig: function(params,result,flags){
  1046. var s = {
  1047. params: rtl.newTIParams(params),
  1048. resulttype: result,
  1049. flags: flags
  1050. };
  1051. return s;
  1052. }
  1053. }
  1054. rtl.module("System",[],function () {
  1055. "use strict";
  1056. var $mod = this;
  1057. var $impl = $mod.$impl;
  1058. this.LineEnding = "\n";
  1059. this.sLineBreak = $mod.LineEnding;
  1060. rtl.createClass($mod,"TObject",null,function () {
  1061. this.$init = function () {
  1062. };
  1063. this.$final = function () {
  1064. };
  1065. this.Create = function () {
  1066. };
  1067. this.AfterConstruction = function () {
  1068. };
  1069. this.BeforeDestruction = function () {
  1070. };
  1071. });
  1072. this.Random = function (Range) {
  1073. return Math.floor(Math.random()*Range);
  1074. };
  1075. this.Trunc = function (A) {
  1076. if (!Math.trunc) {
  1077. Math.trunc = function(v) {
  1078. v = +v;
  1079. if (!isFinite(v)) return v;
  1080. return (v - v % 1) || (v < 0 ? -0 : v === 0 ? v : 0);
  1081. };
  1082. }
  1083. $mod.Trunc = Math.trunc;
  1084. return Math.trunc(A);
  1085. };
  1086. this.Writeln = function () {
  1087. var i = 0;
  1088. var l = 0;
  1089. var s = "";
  1090. l = rtl.length(arguments) - 1;
  1091. if ($impl.WriteCallBack != null) {
  1092. for (var $l1 = 0, $end2 = l; $l1 <= $end2; $l1++) {
  1093. i = $l1;
  1094. $impl.WriteCallBack(arguments[i],i === l);
  1095. };
  1096. } else {
  1097. s = $impl.WriteBuf;
  1098. for (var $l3 = 0, $end4 = l; $l3 <= $end4; $l3++) {
  1099. i = $l3;
  1100. s = s + ("" + arguments[i]);
  1101. };
  1102. console.log(s);
  1103. $impl.WriteBuf = "";
  1104. };
  1105. };
  1106. this.SetWriteCallBack = function (H) {
  1107. var Result = null;
  1108. Result = $impl.WriteCallBack;
  1109. $impl.WriteCallBack = H;
  1110. return Result;
  1111. };
  1112. $mod.$init = function () {
  1113. rtl.exitcode = 0;
  1114. };
  1115. },null,function () {
  1116. "use strict";
  1117. var $mod = this;
  1118. var $impl = $mod.$impl;
  1119. $impl.WriteBuf = "";
  1120. $impl.WriteCallBack = null;
  1121. });
  1122. rtl.module("JS",["System"],function () {
  1123. "use strict";
  1124. var $mod = this;
  1125. });
  1126. rtl.module("SysUtils",["System","JS"],function () {
  1127. "use strict";
  1128. var $mod = this;
  1129. rtl.createClass($mod,"Exception",pas.System.TObject,function () {
  1130. this.$init = function () {
  1131. pas.System.TObject.$init.call(this);
  1132. this.fMessage = "";
  1133. };
  1134. this.Create$1 = function (Msg) {
  1135. this.fMessage = Msg;
  1136. };
  1137. });
  1138. this.IntToStr = function (Value) {
  1139. var Result = "";
  1140. Result = "" + Value;
  1141. return Result;
  1142. };
  1143. rtl.createClass($mod,"TFormatSettings",pas.System.TObject,function () {
  1144. });
  1145. this.FormatSettings = null;
  1146. $mod.$init = function () {
  1147. $mod.FormatSettings = $mod.TFormatSettings.$create("Create");
  1148. };
  1149. });
  1150. rtl.module("math",["System","SysUtils"],function () {
  1151. "use strict";
  1152. var $mod = this;
  1153. this.Floor = function (A) {
  1154. var Result = 0;
  1155. Result = pas.System.Trunc(Math.floor(A));
  1156. return Result;
  1157. };
  1158. });
  1159. rtl.module("Noise",["System","SysUtils","math"],function () {
  1160. "use strict";
  1161. var $mod = this;
  1162. this.kNoisekPerumationMax = 256;
  1163. rtl.createClass($mod,"TNoise",pas.System.TObject,function () {
  1164. this.$init = function () {
  1165. pas.System.TObject.$init.call(this);
  1166. this.repeatValue = 0;
  1167. this.p = rtl.arraySetLength(null,0,512);
  1168. };
  1169. this.$final = function () {
  1170. this.p = undefined;
  1171. pas.System.TObject.$final.call(this);
  1172. };
  1173. this.Create$2 = function (seed) {
  1174. var i = 0;
  1175. this.repeatValue = -1;
  1176. for (i = 0; i <= 511; i++) this.p[i] = seed[i % 256];
  1177. };
  1178. this.GetValue = function (x, y, z) {
  1179. var Self = this;
  1180. var Result = 0.0;
  1181. function FMod(a, b) {
  1182. var Result = 0.0;
  1183. Result = a - (b * pas.System.Trunc(a / b));
  1184. return Result;
  1185. };
  1186. var xi = 0;
  1187. var yi = 0;
  1188. var zi = 0;
  1189. var xf = 0.0;
  1190. var yf = 0.0;
  1191. var zf = 0.0;
  1192. var u = 0.0;
  1193. var v = 0.0;
  1194. var w = 0.0;
  1195. var aaa = 0;
  1196. var aba = 0;
  1197. var aab = 0;
  1198. var abb = 0;
  1199. var baa = 0;
  1200. var bba = 0;
  1201. var bab = 0;
  1202. var bbb = 0;
  1203. var x1 = 0.0;
  1204. var x2 = 0.0;
  1205. var y1 = 0.0;
  1206. var y2 = 0.0;
  1207. if (Self.repeatValue > 0) {
  1208. x = FMod(x,Self.repeatValue);
  1209. y = FMod(y,Self.repeatValue);
  1210. z = FMod(z,Self.repeatValue);
  1211. };
  1212. xi = pas.math.Floor(x) & 255;
  1213. yi = pas.math.Floor(y) & 255;
  1214. zi = pas.math.Floor(z) & 255;
  1215. xf = x - pas.math.Floor(x);
  1216. yf = y - pas.math.Floor(y);
  1217. zf = z - pas.math.Floor(z);
  1218. u = Self.Fade(xf);
  1219. v = Self.Fade(yf);
  1220. w = Self.Fade(zf);
  1221. aaa = Self.p[Self.p[Self.p[xi] + yi] + zi];
  1222. aba = Self.p[Self.p[Self.p[xi] + Self.Inc(yi)] + zi];
  1223. aab = Self.p[Self.p[Self.p[xi] + yi] + Self.Inc(zi)];
  1224. abb = Self.p[Self.p[Self.p[xi] + Self.Inc(yi)] + Self.Inc(zi)];
  1225. baa = Self.p[Self.p[Self.p[Self.Inc(xi)] + yi] + zi];
  1226. bba = Self.p[Self.p[Self.p[Self.Inc(xi)] + Self.Inc(yi)] + zi];
  1227. bab = Self.p[Self.p[Self.p[Self.Inc(xi)] + yi] + Self.Inc(zi)];
  1228. bbb = Self.p[Self.p[Self.p[Self.Inc(xi)] + Self.Inc(yi)] + Self.Inc(zi)];
  1229. x1 = Self.Lerp(Self.Grad(aaa,xf,yf,zf),Self.Grad(baa,xf - 1,yf,zf),u);
  1230. x2 = Self.Lerp(Self.Grad(aba,xf,yf - 1,zf),Self.Grad(bba,xf - 1,yf - 1,zf),u);
  1231. y1 = Self.Lerp(x1,x2,v);
  1232. x1 = Self.Lerp(Self.Grad(aab,xf,yf,zf - 1),Self.Grad(bab,xf - 1,yf,zf - 1),u);
  1233. x2 = Self.Lerp(Self.Grad(abb,xf,yf - 1,zf - 1),Self.Grad(bbb,xf - 1,yf - 1,zf - 1),u);
  1234. y2 = Self.Lerp(x1,x2,v);
  1235. Result = (Self.Lerp(y1,y2,w) + 1) / 2;
  1236. return Result;
  1237. };
  1238. this.GetValue$1 = function (x, y, z, octaves, persistence) {
  1239. var Result = 0.0;
  1240. var total = 0;
  1241. var frequency = 1;
  1242. var amplitude = 1;
  1243. var maxValue = 0;
  1244. var i = 0;
  1245. for (var $l1 = 0, $end2 = octaves - 1; $l1 <= $end2; $l1++) {
  1246. i = $l1;
  1247. total += this.GetValue(x * frequency,y * frequency,z * frequency) * amplitude;
  1248. maxValue += amplitude;
  1249. amplitude *= persistence;
  1250. frequency *= 2;
  1251. };
  1252. Result = total / maxValue;
  1253. return Result;
  1254. };
  1255. this.GetNoise = function (x, y, width, height, scale, frequency) {
  1256. var Result = 0.0;
  1257. var nx = 0.0;
  1258. var ny = 0.0;
  1259. nx = (x / width) - 0.5;
  1260. ny = (y / height) - 0.5;
  1261. Result = (this.GetValue$1(nx * scale,ny * scale,0,frequency,0.5) / 2) + 0.5;
  1262. return Result;
  1263. };
  1264. this.Inc = function (num) {
  1265. var Result = 0;
  1266. num += 1;
  1267. if (this.repeatValue > 0) num = num % this.repeatValue;
  1268. Result = num;
  1269. return Result;
  1270. };
  1271. this.Grad = function (hash, x, y, z) {
  1272. var Result = 0.0;
  1273. var $tmp1 = hash & 0xF;
  1274. if ($tmp1 === 0x0) {
  1275. Result = x + y}
  1276. else if ($tmp1 === 0x1) {
  1277. Result = -x + y}
  1278. else if ($tmp1 === 0x2) {
  1279. Result = x - y}
  1280. else if ($tmp1 === 0x3) {
  1281. Result = -x - y}
  1282. else if ($tmp1 === 0x4) {
  1283. Result = x + z}
  1284. else if ($tmp1 === 0x5) {
  1285. Result = -x + z}
  1286. else if ($tmp1 === 0x6) {
  1287. Result = x - z}
  1288. else if ($tmp1 === 0x7) {
  1289. Result = -x - z}
  1290. else if ($tmp1 === 0x8) {
  1291. Result = y + z}
  1292. else if ($tmp1 === 0x9) {
  1293. Result = -y + z}
  1294. else if ($tmp1 === 0xA) {
  1295. Result = y - z}
  1296. else if ($tmp1 === 0xB) {
  1297. Result = -y - z}
  1298. else if ($tmp1 === 0xC) {
  1299. Result = y + x}
  1300. else if ($tmp1 === 0xD) {
  1301. Result = -y + z}
  1302. else if ($tmp1 === 0xE) {
  1303. Result = y - x}
  1304. else if ($tmp1 === 0xF) {
  1305. Result = -y - z}
  1306. else {
  1307. Result = 0;
  1308. };
  1309. return Result;
  1310. };
  1311. this.Fade = function (t) {
  1312. var Result = 0.0;
  1313. Result = ((t * t) * t) * ((t * ((t * 6) - 15)) + 10);
  1314. return Result;
  1315. };
  1316. this.Lerp = function (a, b, x) {
  1317. var Result = 0.0;
  1318. Result = a + (x * (b - a));
  1319. return Result;
  1320. };
  1321. });
  1322. this.RandomNoiseSeed = function (seed) {
  1323. var Result = rtl.arraySetLength(null,0,256);
  1324. var i = 0;
  1325. for (i = 0; i <= 255; i++) Result[i] = pas.System.Random(256);
  1326. return Result;
  1327. };
  1328. });
  1329. rtl.module("Matrix",["System","JS"],function () {
  1330. "use strict";
  1331. var $mod = this;
  1332. rtl.createClass($mod,"TMatrix",pas.System.TObject,function () {
  1333. this.$init = function () {
  1334. pas.System.TObject.$init.call(this);
  1335. this.table = null;
  1336. this.width = 0;
  1337. this.height = 0;
  1338. };
  1339. this.$final = function () {
  1340. this.table = undefined;
  1341. pas.System.TObject.$final.call(this);
  1342. };
  1343. this.Create$1 = function (w, h) {
  1344. this.width = w;
  1345. this.height = h;
  1346. this.table = new Array(this.width * this.height);
  1347. };
  1348. this.SetValue = function (x, y, value) {
  1349. this.table[this.IndexFor(x,y)] = value;
  1350. };
  1351. this.GetValue = function (x, y) {
  1352. var Result = undefined;
  1353. Result = this.table[this.IndexFor(x,y)];
  1354. return Result;
  1355. };
  1356. this.GetWidth = function () {
  1357. var Result = 0;
  1358. Result = this.width;
  1359. return Result;
  1360. };
  1361. this.GetHeight = function () {
  1362. var Result = 0;
  1363. Result = this.height;
  1364. return Result;
  1365. };
  1366. this.IndexFor = function (x, y) {
  1367. var Result = 0;
  1368. Result = x + (y * this.height);
  1369. return Result;
  1370. };
  1371. });
  1372. });
  1373. rtl.module("Web",["System","JS"],function () {
  1374. "use strict";
  1375. var $mod = this;
  1376. });
  1377. rtl.module("webgl",["System","JS","Web"],function () {
  1378. "use strict";
  1379. var $mod = this;
  1380. });
  1381. rtl.module("GLTypes",["System","webgl","JS","math","SysUtils"],function () {
  1382. "use strict";
  1383. var $mod = this;
  1384. this.TVec2 = function (s) {
  1385. if (s) {
  1386. this.x = s.x;
  1387. this.y = s.y;
  1388. } else {
  1389. this.x = 0.0;
  1390. this.y = 0.0;
  1391. };
  1392. this.$equal = function (b) {
  1393. return (this.x === b.x) && (this.y === b.y);
  1394. };
  1395. };
  1396. this.TVec3 = function (s) {
  1397. if (s) {
  1398. this.x = s.x;
  1399. this.y = s.y;
  1400. this.z = s.z;
  1401. } else {
  1402. this.x = 0.0;
  1403. this.y = 0.0;
  1404. this.z = 0.0;
  1405. };
  1406. this.$equal = function (b) {
  1407. return (this.x === b.x) && ((this.y === b.y) && (this.z === b.z));
  1408. };
  1409. };
  1410. this.V3 = function (x, y, z) {
  1411. var Result = new $mod.TVec3();
  1412. Result.x = x;
  1413. Result.y = y;
  1414. Result.z = z;
  1415. return Result;
  1416. };
  1417. this.Divide = function (v, amount) {
  1418. var Result = new $mod.TVec3();
  1419. Result = new $mod.TVec3($mod.V3(v.x / amount,v.y / amount,v.z / amount));
  1420. return Result;
  1421. };
  1422. this.Magnitude = function (v) {
  1423. var Result = 0.0;
  1424. Result = Math.sqrt((Math.pow(v.x,2) + Math.pow(v.y,2)) + Math.pow(v.z,2));
  1425. return Result;
  1426. };
  1427. this.Normalize = function (v) {
  1428. var Result = new $mod.TVec3();
  1429. Result = new $mod.TVec3($mod.Divide(new $mod.TVec3(v),$mod.Magnitude(new $mod.TVec3(v))));
  1430. return Result;
  1431. };
  1432. this.V2 = function (x, y) {
  1433. var Result = new $mod.TVec2();
  1434. Result.x = x;
  1435. Result.y = y;
  1436. return Result;
  1437. };
  1438. });
  1439. rtl.module("browserconsole",["System","JS","Web"],function () {
  1440. "use strict";
  1441. var $mod = this;
  1442. var $impl = $mod.$impl;
  1443. this.DefaultMaxConsoleLines = 25;
  1444. this.DefaultConsoleStyle = (((((((((((".pasconsole { " + pas.System.sLineBreak) + "font-family: courier;") + pas.System.sLineBreak) + "font-size: 14px;") + pas.System.sLineBreak) + "background: #FFFFFF;") + pas.System.sLineBreak) + "color: #000000;") + pas.System.sLineBreak) + "display: block;") + pas.System.sLineBreak) + "}";
  1445. this.ConsoleElementID = "";
  1446. this.ConsoleStyle = "";
  1447. this.MaxConsoleLines = 0;
  1448. this.ConsoleLinesToBrowserLog = false;
  1449. this.ResetConsole = function () {
  1450. if ($impl.LinesParent === null) return;
  1451. while ($impl.LinesParent.firstElementChild !== null) $impl.LinesParent.removeChild($impl.LinesParent.firstElementChild);
  1452. $impl.AppendLine();
  1453. };
  1454. this.InitConsole = function () {
  1455. if ($impl.ConsoleElement === null) return;
  1456. if ($impl.ConsoleElement.nodeName.toLowerCase() !== "body") {
  1457. while ($impl.ConsoleElement.firstElementChild !== null) $impl.ConsoleElement.removeChild($impl.ConsoleElement.firstElementChild);
  1458. };
  1459. $impl.StyleElement = document.createElement("style");
  1460. $impl.StyleElement.innerText = $mod.ConsoleStyle;
  1461. $impl.ConsoleElement.appendChild($impl.StyleElement);
  1462. $impl.LinesParent = document.createElement("div");
  1463. $impl.ConsoleElement.appendChild($impl.LinesParent);
  1464. };
  1465. this.HookConsole = function () {
  1466. $impl.ConsoleElement = null;
  1467. if ($mod.ConsoleElementID !== "") $impl.ConsoleElement = document.getElementById($mod.ConsoleElementID);
  1468. if ($impl.ConsoleElement === null) $impl.ConsoleElement = document.body;
  1469. if ($impl.ConsoleElement === null) return;
  1470. $mod.InitConsole();
  1471. $mod.ResetConsole();
  1472. pas.System.SetWriteCallBack($impl.WriteConsole);
  1473. };
  1474. $mod.$init = function () {
  1475. $mod.ConsoleLinesToBrowserLog = true;
  1476. $mod.ConsoleElementID = "pasjsconsole";
  1477. $mod.ConsoleStyle = $mod.DefaultConsoleStyle;
  1478. $mod.MaxConsoleLines = 25;
  1479. $mod.HookConsole();
  1480. };
  1481. },null,function () {
  1482. "use strict";
  1483. var $mod = this;
  1484. var $impl = $mod.$impl;
  1485. $impl.LastLine = null;
  1486. $impl.StyleElement = null;
  1487. $impl.LinesParent = null;
  1488. $impl.ConsoleElement = null;
  1489. $impl.AppendLine = function () {
  1490. var CurrentCount = 0;
  1491. var S = null;
  1492. CurrentCount = 0;
  1493. S = $impl.LinesParent.firstChild;
  1494. while (S != null) {
  1495. CurrentCount += 1;
  1496. S = S.nextSibling;
  1497. };
  1498. while (CurrentCount > $mod.MaxConsoleLines) {
  1499. CurrentCount -= 1;
  1500. $impl.LinesParent.removeChild($impl.LinesParent.firstChild);
  1501. };
  1502. $impl.LastLine = document.createElement("div");
  1503. $impl.LastLine.className = "pasconsole";
  1504. $impl.LinesParent.appendChild($impl.LastLine);
  1505. };
  1506. $impl.WriteConsole = function (S, NewLine) {
  1507. var CL = "";
  1508. CL = $impl.LastLine.innerText;
  1509. CL = CL + ("" + S);
  1510. $impl.LastLine.innerText = CL;
  1511. if (NewLine) {
  1512. if ($mod.ConsoleLinesToBrowserLog) window.console.log(CL);
  1513. $impl.AppendLine();
  1514. };
  1515. };
  1516. });
  1517. rtl.module("Mat4",["System","browserconsole","JS","math"],function () {
  1518. "use strict";
  1519. var $mod = this;
  1520. var $impl = $mod.$impl;
  1521. rtl.createClass($mod,"TMat4",pas.System.TObject,function () {
  1522. this.$init = function () {
  1523. pas.System.TObject.$init.call(this);
  1524. this.RawComponents = rtl.arraySetLength(null,0.0,4,4);
  1525. };
  1526. this.$final = function () {
  1527. this.RawComponents = undefined;
  1528. pas.System.TObject.$final.call(this);
  1529. };
  1530. this.Identity = function () {
  1531. this.RawComponents[0][0] = 1.0;
  1532. this.RawComponents[0][1] = 0.0;
  1533. this.RawComponents[0][2] = 0.0;
  1534. this.RawComponents[0][3] = 0.0;
  1535. this.RawComponents[1][0] = 0.0;
  1536. this.RawComponents[1][1] = 1.0;
  1537. this.RawComponents[1][2] = 0.0;
  1538. this.RawComponents[1][3] = 0.0;
  1539. this.RawComponents[2][0] = 0.0;
  1540. this.RawComponents[2][1] = 0.0;
  1541. this.RawComponents[2][2] = 1.0;
  1542. this.RawComponents[2][3] = 0.0;
  1543. this.RawComponents[3][0] = 0.0;
  1544. this.RawComponents[3][1] = 0.0;
  1545. this.RawComponents[3][2] = 0.0;
  1546. this.RawComponents[3][3] = 1.0;
  1547. };
  1548. this.Translate = function (tx, ty, tz) {
  1549. this.RawComponents[0][0] = 1.0;
  1550. this.RawComponents[0][1] = 0.0;
  1551. this.RawComponents[0][2] = 0.0;
  1552. this.RawComponents[0][3] = 0.0;
  1553. this.RawComponents[1][0] = 0.0;
  1554. this.RawComponents[1][1] = 1.0;
  1555. this.RawComponents[1][2] = 0.0;
  1556. this.RawComponents[1][3] = 0.0;
  1557. this.RawComponents[2][0] = 0.0;
  1558. this.RawComponents[2][1] = 0.0;
  1559. this.RawComponents[2][2] = 1.0;
  1560. this.RawComponents[2][3] = 0.0;
  1561. this.RawComponents[3][0] = tx;
  1562. this.RawComponents[3][1] = ty;
  1563. this.RawComponents[3][2] = tz;
  1564. this.RawComponents[3][3] = 1.0;
  1565. };
  1566. this.Perspective = function (fovy, Aspect, zNear, zFar) {
  1567. var Sine = 0.0;
  1568. var Cotangent = 0.0;
  1569. var ZDelta = 0.0;
  1570. var Radians = 0.0;
  1571. Radians = (fovy * 0.5) * 0.017453292519944444;
  1572. ZDelta = zFar - zNear;
  1573. Sine = Math.sin(Radians);
  1574. if (!(((ZDelta === 0) || (Sine === 0)) || (Aspect === 0))) {
  1575. Cotangent = Math.cos(Radians) / Sine;
  1576. this.RawComponents = $impl.Matrix4x4Identity.RawComponents.slice(0);
  1577. this.RawComponents[0][0] = Cotangent / Aspect;
  1578. this.RawComponents[1][1] = Cotangent;
  1579. this.RawComponents[2][2] = -(zFar + zNear) / ZDelta;
  1580. this.RawComponents[2][3] = -1 - 0;
  1581. this.RawComponents[3][2] = -((2.0 * zNear) * zFar) / ZDelta;
  1582. this.RawComponents[3][3] = 0.0;
  1583. };
  1584. };
  1585. this.Multiply = function (m) {
  1586. var Result = null;
  1587. Result = $mod.TMat4.$create("Identity");
  1588. Result.RawComponents[0][0] = (((m.RawComponents[0][0] * this.RawComponents[0][0]) + (m.RawComponents[0][1] * this.RawComponents[1][0])) + (m.RawComponents[0][2] * this.RawComponents[2][0])) + (m.RawComponents[0][3] * this.RawComponents[3][0]);
  1589. Result.RawComponents[0][1] = (((m.RawComponents[0][0] * this.RawComponents[0][1]) + (m.RawComponents[0][1] * this.RawComponents[1][1])) + (m.RawComponents[0][2] * this.RawComponents[2][1])) + (m.RawComponents[0][3] * this.RawComponents[3][1]);
  1590. Result.RawComponents[0][2] = (((m.RawComponents[0][0] * this.RawComponents[0][2]) + (m.RawComponents[0][1] * this.RawComponents[1][2])) + (m.RawComponents[0][2] * this.RawComponents[2][2])) + (m.RawComponents[0][3] * this.RawComponents[3][2]);
  1591. Result.RawComponents[0][3] = (((m.RawComponents[0][0] * this.RawComponents[0][3]) + (m.RawComponents[0][1] * this.RawComponents[1][3])) + (m.RawComponents[0][2] * this.RawComponents[2][3])) + (m.RawComponents[0][3] * this.RawComponents[3][3]);
  1592. Result.RawComponents[1][0] = (((m.RawComponents[1][0] * this.RawComponents[0][0]) + (m.RawComponents[1][1] * this.RawComponents[1][0])) + (m.RawComponents[1][2] * this.RawComponents[2][0])) + (m.RawComponents[1][3] * this.RawComponents[3][0]);
  1593. Result.RawComponents[1][1] = (((m.RawComponents[1][0] * this.RawComponents[0][1]) + (m.RawComponents[1][1] * this.RawComponents[1][1])) + (m.RawComponents[1][2] * this.RawComponents[2][1])) + (m.RawComponents[1][3] * this.RawComponents[3][1]);
  1594. Result.RawComponents[1][2] = (((m.RawComponents[1][0] * this.RawComponents[0][2]) + (m.RawComponents[1][1] * this.RawComponents[1][2])) + (m.RawComponents[1][2] * this.RawComponents[2][2])) + (m.RawComponents[1][3] * this.RawComponents[3][2]);
  1595. Result.RawComponents[1][3] = (((m.RawComponents[1][0] * this.RawComponents[0][3]) + (m.RawComponents[1][1] * this.RawComponents[1][3])) + (m.RawComponents[1][2] * this.RawComponents[2][3])) + (m.RawComponents[1][3] * this.RawComponents[3][3]);
  1596. Result.RawComponents[2][0] = (((m.RawComponents[2][0] * this.RawComponents[0][0]) + (m.RawComponents[2][1] * this.RawComponents[1][0])) + (m.RawComponents[2][2] * this.RawComponents[2][0])) + (m.RawComponents[2][3] * this.RawComponents[3][0]);
  1597. Result.RawComponents[2][1] = (((m.RawComponents[2][0] * this.RawComponents[0][1]) + (m.RawComponents[2][1] * this.RawComponents[1][1])) + (m.RawComponents[2][2] * this.RawComponents[2][1])) + (m.RawComponents[2][3] * this.RawComponents[3][1]);
  1598. Result.RawComponents[2][2] = (((m.RawComponents[2][0] * this.RawComponents[0][2]) + (m.RawComponents[2][1] * this.RawComponents[1][2])) + (m.RawComponents[2][2] * this.RawComponents[2][2])) + (m.RawComponents[2][3] * this.RawComponents[3][2]);
  1599. Result.RawComponents[2][3] = (((m.RawComponents[2][0] * this.RawComponents[0][3]) + (m.RawComponents[2][1] * this.RawComponents[1][3])) + (m.RawComponents[2][2] * this.RawComponents[2][3])) + (m.RawComponents[2][3] * this.RawComponents[3][3]);
  1600. Result.RawComponents[3][0] = (((m.RawComponents[3][0] * this.RawComponents[0][0]) + (m.RawComponents[3][1] * this.RawComponents[1][0])) + (m.RawComponents[3][2] * this.RawComponents[2][0])) + (m.RawComponents[3][3] * this.RawComponents[3][0]);
  1601. Result.RawComponents[3][1] = (((m.RawComponents[3][0] * this.RawComponents[0][1]) + (m.RawComponents[3][1] * this.RawComponents[1][1])) + (m.RawComponents[3][2] * this.RawComponents[2][1])) + (m.RawComponents[3][3] * this.RawComponents[3][1]);
  1602. Result.RawComponents[3][2] = (((m.RawComponents[3][0] * this.RawComponents[0][2]) + (m.RawComponents[3][1] * this.RawComponents[1][2])) + (m.RawComponents[3][2] * this.RawComponents[2][2])) + (m.RawComponents[3][3] * this.RawComponents[3][2]);
  1603. Result.RawComponents[3][3] = (((m.RawComponents[3][0] * this.RawComponents[0][3]) + (m.RawComponents[3][1] * this.RawComponents[1][3])) + (m.RawComponents[3][2] * this.RawComponents[2][3])) + (m.RawComponents[3][3] * this.RawComponents[3][3]);
  1604. return Result;
  1605. };
  1606. this.Inverse = function () {
  1607. var Result = null;
  1608. var t0 = 0.0;
  1609. var t4 = 0.0;
  1610. var t8 = 0.0;
  1611. var t12 = 0.0;
  1612. var d = 0.0;
  1613. t0 = ((((((this.RawComponents[1][1] * this.RawComponents[2][2]) * this.RawComponents[3][3]) - ((this.RawComponents[1][1] * this.RawComponents[2][3]) * this.RawComponents[3][2])) - ((this.RawComponents[2][1] * this.RawComponents[1][2]) * this.RawComponents[3][3])) + ((this.RawComponents[2][1] * this.RawComponents[1][3]) * this.RawComponents[3][2])) + ((this.RawComponents[3][1] * this.RawComponents[1][2]) * this.RawComponents[2][3])) - ((this.RawComponents[3][1] * this.RawComponents[1][3]) * this.RawComponents[2][2]);
  1614. t4 = ((((-((this.RawComponents[1][0] * this.RawComponents[2][2]) * this.RawComponents[3][3]) + ((this.RawComponents[1][0] * this.RawComponents[2][3]) * this.RawComponents[3][2])) + ((this.RawComponents[2][0] * this.RawComponents[1][2]) * this.RawComponents[3][3])) - ((this.RawComponents[2][0] * this.RawComponents[1][3]) * this.RawComponents[3][2])) - ((this.RawComponents[3][0] * this.RawComponents[1][2]) * this.RawComponents[2][3])) + ((this.RawComponents[3][0] * this.RawComponents[1][3]) * this.RawComponents[2][2]);
  1615. t8 = ((((((this.RawComponents[1][0] * this.RawComponents[2][1]) * this.RawComponents[3][3]) - ((this.RawComponents[1][0] * this.RawComponents[2][3]) * this.RawComponents[3][1])) - ((this.RawComponents[2][0] * this.RawComponents[1][1]) * this.RawComponents[3][3])) + ((this.RawComponents[2][0] * this.RawComponents[1][3]) * this.RawComponents[3][1])) + ((this.RawComponents[3][0] * this.RawComponents[1][1]) * this.RawComponents[2][3])) - ((this.RawComponents[3][0] * this.RawComponents[1][3]) * this.RawComponents[2][1]);
  1616. t12 = ((((-((this.RawComponents[1][0] * this.RawComponents[2][1]) * this.RawComponents[3][2]) + ((this.RawComponents[1][0] * this.RawComponents[2][2]) * this.RawComponents[3][1])) + ((this.RawComponents[2][0] * this.RawComponents[1][1]) * this.RawComponents[3][2])) - ((this.RawComponents[2][0] * this.RawComponents[1][2]) * this.RawComponents[3][1])) - ((this.RawComponents[3][0] * this.RawComponents[1][1]) * this.RawComponents[2][2])) + ((this.RawComponents[3][0] * this.RawComponents[1][2]) * this.RawComponents[2][1]);
  1617. d = (((this.RawComponents[0][0] * t0) + (this.RawComponents[0][1] * t4)) + (this.RawComponents[0][2] * t8)) + (this.RawComponents[0][3] * t12);
  1618. Result = $mod.TMat4.$create("Identity");
  1619. if (d !== 0.0) {
  1620. d = 1.0 / d;
  1621. Result.RawComponents[0][0] = t0 * d;
  1622. Result.RawComponents[0][1] = (((((-((this.RawComponents[0][1] * this.RawComponents[2][2]) * this.RawComponents[3][3]) + ((this.RawComponents[0][1] * this.RawComponents[2][3]) * this.RawComponents[3][2])) + ((this.RawComponents[2][1] * this.RawComponents[0][2]) * this.RawComponents[3][3])) - ((this.RawComponents[2][1] * this.RawComponents[0][3]) * this.RawComponents[3][2])) - ((this.RawComponents[3][1] * this.RawComponents[0][2]) * this.RawComponents[2][3])) + ((this.RawComponents[3][1] * this.RawComponents[0][3]) * this.RawComponents[2][2])) * d;
  1623. Result.RawComponents[0][2] = (((((((this.RawComponents[0][1] * this.RawComponents[1][2]) * this.RawComponents[3][3]) - ((this.RawComponents[0][1] * this.RawComponents[1][3]) * this.RawComponents[3][2])) - ((this.RawComponents[1][1] * this.RawComponents[0][2]) * this.RawComponents[3][3])) + ((this.RawComponents[1][1] * this.RawComponents[0][3]) * this.RawComponents[3][2])) + ((this.RawComponents[3][1] * this.RawComponents[0][2]) * this.RawComponents[1][3])) - ((this.RawComponents[3][1] * this.RawComponents[0][3]) * this.RawComponents[1][2])) * d;
  1624. Result.RawComponents[0][3] = (((((-((this.RawComponents[0][1] * this.RawComponents[1][2]) * this.RawComponents[2][3]) + ((this.RawComponents[0][1] * this.RawComponents[1][3]) * this.RawComponents[2][2])) + ((this.RawComponents[1][1] * this.RawComponents[0][2]) * this.RawComponents[2][3])) - ((this.RawComponents[1][1] * this.RawComponents[0][3]) * this.RawComponents[2][2])) - ((this.RawComponents[2][1] * this.RawComponents[0][2]) * this.RawComponents[1][3])) + ((this.RawComponents[2][1] * this.RawComponents[0][3]) * this.RawComponents[1][2])) * d;
  1625. Result.RawComponents[1][0] = t4 * d;
  1626. Result.RawComponents[1][1] = (((((((this.RawComponents[0][0] * this.RawComponents[2][2]) * this.RawComponents[3][3]) - ((this.RawComponents[0][0] * this.RawComponents[2][3]) * this.RawComponents[3][2])) - ((this.RawComponents[2][0] * this.RawComponents[0][2]) * this.RawComponents[3][3])) + ((this.RawComponents[2][0] * this.RawComponents[0][3]) * this.RawComponents[3][2])) + ((this.RawComponents[3][0] * this.RawComponents[0][2]) * this.RawComponents[2][3])) - ((this.RawComponents[3][0] * this.RawComponents[0][3]) * this.RawComponents[2][2])) * d;
  1627. Result.RawComponents[1][2] = (((((-((this.RawComponents[0][0] * this.RawComponents[1][2]) * this.RawComponents[3][3]) + ((this.RawComponents[0][0] * this.RawComponents[1][3]) * this.RawComponents[3][2])) + ((this.RawComponents[1][0] * this.RawComponents[0][2]) * this.RawComponents[3][3])) - ((this.RawComponents[1][0] * this.RawComponents[0][3]) * this.RawComponents[3][2])) - ((this.RawComponents[3][0] * this.RawComponents[0][2]) * this.RawComponents[1][3])) + ((this.RawComponents[3][0] * this.RawComponents[0][3]) * this.RawComponents[1][2])) * d;
  1628. Result.RawComponents[1][3] = (((((((this.RawComponents[0][0] * this.RawComponents[1][2]) * this.RawComponents[2][3]) - ((this.RawComponents[0][0] * this.RawComponents[1][3]) * this.RawComponents[2][2])) - ((this.RawComponents[1][0] * this.RawComponents[0][2]) * this.RawComponents[2][3])) + ((this.RawComponents[1][0] * this.RawComponents[0][3]) * this.RawComponents[2][2])) + ((this.RawComponents[2][0] * this.RawComponents[0][2]) * this.RawComponents[1][3])) - ((this.RawComponents[2][0] * this.RawComponents[0][3]) * this.RawComponents[1][2])) * d;
  1629. Result.RawComponents[2][0] = t8 * d;
  1630. Result.RawComponents[2][1] = (((((-((this.RawComponents[0][0] * this.RawComponents[2][1]) * this.RawComponents[3][3]) + ((this.RawComponents[0][0] * this.RawComponents[2][3]) * this.RawComponents[3][1])) + ((this.RawComponents[2][0] * this.RawComponents[0][1]) * this.RawComponents[3][3])) - ((this.RawComponents[2][0] * this.RawComponents[0][3]) * this.RawComponents[3][1])) - ((this.RawComponents[3][0] * this.RawComponents[0][1]) * this.RawComponents[2][3])) + ((this.RawComponents[3][0] * this.RawComponents[0][3]) * this.RawComponents[2][1])) * d;
  1631. Result.RawComponents[2][2] = (((((((this.RawComponents[0][0] * this.RawComponents[1][1]) * this.RawComponents[3][3]) - ((this.RawComponents[0][0] * this.RawComponents[1][3]) * this.RawComponents[3][1])) - ((this.RawComponents[1][0] * this.RawComponents[0][1]) * this.RawComponents[3][3])) + ((this.RawComponents[1][0] * this.RawComponents[0][3]) * this.RawComponents[3][1])) + ((this.RawComponents[3][0] * this.RawComponents[0][1]) * this.RawComponents[1][3])) - ((this.RawComponents[3][0] * this.RawComponents[0][3]) * this.RawComponents[1][1])) * d;
  1632. Result.RawComponents[2][3] = (((((-((this.RawComponents[0][0] * this.RawComponents[1][1]) * this.RawComponents[2][3]) + ((this.RawComponents[0][0] * this.RawComponents[1][3]) * this.RawComponents[2][1])) + ((this.RawComponents[1][0] * this.RawComponents[0][1]) * this.RawComponents[2][3])) - ((this.RawComponents[1][0] * this.RawComponents[0][3]) * this.RawComponents[2][1])) - ((this.RawComponents[2][0] * this.RawComponents[0][1]) * this.RawComponents[1][3])) + ((this.RawComponents[2][0] * this.RawComponents[0][3]) * this.RawComponents[1][1])) * d;
  1633. Result.RawComponents[3][0] = t12 * d;
  1634. Result.RawComponents[3][1] = (((((((this.RawComponents[0][0] * this.RawComponents[2][1]) * this.RawComponents[3][2]) - ((this.RawComponents[0][0] * this.RawComponents[2][2]) * this.RawComponents[3][1])) - ((this.RawComponents[2][0] * this.RawComponents[0][1]) * this.RawComponents[3][2])) + ((this.RawComponents[2][0] * this.RawComponents[0][2]) * this.RawComponents[3][1])) + ((this.RawComponents[3][0] * this.RawComponents[0][1]) * this.RawComponents[2][2])) - ((this.RawComponents[3][0] * this.RawComponents[0][2]) * this.RawComponents[2][1])) * d;
  1635. Result.RawComponents[3][2] = (((((-((this.RawComponents[0][0] * this.RawComponents[1][1]) * this.RawComponents[3][2]) + ((this.RawComponents[0][0] * this.RawComponents[1][2]) * this.RawComponents[3][1])) + ((this.RawComponents[1][0] * this.RawComponents[0][1]) * this.RawComponents[3][2])) - ((this.RawComponents[1][0] * this.RawComponents[0][2]) * this.RawComponents[3][1])) - ((this.RawComponents[3][0] * this.RawComponents[0][1]) * this.RawComponents[1][2])) + ((this.RawComponents[3][0] * this.RawComponents[0][2]) * this.RawComponents[1][1])) * d;
  1636. Result.RawComponents[3][3] = (((((((this.RawComponents[0][0] * this.RawComponents[1][1]) * this.RawComponents[2][2]) - ((this.RawComponents[0][0] * this.RawComponents[1][2]) * this.RawComponents[2][1])) - ((this.RawComponents[1][0] * this.RawComponents[0][1]) * this.RawComponents[2][2])) + ((this.RawComponents[1][0] * this.RawComponents[0][2]) * this.RawComponents[2][1])) + ((this.RawComponents[2][0] * this.RawComponents[0][1]) * this.RawComponents[1][2])) - ((this.RawComponents[2][0] * this.RawComponents[0][2]) * this.RawComponents[1][1])) * d;
  1637. };
  1638. return Result;
  1639. };
  1640. this.CopyList = function () {
  1641. var Result = [];
  1642. var x = 0;
  1643. var y = 0;
  1644. var list = null;
  1645. list = new Array();
  1646. for (x = 0; x <= 3; x++) for (y = 0; y <= 3; y++) list.push(this.RawComponents[x][y]);
  1647. Result = list;
  1648. return Result;
  1649. };
  1650. });
  1651. $mod.$init = function () {
  1652. $impl.Matrix4x4Identity = $mod.TMat4.$create("Identity");
  1653. };
  1654. },null,function () {
  1655. "use strict";
  1656. var $mod = this;
  1657. var $impl = $mod.$impl;
  1658. $impl.PI = 3.14159265359;
  1659. $impl.DEG2RAD = 3.14159265359 / 180.0;
  1660. $impl.Matrix4x4Identity = null;
  1661. });
  1662. rtl.module("GLUtils",["System","Mat4","GLTypes","browserconsole","Web","webgl","JS","math","SysUtils"],function () {
  1663. "use strict";
  1664. var $mod = this;
  1665. var $impl = $mod.$impl;
  1666. rtl.createClass($mod,"TShader",pas.System.TObject,function () {
  1667. this.$init = function () {
  1668. pas.System.TObject.$init.call(this);
  1669. this.gl = null;
  1670. this.vertexShader = null;
  1671. this.fragmentShader = null;
  1672. this.programID = null;
  1673. };
  1674. this.$final = function () {
  1675. this.gl = undefined;
  1676. this.vertexShader = undefined;
  1677. this.fragmentShader = undefined;
  1678. this.programID = undefined;
  1679. pas.System.TObject.$final.call(this);
  1680. };
  1681. this.Create$1 = function (context, vertexShaderSource, fragmentShaderSource) {
  1682. this.gl = context;
  1683. this.vertexShader = this.CreateShader(this.gl.VERTEX_SHADER,vertexShaderSource);
  1684. this.fragmentShader = this.CreateShader(this.gl.FRAGMENT_SHADER,fragmentShaderSource);
  1685. };
  1686. this.Compile = function () {
  1687. this.programID = this.gl.createProgram();
  1688. this.gl.attachShader(this.programID,this.vertexShader);
  1689. this.gl.attachShader(this.programID,this.fragmentShader);
  1690. };
  1691. this.Link = function () {
  1692. this.gl.linkProgram(this.programID);
  1693. if (!this.gl.getProgramParameter(this.programID,this.gl.LINK_STATUS)) {
  1694. $impl.Fatal(this.gl.getProgramInfoLog(this.programID));
  1695. };
  1696. };
  1697. this.Use = function () {
  1698. this.gl.useProgram(this.programID);
  1699. };
  1700. this.BindAttribLocation = function (index, name) {
  1701. this.gl.bindAttribLocation(this.programID,index,name);
  1702. };
  1703. this.SetUniformMat4 = function (name, value) {
  1704. var list = [];
  1705. list = value.CopyList();
  1706. this.gl.uniformMatrix4fv(this.GetUniformLocation(name),false,list);
  1707. $mod.GLFatal(this.gl,"gl.uniformMatrix4fv");
  1708. };
  1709. this.SetUniformVec3 = function (name, value) {
  1710. this.gl.uniform3f(this.GetUniformLocation(name),value.x,value.y,value.z);
  1711. $mod.GLFatal(this.gl,"gl.uniform3fv");
  1712. };
  1713. this.SetUniformFloat = function (name, value) {
  1714. this.gl.uniform1f(this.GetUniformLocation(name),value);
  1715. $mod.GLFatal(this.gl,"gl.uniform1f");
  1716. };
  1717. this.GetUniformLocation = function (name) {
  1718. var Result = null;
  1719. Result = this.gl.getUniformLocation(this.programID,name);
  1720. $mod.GLFatal(this.gl,"gl.getUniformLocation");
  1721. return Result;
  1722. };
  1723. this.CreateShader = function (theType, source) {
  1724. var Result = null;
  1725. var shader = null;
  1726. shader = this.gl.createShader(theType);
  1727. if (shader === null) $impl.Fatal("create shader failed");
  1728. this.gl.shaderSource(shader,source);
  1729. this.gl.compileShader(shader);
  1730. if (this.gl.getShaderParameter(shader,this.gl.COMPILE_STATUS)) {
  1731. return shader;
  1732. } else {
  1733. $impl.Fatal$1(this.gl.getShaderInfoLog(shader));
  1734. };
  1735. return Result;
  1736. };
  1737. });
  1738. this.TModelData = function (s) {
  1739. if (s) {
  1740. this.verticies = s.verticies;
  1741. this.indicies = s.indicies;
  1742. this.floatsPerVertex = s.floatsPerVertex;
  1743. } else {
  1744. this.verticies = null;
  1745. this.indicies = null;
  1746. this.floatsPerVertex = 0;
  1747. };
  1748. this.$equal = function (b) {
  1749. return (this.verticies === b.verticies) && ((this.indicies === b.indicies) && (this.floatsPerVertex === b.floatsPerVertex));
  1750. };
  1751. };
  1752. this.kModelVertexFloats = (3 + 2) + 3;
  1753. this.TModelVertex = function (s) {
  1754. if (s) {
  1755. this.pos = new pas.GLTypes.TVec3(s.pos);
  1756. this.texCoord = new pas.GLTypes.TVec2(s.texCoord);
  1757. this.normal = new pas.GLTypes.TVec3(s.normal);
  1758. } else {
  1759. this.pos = new pas.GLTypes.TVec3();
  1760. this.texCoord = new pas.GLTypes.TVec2();
  1761. this.normal = new pas.GLTypes.TVec3();
  1762. };
  1763. this.$equal = function (b) {
  1764. return this.pos.$equal(b.pos) && (this.texCoord.$equal(b.texCoord) && this.normal.$equal(b.normal));
  1765. };
  1766. };
  1767. this.ModelVertexAddToArray = function (vertex, list) {
  1768. list.push(vertex.pos.x);
  1769. list.push(vertex.pos.y);
  1770. list.push(vertex.pos.z);
  1771. list.push(vertex.texCoord.x);
  1772. list.push(vertex.texCoord.y);
  1773. list.push(vertex.normal.x);
  1774. list.push(vertex.normal.y);
  1775. list.push(vertex.normal.z);
  1776. };
  1777. rtl.createClass($mod,"TModel",pas.System.TObject,function () {
  1778. this.$init = function () {
  1779. pas.System.TObject.$init.call(this);
  1780. this.gl = null;
  1781. this.data = new $mod.TModelData();
  1782. this.vertexBuffer = null;
  1783. this.indexBuffer = null;
  1784. };
  1785. this.$final = function () {
  1786. this.gl = undefined;
  1787. this.data = undefined;
  1788. this.vertexBuffer = undefined;
  1789. this.indexBuffer = undefined;
  1790. pas.System.TObject.$final.call(this);
  1791. };
  1792. this.Create$1 = function (context, modelData) {
  1793. this.gl = context;
  1794. this.data = new $mod.TModelData(modelData);
  1795. this.Load();
  1796. };
  1797. this.Draw = function () {
  1798. this.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.vertexBuffer);
  1799. this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER,this.indexBuffer);
  1800. this.EnableAttributes();
  1801. this.gl.drawElements(this.gl.TRIANGLES,this.data.indicies.length,this.gl.UNSIGNED_SHORT,0);
  1802. this.gl.bindBuffer(this.gl.ARRAY_BUFFER,null);
  1803. this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER,null);
  1804. };
  1805. this.EnableAttributes = function () {
  1806. var offset = 0;
  1807. var stride = 0;
  1808. offset = 0;
  1809. stride = this.data.floatsPerVertex * $mod.GLSizeof(WebGLRenderingContext.FLOAT);
  1810. this.gl.enableVertexAttribArray(0);
  1811. this.gl.vertexAttribPointer(0,3,this.gl.FLOAT,false,stride,offset);
  1812. offset += $mod.GLSizeof(WebGLRenderingContext.FLOAT) * 3;
  1813. this.gl.enableVertexAttribArray(1);
  1814. this.gl.vertexAttribPointer(1,2,this.gl.FLOAT,false,stride,offset);
  1815. offset += $mod.GLSizeof(WebGLRenderingContext.FLOAT) * 2;
  1816. this.gl.enableVertexAttribArray(2);
  1817. this.gl.vertexAttribPointer(2,3,this.gl.FLOAT,false,stride,offset);
  1818. offset += $mod.GLSizeof(WebGLRenderingContext.FLOAT) * 3;
  1819. };
  1820. this.Load = function () {
  1821. this.indexBuffer = this.gl.createBuffer();
  1822. this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER,this.indexBuffer);
  1823. this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER,this.data.indicies,this.gl.STATIC_DRAW);
  1824. this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER,null);
  1825. this.vertexBuffer = this.gl.createBuffer();
  1826. this.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.vertexBuffer);
  1827. this.gl.bufferData(this.gl.ARRAY_BUFFER,this.data.verticies,this.gl.STATIC_DRAW);
  1828. this.gl.bindBuffer(this.gl.ARRAY_BUFFER,null);
  1829. };
  1830. });
  1831. this.GLSizeof = function (glType) {
  1832. var Result = 0;
  1833. var $tmp1 = glType;
  1834. if (($tmp1 === WebGLRenderingContext.UNSIGNED_BYTE) || ($tmp1 === WebGLRenderingContext.BYTE)) {
  1835. Result = 1}
  1836. else if (($tmp1 === WebGLRenderingContext.SHORT) || ($tmp1 === WebGLRenderingContext.UNSIGNED_SHORT)) {
  1837. Result = 2}
  1838. else if (($tmp1 === WebGLRenderingContext.INT) || ($tmp1 === WebGLRenderingContext.UNSIGNED_INT)) {
  1839. Result = 4}
  1840. else if ($tmp1 === WebGLRenderingContext.FLOAT) {
  1841. Result = 4}
  1842. else {
  1843. $impl.Fatal("GLSizeof type is invalid.");
  1844. };
  1845. return Result;
  1846. };
  1847. this.GLFatal = function (gl, messageString) {
  1848. var error = 0;
  1849. error = gl.getError();
  1850. if (error !== WebGLRenderingContext.NO_ERROR) {
  1851. var $tmp1 = error;
  1852. if ($tmp1 === WebGLRenderingContext.INVALID_VALUE) {
  1853. messageString = messageString + " (GL_INVALID_VALUE)"}
  1854. else if ($tmp1 === WebGLRenderingContext.INVALID_OPERATION) {
  1855. messageString = messageString + " (GL_INVALID_OPERATION)"}
  1856. else if ($tmp1 === WebGLRenderingContext.INVALID_ENUM) {
  1857. messageString = messageString + " (GL_INVALID_ENUM)"}
  1858. else {
  1859. messageString = (messageString + " ") + pas.SysUtils.IntToStr(error);
  1860. };
  1861. $impl.Fatal(messageString);
  1862. };
  1863. };
  1864. },null,function () {
  1865. "use strict";
  1866. var $mod = this;
  1867. var $impl = $mod.$impl;
  1868. $impl.Fatal = function (messageString) {
  1869. pas.System.Writeln("*** FATAL: ",messageString);
  1870. throw pas.SysUtils.Exception.$create("Create$1",["FATAL"]);
  1871. };
  1872. $impl.Fatal$1 = function (messageString) {
  1873. pas.System.Writeln("*** FATAL: ",messageString);
  1874. throw pas.SysUtils.Exception.$create("Create$1",["FATAL"]);
  1875. };
  1876. });
  1877. rtl.module("Terrain",["System","Noise","Matrix","GLTypes","GLUtils","webgl","JS","math"],function () {
  1878. "use strict";
  1879. var $mod = this;
  1880. rtl.createClass($mod,"TTerrain",pas.System.TObject,function () {
  1881. this.$init = function () {
  1882. pas.System.TObject.$init.call(this);
  1883. this.noise$1 = null;
  1884. this.gl = null;
  1885. this.noiseOffset = new pas.GLTypes.TVec2();
  1886. this.terrainSize = 0;
  1887. this.terrainResolution = 0;
  1888. this.heights = null;
  1889. this.model = null;
  1890. };
  1891. this.$final = function () {
  1892. this.noise$1 = undefined;
  1893. this.gl = undefined;
  1894. this.noiseOffset = undefined;
  1895. this.heights = undefined;
  1896. this.model = undefined;
  1897. pas.System.TObject.$final.call(this);
  1898. };
  1899. this.Create$2 = function (context, inNoise, size, resolution, offset) {
  1900. this.gl = context;
  1901. this.noise$1 = inNoise;
  1902. this.noiseOffset = new pas.GLTypes.TVec2(offset);
  1903. this.terrainSize = size;
  1904. this.terrainResolution = resolution;
  1905. };
  1906. this.GetHeightAtPoint = function (x, y) {
  1907. var Result = 0.0;
  1908. Result = rtl.getNumber(this.heights.GetValue(x,y));
  1909. return Result;
  1910. };
  1911. this.GetWidth = function () {
  1912. var Result = 0;
  1913. Result = this.heights.GetWidth();
  1914. return Result;
  1915. };
  1916. this.GetHeight = function () {
  1917. var Result = 0;
  1918. Result = this.heights.GetHeight();
  1919. return Result;
  1920. };
  1921. this.Draw = function () {
  1922. this.model.Draw();
  1923. };
  1924. this.Generate = function () {
  1925. var vertex = new pas.GLUtils.TModelVertex();
  1926. var topLeft = 0;
  1927. var topRight = 0;
  1928. var bottomLeft = 0;
  1929. var bottomRight = 0;
  1930. var x = 0;
  1931. var y = 0;
  1932. var gz = 0;
  1933. var gx = 0;
  1934. var verticies = null;
  1935. var indicies = null;
  1936. var data = new pas.GLUtils.TModelData();
  1937. if (this.noise$1 === null) this.noise$1 = pas.Noise.TNoise.$create("Create$2",[pas.Noise.RandomNoiseSeed(1).slice(0)]);
  1938. this.heights = pas.Matrix.TMatrix.$create("Create$1",[this.terrainResolution,this.terrainResolution]);
  1939. verticies = new Array();
  1940. indicies = new Array();
  1941. for (var $l1 = 0, $end2 = this.heights.GetWidth() - 1; $l1 <= $end2; $l1++) {
  1942. y = $l1;
  1943. for (var $l3 = 0, $end4 = this.heights.GetHeight() - 1; $l3 <= $end4; $l3++) {
  1944. x = $l3;
  1945. vertex.pos.x = (x / (this.heights.GetWidth() - 1)) * this.terrainSize;
  1946. vertex.pos.y = this.GetHeightForVertex(x,y,pas.System.Trunc(this.noiseOffset.x) + x,pas.System.Trunc(this.noiseOffset.y) + y);
  1947. vertex.pos.z = (y / (this.heights.GetHeight() - 1)) * this.terrainSize;
  1948. this.heights.SetValue(x,y,vertex.pos.y);
  1949. vertex.normal = new pas.GLTypes.TVec3(this.CalculateNormal(x,y,pas.System.Trunc(this.noiseOffset.x) + x,pas.System.Trunc(this.noiseOffset.y) + y));
  1950. vertex.texCoord.x = x / (this.heights.GetWidth() - 1);
  1951. vertex.texCoord.y = y / (this.heights.GetHeight() - 1);
  1952. pas.GLUtils.ModelVertexAddToArray(new pas.GLUtils.TModelVertex(vertex),verticies);
  1953. };
  1954. };
  1955. for (var $l5 = 0, $end6 = this.heights.GetWidth() - 2; $l5 <= $end6; $l5++) {
  1956. gz = $l5;
  1957. for (var $l7 = 0, $end8 = this.heights.GetHeight() - 2; $l7 <= $end8; $l7++) {
  1958. gx = $l7;
  1959. topLeft = (gz * this.heights.GetWidth()) + gx;
  1960. topRight = topLeft + 1;
  1961. bottomLeft = ((gz + 1) * this.heights.GetWidth()) + gx;
  1962. bottomRight = bottomLeft + 1;
  1963. indicies.push(topLeft);
  1964. indicies.push(bottomLeft);
  1965. indicies.push(topRight);
  1966. indicies.push(topRight);
  1967. indicies.push(bottomLeft);
  1968. indicies.push(bottomRight);
  1969. };
  1970. };
  1971. data.verticies = new Float32Array(verticies);
  1972. data.indicies = new Uint16Array(indicies);
  1973. data.floatsPerVertex = 8;
  1974. this.model = pas.GLUtils.TModel.$create("Create$1",[this.gl,new pas.GLUtils.TModelData(data)]);
  1975. };
  1976. this.GetHeightForVertex = function (localX, localY, x, y) {
  1977. var Result = 0.0;
  1978. Result = this.noise$1.GetNoise(x,y,this.heights.GetWidth(),this.heights.GetHeight(),4,3);
  1979. Result = Math.pow(Result,5);
  1980. Result = (Result * 20) - 6;
  1981. return Result;
  1982. };
  1983. this.CalculateNormal = function (localX, localY, x, y) {
  1984. var Result = new pas.GLTypes.TVec3();
  1985. var heightL = 0.0;
  1986. var heightR = 0.0;
  1987. var heightD = 0.0;
  1988. var heightU = 0.0;
  1989. heightL = this.GetHeightForVertex(localX,localY,x - 1,y);
  1990. heightR = this.GetHeightForVertex(localX,localY,x + 1,y);
  1991. heightD = this.GetHeightForVertex(localX,localY,x,y - 1);
  1992. heightU = this.GetHeightForVertex(localX,localY,x,y + 1);
  1993. Result = new pas.GLTypes.TVec3(pas.GLTypes.V3(heightL - heightR,2.0,heightD - heightU));
  1994. Result = new pas.GLTypes.TVec3(pas.GLTypes.Normalize(new pas.GLTypes.TVec3(Result)));
  1995. return Result;
  1996. };
  1997. });
  1998. });
  1999. rtl.module("program",["System","Terrain","Noise","Mat4","GLUtils","GLTypes","SysUtils","browserconsole","Web","webgl","JS","math"],function () {
  2000. "use strict";
  2001. var $mod = this;
  2002. this.gl = null;
  2003. this.shader = null;
  2004. this.projTransform = null;
  2005. this.viewTransform = null;
  2006. this.modelTransform = null;
  2007. this.debugConsole = null;
  2008. this.canvasAnimationHandler = 0;
  2009. this.maps = null;
  2010. this.camera = new pas.GLTypes.TVec3();
  2011. this.lightPosition = new pas.GLTypes.TVec3();
  2012. this.terrainNoise = null;
  2013. this.terrainSize = 64 * 3;
  2014. this.terrainResolution = 128;
  2015. this.flySpeed = 1.3;
  2016. this.visiblity = 4;
  2017. this.textureLoaded = false;
  2018. rtl.createClass($mod,"TTilingTerrain",pas.Terrain.TTerrain,function () {
  2019. this.$init = function () {
  2020. pas.Terrain.TTerrain.$init.call(this);
  2021. this.neighbor = null;
  2022. };
  2023. this.$final = function () {
  2024. this.neighbor = undefined;
  2025. pas.Terrain.TTerrain.$final.call(this);
  2026. };
  2027. this.GetHeightForVertex = function (localX, localY, x, y) {
  2028. var Result = 0.0;
  2029. if ((localY === 0) && (this.neighbor !== null)) {
  2030. Result = this.neighbor.GetHeightAtPoint(localX,(localY + this.neighbor.GetWidth()) - 1)}
  2031. else {
  2032. Result = this.noise$1.GetNoise(x,y,this.GetWidth(),this.GetHeight(),6,3);
  2033. Result = Math.pow(Result,9) * 60;
  2034. };
  2035. return Result;
  2036. };
  2037. });
  2038. this.DrawCanvas = function () {
  2039. var terrainCoord = new pas.GLTypes.TVec3();
  2040. var startIndex = 0;
  2041. var endIndex = 0;
  2042. var i = 0;
  2043. var map = null;
  2044. $mod.gl.clear($mod.gl.COLOR_BUFFER_BIT + $mod.gl.DEPTH_BUFFER_BIT);
  2045. $mod.viewTransform = pas.Mat4.TMat4.$create("Identity");
  2046. $mod.viewTransform = $mod.viewTransform.Multiply(pas.Mat4.TMat4.$create("Translate",[$mod.camera.x,$mod.camera.y,$mod.camera.z]));
  2047. $mod.shader.SetUniformMat4("viewTransform",$mod.viewTransform);
  2048. $mod.shader.SetUniformMat4("inverseViewTransform",$mod.viewTransform.Inverse());
  2049. $mod.lightPosition.z += $mod.flySpeed;
  2050. $mod.shader.SetUniformVec3("lightPosition",new pas.GLTypes.TVec3($mod.lightPosition));
  2051. $mod.camera.z -= $mod.flySpeed;
  2052. $mod.camera.y = -($mod.terrainSize / 4) + (Math.sin($mod.camera.z / $mod.terrainSize) * 14);
  2053. $mod.camera.x = -($mod.terrainSize / 2) + (Math.cos($mod.camera.z / $mod.terrainSize) * 20);
  2054. terrainCoord = new pas.GLTypes.TVec3(pas.GLTypes.Divide(new pas.GLTypes.TVec3($mod.camera),$mod.terrainSize));
  2055. endIndex = pas.System.Trunc(Math.abs(terrainCoord.z));
  2056. startIndex = endIndex - $mod.visiblity;
  2057. if (startIndex < 0) startIndex = 0;
  2058. for (var $l1 = startIndex, $end2 = endIndex; $l1 <= $end2; $l1++) {
  2059. i = $l1;
  2060. if (($mod.maps.length === 0) || (($mod.maps.length === i) && ($mod.maps[i] == null))) {
  2061. map = $mod.TTilingTerrain.$create("Create$2",[$mod.gl,$mod.terrainNoise,$mod.terrainSize,$mod.terrainResolution,new pas.GLTypes.TVec2(pas.GLTypes.V2(0,$mod.terrainSize * i))]);
  2062. if ((i - 1) >= 0) map.neighbor = rtl.getObject($mod.maps[i - 1]);
  2063. map.Generate();
  2064. $mod.maps.push(map);
  2065. if ((startIndex - 1) >= 0) $mod.maps[startIndex - 1] = null;
  2066. };
  2067. map = rtl.getObject($mod.maps[i]);
  2068. $mod.modelTransform = pas.Mat4.TMat4.$create("Identity");
  2069. $mod.modelTransform = $mod.modelTransform.Multiply(pas.Mat4.TMat4.$create("Translate",[0,0,$mod.terrainSize * i]));
  2070. $mod.shader.SetUniformMat4("modelTransform",$mod.modelTransform);
  2071. map.Draw();
  2072. };
  2073. };
  2074. this.AnimateCanvas = function (time) {
  2075. if ($mod.textureLoaded) $mod.DrawCanvas();
  2076. if ($mod.canvasAnimationHandler !== 0) $mod.canvasAnimationHandler = window.requestAnimationFrame($mod.AnimateCanvas);
  2077. };
  2078. this.StartAnimatingCanvas = function () {
  2079. $mod.canvasAnimationHandler = window.requestAnimationFrame($mod.AnimateCanvas);
  2080. };
  2081. this.LoadedTexture = function (event) {
  2082. var Result = false;
  2083. var texture = null;
  2084. texture = $mod.gl.createTexture();
  2085. $mod.gl.bindTexture($mod.gl.TEXTURE_2D,texture);
  2086. $mod.gl.texParameteri($mod.gl.TEXTURE_2D,$mod.gl.TEXTURE_WRAP_S,$mod.gl.CLAMP_TO_EDGE);
  2087. $mod.gl.texParameteri($mod.gl.TEXTURE_2D,$mod.gl.TEXTURE_WRAP_T,$mod.gl.CLAMP_TO_EDGE);
  2088. $mod.gl.texParameteri($mod.gl.TEXTURE_2D,$mod.gl.TEXTURE_MIN_FILTER,$mod.gl.LINEAR);
  2089. $mod.gl.texParameteri($mod.gl.TEXTURE_2D,$mod.gl.TEXTURE_MAG_FILTER,$mod.gl.LINEAR);
  2090. $mod.gl.texImage2D($mod.gl.TEXTURE_2D,0,$mod.gl.RGBA,$mod.gl.RGBA,$mod.gl.UNSIGNED_BYTE,rtl.getObject(event.target));
  2091. $mod.textureLoaded = true;
  2092. Result = true;
  2093. return Result;
  2094. };
  2095. this.canvas = null;
  2096. this.vertexShaderSource = "";
  2097. this.fragmentShaderSource = "";
  2098. this.img = null;
  2099. $mod.$main = function () {
  2100. $mod.debugConsole = document.getElementById("debug-console");
  2101. $mod.canvas = document.createElement("canvas");
  2102. $mod.canvas.width = 800;
  2103. $mod.canvas.height = 600;
  2104. document.body.appendChild($mod.canvas);
  2105. $mod.gl = $mod.canvas.getContext("webgl");
  2106. if ($mod.gl === null) {
  2107. pas.System.Writeln("failed to load webgl!");
  2108. return;
  2109. };
  2110. $mod.vertexShaderSource = document.getElementById("vertex.glsl").textContent;
  2111. $mod.fragmentShaderSource = document.getElementById("fragment.glsl").textContent;
  2112. $mod.shader = pas.GLUtils.TShader.$create("Create$1",[$mod.gl,$mod.vertexShaderSource,$mod.fragmentShaderSource]);
  2113. $mod.shader.Compile();
  2114. $mod.shader.BindAttribLocation(0,"in_position");
  2115. $mod.shader.BindAttribLocation(1,"in_texCoord");
  2116. $mod.shader.BindAttribLocation(2,"in_normal");
  2117. $mod.shader.Link();
  2118. $mod.shader.Use();
  2119. $mod.gl.clearColor(0.9,0.9,0.9,1);
  2120. $mod.gl.viewport(0,0,$mod.canvas.width,$mod.canvas.height);
  2121. $mod.gl.enable($mod.gl.DEPTH_TEST);
  2122. $mod.gl.enable($mod.gl.BLEND);
  2123. $mod.gl.enable($mod.gl.CULL_FACE);
  2124. $mod.gl.cullFace($mod.gl.BACK);
  2125. $mod.projTransform = pas.Mat4.TMat4.$create("Perspective",[60.0,$mod.canvas.width / $mod.canvas.height,0.1,2000]);
  2126. $mod.shader.SetUniformMat4("projTransform",$mod.projTransform);
  2127. $mod.lightPosition = new pas.GLTypes.TVec3(pas.GLTypes.V3(0,$mod.terrainSize / 2,-($mod.terrainSize / 2)));
  2128. $mod.shader.SetUniformVec3("lightPosition",new pas.GLTypes.TVec3($mod.lightPosition));
  2129. $mod.shader.SetUniformVec3("lightColor",new pas.GLTypes.TVec3(pas.GLTypes.V3(1,1,1)));
  2130. $mod.shader.SetUniformFloat("shineDamper",1000);
  2131. $mod.shader.SetUniformFloat("reflectivity",1);
  2132. $mod.gl.clear($mod.gl.COLOR_BUFFER_BIT + $mod.gl.DEPTH_BUFFER_BIT);
  2133. $mod.camera.x = -($mod.terrainSize / 2);
  2134. $mod.camera.y = -($mod.terrainSize / 4);
  2135. $mod.camera.z = -($mod.terrainSize / 2);
  2136. $mod.img = document.createElement("IMG");
  2137. $mod.img.setAttribute("height","512");
  2138. $mod.img.setAttribute("width","512");
  2139. $mod.img.setAttribute("crossOrigin","anonymous");
  2140. $mod.img.setAttribute("src","res\/ground.jpg");
  2141. $mod.img.onload = $mod.LoadedTexture;
  2142. $mod.terrainNoise = pas.Noise.TNoise.$create("Create$2",[pas.Noise.RandomNoiseSeed(1).slice(0)]);
  2143. $mod.maps = new Array();
  2144. $mod.StartAnimatingCanvas();
  2145. };
  2146. });