summaryrefslogtreecommitdiffstats
path: root/devtools/client/canvasdebugger/test/doc_webgl-drawArrays.html
blob: 7a6aea907f4f9755ccc9e950f3daca5c106d9206 (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
<!-- 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;
     
      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;

          // 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;
      }

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

        // DrawArrays
        // --------------
        // 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, 1, 1, 1, 1);
        gl.drawArrays(gl.TRIANGLE_STRIP, 0, squareVertexPositionBuffer.numItems);

        // draw square - triangle fan
        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.drawArrays(gl.TRIANGLE_FAN, 0, squareVertexPositionBuffer.numItems);

        // draw triangle
        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.drawArrays(gl.TRIANGLES, 0, triangleVertexPositionBuffer.numItems);

        // 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.drawArrays(gl.POINTS, 0, squareVertexPositionBuffer.numItems);

        // 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.drawArrays(gl.LINES, 0, squareVertexPositionBuffer.numItems);

        // 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.drawArrays(gl.LINE_STRIP, 0, squareVertexPositionBuffer.numItems);

        // 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.drawArrays(gl.LINE_LOOP, 0, triangleVertexPositionBuffer.numItems);

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

</html>