ObJS.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /********************************************************************
  2. * The ObJS objectnitializes the canvas, assignin it to the HTML *
  3. * element, and set the required mouse events. *
  4. * #parameters: none *
  5. * #return: nothing *
  6. ********************************************************************/
  7. function ObJS(){
  8. /********************************************************************
  9. * Parameters for costumization. *
  10. ********************************************************************/
  11. var backColor = "rgba(200, 200, 200, 1)";
  12. var vertColor = "rgba(10, 10, 10, 1)";
  13. var edgeColor = "rgba(100, 70, 70, 1)";
  14. var faceColor = "rgba(180, 180, 255, .6)";
  15. var faceAlpha = .6;
  16. var textColor = "rgba(0, 0, 0, 1)";
  17. var vertSize = 4; //Make it even: at small scales will look better
  18. var rotationSpeed = 90; //Smaller = faster. 100 is a good speed
  19. var zoomSpeed = 0.161; //0-1. 0.2 or 0.3 are good speeds
  20. var moveSpeed = .5;
  21. /********************************************************************
  22. * Variable containing this object, to be refered from nested *
  23. * functions. *
  24. ********************************************************************/
  25. var thisObJS;
  26. /********************************************************************
  27. * Parameter used to scale the measures in the .obj file. Can be *
  28. * integer or float, greater than 0. 50 is a good default. *
  29. ********************************************************************/
  30. var scale;
  31. /********************************************************************
  32. * Variable to determine if the canvas has been initialized. *
  33. ********************************************************************/
  34. var canvasInitialized = false;
  35. /********************************************************************
  36. * Variable to determine if the mouse button is being holded down. *
  37. ********************************************************************/
  38. var mDown = false;
  39. var pinch = false;
  40. var pinchScale = 0;
  41. /********************************************************************
  42. * Variables containing the canvas and its context. *
  43. ********************************************************************/
  44. var canvas;
  45. var ctx;
  46. /********************************************************************
  47. * Variables containing the number of elements. *
  48. ********************************************************************/
  49. var totalVert;
  50. var totalMaterial = 0;
  51. var totalFace
  52. /********************************************************************
  53. * Arrays containing the vertices, faces and materials. *
  54. ********************************************************************/
  55. var material;
  56. var vert;
  57. var face;
  58. /********************************************************************
  59. * Booleans to determine wich elements are to draw. *
  60. ********************************************************************/
  61. var dVerts = true;
  62. var dEdges = true;
  63. var dFaces = true;
  64. var dBackg = true;
  65. /********************************************************************
  66. * Booleans to indicate if mtl file should be used. *
  67. ********************************************************************/
  68. var hasMtl = true;
  69. var useMtl = true;
  70. /********************************************************************
  71. * Global variables that will contain the position of the mouse in *
  72. * the previous frame to calculate rotation. *
  73. ********************************************************************/
  74. var pX = null;
  75. var pY = null;
  76. /********************************************************************
  77. * Function that initializes the canvas, assignin it to the HTML *
  78. * element, and set the required mouse events. *
  79. * #parameters: *
  80. * canv (canvas): canvas to use. If omited, the one named *
  81. * named "ObJSCanvas" will be used. *
  82. * #return: nothing *
  83. * #scope: public *
  84. ********************************************************************/
  85. this.initCanvas = function(canv){
  86. if (!canvasInitialized){
  87. if (canv!= null)
  88. canvas = canv;
  89. else
  90. canvas = document.getElementById("ObJSCanvas");
  91. ctx = canvas.getContext("2d");
  92. ctx.translate(canvas.width / 2, canvas.height / 2);
  93. canvas.onmousedown = function(e){
  94. pX = e.offsetX==undefined?e.layerX:e.offsetX;
  95. pY = e.offsetY==undefined?e.layerY:e.offsetY;
  96. mDown = true;
  97. };
  98. canvas.onmouseup = function(e){
  99. if(mDown)
  100. mouseClick(e);
  101. pX = null;
  102. pY = null;
  103. mDown = false;
  104. };
  105. canvas.onmousemove = function(e){
  106. if(!mDown)
  107. return;
  108. var x = e.offsetX==undefined?e.layerX:e.offsetX;
  109. var y = e.offsetY==undefined?e.layerY:e.offsetY;
  110. if (e.shiftKey){
  111. thisObJS.moveHorizontal(x - pX);
  112. thisObJS.moveVertical(y - pY);
  113. }
  114. else{
  115. thisObJS.rotateY(x - pX);
  116. thisObJS.rotateX(y - pY);
  117. }
  118. pX = x;
  119. pY = y;
  120. return false;
  121. };
  122. canvas.onmouseout = function(e){
  123. if(mDown)
  124. mDown = false;
  125. }
  126. canvas.addEventListener('mousewheel',function(e){
  127. e.preventDefault();
  128. if (event.wheelDelta > 0)
  129. thisObJS.zoom(1);
  130. else
  131. thisObJS.zoom(-1);
  132. return false;
  133. }, false);
  134. canvas.addEventListener('touchstart',function(e){
  135. if(e.touches.length == 2) {
  136. pinch = true;
  137. pinchScale = Math.sqrt((e.touches[0].clientX-e.touches[1].clientX) * (e.touches[0].clientX-e.touches[1].clientX) + (e.touches[0].clientY-e.touches[1].clientY) * (e.touches[0].clientY-e.touches[1].clientY));
  138. }
  139. return false;
  140. }, false);
  141. canvas.addEventListener('touchend',function(e){
  142. if(pinch)
  143. pinch = false;
  144. return false;
  145. }, false);
  146. canvas.addEventListener('touchcancel',function(e){
  147. if(pinch)
  148. pinch = false;
  149. return false;
  150. }, false);
  151. canvas.addEventListener('touchleave',function(e){
  152. if(pinch)
  153. pinch = false;
  154. return false;
  155. }, false);
  156. canvas.addEventListener('touchmove',function(e){
  157. e.preventDefault();
  158. if(pinch){
  159. var dist = Math.sqrt((e.touches[0].clientX-e.touches[1].clientX) * (e.touches[0].clientX-e.touches[1].clientX) + (e.touches[0].clientY-e.touches[1].clientY) * (e.touches[0].clientY-e.touches[1].clientY));
  160. if (dist > pinchScale)
  161. thisObJS.zoom(1);
  162. else if (dist < pinchScale)
  163. thisObJS.zoom(-1);
  164. pinchScale = dist;
  165. }
  166. else{
  167. var x = e.touches[0].clientX
  168. var y = e.touches[0].clientY;
  169. thisObJS.rotateY(x - pX);
  170. thisObJS.rotateX(y - pY);
  171. pX = x;
  172. pY = y;
  173. }
  174. return false;
  175. }, false);
  176. canvasInitialized = true;
  177. }
  178. };
  179. /********************************************************************
  180. * Main function, called every time a file is to be loaded. *
  181. * #parameters: *
  182. * file (string): name of the file, without path or extension. *
  183. * #return: nothing *
  184. * #scope: public *
  185. ********************************************************************/
  186. this.load = function(file){
  187. if(!canvasInitialized)
  188. initCanvas();
  189. //Get obj file
  190. var xmlhttp = new XMLHttpRequest();
  191. xmlhttp.open("GET", file, false);
  192. xmlhttp.send();
  193. var fileContent = xmlhttp.responseText;
  194. //Get mtl file
  195. xmlhttp = new XMLHttpRequest();
  196. xmlhttp.open("GET",file.substring(0, file.lastIndexOf(".")) + ".mtl", false);
  197. xmlhttp.send();
  198. var mtlContent = xmlhttp.responseText;
  199. initArrays();
  200. readFile(fileContent);
  201. readMtlFile(mtlContent);
  202. draw();
  203. };
  204. /********************************************************************
  205. * Getters that determine if an element is being drawn. *
  206. ********************************************************************/
  207. this.isBackgroundDrawn = function(){
  208. return dBackg;
  209. };
  210. this.isVertexDrawn = function(){
  211. return dVerts;
  212. };
  213. this.isEdgeDrawn = function(){
  214. return dEdges;
  215. };
  216. this.isFaceDrawn = function(){
  217. return dFaces;
  218. };
  219. /********************************************************************
  220. * Getters that determine if the model has associated materials. *
  221. ********************************************************************/
  222. this.hasMaterial = function(){
  223. return hasMtl;
  224. };
  225. /********************************************************************
  226. * Functions switching the elements drawn (dVerts, dEdges, dFaces). *
  227. * #parameters: *
  228. * on (boolean): indicating if the element is to be drawn. *
  229. * #return: nothing *
  230. * #scope: public *
  231. ********************************************************************/
  232. this.drawVertices = function(on){
  233. if (on == true || on == false){
  234. dVerts = on;
  235. draw();
  236. }
  237. };
  238. this.drawEdges = function(on){
  239. if (on == true || on == false){
  240. dEdges = on;
  241. draw();
  242. }
  243. };
  244. this.drawFaces = function(on){
  245. if (on == true || on == false){
  246. dFaces = on;
  247. draw();
  248. }
  249. };
  250. this.drawBackground = function(on){
  251. if (on == true || on == false){
  252. dBackg = on;
  253. draw();
  254. }
  255. };
  256. /********************************************************************
  257. * Functions switching the color of the elements. *
  258. * #parameters: *
  259. * code (string): hex color value, in #fff or #ffffff form. If *
  260. * wrong, a message will be printed to console, *
  261. * and nothing will be changed. *
  262. * #return: nothing *
  263. * #scope: public *
  264. ********************************************************************/
  265. this.setVerticesColor = function(code){
  266. if (isColor(code)){
  267. var color = hexToRgb(code);
  268. vertColor = "rgba(" + color.r + ", " + color.g + ", " + color.b + ", 1)";
  269. draw();
  270. }
  271. else
  272. console.log("setVerticesColor(" + code + ") ERROR: " + code + " not a valid hex color");
  273. };
  274. this.setEdgesColor = function(code){
  275. if (isColor(code)){
  276. var color = hexToRgb(code);
  277. edgeColor = "rgba(" + color.r + ", " + color.g + ", " + color.b + ", 1)";
  278. draw();
  279. }
  280. else
  281. console.log("setEdgesColor(" + code + ") ERROR: " + code + " not a valid hex color");
  282. };
  283. this.setFacesColor = function(code){
  284. if (isColor(code)){
  285. var color = hexToRgb(code);
  286. faceColor = "rgba(" + color.r + ", " + color.g + ", " + color.b + ", 1)";
  287. draw();
  288. }
  289. else
  290. console.log("setFacesColor(" + code + ") ERROR: " + code + " not a valid hex color");
  291. };
  292. this.setBackgroundColor = function(code){
  293. if (isColor(code)){
  294. var color = hexToRgb(code);
  295. backColor = "rgba(" + color.r + ", " + color.g + ", " + color.b + ", 1)";
  296. draw();
  297. }
  298. else
  299. console.log("setBackgroundColor(" + code + ") ERROR: " + code + " not a valid hex color");
  300. };
  301. /********************************************************************
  302. * Function switching the use of mtl file for color. *
  303. * #parameters: *
  304. * value (boolean): indicating if the element is to be drawn. *
  305. * #return: nothing *
  306. * #scope: public *
  307. ********************************************************************/
  308. this.linkMaterial = function(value){
  309. useMtl = value;
  310. draw();
  311. }
  312. /********************************************************************
  313. * Function switching the alpha value of the faces. *
  314. * #parameters: *
  315. * percent (int): Value (0-100) of alpha. Must be processed (0-1) *
  316. * #return: nothing *
  317. * #scope: public *
  318. ********************************************************************/
  319. this.setAlpha = function(percent){
  320. if (percent >= 0 && percent <= 100){
  321. faceAlpha = percent / 100;
  322. faceColor = faceColor.substring(0, faceColor.lastIndexOf(",") + 2) + faceAlpha + ")";
  323. draw();
  324. }
  325. else
  326. console.log("setAlpha(" + percent + "); ERROR: Only values between 0 and 100 are allowed");
  327. };
  328. /********************************************************************
  329. * Function switching the rotation speed. *
  330. * #parameters: *
  331. * percent (int): Value (0-10) of speed. *
  332. * #return: nothing *
  333. * #scope: public *
  334. ********************************************************************/
  335. this.setRotationSpeed = function(percent){
  336. if (percent >= 0 && percent <= 10){
  337. rotationSpeed = Math.round((10 - percent) * 30);
  338. if (rotationSpeed == 0)
  339. rotationSpeed = 1;
  340. }
  341. else
  342. console.log("setRotationSpeed(" + percent + "); ERROR: Only values between 0 and 10 are allowed");
  343. }
  344. /********************************************************************
  345. * Functions that rotate elements in the canvas along the *
  346. * correspondent axys, calculating the new position for each vertex *
  347. * in the vert array. *
  348. * #parameters: *
  349. * des (int): distance to rotate. *
  350. * #return: nothing *
  351. * #scope: public *
  352. ********************************************************************/
  353. this.rotateY = function(des){
  354. for (var i = 0; i < vert.length; i ++) {
  355. var x = vert[i][0];
  356. var z = vert[i][2];
  357. vert[i][0] = x * Math.cos(des / rotationSpeed) - z * Math.sin(des / rotationSpeed);
  358. vert[i][2] = z * Math.cos(des / rotationSpeed) + x * Math.sin(des / rotationSpeed);
  359. }
  360. draw();
  361. };
  362. this.rotateX = function(des){
  363. for (var i = 0; i < vert.length; i ++) {
  364. var y = vert[i][1];
  365. var z = vert[i][2];
  366. vert[i][1] = y * Math.cos(des / rotationSpeed) - z * Math.sin(des / rotationSpeed);
  367. vert[i][2] = z * Math.cos(des / rotationSpeed) + y * Math.sin(des / rotationSpeed);
  368. }
  369. draw();
  370. };
  371. /********************************************************************
  372. * Function switching the movement speed of the model. *
  373. * #parameters: *
  374. * percent (int): Value (0-10) of speed. *
  375. * #return: nothing *
  376. * #scope: public *
  377. ********************************************************************/
  378. this.setMovementSpeed = function(percent){
  379. if (percent >= 0 && percent <= 10)
  380. moveSpeed = ((percent * 0.8) / 10) + 0.01;
  381. else
  382. console.log("setMoveSpeed(" + percent + "); ERROR: Only values between 0 and 10 are allowed");
  383. }
  384. /********************************************************************
  385. * Functions that move elements in the canvas along the selected *
  386. * direction, calculating the new position for each vertex in the *
  387. * vert array. *
  388. * #parameters: *
  389. * des (int): distance to move. *
  390. * #return: nothing *
  391. * #scope: public *
  392. ********************************************************************/
  393. this.moveVertical = function(des){
  394. for (var i = 0; i < vert.length; i ++)
  395. vert[i][1] = (vert[i][1] * 1) + des * (moveSpeed / 50);
  396. draw();
  397. };
  398. this.moveHorizontal = function(des){
  399. for (var i = 0; i < vert.length; i ++)
  400. vert[i][0] = (vert[i][0] * 1) + des * (moveSpeed / 50);
  401. draw();
  402. };
  403. /********************************************************************
  404. * Function switching the zoom speede of the faces. *
  405. * #parameters: *
  406. * percent (int): Value (0-100) of speed. *
  407. * #return: nothing *
  408. * #scope: public *
  409. ********************************************************************/
  410. this.setZoomSpeed = function(percent){
  411. if (percent >= 0 && percent <= 10)
  412. zoomSpeed = ((percent * 0.8) / 10) + 0.01;
  413. else
  414. console.log("setZoomSpeed(" + percent + "); ERROR: Only values between 0 and 10 are allowed");
  415. }
  416. /********************************************************************
  417. * Function that changes the scale variable, witch results in a *
  418. * zoom in or zoom out effect. *
  419. * #parameters: none *
  420. * #return: nothing *
  421. * #scope: public *
  422. ********************************************************************/
  423. this.zoom = function(val){
  424. scale = scale + val * scale * zoomSpeed;
  425. draw();
  426. };
  427. /********************************************************************
  428. * Function that clears the arrays and the canvas. Use to unload a *
  429. * model. *
  430. * #parameters: none *
  431. * #return: nothing *
  432. * #scope: public *
  433. ********************************************************************/
  434. this.clear = function(){
  435. initArrays();
  436. clearCanvas();
  437. };
  438. /********************************************************************
  439. * Function that turns a hex color code in RGB. *
  440. * stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb *
  441. * #parameters: *
  442. * hex (string): color code, for example "#33ff99" *
  443. * #return: (Array) r, g, b, with their correspondent color. *
  444. * #scope: private *
  445. ********************************************************************/
  446. var hexToRgb = function(hex) {
  447. var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  448. return result ? {
  449. r: parseInt(result[1], 16),
  450. g: parseInt(result[2], 16),
  451. b: parseInt(result[3], 16)
  452. } : null;
  453. };
  454. /********************************************************************
  455. * Function that checks if a string is a valid hexadecimal color *
  456. * code, in the format '#ffffff' or '#fff' *
  457. * #parameters: *
  458. * code (string): Code to check. *
  459. * #return: (Boolean) True if valid color code, false otherwise. *
  460. * #scope: private *
  461. ********************************************************************/
  462. var isColor = function(code){
  463. if (code[0] != '#')
  464. return false;
  465. if (code.length != 4 && code.length != 7)
  466. return false
  467. for (var i = 1; i < code.length; i ++)
  468. if (code[i] < '0' || (code[i] > 9 && code[i] < 'A') || (code[i] > 'Z' && code[i] < 'a') || code[i] > 'z')
  469. return false;
  470. return true;
  471. };
  472. /********************************************************************
  473. * Function that clears the canvas and gets it ready to draw on it. *
  474. * #parameters: none *
  475. * #return: nothing *
  476. * #scope: private *
  477. ********************************************************************/
  478. var clearCanvas = function(){
  479. ctx.clearRect(0, 0, canvas.width, canvas.height);
  480. if (dBackg)
  481. ctx.fillStyle = backColor;
  482. else
  483. ctx.fillStyle = "#ffffff";
  484. ctx.fillRect(-canvas.width / 2, -canvas.height / 2, canvas.width, canvas.height);
  485. };
  486. /********************************************************************
  487. * Function initializing the arrays obj, vert and face as empty *
  488. * arrays. *
  489. * #parameters: none *
  490. * #return: nothing *
  491. * #scope: private *
  492. ********************************************************************/
  493. var initArrays = function(){
  494. material = null;
  495. vert = null;
  496. face = null;
  497. material = new Array();
  498. vert = new Array();
  499. face = new Array();
  500. };
  501. /********************************************************************
  502. * Function tat populates the vert[] array with and array of three *
  503. * elements, containig the coordinates, and the face[] array with *
  504. * the vertices forming the face, plus one element indicating the *
  505. * codename of the material. *
  506. * #parameters: *
  507. * text (string): the content of the obj file. *
  508. * #return: nothing *
  509. * #scope: private *
  510. ********************************************************************/
  511. var readFile = function(text){
  512. var line;
  513. var v = 0;
  514. var f = 0;
  515. var m = 0;
  516. var max = 0;
  517. var j;
  518. var dist;
  519. var fac;
  520. var mat = "null";
  521. text = text + "\n";
  522. while (text.indexOf("\n") != -1){
  523. line = text.substring(0, text.indexOf("\n"));
  524. if (line.substring(0, 2) == "v "){
  525. vert[v] = new Array(3);
  526. line = line.substring(2);
  527. vert[v][0] = line.substring(0, line.indexOf(" "));
  528. line = line.substring(line.indexOf(" ") + 1);
  529. vert[v][1] = line.substring(0, line.indexOf(" "));
  530. line = line.substring(line.indexOf(" ") + 1);
  531. vert[v][2] = line;
  532. dist = Math.sqrt(vert[v][0] * vert[v][0] + vert[v][1] * vert[v][1] + vert[v][2] * vert[v][2]);
  533. if (dist > max)
  534. max = dist;
  535. v = v + 1;
  536. }
  537. else if (line.substring(0,2) == "f "){
  538. line = line.substring(2);
  539. face[f] = new Array();
  540. j = 0;
  541. while(line.indexOf(" ") != -1){
  542. face[f][j] = line.substring(0, line.indexOf(" "));
  543. if (face[f][j].indexOf('/') != -1)
  544. face[f][j] = face[f][j].substring(0, face[f][j].indexOf('/'));
  545. face[f][j] = face[f][j] - 1;
  546. line = line.substring(line.indexOf(" ") + 1);
  547. j = j + 1;
  548. }
  549. face[f][j] = line;
  550. if (face[f][j].indexOf('/') != -1)
  551. face[f][j] = face[f][j].substring(0, face[f][j].indexOf('/'));
  552. face[f][j] = face[f][j] - 1;
  553. face[f][j + 1] = mat;
  554. f = f + 1;
  555. }
  556. else if (line.substring(0, 6) == "usemtl"){
  557. mat = line.substring(7);
  558. }
  559. text = text.substring(text.indexOf("\n") + 1);
  560. }
  561. scale = calculateScale(max);
  562. totalVert = vert.length;
  563. totalFace = face.length;
  564. };
  565. /********************************************************************
  566. * Function tat populates the material[] array with an array *
  567. * containig, four values: the material name, and the three RGB *
  568. * values. *
  569. * #parameters: *
  570. * text (string): the content of the mtl file. *
  571. * #return: nothing *
  572. * #scope: private *
  573. ********************************************************************/
  574. var readMtlFile = function(text){
  575. text = text + "\n";
  576. i = -1;
  577. while (text.indexOf("\n") != -1){
  578. line = text.substring(0, text.indexOf("\n"));
  579. if (line.substring(0, 6) == "newmtl"){
  580. i = i + 1;
  581. material[i] = new Array(4);
  582. material[i][0] = line.substring(line.indexOf(" ") + 1);
  583. }
  584. //Kd, ddifuse color, usefull
  585. //Ka, ambient color, not now
  586. //Ks, specular color, not now
  587. //d or TR, transparency, not now
  588. else if (line.substring(0, 3) == "Kd "){
  589. line = line.substring(3);
  590. material[i][1] = line.substring(0, line.indexOf(" "));
  591. line = line.substring(line.indexOf(" ") + 1);
  592. material[i][2] = line.substring(0, line.indexOf(" "));
  593. line = line.substring(line.indexOf(" ") + 1);
  594. material[i][3] = line;
  595. }
  596. text = text.substring(text.indexOf("\n") + 1);
  597. }
  598. totalMaterial = material.length;
  599. if (totalMaterial == 0)
  600. hasMtl = false;
  601. };
  602. /********************************************************************
  603. * Function that automatically calculates the best scale for the *
  604. * modelermined number of elements, containing the verices that *
  605. * form a face, so the bigest dimension from the object can fit in *
  606. * the smallest dimension of the canvas. *
  607. * #parameters: *
  608. * text (int): the farthest vertex, from the center. *
  609. * #return: (int) The scale to be aplied. *
  610. * #scope: private *
  611. ********************************************************************/
  612. var calculateScale = function(max){
  613. var dim;
  614. if (canvas.width < canvas.height)
  615. dim = canvas.width;
  616. else
  617. dim = canvas.height;
  618. var sc = Math.round((0.95 * dim) / (2 * max));
  619. return sc;
  620. };
  621. /********************************************************************
  622. * Function that compares the average Z coordinate of two faces. *
  623. * Must be used when sorting the face array. *
  624. * #parameters: *
  625. * a (Array): One face. *
  626. * b (Array): The other face. *
  627. * #return: (int) -1, 1 or 0. *
  628. * #scope: private *
  629. ********************************************************************/
  630. var faceComparator = function(a,b){
  631. var aZ;
  632. var bZ;
  633. var i = 0;
  634. var sum = 0.0;
  635. while (i < a.length - 1){
  636. sum = sum + parseFloat(vert[a[i]][2]);
  637. i = i + 1;
  638. }
  639. aZ = sum / parseFloat(i);
  640. i = 0;
  641. sum = 0;
  642. while (i < b.length - 1){
  643. sum = sum + parseFloat(1 * vert[b[i]][2]);
  644. i = i + 1;
  645. }
  646. bZ = sum / parseFloat(i);
  647. if (aZ < bZ) return -1;
  648. if (aZ > bZ) return 1;
  649. return 0;
  650. };
  651. /********************************************************************
  652. * Function that writes text in the corner of the canvas. Must be *
  653. * called when everithing has been drawn. *
  654. * #parameters: none *
  655. * #return: nothing *
  656. * #scope: private *
  657. ********************************************************************/
  658. var writeCredits = function(){
  659. ctx.fillStyle = textColor;
  660. ctx.font = "12px Arial";
  661. var h = canvas.height / 2 - 14;
  662. var w = 10 - (canvas.width / 2);
  663. ctx.fillText("ObJS: " + totalVert + " vertices " + totalFace + " faces " + totalMaterial + " materials", w, h);
  664. };
  665. /********************************************************************
  666. * Function that draws th vertices, faces and edges in the canvas. *
  667. * #parameters: none *
  668. * #return: nothing *
  669. * #scope: private *
  670. ********************************************************************/
  671. var draw = function(){
  672. face = face.sort(faceComparator).reverse();
  673. clearCanvas();
  674. for (var i = 0; i < face.length; i++){
  675. ctx.strokeStyle = edgeColor;
  676. ctx.fillStyle = vertColor;
  677. ctx.beginPath();
  678. ctx.moveTo(scale * vert[face[i][0]][0], scale * vert[face[i][0]][1]);
  679. for (var j = 1; j < face[i].length - 1; j++) {
  680. ctx.lineTo(scale * vert[face[i][j]][0], scale * vert[face[i][j]][1]);
  681. if(dEdges)
  682. ctx.stroke();
  683. if(dVerts){
  684. var x = scale * vert[face[i][j]][0] - (vertSize / 2);
  685. var w = vertSize;
  686. var y = scale * vert[face[i][j]][1] - (vertSize / 2);
  687. var h = vertSize;
  688. ctx.fillRect(x, y, w, h);
  689. }
  690. }
  691. ctx.closePath();
  692. if (useMtl){
  693. //Get material with the same name as the one in the last element of the face
  694. j = 0;
  695. while (material[j][0] != face[i][face[i].length - 1] && j < material.length)
  696. j = j + 1;
  697. if (material[j][0] == face[i][face[i].length - 1])
  698. ctx.fillStyle = "rgba(" + Math.round(255 * material[j][1]) + ", " + Math.round(255 * material[j][2]) + ", " + Math.round(255 * material[j][3]) + ", " + faceAlpha + ")";
  699. else
  700. ctx.fillStyle = faceColor;
  701. }
  702. else
  703. ctx.fillStyle = faceColor;
  704. if(dFaces)
  705. ctx.fill();
  706. }
  707. writeCredits();
  708. };
  709. /********************************************************************
  710. * TODO: Function used when something in the canvas is clicked. *
  711. * #parameters: none *
  712. * #return: nothing *
  713. * #scope: private *
  714. ********************************************************************/
  715. var mouseClick = function(){
  716. };
  717. thisObJS = this;
  718. return this;
  719. }