OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 |
| 3 /* |
| 4 ** Copyright (c) 2012 The Khronos Group Inc. |
| 5 ** |
| 6 ** Permission is hereby granted, free of charge, to any person obtaining a |
| 7 ** copy of this software and/or associated documentation files (the |
| 8 ** "Materials"), to deal in the Materials without restriction, including |
| 9 ** without limitation the rights to use, copy, modify, merge, publish, |
| 10 ** distribute, sublicense, and/or sell copies of the Materials, and to |
| 11 ** permit persons to whom the Materials are furnished to do so, subject to |
| 12 ** the following conditions: |
| 13 ** |
| 14 ** The above copyright notice and this permission notice shall be included |
| 15 ** in all copies or substantial portions of the Materials. |
| 16 ** |
| 17 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 21 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 22 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 23 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 24 */ |
| 25 |
| 26 --> |
| 27 |
| 28 <!DOCTYPE html> |
| 29 <html> |
| 30 <head> |
| 31 <meta charset="utf-8"> |
| 32 <title>WebGL TexParameter conformance test.</title> |
| 33 <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
| 34 <script src="../../resources/js-test-pre.js"></script> |
| 35 <script src="../resources/webgl-test.js"> </script> |
| 36 </head> |
| 37 <body> |
| 38 <canvas id="example" width="24" height="24"></canvas> |
| 39 <canvas id="canvas2d" width="2" height="2"></canvas> |
| 40 <div id="description"></div> |
| 41 <div id="console"></div> |
| 42 <script id="vshader" type="x-shader/x-vertex"> |
| 43 uniform mat4 world; |
| 44 attribute vec3 vPosition; |
| 45 attribute vec2 texCoord0; |
| 46 varying vec2 texCoord; |
| 47 void main() |
| 48 { |
| 49 gl_Position = world * vec4(vPosition, 1); |
| 50 texCoord = texCoord0; |
| 51 } |
| 52 </script> |
| 53 |
| 54 <script id="fshader" type="x-shader/x-fragment"> |
| 55 precision mediump float; |
| 56 |
| 57 uniform sampler2D tex; |
| 58 varying vec2 texCoord; |
| 59 void main() |
| 60 { |
| 61 gl_FragColor = texture2D(tex, texCoord); |
| 62 } |
| 63 </script> |
| 64 |
| 65 <script> |
| 66 function init() |
| 67 { |
| 68 if (window.initNonKhronosFramework) { |
| 69 window.initNonKhronosFramework(false); |
| 70 } |
| 71 |
| 72 description("Tests TexParameter works as expected"); |
| 73 debug(""); |
| 74 |
| 75 var canvas2d = document.getElementById("canvas2d"); |
| 76 var ctx2d = canvas2d.getContext("2d"); |
| 77 |
| 78 gl = initWebGL("example"); |
| 79 program = setupProgram(gl, "vshader", "fshader", [ "vPosition", "texCoord0"]); |
| 80 |
| 81 gl.disable(gl.DEPTH_TEST); |
| 82 gl.disable(gl.BLEND); |
| 83 |
| 84 var vertexObject = gl.createBuffer(); |
| 85 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject); |
| 86 gl.bufferData( |
| 87 gl.ARRAY_BUFFER, |
| 88 new Float32Array([-1, 1,0, 1,1,0, -1,-1,0, |
| 89 -1,-1,0, 1,1,0, 1,-1,0]), |
| 90 gl.STATIC_DRAW); |
| 91 gl.enableVertexAttribArray(0); |
| 92 gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0); |
| 93 |
| 94 var vertexObject = gl.createBuffer(); |
| 95 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject); |
| 96 gl.bufferData( |
| 97 gl.ARRAY_BUFFER, |
| 98 new Float32Array([ -2.5,-2.5, 3.5,-2.5, -2.5,3.5, |
| 99 -2.5,3.5, 3.5,-2.5, 3.5,3.5]), |
| 100 gl.STATIC_DRAW); |
| 101 gl.enableVertexAttribArray(1); |
| 102 gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0); |
| 103 |
| 104 var colors = [ |
| 105 [0,255,128,255], |
| 106 [128,64,255,255], |
| 107 [192,255,64,255], |
| 108 [200,0,255,255]]; |
| 109 var texParam = [ |
| 110 gl.REPEAT, |
| 111 gl.CLAMP_TO_EDGE, |
| 112 gl.MIRRORED_REPEAT, |
| 113 gl.REPEAT]; |
| 114 |
| 115 // Make textures setting the texture parameters differently each time.. |
| 116 // This verifies both that the render correct AND that texture parameters |
| 117 // are associated with the textures, not with the texture-units. |
| 118 var textures = []; |
| 119 for (var ii = 0; ii < colors.length; ++ii) { |
| 120 var c = colors[ii]; |
| 121 ctx2d.fillStyle = |
| 122 "rgba(" + c[0] + "," + c[1] + "," + c[2] + "," + c[3] + ")"; |
| 123 ctx2d.fillRect(0, 0, 1, 1); |
| 124 var tex = gl.createTexture(); |
| 125 gl.bindTexture(gl.TEXTURE_2D, tex); |
| 126 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas2d
); |
| 127 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
| 128 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); |
| 129 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, texParam[ii]); |
| 130 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, texParam[ii]); |
| 131 textures[ii] = tex; |
| 132 } |
| 133 |
| 134 var textureLoc = gl.getUniformLocation(program, "tex"); |
| 135 var worldLoc = gl.getUniformLocation(program, "world"); |
| 136 |
| 137 gl.clearColor(1,1,1,1); |
| 138 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 139 |
| 140 for (var ii = 0; ii < colors.length; ++ii) { |
| 141 var x = ii % 2; |
| 142 var y = Math.floor(ii / 2); |
| 143 gl.bindTexture(gl.TEXTURE_2D, textures[ii]); |
| 144 gl.uniformMatrix4fv( |
| 145 worldLoc, false, |
| 146 [0.5, 0, 0, 0, |
| 147 0, 0.5, 0, 0, |
| 148 0, 0, 1, 0, |
| 149 -0.5 + x, -0.5 + y, 0, 1]); |
| 150 gl.drawArrays(gl.TRIANGLES, 0, 6); |
| 151 } |
| 152 |
| 153 var buf = new Uint8Array(24 * 24 * 4); |
| 154 gl.readPixels(0, 0, 24, 24, gl.RGBA, gl.UNSIGNED_BYTE, buf); |
| 155 var passed = true; |
| 156 for (var ii = 0; ii < colors.length; ++ii) { |
| 157 var x = ii % 2; |
| 158 var y = Math.floor(ii / 2); |
| 159 var c = colors[ii]; |
| 160 for (var yy = 0; yy < 12; ++yy) { |
| 161 for (var xx = 0; xx < 12; ++xx) { |
| 162 var ec = [0,0,0,0]; |
| 163 switch (texParam[ii]) { |
| 164 case gl.REPEAT: |
| 165 if (xx % 2 == 1 && yy % 2 == 0) { |
| 166 ec = c; |
| 167 } |
| 168 break; |
| 169 case gl.CLAMP_TO_EDGE: |
| 170 if (xx < 6 && yy >= 6) { |
| 171 ec = c; |
| 172 } |
| 173 break; |
| 174 case gl.MIRRORED_REPEAT: |
| 175 if (xx % 4 < 2 && yy % 4 >= 2) { |
| 176 ec = c; |
| 177 } |
| 178 break; |
| 179 } |
| 180 var off = ((y * 12 + yy) * 24 + x * 12 + xx) * 4; |
| 181 if (buf[off + 0] != ec[0] || |
| 182 buf[off + 1] != ec[1] || |
| 183 buf[off + 2] != ec[2] || |
| 184 buf[off + 3] != ec[3]) { |
| 185 var msg = 'at (' + (x * 12 + xx) + ', ' + (y * 12 + yy) + |
| 186 ') expected: ' + |
| 187 ec[0] + ', ' + ec[1] + ', ' + ec[2] + ', ' + ec[3] + ' found: ' + |
| 188 buf[off + 0] + ', ' + |
| 189 buf[off + 1] + ', ' + |
| 190 buf[off + 2] + ', ' + |
| 191 buf[off + 3]; |
| 192 testFailed(msg); |
| 193 passed = false; |
| 194 } |
| 195 } |
| 196 } |
| 197 } |
| 198 if (passed) { |
| 199 testPassed("rendered as expected"); |
| 200 } |
| 201 } |
| 202 |
| 203 init(); |
| 204 successfullyParsed = true; |
| 205 </script> |
| 206 <script src="../../resources/js-test-post.js"></script> |
| 207 |
| 208 </body> |
| 209 </html> |
| 210 |
OLD | NEW |