-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
137 lines (113 loc) · 3.31 KB
/
app.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var webGLStart = (function(){
var gl,
time = 0,
timeInc = 0.05,
shader,
projection = mat4.create(),
unitSquareVertices;
function webGLStart() {
var canvas = document.getElementById("canvas");
initGL(canvas);
initShaders();
initBuffers();
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.enable(gl.DEPTH_TEST);
(function animloop(){
requestAnimFrame(animloop);
resizeIfNecessary();
drawScene();
incrementSimulation();
})();
}
function initGL(canvas) {
try {
gl = canvas.getContext("experimental-webgl");
} catch (e) {
} if (!gl) { alert("Could not initialise WebGL"); }
}
function initShaders() {
var fragmentShader = getShader(gl, "shader-fs");
var vertexShader = getShader(gl, "shader-vs");
shader = gl.createProgram();
gl.attachShader(shader, vertexShader);
gl.attachShader(shader, fragmentShader);
gl.linkProgram(shader);
if (!gl.getProgramParameter(shader, gl.LINK_STATUS)) {
alert("Could not initialise shaders");
}
gl.useProgram(shader);
gl.enableVertexAttribArray(shader.projection);
shader.projection = gl.getUniformLocation(shader, "projection");
shader.time = gl.getUniformLocation(shader, "time");
}
function getShader(gl, id) {
var shaderScript = document.getElementById(id);
if (!shaderScript) {
return null;
}
var str = "";
var k = shaderScript.firstChild;
while (k) {
if (k.nodeType == 3) {
str += k.textContent;
}
k = k.nextSibling;
}
var shader;
if (shaderScript.type == "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (shaderScript.type == "x-shader/x-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
function initBuffers() {
unitSquareVertices = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, unitSquareVertices);
var vertices = [
1, 1, 0,
-1, 1, 0,
1, -1, 0,
-1, -1, 0
];
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array(vertices),
gl.STATIC_DRAW
);
unitSquareVertices.itemSize = 3;
unitSquareVertices.numItems = 4;
}
function resizeIfNecessary(){
widthChanged = canvas.width != window.innerWidth;
heightChanged = canvas.height != window.innerHeight;
if(widthChanged || heightChanged){
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
}
function drawScene() {
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
mat4.ortho(projection, -1, 1, -1, 1, -1, 1);
gl.bindBuffer(gl.ARRAY_BUFFER, unitSquareVertices);
gl.vertexAttribPointer(
shader.projection,
unitSquareVertices.itemSize,
gl.FLOAT, false, 0, 0
);
gl.uniformMatrix4fv(shader.projection, false, projection);
gl.uniform1f(shader.time, time);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, unitSquareVertices.numItems);
}
function incrementSimulation(){ time += timeInc; }
return webGLStart;
})();