doc_webgl-drawElements.html 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <!-- Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ -->
  3. <!doctype html>
  4. <html>
  5. <head>
  6. <meta charset="utf-8"/>
  7. <title>WebGL editor test page</title>
  8. </head>
  9. <body>
  10. <canvas id="canvas" width="128" height="128"></canvas>
  11. <script id="shader-fs" type="x-shader/x-fragment">
  12. precision mediump float;
  13. uniform vec4 mtrColor;
  14. void main(void) {
  15. gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0) * mtrColor;
  16. }
  17. </script>
  18. <script id="shader-vs" type="x-shader/x-vertex">
  19. attribute vec3 aVertexPosition;
  20. void main(void) {
  21. gl_PointSize = 5.0;
  22. gl_Position = vec4(aVertexPosition, 1.0);
  23. }
  24. </script>
  25. <script type="text/javascript;version=1.8">
  26. "use strict";
  27. let canvas, gl, shaderProgram;
  28. let triangleVertexPositionBuffer, squareVertexPositionBuffer;
  29. let triangleIndexBuffer;
  30. let squareIndexBuffer, squareStripIndexBuffer, squareFanIndexBuffer
  31. window.onload = function() {
  32. canvas = document.querySelector("canvas");
  33. gl = canvas.getContext("webgl", { preserveDrawingBuffer: true });
  34. gl.viewportWidth = canvas.width;
  35. gl.viewportHeight = canvas.height;
  36. initShaders();
  37. initBuffers();
  38. gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
  39. gl.clearColor(0.0, 0.0, 0.0, 1.0);
  40. gl.disable(gl.DEPTH_TEST);
  41. drawScene();
  42. }
  43. function getShader(gl, id) {
  44. var shaderScript = document.getElementById(id);
  45. if (!shaderScript) {
  46. return null;
  47. }
  48. var str = "";
  49. var k = shaderScript.firstChild;
  50. while (k) {
  51. if (k.nodeType == 3) {
  52. str += k.textContent;
  53. }
  54. k = k.nextSibling;
  55. }
  56. var shader;
  57. if (shaderScript.type == "x-shader/x-fragment") {
  58. shader = gl.createShader(gl.FRAGMENT_SHADER);
  59. } else if (shaderScript.type == "x-shader/x-vertex") {
  60. shader = gl.createShader(gl.VERTEX_SHADER);
  61. } else {
  62. return null;
  63. }
  64. gl.shaderSource(shader, str);
  65. gl.compileShader(shader);
  66. if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  67. alert(gl.getShaderInfoLog(shader));
  68. return null;
  69. }
  70. return shader;
  71. }
  72. function initShaders() {
  73. var fragmentShader = getShader(gl, "shader-fs");
  74. var vertexShader = getShader(gl, "shader-vs");
  75. shaderProgram = gl.createProgram();
  76. gl.attachShader(shaderProgram, vertexShader);
  77. gl.attachShader(shaderProgram, fragmentShader);
  78. gl.linkProgram(shaderProgram);
  79. if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
  80. alert("Could not initialise shaders");
  81. }
  82. gl.useProgram(shaderProgram);
  83. shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
  84. shaderProgram.pMaterialColor = gl.getUniformLocation(shaderProgram, "mtrColor");
  85. gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
  86. }
  87. function initBuffers() {
  88. // Create triangle vertex/index buffer
  89. triangleVertexPositionBuffer = gl.createBuffer();
  90. gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
  91. var vertices = [
  92. 0.0, 0.5, 0.0,
  93. -0.5, -0.5, 0.0,
  94. 0.5, -0.5, 0.0
  95. ];
  96. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
  97. triangleVertexPositionBuffer.itemSize = 3;
  98. triangleVertexPositionBuffer.numItems = 3;
  99. triangleIndexBuffer = gl.createBuffer();
  100. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triangleIndexBuffer);
  101. var indices = [
  102. 0, 1, 2
  103. ];
  104. gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
  105. triangleIndexBuffer.itemSize = 1;
  106. triangleIndexBuffer.numItems = 3;
  107. // Create square vertex/index buffer
  108. squareVertexPositionBuffer = gl.createBuffer();
  109. gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
  110. vertices = [
  111. 0.8, 0.8, 0.0,
  112. -0.8, 0.8, 0.0,
  113. 0.8, -0.8, 0.0,
  114. -0.8, -0.8, 0.0
  115. ];
  116. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
  117. squareVertexPositionBuffer.itemSize = 3;
  118. squareVertexPositionBuffer.numItems = 4;
  119. squareIndexBuffer = gl.createBuffer();
  120. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareIndexBuffer);
  121. indices = [
  122. 0, 1, 2,
  123. 1, 3, 2
  124. ];
  125. gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
  126. squareIndexBuffer.itemSize = 1;
  127. squareIndexBuffer.numItems = 6;
  128. squareStripIndexBuffer = gl.createBuffer();
  129. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer);
  130. indices = [
  131. 0, 1, 2, 3
  132. ];
  133. gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
  134. squareStripIndexBuffer.itemSize = 1;
  135. squareStripIndexBuffer.numItems = 4;
  136. }
  137. function drawScene() {
  138. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
  139. // DrawElements
  140. // --------------
  141. // draw triangle
  142. gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
  143. gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
  144. gl.uniform4f(shaderProgram.pMaterialColor, 1, 1, 1, 1);
  145. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareIndexBuffer);
  146. gl.drawElements(gl.TRIANGLES, squareIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
  147. // draw square - triangle strip
  148. gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
  149. gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
  150. gl.uniform4f(shaderProgram.pMaterialColor, 0, 1, 0, 1);
  151. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer);
  152. gl.drawElements(gl.TRIANGLE_FAN, squareStripIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
  153. // draw square - triangle fan
  154. gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
  155. gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
  156. gl.uniform4f(shaderProgram.pMaterialColor, 1, 0, 0, 1);
  157. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triangleIndexBuffer);
  158. gl.drawElements(gl.TRIANGLE_FAN, triangleIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
  159. // draw points
  160. gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
  161. gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
  162. gl.uniform4f(shaderProgram.pMaterialColor, 1, 0, 1, 1);
  163. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer);
  164. gl.drawElements(gl.POINTS, squareStripIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
  165. // draw lines
  166. gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
  167. gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
  168. gl.uniform4f(shaderProgram.pMaterialColor, 0, 0, 1, 1);
  169. gl.lineWidth(8.0);
  170. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer);
  171. gl.drawElements(gl.LINES, squareStripIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
  172. // draw line strip
  173. gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
  174. gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
  175. gl.uniform4f(shaderProgram.pMaterialColor, 0.9, 0.6, 0, 1);
  176. gl.lineWidth(3.0);
  177. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer);
  178. gl.drawElements(gl.LINE_STRIP, squareStripIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
  179. // draw line loop
  180. gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
  181. gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
  182. gl.uniform4f(shaderProgram.pMaterialColor, 0, 1, 1, 1);
  183. gl.lineWidth(3.0);
  184. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triangleIndexBuffer);
  185. gl.drawElements(gl.LINE_LOOP, triangleIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
  186. window.requestAnimationFrame(drawScene);
  187. }
  188. </script>
  189. </body>
  190. </html>