2 <meta http-equiv=
"content-type" content=
"text/html; charset=utf-8" />
4 <title>WebGL test: Basic drawing
</title>
6 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
7 <link rel=
"stylesheet" href=
"/tests/SimpleTest/test.css">
8 <script src=
"driver-info.js"></script>
9 <script src=
"webgl-util.js"></script>
12 <script id=
"vs" type=
"x-shader/x-vertex">
14 attribute vec2 aVertCoord;
17 gl_Position = vec4(aVertCoord,
0.0,
1.0);
21 <script id=
"fs" type=
"x-shader/x-fragment">
23 precision mediump float;
26 gl_FragColor = vec4(
0.0,
1.0,
0.0,
1.0);
31 <canvas id=
"c" width=
"64" height=
"64"></canvas>
34 // Give ourselves a scope to return early from:
36 var gl
= c
.getContext('webgl');
38 todo(false, 'WebGL is unavailable.');
42 gl
.disable(gl
.DEPTH_TEST
);
44 var prog
= WebGLUtil
.createProgramByIds(gl
, 'vs', 'fs');
46 ok(false, 'Program linking should succeed.');
50 prog
.aVertCoord
= gl
.getAttribLocation(prog
, "aVertCoord");
51 ok(prog
.aVertCoord
>= 0, '`aVertCoord` should be valid.');
53 function checkGLError(func
, info
, refValue
) {
57 var error
= gl
.getError();
58 func(error
== refValue
,
59 '[' + info
+ '] gl.getError should be 0x' + refValue
.toString(16) +
60 ', was 0x' + error
.toString(16) + '.');
63 var vertCoordArr
= new Float32Array([
69 var vertCoordBuff
= gl
.createBuffer();
70 gl
.bindBuffer(gl
.ARRAY_BUFFER
, vertCoordBuff
);
71 gl
.bufferData(gl
.ARRAY_BUFFER
, vertCoordArr
, gl
.STATIC_DRAW
);
73 var indexArr
= new Uint16Array([
77 var indexBuff
= gl
.createBuffer();
78 gl
.bindBuffer(gl
.ELEMENT_ARRAY_BUFFER
, indexBuff
);
79 gl
.bufferData(gl
.ELEMENT_ARRAY_BUFFER
, indexArr
, gl
.STATIC_DRAW
);
82 function testPixel(x
, y
, refData
, func
, infoString
) {
83 var pixel
= new Uint8Array(4);
84 gl
.readPixels(x
, y
, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, pixel
);
86 var pixelMatches
= pixel
[0] == refData
[0] &&
87 pixel
[1] == refData
[1] &&
88 pixel
[2] == refData
[2] &&
89 pixel
[3] == refData
[3];
90 func(pixelMatches
, infoString
);
93 function preDraw(info
) {
94 gl
.clearColor(1.0, 0.0, 0.0, 1.0);
95 gl
.clear(gl
.COLOR_BUFFER_BIT
);
97 testPixel(0, 0, [255, 0, 0, 255], ok
, '[' + info
+ '] Should be red before drawing.');
100 function postDraw(info
) {
101 testPixel(0, 0, [0, 255, 0, 255], ok
, '[' + info
+ '] Should be green before drawing.');
105 gl
.enableVertexAttribArray(prog
.aVertCoord
);
106 gl
.vertexAttribPointer(prog
.aVertCoord
, 2, gl
.FLOAT
, false, 0, 0);
109 checkGLError(ok
, 'after setup');
111 preDraw('DrawArrays');
112 gl
.drawArrays(gl
.TRIANGLE_STRIP
, 0, 4);
113 postDraw('DrawArrays');
114 checkGLError(ok
, 'after DrawArrays');
116 preDraw('DrawElements');
117 gl
.drawElements(gl
.TRIANGLE_STRIP
, 4, gl
.UNSIGNED_SHORT
, 0);
118 postDraw('DrawElements');
119 checkGLError(ok
, 'after DrawElements');
121 ok(true, 'Test complete.');