|
| 1 | +// tslint:disable: no-console |
| 2 | + |
| 3 | +import { mat4 } from 'gl-matrix'; |
| 4 | + |
| 5 | +const vertexShaderSrc = `#version 300 es |
| 6 | +#pragma vscode_glsllint_stage: vert |
| 7 | +
|
| 8 | +uniform mat4 uModel; |
| 9 | +uniform mat4 uView; |
| 10 | +uniform mat4 uProjection; |
| 11 | +
|
| 12 | +layout(location=0) in vec4 aPosition; |
| 13 | +layout(location=1) in vec4 aColor; |
| 14 | +
|
| 15 | +out vec4 vColor; |
| 16 | +
|
| 17 | +void main() |
| 18 | +{ |
| 19 | + vColor = aColor; |
| 20 | + gl_Position = uProjection * uView * uModel * aPosition; |
| 21 | +}`; |
| 22 | + |
| 23 | +const fragmentShaderSrc = `#version 300 es |
| 24 | +#pragma vscode_glsllint_stage: frag |
| 25 | +
|
| 26 | +precision mediump float; |
| 27 | +
|
| 28 | +in vec4 vColor; |
| 29 | +
|
| 30 | +out vec4 fragColor; |
| 31 | +
|
| 32 | +void main() |
| 33 | +{ |
| 34 | + fragColor = vColor; |
| 35 | +}`; |
| 36 | + |
| 37 | +const gl = document.querySelector('canvas').getContext('webgl2'); |
| 38 | + |
| 39 | + |
| 40 | +const program = gl.createProgram(); |
| 41 | + |
| 42 | +const vertexShader = gl.createShader(gl.VERTEX_SHADER); |
| 43 | +gl.shaderSource(vertexShader, vertexShaderSrc); |
| 44 | +gl.compileShader(vertexShader); |
| 45 | +gl.attachShader(program, vertexShader); |
| 46 | + |
| 47 | +const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER); |
| 48 | +gl.shaderSource(fragmentShader, fragmentShaderSrc); |
| 49 | +gl.compileShader(fragmentShader); |
| 50 | +gl.attachShader(program, fragmentShader); |
| 51 | + |
| 52 | +gl.linkProgram(program); |
| 53 | + |
| 54 | +if (!gl.getProgramParameter(program, gl.LINK_STATUS)) { |
| 55 | + console.log(gl.getShaderInfoLog(vertexShader)); |
| 56 | + console.log(gl.getShaderInfoLog(fragmentShader)); |
| 57 | +} |
| 58 | + |
| 59 | +gl.useProgram(program); |
| 60 | + |
| 61 | +gl.enable(gl.DEPTH_TEST); |
| 62 | + |
| 63 | +const vertexData = new Float32Array([ |
| 64 | + -.5,-.5,-.5, 0,1,1, |
| 65 | + -.5, .5, .5, 0,1,1, |
| 66 | + -.5, .5,-.5, 0,1,1, |
| 67 | + -.5,-.5, .5, 0,1,1, |
| 68 | + -.5, .5, .5, 0,1,1, |
| 69 | + -.5,-.5,-.5, 0,1,1, |
| 70 | + |
| 71 | + .5 ,-.5,-.5, 1,0,1, |
| 72 | + .5 , .5,-.5, 1,0,1, |
| 73 | + .5 , .5, .5, 1,0,1, |
| 74 | + .5 , .5, .5, 1,0,1, |
| 75 | + .5 ,-.5, .5, 1,0,1, |
| 76 | + .5 ,-.5,-.5, 1,0,1, |
| 77 | + |
| 78 | + -.5,-.5,-.5, 0,1,0, |
| 79 | + .5,-.5,-.5, 0,1,0, |
| 80 | + .5,-.5, .5, 0,1,0, |
| 81 | + .5,-.5, .5, 0,1,0, |
| 82 | + -.5,-.5, .5, 0,1,0, |
| 83 | + -.5,-.5,-.5, 0,1,0, |
| 84 | + |
| 85 | + -.5, .5,-.5, 1,1,0, |
| 86 | + .5, .5, .5, 1,1,0, |
| 87 | + .5, .5,-.5, 1,1,0, |
| 88 | + -.5, .5, .5, 1,1,0, |
| 89 | + .5, .5, .5, 1,1,0, |
| 90 | + -.5, .5,-.5, 1,1,0, |
| 91 | + |
| 92 | + .5,-.5,-.5, 0,0,1, |
| 93 | + -.5,-.5,-.5, 0,0,1, |
| 94 | + .5, .5,-.5, 0,0,1, |
| 95 | + -.5, .5,-.5, 0,0,1, |
| 96 | + .5, .5,-.5, 0,0,1, |
| 97 | + -.5,-.5,-.5, 0,0,1, |
| 98 | + |
| 99 | + -.5,-.5, .5, 1,0,0, |
| 100 | + .5,-.5, .5, 1,0,0, |
| 101 | + .5, .5, .5, 1,0,0, |
| 102 | + .5, .5, .5, 1,0,0, |
| 103 | + -.5, .5, .5, 1,0,0, |
| 104 | + -.5,-.5, .5, 1,0,0, |
| 105 | +]); |
| 106 | + |
| 107 | +const vertexBuffer = gl.createBuffer(); |
| 108 | +gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer); |
| 109 | +gl.bufferData(gl.ARRAY_BUFFER, vertexData, gl.STATIC_DRAW); |
| 110 | +gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 24, 0); |
| 111 | +gl.vertexAttribPointer(1, 3, gl.FLOAT, false, 24, 12); |
| 112 | +gl.enableVertexAttribArray(0); |
| 113 | +gl.enableVertexAttribArray(1); |
| 114 | + |
| 115 | +const modelLoc = gl.getUniformLocation(program, 'uModel'); |
| 116 | +const viewLoc = gl.getUniformLocation(program, 'uView'); |
| 117 | +const projectionLoc = gl.getUniformLocation(program, 'uProjection'); |
| 118 | + |
| 119 | +const model = mat4.create(); |
| 120 | +const view = mat4.create(); |
| 121 | +const projection = mat4.create(); |
| 122 | + |
| 123 | +mat4.rotateZ(model, model, .1); |
| 124 | +mat4.scale(model, model, [.8, .8, .8]); |
| 125 | + |
| 126 | +mat4.lookAt(view, [.6,.6,.6], [0,0,0], [0,1,0]); |
| 127 | + |
| 128 | +// mat4.perspective(projection, Math.PI / 1.5, gl.canvas.width / gl.canvas.height, .1, 10); |
| 129 | +mat4.ortho(projection, -1,1, -1,1, -1,2); |
| 130 | + |
| 131 | +gl.uniformMatrix4fv(viewLoc, false, view); |
| 132 | +gl.uniformMatrix4fv(projectionLoc, false, projection); |
| 133 | + |
| 134 | +const draw = () => { |
| 135 | + requestAnimationFrame(draw); |
| 136 | + |
| 137 | + mat4.rotate(model, model, 0.02, [1,1,0]); |
| 138 | + gl.uniformMatrix4fv(modelLoc, false, model); |
| 139 | + |
| 140 | + gl.drawArrays(gl.TRIANGLES, 0, 36); |
| 141 | +}; |
| 142 | +draw(); |
0 commit comments