-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebGLUtils.js
129 lines (99 loc) · 3.12 KB
/
WebGLUtils.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
(function () {
var gl;
function initContext(canvas) {
try {
gl = canvas.getContext("experimental-webgl");
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
} catch(e) {
}
if (!gl) {
alert("Get a Webkit nightly build from here http://nightly.webkit.org/");
}
}
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;
}
var program;
function initShaders() {
var fragmentShader = getShader(gl, "shader-fs");
var vertexShader = getShader(gl, "shader-vs");
program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
alert("Could not initialise shaders");
}
gl.useProgram(program);
var props = {
'viewMatrix': gl.getUniformLocation(program, 'viewMatrix'),
'projectionMatrix': gl.getUniformLocation(program, 'projectionMatrix'),
'normalMatrix': gl.getUniformLocation(program, 'normalMatrix'),
'color': gl.getUniformLocation(program, 'color'),
'enableLighting': gl.getUniformLocation(program, 'enableLighting'),
'ambientColor': gl.getUniformLocation(program, 'ambientColor'),
'directionalColor': gl.getUniformLocation(program, 'directionalColor'),
'lightingDirection': gl.getUniformLocation(program, 'lightingDirection'),
'position': gl.getAttribLocation(program, 'position'),
'normal': gl.getAttribLocation(program, 'normal'),
};
for (var p in props) {
program[p] = props[p];
}
gl.enableVertexAttribArray(program.position);
gl.enableVertexAttribArray(program.normal);
}
var vertexBuffer, indexBuffer, normalBuffer;
function initBuffers() {
vertexBuffer = gl.createBuffer();
vertexBuffer.itemSize = 3;
indexBuffer = gl.createBuffer();
normalBuffer = gl.createBuffer();
normalBuffer.itemSize = 3;
}
this.initWebGL = function() {
var canvas = document.getElementById("viz");
initContext(canvas);
initShaders();
initBuffers();
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clearDepth(1.0);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
gl.enable(gl.BLEND);
return {
ctx: gl,
indexBuffer: indexBuffer,
normalBuffer: normalBuffer,
vertexBuffer: vertexBuffer,
program: program
};
}
})();