|
173 | 173 | gl.drawArrays(gl.TRIANGLES, 0, 3); |
174 | 174 | } |
175 | 175 | </script> |
| 176 | + |
| 177 | +<script id=WebGLRenderingContext#shader_program_drawElements_uniform4f> |
| 178 | +{ |
| 179 | + const element = document.createElement("canvas"); |
| 180 | + element.width = 64; |
| 181 | + element.height = 64; |
| 182 | + const gl = element.getContext("webgl"); |
| 183 | + |
| 184 | + const vertexShader = gl.createShader(gl.VERTEX_SHADER); |
| 185 | + gl.shaderSource(vertexShader, ` |
| 186 | + attribute vec2 a_position; |
| 187 | + void main() { |
| 188 | + gl_Position = vec4(a_position, 0.0, 1.0); |
| 189 | + } |
| 190 | + `); |
| 191 | + gl.compileShader(vertexShader); |
| 192 | + testing.expectEqual(true, gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)); |
| 193 | + |
| 194 | + const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER); |
| 195 | + gl.shaderSource(fragmentShader, ` |
| 196 | + uniform vec4 u_color; |
| 197 | + void main() { |
| 198 | + gl_FragColor = u_color; |
| 199 | + } |
| 200 | + `); |
| 201 | + gl.compileShader(fragmentShader); |
| 202 | + testing.expectEqual(true, gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS)); |
| 203 | + |
| 204 | + const program = gl.createProgram(); |
| 205 | + gl.attachShader(program, vertexShader); |
| 206 | + gl.attachShader(program, fragmentShader); |
| 207 | + gl.linkProgram(program); |
| 208 | + testing.expectEqual(true, gl.getProgramParameter(program, gl.LINK_STATUS)); |
| 209 | + |
| 210 | + const vertices = gl.createBuffer(); |
| 211 | + gl.bindBuffer(gl.ARRAY_BUFFER, vertices); |
| 212 | + gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ |
| 213 | + -0.75, -0.75, |
| 214 | + 0.75, -0.75, |
| 215 | + 0.00, 0.80, |
| 216 | + ]), gl.STATIC_DRAW); |
| 217 | + |
| 218 | + const elements = gl.createBuffer(); |
| 219 | + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elements); |
| 220 | + gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array([0, 1, 2]), gl.STATIC_DRAW); |
| 221 | + |
| 222 | + const location = gl.getAttribLocation(program, "a_position"); |
| 223 | + testing.expectEqual(0, location); |
| 224 | + const colorLocation = gl.getUniformLocation(program, "u_color"); |
| 225 | + testing.expectEqual(true, !!colorLocation); |
| 226 | + |
| 227 | + gl.useProgram(program); |
| 228 | + gl.viewport(0, 0, 64, 64); |
| 229 | + gl.clearColor(1, 1, 1, 1); |
| 230 | + gl.clear(gl.COLOR_BUFFER_BIT); |
| 231 | + gl.enableVertexAttribArray(location); |
| 232 | + gl.vertexAttribPointer(location, 2, gl.FLOAT, false, 0, 0); |
| 233 | + gl.uniform4f(colorLocation, 0.0, 0.0, 1.0, 1.0); |
| 234 | + gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_SHORT, 0); |
| 235 | +} |
| 236 | +</script> |
0 commit comments