Advertisement
Guest User

d

a guest
Oct 15th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.01 KB | None | 0 0
  1. "use strict"
  2.  
  3. var canvas;
  4. var gl;
  5. var select;
  6. var toDraw = []
  7. var thetaRot = [10, 10, 10];
  8. var thetaLoc;
  9. var coeff = 1;
  10. var coeffLoc;
  11. var trCoeff = [0, 0, 0];
  12. var trCoeffLoc;
  13.  
  14. var flag = true;
  15. var axis = 0;
  16.  
  17. var xAxis = 0;
  18. var yAxis = 1;
  19. var zAxis = 2;
  20.  
  21. var near = 0.3;
  22. var far = 3.0;
  23. var radius = 4.0;
  24. var theta = 0.0;
  25. var phi = 0.0;
  26. var dr = 5.0 * Math.PI / 180.0;
  27.  
  28. var fovy = 45.0; // Field-of-view in Y direction angle (in degrees)
  29. var aspect; // Viewport aspect ratio
  30.  
  31. var mvMatrix, pMatrix;
  32. var modelView, projection;
  33. var eye;
  34. const at = vec3(0.0, 0.0, 0.0);
  35. const up = vec3(0.0, 1.0, 0.0);
  36.  
  37. var coord = {
  38. // For cube
  39. '1': vec3(0.1, 0.1, 0.1),
  40. '2': vec3(0.5, 0.1, 0.1),
  41. '3': vec3(0.5, 0.1, 0.5),
  42. '4': vec3(0.1, 0.1, 0.5),
  43. '5': vec3(0.1, 0.5, 0.1),
  44. '6': vec3(0.1, 0.5, 0.5),
  45. '7': vec3(0.5, 0.5, 0.5),
  46. '8': vec3(0.5, 0.5, 0.1),
  47.  
  48. }
  49.  
  50. var vertices = [
  51. // For cube
  52. coord[4], coord[3], coord[2],
  53. coord[2], coord[4], coord[1],
  54. coord[1], coord[2], coord[5],
  55. coord[5], coord[8], coord[2],
  56. coord[2], coord[3], coord[8],
  57. coord[8], coord[3], coord[7],
  58. coord[7], coord[3], coord[4],
  59. coord[4], coord[7], coord[6],
  60. coord[6], coord[4], coord[1],
  61. coord[1], coord[5], coord[6],
  62. coord[6], coord[7], coord[8],
  63. coord[8], coord[6], coord[5],
  64.  
  65. ]
  66.  
  67. var colors = [
  68. // each face of the cube
  69. 0, 0, 1, 1.0,
  70. 0, 0, 1, 1.0,
  71. 0, 0, 1, 1.0,
  72. 0, 0, 1, 1.0,
  73. 0, 0, 1, 1.0,
  74. 0, 0, 1, 1.0,
  75.  
  76. 0, 1, 0, 1.0,
  77. 0, 1, 0, 1.0,
  78. 0, 1, 0, 1.0,
  79. 0, 1, 0, 1.0,
  80. 0, 1, 0, 1.0,
  81. 0, 1, 0, 1.0,
  82.  
  83. 0, 0.7, 1, 1.0,
  84. 0, 0.7, 1, 1.0,
  85. 0, 0.7, 1, 1.0,
  86. 0, 0.7, 1, 1.0,
  87. 0, 0.7, 1, 1.0,
  88. 0, 0.7, 1, 1.0,
  89.  
  90. 0.5, 0, 1, 1.0,
  91. 0.5, 0, 1, 1.0,
  92. 0.5, 0, 1, 1.0,
  93. 0.5, 0, 1, 1.0,
  94. 0.5, 0, 1, 1.0,
  95. 0.5, 0, 1, 1.0,
  96.  
  97. 1, 0, 0, 1.0,
  98. 1, 0, 0, 1.0,
  99. 1, 0, 0, 1.0,
  100. 1, 0, 0, 1.0,
  101. 1, 0, 0, 1.0,
  102. 1, 0, 0, 1.0,
  103.  
  104. 0.2, 0, 1, 1.0,
  105. 0.2, 0, 1, 1.0,
  106. 0.2, 0, 1, 1.0,
  107. 0.2, 0, 1, 1.0,
  108. 0.2, 0, 1, 1.0,
  109. 0.2, 0, 1, 1.0,
  110.  
  111. ]
  112.  
  113. theta = [9, 10, 78]
  114. // add vertices for cone
  115.  
  116. function addFigureCube(shape, program){
  117. cube = new Drawable(vertices,colors,program)
  118. toDraw.push(cube)
  119.  
  120. }
  121.  
  122. window.onload = init;
  123.  
  124. function init() {
  125. canvas = document.getElementById("gl-canvas");
  126.  
  127. gl = WebGLUtils.setupWebGL(canvas);
  128.  
  129. if (!gl) { alert("WebGL isn't available"); }
  130.  
  131. aspect = canvas.width / canvas.height;
  132.  
  133. var program = initShaders(gl, "vertex-shader", "fragment-shader");
  134. gl.useProgram(program);
  135.  
  136. gl.enable(gl.DEPTH_TEST);
  137.  
  138.  
  139. var bufferId = gl.createBuffer();
  140. gl.bindBuffer(gl.ARRAY_BUFFER, bufferId);
  141. gl.bufferData(gl.ARRAY_BUFFER, 50000, gl.STATIC_DRAW);
  142. gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(vertices));
  143.  
  144. var vPosition = gl.getAttribLocation(program, "vPosition");
  145. gl.vertexAttribPointer(vPosition, 3, gl.FLOAT, false, 0, 0);
  146. gl.enableVertexAttribArray(vPosition);
  147.  
  148. var bufferColor = gl.createBuffer();
  149. gl.bindBuffer(gl.ARRAY_BUFFER, bufferColor);
  150. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
  151.  
  152. var vColors = gl.getAttribLocation(program, "vColors");
  153. gl.vertexAttribPointer(vColors, 4, gl.FLOAT, false, 4 * Float32Array.BYTES_PER_ELEMENT, 0);
  154. gl.enableVertexAttribArray(vColors);
  155.  
  156. thetaLoc = gl.getUniformLocation(program, "theta");
  157.  
  158.  
  159. document.getElementById("xButton").onclick = function () {
  160. flag = false
  161. axis = xAxis;
  162. };
  163. document.getElementById("yButton").onclick = function () {
  164. flag = false
  165. axis = yAxis;
  166. };
  167. document.getElementById("zButton").onclick = function () {
  168. flag = false
  169. axis = zAxis;
  170. };
  171. document.getElementById("stopButton").onclick = function () {
  172. flag = true;
  173. };
  174.  
  175.  
  176. document.getElementById("sUpButton").onclick = function () {
  177. select.coeff += 0.1;
  178. };
  179. document.getElementById("sDownButton").onclick = function () {
  180. select.coeff -= 0.1;
  181. };
  182.  
  183.  
  184.  
  185. document.getElementById("leftButton").onclick = function () {
  186. select.trCoeff[0] += 0.1;
  187. };
  188. document.getElementById("rightButton").onclick = function () {
  189. select.trCoeff[0] -= 0.1;
  190. };
  191. document.getElementById("upButton").onclick = function () {
  192. select.trCoeff[1] += 0.1;
  193. };
  194. document.getElementById("downButton").onclick = function () {
  195. select.trCoeff[1] -= 0.1;
  196. };
  197. document.getElementById("closeButton").onclick = function () {
  198. select.trCoeff[2] += 0.1;
  199. };
  200. document.getElementById("furtherButton").onclick = function () {
  201. // select.trCoeff[2] -= 0.1;
  202. select.setScale(1,5,100);
  203. };
  204. document.getElementById("Button9").onclick = function () {
  205. var cube = new Drawable(vertices, colors, program);
  206. toDraw.push(cube);
  207. select = cube;
  208.  
  209. };
  210.  
  211. modelView = gl.getUniformLocation(program, "modelView");
  212. projection = gl.getUniformLocation(program, "projection");
  213.  
  214. document.getElementById("Button1").onclick = function () { select.near *= 1.1; select.far *= 1.1; };
  215. document.getElementById("Button2").onclick = function () { select.near *= 0.9; select.far *= 0.9; };
  216. document.getElementById("Button3").onclick = function () { select.radius *= 2.0; };
  217. document.getElementById("Button4").onclick = function () { select.radius *= 0.5; };
  218. document.getElementById("Button5").onclick = function () { select.theta += select.dr; };
  219. document.getElementById("Button6").onclick = function () { select.theta -= select.dr; };
  220. document.getElementById("Button7").onclick = function () { select.phi += select.dr; };
  221. document.getElementById("Button8").onclick = function () { select.phi -= select.dr; };
  222.  
  223. coeffLoc = gl.getUniformLocation(program, "coeff");
  224. trCoeffLoc = gl.getUniformLocation(program, "trCoeff");
  225.  
  226.  
  227.  
  228.  
  229.  
  230. render();
  231. }
  232.  
  233. function render() {
  234. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  235.  
  236. // if (select && rotation){
  237. // select.theta[axis]+=1.5;
  238. // }
  239. toDraw.forEach(element => {element.draw();})
  240.  
  241. requestAnimFrame(render);
  242. }
  243.  
  244. class Drawable
  245. {
  246. constructor(vertices, colors, program) {
  247. this.program = program;
  248. this.vBuffer = gl.createBuffer();
  249. gl.bindBuffer(gl.ARRAY_BUFFER, this.vBuffer);
  250. gl.bufferData(gl.ARRAY_BUFFER, flatten(vertices), gl.STATIC_DRAW); // SET THE DATA, SPECIFY THE ARRAY, vertices in this cas
  251.  
  252. this.cBuffer = gl.createBuffer();
  253. gl.bindBuffer(gl.ARRAY_BUFFER, this.cBuffer);
  254. gl.bufferData(gl.ARRAY_BUFFER, flatten(colors), gl.STATIC_DRAW);
  255.  
  256. this.vAttributeLocation = gl.getAttribLocation(program, 'vPosition');
  257. this.cAttributeLocation = gl.getAttribLocation(program, 'vColors');
  258. this.translationLocation = gl.getUniformLocation(program, 'translation');
  259. this.scaleLocation = gl.getUniformLocation(program, 'scale');
  260. this.vertices = vertices;
  261. this.translation = vec3(0, 0, 0);
  262. this.scale = vec3(1, 1, 1);
  263. // coeffLoc = gl.getUniformLocation(program, "coeff");
  264. // trCoeffLoc = gl.getUniformLocation(program, "trCoeff");
  265. }
  266.  
  267. draw()
  268. {
  269. gl.bindBuffer(gl.ARRAY_BUFFER, this.vBuffer);
  270. gl.vertexAttribPointer(this.vAttributeLocation, 3, gl.FLOAT, false, 0, 0); // DESCRIBE THE DATA: EACH vertex has 3 values of type FLOAT
  271. gl.enableVertexAttribArray(this.vAttributeLocation);
  272.  
  273. gl.bindBuffer(gl.ARRAY_BUFFER, this.cBuffer);
  274. gl.vertexAttribPointer(this.cAttributeLocation, 4, gl.FLOAT, false, 0, 0); // DESCRIBE THE DATA: EACH vertex has 4 values of type FLOAT
  275. gl.enableVertexAttribArray(this.cAttributeLocation);
  276.  
  277. // console.log(this.translationLocation);
  278. gl.uniform3fv(this.translationLocation, this.translation);
  279. gl.uniform3fv(this.scaleLocation, this.scale);
  280.  
  281. //gl.uniform3fv(this.trCoeffLoc, this.coeff);
  282. //gl.uniform3fv(this.scaleLocation, this.scale);
  283.  
  284. gl.drawArrays(gl.TRIANGLES, 0, this.vertices.length);
  285. }
  286.  
  287. setTranslation(x, y, z)
  288. {
  289. this.translation = vec3(x, y, z);
  290. }
  291. setScale(x, y, z)
  292. {
  293. this.scale = vec3(x, y, z);
  294. }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement