summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/attribs/gl-vertex-attrib-render.html
blob: b10ebfe6886b63a20533e004c59491a0a77a9c26 (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
<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"></script>
<script id='vshader' type='x-shader'>
attribute vec4 a;
attribute vec2 p;
void main() {
  gl_Position = vec4(p.x + a.x + a.y + a.z + a.w, p.y, 0.0, 1.0);
}
</script>
<script id='fshader' type='x-shader'>
precision mediump float;
void main() {
  gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
</script>
<script>
"use strict";
function checkRedPortion(gl, w, low, high) {
  var buf = new Uint8Array(w * w * 4);
  gl.readPixels(0, 0, w, w, gl.RGBA, gl.UNSIGNED_BYTE, buf);
  var i = 0;
  for (; i < w; ++i) {
    if (buf[i * 4 + 0] == 255 && buf[i * 4 + 1] == 0 && buf[i * 4 + 2] == 0 && buf[i * 4 + 3] == 255) {
      break;
    }
  }
  return low <= i && i <= high;
}

function runTest() {
  var wtu = WebGLTestUtils;
  var gl = wtu.create3DContext('testbed', { preserveDrawingBuffer : true });
  if (!gl) {
    testFailed('could not create context');
    return;
  }
  var program = wtu.setupProgram(gl, ['vshader', 'fshader'], ['p', 'a'])

  gl.enableVertexAttribArray(gl.p);
  var pos = gl.createBuffer();
  pos.type = gl.FLOAT;
  pos.size = 2;
  pos.num = 4;
  gl.bindBuffer(gl.ARRAY_BUFFER, pos);
  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW);

  gl.vertexAttribPointer(0, pos.size, pos.type, false, 0, 0);

  debug('Test vertexAttrib[1..4]fv by setting different combinations that add up to 1.5 and use that when rendering.');
  var vals = [[0.5], [0.1,0.4], [0.2,-0.2,0.5], [-1.0,0.3,0.2,2.0]];

  for (var j = 0; j < 4; ++j) {
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    gl['vertexAttrib' + (j+1) + 'fv'](1, vals[j]);
    gl.drawArrays(gl.TRIANGLE_STRIP, 0, pos.num);

    if (checkRedPortion(gl, 50, 50 * 0.7, 50 * 0.8)) {
      testPassed('Attribute of size ' + (j+1) + ' was set correctly');
    } else {
      testFailed('Attribute of size ' + (j+1) + ' was not set correctly');
    }
  }
}
</script>
</head>
<body>
<canvas id="testbed" width="50" height="50"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description('Verify that using constant attributes works.');
runTest();
var successfullyParsed = true;
</script>
<script src="../../js/js-test-post.js"></script>
</body>
</html>