|
| 1 | +const shell = require('gl-now')() |
| 2 | +const createShader = require('gl-shader') |
| 3 | +const createBuffer = require('gl-buffer') |
| 4 | +const mat4 = require('gl-mat4') |
| 5 | + |
| 6 | +let shader, buffer |
| 7 | +shell.on('gl-init', function () { |
| 8 | + const gl = shell.gl |
| 9 | + shader = createShader(gl, ` |
| 10 | + uniform mat4 model; |
| 11 | + uniform mat4 camera; |
| 12 | + attribute vec3 position; |
| 13 | + void main() { |
| 14 | + gl_Position = camera * model * vec4(position, 1.0); |
| 15 | + } |
| 16 | + `, ` |
| 17 | + void main() { |
| 18 | + gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); |
| 19 | + } |
| 20 | + `) |
| 21 | + buffer = createBuffer(gl, [ |
| 22 | + 0.0, 0.5, 0.0, |
| 23 | + -0.5, -0.5, 0.0, |
| 24 | + 0.5, -0.5, 0.0, |
| 25 | + ]) |
| 26 | +}) |
| 27 | + |
| 28 | +shell.on('gl-render', function () { |
| 29 | + const gl = shell.gl |
| 30 | + shader.bind() |
| 31 | + buffer.bind() |
| 32 | + |
| 33 | + const camera = mat4.create() |
| 34 | + mat4.perspective(camera, 45 * Math.PI / 180, shell.width / shell.height, 0.1, 1000.0) |
| 35 | + mat4.translate(camera, camera, [0, 0, -2]) |
| 36 | + shader.uniforms.camera = camera |
| 37 | + |
| 38 | + const model = mat4.create() |
| 39 | + mat4.translate(model, model, [-.3, 0, 0]) |
| 40 | + //mat4.rotate(model, model, 45, [0, 0, 1]) |
| 41 | + mat4.scale(model, model, [.5, .5, 1]) |
| 42 | + shader.uniforms.model = model |
| 43 | + shader.attributes.position.pointer() |
| 44 | + gl.drawArrays(gl.TRIANGLES, 0, 3) |
| 45 | +}) |
| 46 | + |
| 47 | + |
| 48 | + |
0 commit comments