gl-vertex-attrib-render.html (2575B)
1 <!-- 2 Copyright (c) 2019 The Khronos Group Inc. 3 Use of this source code is governed by an MIT-style license that can be 4 found in the LICENSE.txt file. 5 --> 6 7 <!DOCTYPE html> 8 <html> 9 <head> 10 <meta charset="utf-8"> 11 <link rel="stylesheet" href="../../resources/js-test-style.css"/> 12 <script src="../../js/js-test-pre.js"></script> 13 <script src="../../js/webgl-test-utils.js"></script> 14 <script id='vshader' type='x-shader'> 15 attribute vec4 a; 16 attribute vec2 p; 17 void main() { 18 gl_Position = vec4(p.x + a.x + a.y + a.z + a.w, p.y, 0.0, 1.0); 19 } 20 </script> 21 <script id='fshader' type='x-shader'> 22 precision mediump float; 23 void main() { 24 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); 25 } 26 </script> 27 <script> 28 "use strict"; 29 function checkRedPortion(gl, w, low, high) { 30 var buf = new Uint8Array(w * w * 4); 31 gl.readPixels(0, 0, w, w, gl.RGBA, gl.UNSIGNED_BYTE, buf); 32 var i = 0; 33 for (; i < w; ++i) { 34 if (buf[i * 4 + 0] == 255 && buf[i * 4 + 1] == 0 && buf[i * 4 + 2] == 0 && buf[i * 4 + 3] == 255) { 35 break; 36 } 37 } 38 return low <= i && i <= high; 39 } 40 41 function runTest() { 42 var wtu = WebGLTestUtils; 43 var gl = wtu.create3DContext('testbed', { preserveDrawingBuffer : true }); 44 if (!gl) { 45 testFailed('could not create context'); 46 return; 47 } 48 var program = wtu.setupProgram(gl, ['vshader', 'fshader'], ['p', 'a']) 49 50 gl.enableVertexAttribArray(gl.p); 51 var pos = gl.createBuffer(); 52 pos.type = gl.FLOAT; 53 pos.size = 2; 54 pos.num = 4; 55 gl.bindBuffer(gl.ARRAY_BUFFER, pos); 56 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW); 57 58 gl.vertexAttribPointer(0, pos.size, pos.type, false, 0, 0); 59 60 debug('Test vertexAttrib[1..4]fv by setting different combinations that add up to 1.5 and use that when rendering.'); 61 var vals = [[0.5], [0.1,0.4], [0.2,-0.2,0.5], [-1.0,0.3,0.2,2.0]]; 62 63 for (var j = 0; j < 4; ++j) { 64 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); 65 gl['vertexAttrib' + (j+1) + 'fv'](1, vals[j]); 66 gl.drawArrays(gl.TRIANGLE_STRIP, 0, pos.num); 67 68 if (checkRedPortion(gl, 50, 50 * 0.7, 50 * 0.8)) { 69 testPassed('Attribute of size ' + (j+1) + ' was set correctly'); 70 } else { 71 testFailed('Attribute of size ' + (j+1) + ' was not set correctly'); 72 } 73 } 74 } 75 </script> 76 </head> 77 <body> 78 <canvas id="testbed" width="50" height="50"></canvas> 79 <div id="description"></div> 80 <div id="console"></div> 81 <script> 82 "use strict"; 83 description('Verify that using constant attributes works.'); 84 runTest(); 85 var successfullyParsed = true; 86 </script> 87 <script src="../../js/js-test-post.js"></script> 88 </body> 89 </html>