-
Notifications
You must be signed in to change notification settings - Fork 0
/
glt.js
216 lines (171 loc) · 6.44 KB
/
glt.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* Created with JetBrains WebStorm.
* User: admin
* Date: 27/08/2013
* Time: 19:00
* To change this template use File | Settings | File Templates.
*/
(function(){
"use strict";
function Mesh(gl, program, vertices, vComponents, indices, texCoordinates, drawMode) {
this.gl = gl;
this.program = program;
this.drawMode = drawMode || gl.TRIANGLES;
this.vertexBuffer = createArrayBuffer(gl, vertices, vComponents);
this.textureCoordBuffer = createArrayBuffer(gl, texCoordinates, 2);
this.indexBuffer = createElementArrayBuffer(gl, indices, 1)
}
Mesh.prototype = {
draw : function() {
var gl = this.gl;
var program = this.program;
gl.useProgram(program);
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.vertexAttribPointer(program.vertexPosAttrib, this.vertexBuffer.numComponents, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, this.textureCoordBuffer);
gl.vertexAttribPointer(program.textureCoordAttrib, this.textureCoordBuffer.numComponents, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
gl.drawElements(this.drawMode, this.indexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
}
}
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)) {
throw new Error('Shader Compilation Error: ' + gl.getShaderInfoLog(shader) );
return null;
}
return shader;
}
function createProgram(gl, shaders) {
var p = gl.createProgram();
gl.attachShader(p, shaders[0]);
gl.attachShader(p, shaders[1]);
gl.linkProgram(p);
if (!gl.getProgramParameter(p, gl.LINK_STATUS)) {
throw new Error('Shader Program Link Error: ' + gl.getProgramInfoLog(p));
return null;
}
return p;
}
function getGL(canvas, options) {
try {
gl = canvas.getContext("experimental-webgl", options);
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
return gl
} catch (e) {
}
if (!gl) {
throw new Error("Could not initialise WebGL");
}
}
function screenQuad(gl) {
var vertexPosBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBuffer);
var vertices = [ -1.0,-1.0,
1.0,-1.0,
1.0,1.0,
-1.0,1.0];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
vertexPosBuffer.numComponents = 2;
vertexPosBuffer.numItems = 4;
return vertexPosBuffer;
}
function createArrayBuffer(gl, vertices, components) {
var buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
buffer.numComponents = components;
buffer.numItems = vertices.length / components;
return buffer;
}
function createElementArrayBuffer(gl, indices, components) {
var buffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
buffer.numComponents = components;
buffer.numItems = indices.length / components;
return buffer;
}
function createTexture(gl, flipY) {
flipY = flipY || false;
var t = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, t);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.bindTexture(gl.TEXTURE_2D, null);
return t;
}
function createStateTexture(gl) {
var t = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, t);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
gl.bindTexture(gl.TEXTURE_2D, null);
return t;
}
function raf() {
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
}
window.glt = {
Mesh : Mesh,
compileShader : function(gl, shader) {
return getShader(gl, shader);
},
createTexture : function(gl) {
return createTexture(gl);
},
createStateTexture : function(gl) {
return createStateTexture(gl);
},
createProgram : function(gl, shaders) {
return createProgram(gl, shaders);
},
getGL : function(canvas, options) {
return getGL(canvas);
},
screenQuad : function(gl) {
return screenQuad(gl);
},
createArrayBuffer : function(gl, vertices, components) {
return createArrayBuffer(gl, vertices, components);
},
raf : function() {
raf();
}
}
})()