Advertisement
arealloudbird

Untitled

Sep 17th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1. /**
  2. * @file A simple WebGL example drawing a circle
  3. * @author Eric Shaffer <shaffer1@illinois.edu>
  4. */
  5.  
  6. /** @global The WebGL context */
  7. var gl;
  8.  
  9. /** @global The HTML5 canvas we draw on */
  10. var canvas;
  11.  
  12. /** @global A simple GLSL shader program */
  13. var shaderProgram;
  14.  
  15. /** @global The WebGL buffer holding the triangle */
  16. var vertexPositionBuffer;
  17.  
  18. /** @global The WebGL buffer holding the vertex colors */
  19. var vertexColorBuffer;
  20.  
  21. /** @global The Modelview matrix */
  22. var mvMatrix = mat4.create();
  23.  
  24. /** @global The Projection matrix */
  25. var pMatrix = mat4.create();
  26.  
  27. /** @global The angle of rotation around the x axis */
  28. var defAngle = 0;
  29.  
  30. /** @global Number of vertices around the circle boundary */
  31. var numCircleVerts = 100;
  32.  
  33. /** @global Two times pi to save some multiplications...*/
  34. var twicePi=2.0*3.14159;
  35.  
  36. //----------------------------------------------------------------------------------
  37. /**
  38. * Sends projection/modelview matrices to shader
  39. */
  40. function setMatrixUniforms() {
  41. gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
  42. gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
  43. }
  44.  
  45. /**
  46. * Translates degrees to radians
  47. * @param {Number} degrees Degree input to function
  48. * @return {Number} The radians that correspond to the degree input
  49. */
  50. function degToRad(degrees) {
  51. return degrees * Math.PI / 180;
  52. }
  53.  
  54.  
  55. /**
  56. * Creates a context for WebGL
  57. * @param {element} canvas WebGL canvas
  58. * @return {Object} WebGL context
  59. */
  60. function createGLContext(canvas) {
  61. var names = ["webgl", "experimental-webgl"];
  62. var context = null;
  63. for (var i=0; i < names.length; i++) {
  64. try {
  65. context = canvas.getContext(names[i]);
  66. } catch(e) {}
  67. if (context) {
  68. break;
  69. }
  70. }
  71. if (context) {
  72. context.viewportWidth = canvas.width;
  73. context.viewportHeight = canvas.height;
  74. } else {
  75. alert("Failed to create WebGL context!");
  76. }
  77. return context;
  78. }
  79.  
  80. /**
  81. * Loads Shaders
  82. * @param {string} id ID string for shader to load. Either vertex shader/fragment shader
  83. */
  84. function loadShaderFromDOM(id) {
  85. var shaderScript = document.getElementById(id);
  86.  
  87. // If we don't find an element with the specified id
  88. // we do an early exit
  89. if (!shaderScript) {
  90. return null;
  91. }
  92.  
  93. // Loop through the children for the found DOM element and
  94. // build up the shader source code as a string
  95. var shaderSource = "";
  96. var currentChild = shaderScript.firstChild;
  97. while (currentChild) {
  98. if (currentChild.nodeType == 3) { // 3 corresponds to TEXT_NODE
  99. shaderSource += currentChild.textContent;
  100. }
  101. currentChild = currentChild.nextSibling;
  102. }
  103.  
  104. var shader;
  105. if (shaderScript.type == "x-shader/x-fragment") {
  106. shader = gl.createShader(gl.FRAGMENT_SHADER);
  107. } else if (shaderScript.type == "x-shader/x-vertex") {
  108. shader = gl.createShader(gl.VERTEX_SHADER);
  109. } else {
  110. return null;
  111. }
  112.  
  113. gl.shaderSource(shader, shaderSource);
  114. gl.compileShader(shader);
  115.  
  116. if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  117. alert(gl.getShaderInfoLog(shader));
  118. return null;
  119. }
  120. return shader;
  121. }
  122.  
  123. /**
  124. * Setup the fragment and vertex shaders
  125. */
  126. function setupShaders() {
  127. vertexShader = loadShaderFromDOM("shader-vs");
  128. fragmentShader = loadShaderFromDOM("shader-fs");
  129.  
  130. shaderProgram = gl.createProgram();
  131. gl.attachShader(shaderProgram, vertexShader);
  132. gl.attachShader(shaderProgram, fragmentShader);
  133. gl.linkProgram(shaderProgram);
  134.  
  135. if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
  136. alert("Failed to setup shaders");
  137. }
  138.  
  139. gl.useProgram(shaderProgram);
  140. shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
  141. gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
  142.  
  143. shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");
  144. gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);
  145. shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
  146. shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
  147. }
  148.  
  149. /**
  150. * Populate buffers with data
  151. @param {number} number of vertices to use around the circle boundary
  152. */
  153. function setupBuffers() {
  154.  
  155. vertexPositionBuffer = gl.createBuffer();
  156. gl.bindBuffer(gl.ARRAY_BUFFER, vertexPositionBuffer);
  157. var triangleVertices = [
  158. // top of the I
  159. -0.6, 1.0, 0.0,
  160. -0.6, 0.6, 0.0,
  161. -0.2, 0.6, 0.0,
  162.  
  163. -0.6, 1.0, 0.0,
  164. -0.2, 0.6, 0.0,
  165. 0.6, 1.0, 0.0,
  166.  
  167. -0.2, 0.6, 0.0,
  168. 0.2, 0.6, 0.0,
  169. 0.6, 1.0, 0.0,
  170.  
  171. 0.2, 0.6, 0.0,
  172. 0.6, 0.6, 0.0,
  173. 0.6, 1.0, 0.0,
  174.  
  175. // middle of the I
  176. -0.2, 0.6, 0.0,
  177. 0.2, 0.6, 0.0,
  178. -0.2, -0.6, 0.0,
  179.  
  180. 0.2, 0.6, 0.0,
  181. -0.2, -0.6, 0.0,
  182. 0.2, -0.6, 0.0,
  183.  
  184. // bottom of the I
  185. -0.6, -1.0, 0.0,
  186. -0.6, -0.6, 0.0,
  187. -0.2, -0.6, 0.0,
  188.  
  189. -0.6, -1.0, 0.0,
  190. -0.2, -0.6, 0.0,
  191. 0.6, -1.0, 0.0,
  192.  
  193. -0.2, -0.6, 0.0,
  194. 0.2, -0.6, 0.0,
  195. 0.6, -1.0, 0.0,
  196.  
  197. 0.2, -0.6, 0.0,
  198. 0.6, -0.6, 0.0,
  199. 0.6, -1.0, 0.0,
  200. ];
  201.  
  202. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(triangleVertices), gl.DYNAMIC_DRAW);
  203. vertexPositionBuffer.itemSize = 3;
  204. vertexPositionBuffer.numberOfItems = 30;
  205.  
  206. vertexColorBuffer = gl.createBuffer();
  207. gl.bindBuffer(gl.ARRAY_BUFFER, vertexColorBuffer);
  208. var colors = [
  209. // top of the I
  210. 0.91, 0.29, 0.15, 1.0,
  211. 0.07, 0.16, 0.29, 1.0,
  212. 0.07, 0.16, 0.29, 1.0,
  213.  
  214. 0.91, 0.29, 0.15, 1.0,
  215. 0.07, 0.16, 0.29, 1.0,
  216. 0.91, 0.29, 0.15, 1.0,
  217.  
  218. 0.07, 0.16, 0.29, 1.0,
  219. 0.07, 0.16, 0.29, 1.0,
  220. 0.91, 0.29, 0.15, 1.0,
  221.  
  222. 0.07, 0.16, 0.29, 1.0,
  223. 0.07, 0.16, 0.29, 1.0,
  224. 0.91, 0.29, 0.15, 1.0,
  225.  
  226. // middle of the I
  227. 0.07, 0.16, 0.29, 1.0,
  228. 0.07, 0.16, 0.29, 1.0,
  229. 0.91, 0.29, 0.15, 1.0,
  230.  
  231. 0.07, 0.16, 0.29, 1.0,
  232. 0.91, 0.29, 0.15, 1.0,
  233. 0.91, 0.29, 0.15, 1.0,
  234.  
  235. // bottom of the I
  236. 0.07, 0.16, 0.29, 1.0,
  237. 0.91, 0.29, 0.15, 1.0,
  238. 0.91, 0.29, 0.15, 1.0,
  239.  
  240. 0.07, 0.16, 0.29, 1.0,
  241. 0.91, 0.29, 0.15, 1.0,
  242. 0.07, 0.16, 0.29, 1.0,
  243.  
  244. 0.91, 0.29, 0.15, 1.0,
  245. 0.91, 0.29, 0.15, 1.0,
  246. 0.07, 0.16, 0.29, 1.0,
  247.  
  248. 0.91, 0.29, 0.15, 1.0,
  249. 0.91, 0.29, 0.15, 1.0,
  250. 0.07, 0.16, 0.29, 1.0
  251. ];
  252. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
  253. vertexColorBuffer.itemSize = 4;
  254. vertexColorBuffer.numItems = 30;
  255. }
  256.  
  257. /**
  258. * Draw call that applies matrix transformations to model and draws model in frame
  259. */
  260. function draw() {
  261. gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
  262. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  263.  
  264. mat4.identity(mvMatrix);
  265. mat4.identity(pMatrix);
  266.  
  267. mat4.ortho(pMatrix,-1,1,-1,1,1,-1);
  268.  
  269.  
  270. gl.bindBuffer(gl.ARRAY_BUFFER, vertexPositionBuffer);
  271. gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute,
  272. vertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
  273. gl.bindBuffer(gl.ARRAY_BUFFER, vertexColorBuffer);
  274. gl.vertexAttribPointer(shaderProgram.vertexColorAttribute,
  275. vertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
  276.  
  277. setMatrixUniforms();
  278. gl.drawArrays(gl.TRIANGLES, 0, vertexPositionBuffer.numberOfItems);
  279. }
  280.  
  281. /**
  282. * Animation to be called from tick. Updates globals and performs animation for each tick.
  283. */
  284. function animate() {
  285. defAngle= (defAngle+1.0) % 360;
  286. loadVertices(numCircleVerts);
  287. }
  288.  
  289. /**
  290. * Startup function called from html code to start program.
  291. */
  292. function startup() {
  293. canvas = document.getElementById("myGLCanvas");
  294. gl = createGLContext(canvas);
  295. setupShaders();
  296. setupBuffers();
  297. gl.clearColor(0.0, 0.0, 0.0, 1.0);
  298. gl.enable(gl.DEPTH_TEST);
  299. tick();
  300. //draw();
  301. }
  302.  
  303. /**
  304. * Tick called for every animation frame.
  305. */
  306. function tick() {
  307. requestAnimFrame(tick);
  308. draw();
  309. animate();
  310. }
  311.  
  312. function deformSin(x,y,angle) {
  313. var circPt = vec2.fromValues(x,y);
  314. var dist = 0.2*Math.sin((angle)+degToRad(defAngle));
  315. vec2.normalize(circPt,circPt);
  316. vec2.scale(circPt,circPt,dist);
  317. return circPt;
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement