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>gl-pointcoord 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 <script src="../../resources/webgl-test-utils.js"> </script> |
| 37 </head> |
| 38 <body> |
| 39 <canvas id="example" width="256" height="256"> |
| 40 </canvas> |
| 41 <div id="description"></div> |
| 42 <div id="console"></div> |
| 43 <script id="vshader" type="x-shader/x-vertex"> |
| 44 attribute vec4 vPosition; |
| 45 uniform float uPointSize; |
| 46 void main() |
| 47 { |
| 48 gl_PointSize = uPointSize; |
| 49 gl_Position = vPosition; |
| 50 } |
| 51 </script> |
| 52 |
| 53 <script id="fshader" type="x-shader/x-fragment"> |
| 54 precision mediump float; |
| 55 void main() |
| 56 { |
| 57 gl_FragColor = vec4( |
| 58 gl_PointCoord.x, |
| 59 gl_PointCoord.y, |
| 60 0, |
| 61 1); |
| 62 } |
| 63 </script> |
| 64 |
| 65 <script> |
| 66 if (window.initNonKhronosFramework) { |
| 67 window.initNonKhronosFramework(false); |
| 68 } |
| 69 |
| 70 description("Checks gl_PointCoord and gl_PointSize"); |
| 71 debug(""); |
| 72 |
| 73 // NOTE: I'm not 100% confident in this test. I think it is correct. |
| 74 |
| 75 wtu = WebGLTestUtils; |
| 76 var gl = initWebGL("example"); |
| 77 shouldBeNonNull("gl"); |
| 78 var program = setupProgram(gl, "vshader", "fshader", [ "vPosition"]); |
| 79 shouldBe("gl.getError()", "gl.NO_ERROR"); |
| 80 |
| 81 var canvas = gl.canvas; |
| 82 var width = canvas.width; |
| 83 var height = canvas.height; |
| 84 shouldBe("width", "height"); |
| 85 |
| 86 var maxPointSize = gl.getParameter(gl.ALIASED_POINT_SIZE_RANGE)[1]; |
| 87 shouldBeTrue("maxPointSize >= 1"); |
| 88 shouldBeTrue("maxPointSize % 1 == 0"); |
| 89 |
| 90 maxPointSize = Math.min(maxPointSize, 64); |
| 91 var pointWidth = maxPointSize / width; |
| 92 var pointStep = Math.floor(maxPointSize / 4); |
| 93 var pointStep = Math.max(1, pointStep); |
| 94 |
| 95 var pointSizeLoc = gl.getUniformLocation(program, "uPointSize"); |
| 96 gl.uniform1f(pointSizeLoc, maxPointSize); |
| 97 |
| 98 var pixelOffset = (maxPointSize % 2) ? (1 / width) : 0; |
| 99 var vertexObject = gl.createBuffer(); |
| 100 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject); |
| 101 gl.bufferData( |
| 102 gl.ARRAY_BUFFER, |
| 103 new Float32Array( |
| 104 [-0.5 + pixelOffset, -0.5 + pixelOffset, |
| 105 0.5 + pixelOffset, -0.5 + pixelOffset, |
| 106 -0.5 + pixelOffset, 0.5 + pixelOffset, |
| 107 0.5 + pixelOffset, 0.5 + pixelOffset]), |
| 108 gl.STATIC_DRAW); |
| 109 gl.enableVertexAttribArray(0); |
| 110 gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); |
| 111 |
| 112 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 113 |
| 114 gl.drawArrays(gl.POINTS, 0, 4); |
| 115 shouldBe("gl.getError()", "gl.NO_ERROR"); |
| 116 |
| 117 function s2p(s) { |
| 118 return (s + 1.0) * 0.5 * width; |
| 119 } |
| 120 |
| 121 //function print(x, y) { |
| 122 // var b = new Uint8Array(4); |
| 123 // gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, b); |
| 124 // debug("" + x + "," + y + ": " + b[0] + "," + b[1] + "," + b[2]); |
| 125 //} |
| 126 // |
| 127 //for (var ii = 0; ii < 100; ++ii) { |
| 128 // print(ii, ii); |
| 129 //} |
| 130 |
| 131 for (var py = 0; py < 2; ++py) { |
| 132 for (var px = 0; px < 2; ++px) { |
| 133 debug(""); |
| 134 var pointX = -0.5 + px + pixelOffset; |
| 135 var pointY = -0.5 + py + pixelOffset; |
| 136 for (var yy = 0; yy < maxPointSize; yy += pointStep) { |
| 137 for (var xx = 0; xx < maxPointSize; xx += pointStep) { |
| 138 // formula for s and t from OpenGL ES 2.0 spec section 3.3 |
| 139 var xw = s2p(pointX); |
| 140 var yw = s2p(pointY); |
| 141 //debug("xw: " + xw + " yw: " + yw); |
| 142 var u = xx / maxPointSize * 2 - 1; |
| 143 var v = yy / maxPointSize * 2 - 1; |
| 144 var xf = Math.floor(s2p(pointX + u * pointWidth)); |
| 145 var yf = Math.floor(s2p(pointY + v * pointWidth)); |
| 146 //debug("xf: " + xf + " yf: " + yf); |
| 147 var s = 0.5 + (xf + 0.5 - xw) / maxPointSize; |
| 148 var t = 0.5 + (yf + 0.5 - yw) / maxPointSize; |
| 149 //debug("s: " + s + " t: " + t); |
| 150 var color = [Math.floor(s * 255), Math.floor((1 - t) * 255), 0]; |
| 151 var msg = "pixel " + xf + "," + yf + " should be " + color; |
| 152 wtu.checkCanvasRect(gl, xf, yf, 1, 1, color, msg, 4); |
| 153 } |
| 154 } |
| 155 } |
| 156 } |
| 157 |
| 158 successfullyParsed = true; |
| 159 </script> |
| 160 <script src="../../../resources/js-test-post.js"></script> |
| 161 |
| 162 </body> |
| 163 </html> |
OLD | NEW |