summaryrefslogtreecommitdiffstats
path: root/WebCore/manual-tests/webgl/SpinningBox.html
blob: e68711c986905f16df9cb8f0dd355a07ccaefd66 (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
<!DOCTYPE html>
<html>
  <head>
    <title>Canvas3d example</title>
    <script src="resources/CanvasMatrix.js"> </script>
    <script src="resources/utils3d.js"> </script>
    <script id="vshader" type="x-shader/x-vertex">
        uniform mat4 pMatrix;
        uniform mat4 mvMatrix;
        uniform vec3 lightDir;

        attribute vec3 vNormal;
        attribute vec4 vColor;
        attribute vec4 vPosition;
        
        varying float v_Dot;
        
        void main()
        {
            gl_FrontColor = vColor;
            vec4 transNormal = mvMatrix * vec4(vNormal,1);
            v_Dot = max(dot(transNormal.xyz, lightDir), 0.0);
            gl_Position = pMatrix * mvMatrix * vPosition;
        }
    </script>

    <script id="fshader" type="x-shader/x-fragment">
        varying float v_Dot;
        
        void main()
        {
            gl_FragColor = vec4(gl_Color.xyz * v_Dot, gl_Color.a);
        }
    </script>

    <script>
        function init()
        {
            var gl = initWebGL("example", "vshader", "fshader", 
                                [ "vNormal", "vColor", "vPosition"],
                                [ 0, 0, 0, 1 ], 10000);

            gl.uniform3f(gl.getUniformLocation(gl.program, "lightDir"), 0, 0, 1);
            
            gl.box = makeBox(gl);

            // color array
            var colors = new WebGLUnsignedByteArray(
                [  0, 0, 1, 1,   0, 0, 1, 1,   0, 0, 1, 1,   0, 0, 1, 1,     // v0-v1-v2-v3 front
                   1, 0, 0, 1,   1, 0, 0, 1,   1, 0, 0, 1,   1, 0, 0, 1,     // v0-v3-v4-v5 right
                   0, 1, 0, 1,   0, 1, 0, 1,   0, 1, 0, 1,   0, 1, 0, 1,     // v0-v5-v6-v1 top
                   1, 1, 0, 1,   1, 1, 0, 1,   1, 1, 0, 1,   1, 1, 0, 1,     // v1-v6-v7-v2 left
                   1, 0, 1, 1,   1, 0, 1, 1,   1, 0, 1, 1,   1, 0, 1, 1,     // v7-v4-v3-v2 bottom
                   0, 1, 1, 1,   0, 1, 1, 1,   0, 1, 1, 1,   0, 1, 1, 1 ]    // v4-v7-v6-v5 back
            );
            
            gl.box.colorObject = gl.createBuffer();
            gl.bindBuffer(gl.ARRAY_BUFFER, gl.box.colorObject);
            gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW);
                                    
            return gl;
        }
        
        width = -1;
        height = -1;
        
        function reshape(gl)
        {
            var canvas = document.getElementById('example');
            if (canvas.clientWidth == width && canvas.clientHeight == height)
                return;

            width = canvas.clientWidth;
            height = canvas.clientHeight;
            
            gl.viewport(0, 0, width, height);
            var pMatrix = new CanvasMatrix4();
            pMatrix.lookat(0,0,10, 0, 0, 0, 0, 1, 0);
            pMatrix.perspective(30, width/height, 1, 10000);
            gl.uniformMatrix4fv(gl.getUniformLocation(gl.program, "pMatrix"), false, pMatrix.getAsCanvasFloatArray());
        }
        
        function drawPicture(gl)
        {
            reshape(gl);
            gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

            var mvMatrix = new CanvasMatrix4();
            mvMatrix.rotate(currentAngle, 0,1,0);
            mvMatrix.rotate(20, 1,0,0);
            gl.uniformMatrix4fv(gl.getUniformLocation(gl.program, "mvMatrix"), false, mvMatrix.getAsCanvasFloatArray());

            gl.enableVertexAttribArray(0);
            gl.enableVertexAttribArray(1);
            gl.enableVertexAttribArray(2);

            gl.bindBuffer(gl.ARRAY_BUFFER, gl.box.vertexObject);
            gl.vertexAttribPointer(2, 3, gl.FLOAT, false, 0, 0);
            
            gl.bindBuffer(gl.ARRAY_BUFFER, gl.box.normalObject);
            gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
            
            gl.bindBuffer(gl.ARRAY_BUFFER, gl.box.colorObject);
            gl.vertexAttribPointer(1, 4, gl.UNSIGNED_BYTE, false, 0, 0);

            gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, gl.box.indexObject);
            gl.drawElements(gl.TRIANGLES, gl.box.numIndices, gl.UNSIGNED_BYTE, 0);

            gl.flush();
            
            framerate.snapshot();
            
            currentAngle += incAngle;
            if (currentAngle > 360)
                currentAngle -= 360;
        }
        
        function start()
        {
            var gl = init();
            currentAngle = 0;
            incAngle = 0.5;
            framerate = new Framerate("framerate");
            setInterval(function() { drawPicture(gl) }, 10);
        }
    </script>
    <style type="text/css">
        canvas {
            border: 2px solid black;
            width:90%;
            height:90%;
        }
        .text {
            position:absolute;
            top:100px;
            left:20px;
            font-size:2em;
            color: blue;
        }
    </style>
  </head>
  <body onload="start()">
    <canvas id="example">
    There is supposed to be an example drawing here, but it's not important.
    </canvas>
    <div class="text">This is some text under the canvas</div>
    <div id="framerate"></div>
  </body>
</html>