summaryrefslogtreecommitdiffstats
path: root/devtools/client/canvasdebugger/test/doc_webgl-drawElements.html
blob: a8ba4a3e86a9c97e20005a0b57e2e45dcf5ebe98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<!-- Any copyright is dedicated to the Public Domain.
     http://creativecommons.org/publicdomain/zero/1.0/ -->
<!doctype html>

<html>
  <head>
    <meta charset="utf-8"/>
    <title>WebGL editor test page</title>
  </head>

  <body>
    <canvas id="canvas" width="128" height="128"></canvas>
    <script id="shader-fs" type="x-shader/x-fragment">
      precision mediump float;
      uniform vec4 mtrColor;

      void main(void) {
        gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0) * mtrColor;
      }
    </script>
    <script id="shader-vs" type="x-shader/x-vertex">
      attribute vec3 aVertexPosition;

      void main(void) {
        gl_PointSize = 5.0;
        gl_Position = vec4(aVertexPosition, 1.0);
      }
    </script>
    <script type="text/javascript;version=1.8">
      "use strict";

      let canvas, gl, shaderProgram;
      let triangleVertexPositionBuffer, squareVertexPositionBuffer;
      let triangleIndexBuffer;
      let squareIndexBuffer, squareStripIndexBuffer, squareFanIndexBuffer
     
      window.onload = function() {
        canvas = document.querySelector("canvas");
        gl = canvas.getContext("webgl", { preserveDrawingBuffer: true });
        gl.viewportWidth = canvas.width;
        gl.viewportHeight = canvas.height;
       
        initShaders();
        initBuffers();

        gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
        gl.clearColor(0.0, 0.0, 0.0, 1.0);
        gl.disable(gl.DEPTH_TEST);
        drawScene();
      }

      function getShader(gl, id) {
        var shaderScript = document.getElementById(id);
        if (!shaderScript) {
            return null;
        }

        var str = "";
        var k = shaderScript.firstChild;
        while (k) {
            if (k.nodeType == 3) {
                str += k.textContent;
            }
            k = k.nextSibling;
        }

        var shader;
        if (shaderScript.type == "x-shader/x-fragment") {
            shader = gl.createShader(gl.FRAGMENT_SHADER);
        } else if (shaderScript.type == "x-shader/x-vertex") {
            shader = gl.createShader(gl.VERTEX_SHADER);
        } else {
            return null;
        }

        gl.shaderSource(shader, str);
        gl.compileShader(shader);

        if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
            alert(gl.getShaderInfoLog(shader));
            return null;
        }

        return shader;
      }
      
      function initShaders() {
        var fragmentShader = getShader(gl, "shader-fs");
        var vertexShader = getShader(gl, "shader-vs");

        shaderProgram = gl.createProgram();
        gl.attachShader(shaderProgram, vertexShader);
        gl.attachShader(shaderProgram, fragmentShader);
        gl.linkProgram(shaderProgram);

        if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
            alert("Could not initialise shaders");
        }

        gl.useProgram(shaderProgram);

        shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
        shaderProgram.pMaterialColor = gl.getUniformLocation(shaderProgram, "mtrColor");
        gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
      }

      function initBuffers() {
          // Create triangle vertex/index buffer
          triangleVertexPositionBuffer = gl.createBuffer();
          gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
          var vertices = [
             0.0,  0.5,  0.0,
            -0.5, -0.5,  0.0,
             0.5, -0.5,  0.0
          ];
          gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
          triangleVertexPositionBuffer.itemSize = 3;
          triangleVertexPositionBuffer.numItems = 3;

          triangleIndexBuffer = gl.createBuffer();
          gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triangleIndexBuffer);
          var indices = [
              0, 1, 2
          ];
          gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
          triangleIndexBuffer.itemSize = 1;
          triangleIndexBuffer.numItems = 3;

          // Create square vertex/index buffer
          squareVertexPositionBuffer = gl.createBuffer();
          gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
          vertices = [
             0.8,  0.8,  0.0,
            -0.8,  0.8,  0.0,
             0.8, -0.8,  0.0,
            -0.8, -0.8,  0.0
          ];
          gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
          squareVertexPositionBuffer.itemSize = 3;
          squareVertexPositionBuffer.numItems = 4;

          squareIndexBuffer = gl.createBuffer();
          gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareIndexBuffer);
          indices = [ 
            0, 1, 2,
            1, 3, 2
          ];
          gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
          squareIndexBuffer.itemSize = 1;
          squareIndexBuffer.numItems = 6;

          squareStripIndexBuffer = gl.createBuffer();
          gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer);
          indices = [ 
            0, 1, 2, 3
          ];
          gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
          squareStripIndexBuffer.itemSize = 1;
          squareStripIndexBuffer.numItems = 4;

      }

      function drawScene() {
        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);

        // DrawElements
        // --------------
        // draw triangle
        gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        gl.uniform4f(shaderProgram.pMaterialColor, 1, 1, 1, 1);
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareIndexBuffer);
        gl.drawElements(gl.TRIANGLES, squareIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);

        // draw square - triangle strip
        gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        gl.uniform4f(shaderProgram.pMaterialColor, 0, 1, 0, 1);
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer);
        gl.drawElements(gl.TRIANGLE_FAN, squareStripIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);

        // draw square - triangle fan
        gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        gl.uniform4f(shaderProgram.pMaterialColor, 1, 0, 0, 1);
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triangleIndexBuffer);
        gl.drawElements(gl.TRIANGLE_FAN, triangleIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);

        // draw points
        gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        gl.uniform4f(shaderProgram.pMaterialColor, 1, 0, 1, 1);
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer);
        gl.drawElements(gl.POINTS, squareStripIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);

        // draw lines
        gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        gl.uniform4f(shaderProgram.pMaterialColor, 0, 0, 1, 1);
        gl.lineWidth(8.0);
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer);
        gl.drawElements(gl.LINES, squareStripIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);

        // draw line strip
        gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        gl.uniform4f(shaderProgram.pMaterialColor, 0.9, 0.6, 0, 1);
        gl.lineWidth(3.0);
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer);
        gl.drawElements(gl.LINE_STRIP, squareStripIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);

        // draw line loop
        gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        gl.uniform4f(shaderProgram.pMaterialColor, 0, 1, 1, 1);
        gl.lineWidth(3.0);
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triangleIndexBuffer);
        gl.drawElements(gl.LINE_LOOP, triangleIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);

        window.requestAnimationFrame(drawScene);
      }
    </script>
  </body>

</html>