-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
111 lines (92 loc) · 2.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import Sylvester from 'sylvester'
import 'sylvester-utils'
const { Matrix, Vector, makePerspective } = Sylvester
const $V = Vector.create
const canvas = document.getElementById('screen')
const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl')
let resized = true
window.addEventListener('resize', () => {
resized = true
})
gl.clearColor(0.0, 0.0, 0.0, 1.0)
gl.clearDepth(1.0)
gl.enable(gl.DEPTH_TEST)
gl.depthFunc(gl.LEQUAL)
function makeShader(shader, code) {
gl.shaderSource(shader, code)
gl.compileShader(shader)
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
throw Error('unimplemented')
}
return shader
}
const vs = makeShader(
gl.createShader(gl.VERTEX_SHADER),
`
attribute vec3 aVertexPosition;
attribute vec4 aVertexColor;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
varying lowp vec4 vColor;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
vColor = aVertexColor;
}
`,
)
const fs = makeShader(
gl.createShader(gl.FRAGMENT_SHADER),
`
varying lowp vec4 vColor;
void main(void) {
gl_FragColor = vColor;
}
`,
)
const program = gl.createProgram()
gl.attachShader(program, vs)
gl.attachShader(program, fs)
gl.linkProgram(program)
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
throw Error('unimplemented')
}
gl.useProgram(program)
function makeAttr(name) {
const attr = gl.getAttribLocation(program, name)
gl.enableVertexAttribArray(attr)
return attr
}
const vertexPositionAttribute = makeAttr('aVertexPosition')
const vertexColorAttribute = makeAttr('aVertexColor')
function makeBuffer(data) {
const ret = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, ret)
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW)
return ret
}
const vb = makeBuffer([+1, +1, +0, -1, +1, +0, +1, -1, +0, -1, -1, +0])
const colorb = makeBuffer([1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1])
setInterval(render, 15)
function render() {
const mvMatrix = Matrix.Translation($V([0, 0, -6])).ensure4x4()
const mvUniform = gl.getUniformLocation(program, 'uMVMatrix')
gl.uniformMatrix4fv(mvUniform, false, new Float32Array(mvMatrix.flatten()))
const pMatrix = makePerspective(45, 1, 0.1, 100.0)
const pUniform = gl.getUniformLocation(program, 'uPMatrix')
gl.uniformMatrix4fv(pUniform, false, new Float32Array(pMatrix.flatten()))
gl.bindBuffer(gl.ARRAY_BUFFER, vb)
gl.vertexAttribPointer(vertexPositionAttribute, 3, gl.FLOAT, false, 0, 0)
gl.bindBuffer(gl.ARRAY_BUFFER, colorb)
gl.vertexAttribPointer(vertexColorAttribute, 4, gl.FLOAT, false, 0, 0)
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4)
if (resized) {
if (gl.canvas.width !== gl.canvas.clientWidth) {
gl.canvas.width = gl.canvas.clientWidth
}
if (gl.canvas.height !== gl.canvas.clientHeight) {
gl.canvas.height = gl.canvas.clientHeight
}
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height)
}
}