-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext-gl-triangle.js
53 lines (46 loc) · 1.34 KB
/
context-gl-triangle.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
import {CreateApp} from "../foam-app/App";
const glsl =
`#ifdef VERTEX_SHADER
precision mediump float;
attribute vec4 aPosition;
void main() {
gl_Position = aPosition;
}
#endif
#ifdef FRAGMENT_SHADER
precision mediump float;
uniform vec3 uColor;
void main(){
gl_FragColor = vec4(uColor,1.0);
}
#endif`;
function setup(){
this._ctx.setClearColor([0,0,1,1]);
this._ctx.setProgram(this._ctx.createProgram(glsl));
this._ctx.setVertexArray(this._ctx.createVertexArray([{
location: this._ctx.ATTRIB_LOCATION_POSITION,
buffer: this._ctx.createVertexBuffer(new Float32Array([
-0.5,-0.5, -0.5,0.5, 0.5,0.5
])),
size: this._ctx.SIZE_VEC2
}]));
const resize = ()=>{
this.setWindowSize([window.innerWidth,window.innerHeight]);
this._ctx.setViewport(this.getWindowBounds());
};
resize();
window.addEventListener('resize',resize);
}
function update(){
const time = this.getSecondsElapsed();
this._ctx.clear(this._ctx.COLOR_BIT);
this._ctx.setProgramUniform('uColor',
0.5 + Math.sin(time * Math.PI * 1.0) * 0.5,
0.5 + Math.sin(time * Math.PI * 2.0) * 0.5,
0.5 + Math.sin(time * Math.PI * 4.0) * 0.5
);
this._ctx.drawArrays(this._ctx.TRIANGLES,0,3);
}
window.addEventListener('load', function(){
CreateApp({setup,update});
});