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 Lots of polygons example.</title> |
| 33 <link rel="stylesheet" href="../resources/js-test-style.css"/> |
| 34 <script src="../resources/js-test-pre.js"></script> |
| 35 <script src="../conformance/resources/webgl-test-utils.js"> </script> |
| 36 <style> |
| 37 html, body { |
| 38 width: 100%; |
| 39 height: 100%; |
| 40 } |
| 41 canvas { |
| 42 border: 1px solid black; |
| 43 width: 95%; |
| 44 height: 80%; |
| 45 } |
| 46 </style |
| 47 </head> |
| 48 <body> |
| 49 <canvas id="example"></canvas> |
| 50 <div id="description"></div> |
| 51 <div id="console"></div> |
| 52 <script> |
| 53 window.onload = init; |
| 54 debug("Tests a WebGL program that draws a bunch of large polygons from a quad me
sh"); |
| 55 |
| 56 function init() { |
| 57 if (confirm( |
| 58 "after clicking ok your machine may be come unresponsive or crash")) { |
| 59 main(); |
| 60 } else { |
| 61 debug("cancelled"); |
| 62 } |
| 63 } |
| 64 |
| 65 function main() { |
| 66 var wtu = WebGLTestUtils; |
| 67 var canvas = document.getElementById("example"); |
| 68 var gridRes = 1000; |
| 69 canvas.width = canvas.clientWidth; |
| 70 canvas.heigt = canvas.clientHeight; |
| 71 canvas.addEventListener("webglcontextlost", function(e) { e.preventDefault();
}, false); |
| 72 canvas.addEventListener("webglcontextrestored", function(e) { }, false); |
| 73 |
| 74 var gl = wtu.create3DContext(canvas); |
| 75 var program = wtu.setupProgram( |
| 76 gl, ['vshader', 'fshader'], ['vPosition'], [0]); |
| 77 |
| 78 wtu.setupQuad(gl, gridRes, 0, true); |
| 79 |
| 80 // make 1 texture since we'd have at least that in CSS shaders |
| 81 var size = 256; |
| 82 var pixels = new Uint8Array(size * size * 4); |
| 83 for (var y = 0; y < size; ++y) { |
| 84 for (var x = 0; x < size; ++x) { |
| 85 var offset = (y * size + x) * 4; |
| 86 pixels[offset + 0] = x * 255 / size; |
| 87 pixels[offset + 1] = y * 255 / size; |
| 88 pixels[offset + 2] = x * y * 255 / (size * size); |
| 89 pixels[offset + 3] = 255; |
| 90 } |
| 91 } |
| 92 var tex = gl.createTexture(); |
| 93 gl.bindTexture(gl.TEXTURE_2D, tex); |
| 94 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 256, 256, 0, gl.RGBA, gl.UNSIGNED_BYT
E, pixels); |
| 95 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); |
| 96 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); |
| 97 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); |
| 98 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); |
| 99 |
| 100 // add test colors. |
| 101 if (false) { |
| 102 var vertsAcross = gridRes + 1; |
| 103 var numQuads = vertsAcross * vertsAcross; |
| 104 var colors = new Float32Array(numQuads * 4); |
| 105 for (var ii = 0; ii < numQuads; ++ii) { |
| 106 var offset = ii * 4; |
| 107 colors[offset + 0] = Math.random(); |
| 108 colors[offset + 1] = Math.random(); |
| 109 colors[offset + 2] = Math.random(); |
| 110 colors[offset + 3] = 1; |
| 111 } |
| 112 var colorLocation = gl.getAttribLocation(program, "color") |
| 113 var buf = gl.createBuffer(); |
| 114 gl.bindBuffer(gl.ARRAY_BUFFER, buf); |
| 115 gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW); |
| 116 gl.enableVertexAttribArray(colorLocation); |
| 117 gl.vertexAttribPointer(colorLocation, 3, gl.FLOAT, false, 0, 0); |
| 118 } |
| 119 |
| 120 var gridResLoc = gl.getUniformLocation(program, "gridRes"); |
| 121 gl.uniform1f(gridResLoc, gridRes); |
| 122 |
| 123 assertMsg(gl.getError() == gl.NO_ERROR, "Should be no errors from setup."); |
| 124 |
| 125 gl.enable(gl.BLEND); |
| 126 //gl.enable(gl.CULL_FACE); |
| 127 //gl.cullFace(gl.FRONT); |
| 128 |
| 129 gl.drawElements(gl.TRIANGLES, gridRes * gridRes * 6, gl.UNSIGNED_SHORT, 0); |
| 130 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "after drawing"); |
| 131 |
| 132 successfullyParsed = true; |
| 133 } |
| 134 </script> |
| 135 <script id="vshader" type="x-shader/x-vertex"> |
| 136 attribute vec4 vPosition; |
| 137 varying vec2 v_texCoord; |
| 138 uniform float gridRes; |
| 139 |
| 140 #ifdef ADD_TEST_COLORS |
| 141 attribute vec4 color; |
| 142 varying vec4 v_color; |
| 143 #endif |
| 144 |
| 145 void main() |
| 146 { |
| 147 // expand each quad to cover the entire element. |
| 148 vec2 p = mod((vPosition.xy * 0.5 + 0.5) * gridRes, 2.0) * 2.0 - 1.0; |
| 149 gl_Position = vec4(p, 0, 1); |
| 150 v_texCoord = vPosition.xy; |
| 151 |
| 152 #ifdef ADD_TEST_COLORS |
| 153 v_color = color; |
| 154 #endif |
| 155 } |
| 156 </script> |
| 157 |
| 158 <script id="fshader" type="x-shader/x-fragment"> |
| 159 precision mediump float; |
| 160 varying vec4 v_color; |
| 161 varying vec2 v_texCoord; |
| 162 uniform sampler2D tex; |
| 163 void main() |
| 164 { |
| 165 #ifdef ADD_TEST_COLORS |
| 166 gl_FragColor = v_color; |
| 167 #else |
| 168 gl_FragColor = texture2D(tex, v_texCoord); |
| 169 #endif |
| 170 } |
| 171 </script> |
| 172 </body> |
| 173 </html> |
| 174 |
| 175 |
OLD | NEW |