4 <meta charset='UTF-
8'
/>
5 <script src='/tests/SimpleTest/SimpleTest.js'
></script>
6 <link rel='stylesheet' href='/tests/SimpleTest/test.css'
>
7 <script src='../webgl-util.js'
></script>
8 <script id='vs' type='x-shader/x-vertex'
>
10 attribute vec2 aPosition;
14 gl_Position = vec4(aPosition,
0,
1);
18 <script id='fs' type='x-shader/x-fragment'
>
20 precision mediump float;
25 gl_FragColor = uColor;
33 function GetPixel(gl
, x
, y
) {
34 var pixel
= new Uint8Array(4);
35 gl
.readPixels(x
, y
, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, pixel
);
39 function ColorStr(arr
) {
40 return '{' + arr
.map(function(x
) { return '' + x
; }).join(', ') + '}';
43 function PixelShouldBe(gl
, x
, y
, ref
, prefix
) {
48 var test
= GetPixel(gl
, x
, y
);
50 var testStr
= ColorStr(test
);
51 var refStr
= ColorStr(ref
.map(value
=> value
* 255));
52 ok(testStr
== refStr
, prefix
+ 'Should be ' + refStr
+ ', was ' + testStr
+ '.');
55 function GetProgram(gl
) {
56 var prog
= WebGLUtil
.createProgramByIds(gl
, 'vs', 'fs');
58 prog
.aPosition
= gl
.getAttribLocation(prog
, 'aPosition');
59 ok(prog
.aPosition
>= 0, '`aPosition` should be valid.');
61 prog
.uColor
= gl
.getUniformLocation(prog
, 'uColor');
62 ok(prog
.uColor
, '`uColor` should be valid.');
67 // Give ourselves a scope to return early from:
69 var c
= document
.createElement('canvas');
70 document
.body
.appendChild(c
);
71 var gl
= c
.getContext('webgl', { depth
: false, antialias
: false });
73 todo(false, 'WebGL is unavailable.');
79 // With default culling, it works fine.
80 // The problem seems to be that the virtual quads generated from points are wound 'backwards'.
81 gl
.enable(gl
.CULL_FACE
);
82 gl
.cullFace(gl
.BACK
); // Cull back faces.
86 var vertArr
= new Float32Array([
92 var vbo
= gl
.createBuffer();
93 gl
.bindBuffer(gl
.ARRAY_BUFFER
, vbo
);
94 gl
.bufferData(gl
.ARRAY_BUFFER
, vertArr
, gl
.STATIC_DRAW
);
98 var triProg
= GetProgram(gl
);
99 var pointProg
= GetProgram(gl
);
100 if (!triProg
|| !pointProg
) {
101 ok(false, 'Program linking should succeed.');
105 ok(triProg
.aPosition
== pointProg
.aPosition
, 'aPosition should match.');
106 gl
.enableVertexAttribArray(triProg
.aPosition
);
107 gl
.vertexAttribPointer(triProg
.aPosition
, 2, gl
.FLOAT
, false, 0, 0);
111 gl
.useProgram(triProg
);
112 var triColor
= [1, 0, 0, 1];
113 gl
.uniform4fv(triProg
.uColor
, triColor
);
115 gl
.useProgram(pointProg
);
116 var pointColor
= [0, 1, 0, 1];
117 gl
.uniform4fv(pointProg
.uColor
, pointColor
);
121 gl
.clearColor(0, 0, 0, 1);
122 gl
.clear(gl
.COLOR_BUFFER_BIT
);
124 gl
.useProgram(triProg
);
125 gl
.drawArrays(gl
.TRIANGLES
, 0, 3);
127 gl
.useProgram(pointProg
);
128 gl
.drawArrays(gl
.POINTS
, 0, 3);
132 PixelShouldBe(gl
, 32, 32, triColor
, 'Tri');
133 PixelShouldBe(gl
, 0, 0, pointColor
, 'Point');
135 ok(true, 'Test complete');