Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. function MyTriangle(scene, x1, y1, z1, x2, y2, z2, x3, y3, z3) {
  2.  
  3. this.x1 = x1;
  4. this.x2 = x2;
  5. this.x3 = x3;
  6.  
  7. this.y1 = y1;
  8. this.y2 = y2;
  9. this.y3 = y3;
  10.  
  11. this.z1 = z1;
  12. this.z2 = z2;
  13. this.z3 = z3;
  14.  
  15. this.lengths = 1;
  16. this.lengtht = 1;
  17.  
  18. CGFobject.call(this, scene);
  19. this.initBuffers();
  20. };
  21.  
  22. MyTriangle.prototype = Object.create(CGFobject.prototype);
  23. MyTriangle.prototype.constructor = MyTriangle;
  24.  
  25. MyTriangle.prototype.initBuffers = function () {
  26.  
  27.  
  28. this.vertices = [this.x1, this.y1, this.z1,
  29. this.x2, this.y2, this.z2,
  30. this.x3, this.y3, this.z3
  31. ];
  32.  
  33. this.indices = [0, 1, 2];
  34.  
  35. this.normals = [];
  36.  
  37. var vecx = (this.y2 - this.y1) * (this.z3 - this.z1) - (this.z2 - this.z1) * (this.y3 - this.y1);
  38. var vecy = (this.x2 - this.x1) * (this.z3 - this.z1) - (this.z2 - this.z1) * (this.x3 - this.x1);
  39. var vecz = (this.x2 - this.x1) * (this.y3 - this.y1) - (this.y2 - this.y1) * (this.x3 - this.x1);
  40.  
  41. this.normals.push(vecx, vecy, vecz);
  42. this.normals.push(vecx, vecy, vecz);
  43. this.normals.push(vecx, vecy, vecz);
  44.  
  45. /*this.auxTexCoords = [
  46.  
  47. 0, 1,
  48. this.c, 1,
  49. this.c - (this.a * this.beta),
  50. 1 - (this.a * Math.sin(this.angle))
  51. ];
  52.  
  53. this.texCoords = this.auxTexCoords.slice();*/
  54.  
  55. this.texCoords= [
  56. 0,1,
  57. 1,1,
  58. 1,0,
  59. ]
  60. this.primitiveType = this.scene.gl.TRIANGLES;
  61. this.initGLBuffers();
  62. }
  63.  
  64. MyTriangle.prototype.loadTexture = function (texture) {
  65.  
  66. var lengths = texture[1];
  67. var lengtht = texture[2];
  68.  
  69. //a 1-2
  70. //b 0-2
  71. //c 0-1
  72. var a = Math.sqrt(Math.pow(this.x2 - this.x3, 2) + Math.pow(this.y2 - this.y3, 2) + Math.pow(this.z2 - this.z3, 2));
  73. var b = Math.sqrt(Math.pow(this.x1 - this.x3, 2) + Math.pow(this.y1 - this.y3, 2) + Math.pow(this.z1 - this.z3, 2));
  74. var c = Math.sqrt(Math.pow(this.x1 - this.x2, 2) + Math.pow(this.y1 - this.y2, 2) + Math.pow(this.z1 - this.z2, 2));
  75.  
  76. var beta = (Math.pow(a, 2) - Math.pow(b, 2) + Math.pow(c, 2)) / (2 * a * c);
  77. var angle = Math.acos(beta);
  78.  
  79. var h= a * Math.sin(angle);
  80.  
  81.  
  82. /*for (let i = 0; i < this.auxTexCoords.length; i += 2) {
  83. this.texCoords[i] = this.auxTexCoords[i] / this.lengths;
  84. this.texCoords[i + 1] = this.auxTexCoords[i + 1] / this.lengtht;
  85. }*/
  86.  
  87. this.texCoords = [
  88.  
  89. 0, h/lengtht,
  90. c/lengths, h/lengtht,
  91. (c - a * beta)/lengths, (h - a * Math.sin(angle))/lengtht
  92. ];
  93.  
  94.  
  95. this.updateTexCoordsGLBuffers()
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement