4 <style type=
"text/css" media=
"screen">
10 border:
2px solid black;
11 -webkit-box-reflect: below
20px;
12 outline:
10px solid transparent; /* affects layer sizes */
16 <script id=
"vertexShader" type=
"x-shader/x-vertex">
17 attribute vec4 vPosition;
20 gl_Position = vPosition;
24 <script id=
"fragmentShader" type=
"x-shader/x-fragment">
26 gl_FragColor = vec4(
0.0,
0.5,
0.0,
1.0);
30 if (window
.testRunner
)
31 testRunner
.overridePreference("WebKitWebGLEnabled", "1");
37 var vertices
= [ 0.0, 0.8, 0.0,
40 gl
.bufferData(gl
.ARRAY_BUFFER
, new Float32Array(vertices
), gl
.STATIC_DRAW
);
42 gl
.vertexAttribPointer(0, 3, gl
.FLOAT
, false, 0, 0); // Load the vertex data
43 gl
.enableVertexAttribArray(0);
44 gl
.drawArrays(gl
.TRIANGLES
, 0, 3);
48 function getFragmentShader()
50 var shaderNode
= document
.getElementById("fragmentShader"); // fragmentShader has been defined at the top
51 var shaderSource
= getShaderSource(shaderNode
);
53 var shader
= gl
.createShader(gl
.FRAGMENT_SHADER
);
54 gl
.shaderSource(shader
, shaderSource
);
55 gl
.compileShader(shader
);
60 function getShaderSource(shaderNode
)
62 var shaderSource
= "";
63 var node
= shaderNode
.firstChild
;
65 if (node
.nodeType
== 3) // Node.TEXT_NODE
66 shaderSource
+= node
.textContent
;
67 node
= node
.nextSibling
;
73 function getVertexShader()
75 var shaderNode
= document
.getElementById("vertexShader");
76 var shaderSource
= getShaderSource(shaderNode
);
78 var shader
= gl
.createShader(gl
.VERTEX_SHADER
);
79 gl
.shaderSource(shader
, shaderSource
);
80 gl
.compileShader(shader
);
87 var theCanvas
= document
.getElementById("canvas");
88 gl
= theCanvas
.getContext("webgl");
90 var vertexShader
= getVertexShader();
91 var fragmentShader
= getFragmentShader();
93 var shaderProgram
= gl
.createProgram();
94 gl
.attachShader(shaderProgram
, vertexShader
);
95 gl
.attachShader(shaderProgram
, fragmentShader
);
96 gl
.bindAttribLocation(shaderProgram
, 0, "vPosition"); // vPosition has been defined at the top
97 gl
.linkProgram(shaderProgram
);
99 gl
.useProgram(shaderProgram
);
101 var buffer
= gl
.createBuffer();
102 gl
.bindBuffer(gl
.ARRAY_BUFFER
, buffer
);
105 function drawCanvas()
112 <body onload=
"drawCanvas()">
114 <p>You should see a green triangle and its reflection.
</p>
115 <canvas id=
"canvas" style=
"left: 20px" width=
"200" height=
"200"></canvas>