ObJS.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /********************************************************************
  2. * Parameters used all along the script. Only for *
  3. * customization. *
  4. ********************************************************************/
  5. var backColor = "rgba(200, 200, 200, 1)";
  6. var vertColor = "rgba(10, 10, 10, 1)";
  7. var edgeColor = "rgba(100, 70, 70, 1)";
  8. var faceColor = "rgba(180, 180, 255, .3)";
  9. var textColor = "rgba(0, 0, 0, 1)";
  10. var vertSize = 2; //Make it even: at small scales will look better
  11. var rotationSpeed = 100; //Smaller = faster. 100 is a good speed
  12. /********************************************************************
  13. * Parameters used to scale the measures in the .obj file. Can be *
  14. * integer or float, greater than 0 *
  15. * TODO: Must be automatically calculated according to the canvas *
  16. * dimensions, so the bigest dimension from the object can fit in *
  17. * the smallest dimension of the canvas. *
  18. ********************************************************************/
  19. var scale = 50;
  20. /********************************************************************
  21. * Global variable to determine if the mouse button is being holded *
  22. * down. *
  23. ********************************************************************/
  24. var mDown = false;
  25. /********************************************************************
  26. * Global variables containing the canvas and its context. *
  27. ********************************************************************/
  28. var canvas;
  29. var ctx;
  30. /********************************************************************
  31. * Global variables containing the number of elements. *
  32. ********************************************************************/
  33. var totalVert;
  34. var totalEdge;//TODO: I dont know how to calculate
  35. var totalFace
  36. /********************************************************************
  37. * Arrays containing the vertices, edges and faces. *
  38. ********************************************************************/
  39. var obj; //Unused
  40. var vert;
  41. var face;
  42. /********************************************************************
  43. * Booleans to determine wich elements are to draw. *
  44. ********************************************************************/
  45. var dVerts = true;
  46. var dEdges = true;
  47. var dFaces = true;
  48. /********************************************************************
  49. * Global variables that will contain the position of the mouse in *
  50. * the previous frame to calculate rotation. *
  51. ********************************************************************/
  52. var pX = null;
  53. var pY = null;
  54. /********************************************************************
  55. * Function switching the elements to draw (dVerts, dEdges, dFaces) *
  56. * @parameters: *
  57. * name (string): key to the element to draw. *
  58. * value (boolean): indicating if the element is to be drawn. *
  59. * @return: nothing *
  60. ********************************************************************/
  61. function drawElement(name, value){
  62. switch (name){
  63. case "verts":
  64. dVerts = value;
  65. break;
  66. case "edges":
  67. dEdges = value;
  68. break;
  69. case "faces":
  70. dFaces = value;
  71. break;
  72. }
  73. draw();
  74. }
  75. /********************************************************************
  76. * Main function, called every time a file is to be loaded. *
  77. * @parameters: *
  78. * val (string): name of the file, without path or extension. *
  79. * @return: nothing *
  80. ********************************************************************/
  81. function loadModel(val){
  82. //Get file
  83. xmlhttp = new XMLHttpRequest();
  84. xmlhttp.open("GET","/obj/" + val.value + ".obj", false);
  85. xmlhttp.send();
  86. var fileContent = xmlhttp.responseText;
  87. initArrays();
  88. readVerts(fileContent);
  89. readFaces(fileContent);
  90. draw();
  91. }
  92. /********************************************************************
  93. * Function initializing the arrays obj, vert and face as empty *
  94. * arrays. *
  95. * @parameters: none *
  96. * @return: nothing *
  97. ********************************************************************/
  98. function initArrays(){
  99. obj = null;
  100. vert = null;
  101. face = null;
  102. obj = new Array();
  103. vert = new Array();
  104. face = new Array();
  105. }
  106. /********************************************************************
  107. * Function tat populates the vert[] array with and array of three *
  108. * elements, containig the coordinates, with the scale applied. *
  109. * @parameters: *
  110. * text (string): the content of the obj file. *
  111. * @return: nothing *
  112. ********************************************************************/
  113. function readVerts(text){
  114. var line;
  115. var i = 0;
  116. while (text.indexOf("v ") != -1){
  117. line = text.substring(text.indexOf("v "), text.indexOf("\n", text.indexOf("v ")));
  118. vert[i] = new Array(3);
  119. line = line.substring(2);
  120. vert[i][0] = Math.round(scale * line.substring(0, line.indexOf(" ")));
  121. line = line.substring(line.indexOf(" ") + 1);
  122. vert[i][1] = Math.round(scale * line.substring(0, line.indexOf(" ")));
  123. line = line.substring(line.indexOf(" ") + 1);
  124. vert[i][2] = Math.round(scale * line);
  125. i = i + 1;
  126. text = text.substring(text.indexOf("v ") + 1)
  127. }
  128. totalVert = vert.length;
  129. }
  130. /********************************************************************
  131. * Function that populates the face[] array with and array of an *
  132. * undetermined number of elements, containing the verices that *
  133. * form a face. *
  134. * @parameters: *
  135. * text (string): the content of the obj file. *
  136. * @return: nothing *
  137. ********************************************************************/
  138. function readFaces(text){
  139. var finish = false;
  140. var line;
  141. var i = 0;
  142. var j;
  143. while (text.indexOf("f ") != -1){
  144. line = text.substring(text.indexOf("f "), text.indexOf("\n", text.indexOf("f ")));
  145. line = line.substring(2);
  146. face[i] = new Array();
  147. j = 0;
  148. while(line.indexOf(" ") != -1){
  149. face[i][j] = line.substring(0, line.indexOf(" ")) - 1;
  150. line = line.substring(line.indexOf(" ") + 1);
  151. j = j + 1;
  152. }
  153. face[i][j] = line - 1;
  154. i = i + 1;
  155. text = text.substring(text.indexOf("f ") + 1)
  156. }
  157. totalFace = face.length;
  158. }
  159. /********************************************************************
  160. * Function that draws the vertizes in the canvas. *
  161. * @parameters: none *
  162. * @return: nothing *
  163. ********************************************************************/
  164. function drawVerts(){
  165. ctx.fillStyle = vertColor;
  166. var x;
  167. var y;
  168. var w;
  169. var h;
  170. //console.log("Drawing " + vert.length + " vertices")
  171. for (var i = 0; i < vert.length; i++) {
  172. x = vert[i][0] - (vertSize / 2);
  173. w = vertSize;
  174. y = vert[i][1] - (vertSize / 2);
  175. h = vertSize;
  176. ctx.fillRect(x, y, w, h);
  177. }
  178. }
  179. /********************************************************************
  180. * Function that draws the edges and the faces in the canvas, if *
  181. * they are to be drawn according to dEdges and dFaces. *
  182. * @parameters: none *
  183. * @return: nothing *
  184. ********************************************************************/
  185. function drawFaces(){
  186. //console.log("Drawing " + face.length + " faces")
  187. for (var i = 0; i < face.length; i++){
  188. ctx.strokeStyle = edgeColor;
  189. ctx.beginPath();
  190. ctx.moveTo(vert[face[i][0]][0], vert[face[i][0]][1]);
  191. for (var j = 1; j < face[i].length; j++) {
  192. ctx.lineTo(vert[face[i][j]][0], vert[face[i][j]][1]);
  193. if(dEdges)
  194. ctx.stroke();
  195. }
  196. ctx.closePath();
  197. ctx.fillStyle = faceColor;
  198. if(dFaces)
  199. ctx.fill();
  200. }
  201. }
  202. /********************************************************************
  203. * Function that writes text in the corner of the canvas. Must be *
  204. * called when everithing has been drawn. *
  205. * @parameters: none *
  206. * @return: nothing *
  207. ********************************************************************/
  208. function writeCredits(){
  209. ctx.fillStyle = textColor;
  210. ctx.font = "12px Arial";
  211. var h = canvas.height / 2 - 14;
  212. var w = 10 - (canvas.height / 2);
  213. ctx.fillText("ObJS " + totalVert + " vertizes " + totalFace + " faces", w, h);
  214. }
  215. /********************************************************************
  216. * Function that calls every neccesary function to draw the canvas. *
  217. * @parameters: none *
  218. * @return: nothing *
  219. ********************************************************************/
  220. function draw(){
  221. initCanvas();
  222. if (dFaces || dEdges)
  223. drawFaces();
  224. if (dVerts)
  225. drawVerts();
  226. writeCredits();
  227. }
  228. /********************************************************************
  229. * Function that rotates elements in the canvas along the Y axis, *
  230. * calculating the new position for each vertex in the vert array. *
  231. * @parameters: *
  232. * des (int): mouse displacement from the last frame. *
  233. * @return: nothing *
  234. ********************************************************************/
  235. function rotateY(des) {
  236. for (var i = 0; i < vert.length; i ++) {
  237. var x = vert[i][0];
  238. var z = vert[i][2];
  239. vert[i][0] = x * Math.cos(des / rotationSpeed) - z * Math.sin(des / rotationSpeed);
  240. vert[i][2] = z * Math.cos(des / rotationSpeed) + x * Math.sin(des / rotationSpeed);
  241. }
  242. };
  243. /********************************************************************
  244. * Function that rotates elements in the canvas along the X axis, *
  245. * calculating the new position for each vertex in the vert array. *
  246. * @parameters: *
  247. * des (int): mouse displacement from the last frame. *
  248. * @return: nothing *
  249. ********************************************************************/
  250. function rotateX(des) {
  251. for (var i = 0; i < vert.length; i ++) {
  252. var y = vert[i][1];
  253. var z = vert[i][2];
  254. vert[i][1] = y * Math.cos(des / rotationSpeed) - z * Math.sin(des / rotationSpeed);
  255. vert[i][2] = z * Math.cos(des / rotationSpeed) + y * Math.sin(des / rotationSpeed);
  256. }
  257. };
  258. /********************************************************************
  259. * Function that gets the canvas ready to be drawn. It initializes *
  260. * the canvas and ctx variables if they arent already, seting mouse *
  261. * events for them, clears the canvas and sets the background. *
  262. * @parameters: none *
  263. * @return: nothing *
  264. ********************************************************************/
  265. function initCanvas(){
  266. if (canvas == null){
  267. canvas = document.getElementById("ObJSCanvas");
  268. ctx = canvas.getContext("2d");
  269. ctx.translate(canvas.width / 2, canvas.height / 2);
  270. canvas.onmousedown = function(e){
  271. pX = e.offsetX==undefined?e.layerX:e.offsetX;
  272. pY = e.offsetY==undefined?e.layerY:e.offsetY;
  273. mDown = true;
  274. }
  275. canvas.onmouseup = function(e){
  276. if(mDown)
  277. mouseClick(e);
  278. pX = null;
  279. pY = null;
  280. mDown = false;
  281. }
  282. canvas.onmousemove = function(e){
  283. if(!mDown)
  284. return;
  285. var x = e.offsetX==undefined?e.layerX:e.offsetX;
  286. var y = e.offsetY==undefined?e.layerY:e.offsetY;
  287. rotateY(x - pX);
  288. rotateX(y - pY);
  289. pX = x;
  290. pY = y;
  291. draw();
  292. return false;
  293. }
  294. }
  295. ctx.clearRect(0, 0, canvas.width, canvas.height);
  296. ctx.fillStyle = backColor;
  297. ctx.fillRect(-canvas.width / 2, -canvas.height / 2, canvas.width, canvas.height);
  298. }
  299. /********************************************************************
  300. * TODO: Function used when something in the canvas is clicked. *
  301. * @parameters: none *
  302. * @return: nothing *
  303. ********************************************************************/
  304. function mouseClick(){
  305. }