1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
2 "http://www.w3.org/TR/html4/loose.dtd">
5 <title>WebGL pixelStorei Test
</title>
6 <script src=
"../../../resources/js-test.js"></script>
7 <script src=
"resources/webgl-test.js"> </script>
8 <script src=
"resources/desktop-gl-constants.js" type=
"text/javascript"></script>
11 <canvas id=
"example" width=
"50" height=
"50"></canvas>
12 <canvas id=
"2d00" width=
"50" height=
"50"></canvas>
13 <canvas id=
"2d01" width=
"50" height=
"50"></canvas>
14 <canvas id=
"2d02" width=
"50" height=
"50"></canvas>
15 <canvas id=
"2d03" width=
"50" height=
"50"></canvas>
16 <div id=
"description"></div>
17 <div id=
"console"></div>
18 <script id=
"vshader" type=
"x-shader/x-vertex">
19 attribute vec4 vPosition;
21 gl_Position = vPosition;
25 <script id=
"fshader" type=
"x-shader/x-fragment">
27 gl_FragColor = vec4(
1.0,
0.0,
0.0,
1.0);
32 function fail(x
,y
, name
, buf
, shouldBe
) {
34 var reason
= "pixel in "+name
+" at ("+x
+","+y
+") is ("+buf
[i
]+","+buf
[i
+1]+","+buf
[i
+2]+","+buf
[i
+3]+"), should be "+shouldBe
;
39 testPassed("drawing is correct in " + name
);
43 debug("There should be 5 red triangles on 5 black squares above");
46 debug("This test checks that drawImage and readPixels are not effected by gl.Pixelstorei(gl.PACK_ALIGNMENT) and visa versa");
49 var canvas3d
= document
.getElementById("example");
50 gl
= initWebGL("example", "vshader", "fshader", [ "vPosition"], [ 0, 0, 0, 1 ], 1);
52 var vertexObject
= gl
.createBuffer();
53 gl
.bindBuffer(gl
.ARRAY_BUFFER
, vertexObject
);
54 gl
.bufferData(gl
.ARRAY_BUFFER
, new Float32Array([ 0,0.5,0, -0.5,-0.5,0, 0.5,-0.5,0 ]), gl
.STATIC_DRAW
);
55 gl
.enableVertexAttribArray(0);
56 gl
.vertexAttribPointer(0, 3, gl
.FLOAT
, false, 0, 0);
58 gl
.clear(gl
.COLOR_BUFFER_BIT
| gl
.DEPTH_BUFFER_BIT
);
59 gl
.drawArrays(gl
.TRIANGLES
, 0, 3);
62 function checkData(buf
, name
) {
63 // Test several locations
64 // First line should be all black
65 for (var i
= 0; i
< 50; ++i
) {
66 if (buf
[i
*4] != 0 || buf
[i
*4+1] != 0 || buf
[i
*4+2] != 0 || buf
[i
*4+3] != 255) {
67 fail(i
, 0, name
, buf
, "(0,0,0,255)");
72 // Line 25 should be red for at least 6 red pixels starting 22 pixels in
73 var offset
= (25*50+22) * 4;
74 for (var i
= 0; i
< 6; ++i
) {
75 if (buf
[offset
+i
*4] != 255 || buf
[offset
+i
*4+1] != 0 || buf
[offset
+i
*4+2] != 0 || buf
[offset
+i
*4+3] != 255) {
76 fail(22 + i
, 25, name
, buf
, "(255,0,0,255)");
81 // Last line should be all black
83 for (var i
= 0; i
< 50; ++i
) {
84 if (buf
[offset
+i
*4] != 0 || buf
[offset
+i
*4+1] != 0 || buf
[offset
+i
*4+2] != 0 || buf
[offset
+i
*4+3] != 255) {
85 fail(i
, 49, name
, buf
, "(0,0,0,255)");
93 function checkColors() {
94 var buf
= new Uint8Array(50 * 50 * 4);
95 gl
.readPixels(0, 0, 50, 50, gl
.RGBA
, gl
.UNSIGNED_BYTE
, buf
);
96 checkData(buf
, "3d context");
97 var imgData
= ctx2d
.getImageData(0, 0, 50, 50);
98 checkData(imgData
.data
, "2d context");
101 var table
= [1, 2, 4, 8];
102 for (var ii
= 0; ii
< table
.length
; ++ii
) {
103 gl
.pixelStorei(gl
.PACK_ALIGNMENT
, table
[ii
]);
104 ctx2d
= document
.getElementById("2d0" + ii
).getContext("2d");
105 ctx2d
.globalCompositeOperation
= 'copy';
106 ctx2d
.drawImage(canvas3d
, 0, 0);
108 assertMsg(gl
.getParameter(gl
.PACK_ALIGNMENT
) == table
[ii
],
109 "PACK_ALIGNMENT is " + table
[ii
]);