-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl-utils.js
171 lines (141 loc) · 5.03 KB
/
gl-utils.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
// mostly cribbed from the unimpeachable https://github.com/magcius
function fetchFile(path) {
var request = new XMLHttpRequest();
request.open('GET', path, false);
request.overrideMimeType('text/plain');
request.send();
return request.responseText;
}
function compileShader(gl, str, type) {
var shader = gl.createShader(type);
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.error(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
function loadObjFile(gl, path) {
var objData = fetchFile(path);
var lines = objData.split('\n');
var verts = [0.0, 0.0, 0.0]; // OBJ files are 1 indexed for some reason..?
var indxs = [];
lines.forEach(function (line, i) {
if (line.length === 0) return;
var tokens = line.split(' ');
switch (tokens[0]) {
case '#': // ignore comments
break;
case 'v':
verts.push(parseFloat(tokens[1]));
verts.push(parseFloat(tokens[2]));
verts.push(parseFloat(tokens[3]));
break;
case 'f':
indxs.push(parseInt(tokens[1]));
indxs.push(parseInt(tokens[2]));
indxs.push(parseInt(tokens[3]));
break;
default:
throw new Error('wtf is ' + tokens[0]);
}
});
var model = {
verts: {
buffer: gl.createBuffer(),
length: verts.length,
size: verts.length * Float32Array.BYTES_PER_ELEMENT,
},
indxs: {
buffer: gl.createBuffer(),
length: indxs.length,
size: indxs.length * Uint16Array.BYTES_PER_ELEMENT,
},
norms: {
buffer: gl.createBuffer(),
length: verts.length,
size: verts.length * Float32Array.BYTES_PER_ELEMENT,
}
};
var norms = calcNorms(verts, indxs);
gl.bindBuffer(gl.ARRAY_BUFFER, model.norms.buffer);
gl.bufferData(gl.ARRAY_BUFFER, Float32Array.from(norms), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, model.verts.buffer);
gl.bufferData(gl.ARRAY_BUFFER, Float32Array.from(verts), gl.STATIC_DRAW);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, model.indxs.buffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, Uint16Array.from(indxs), gl.STATIC_DRAW);
return model;
}
function calcNorms(verts, indxs) {
var vec3norms = [], vec3verts = [];
verts.forEach(function (vert, i) {
vec3norms[i] = vec3.create();
var v1 = verts[i*3+0];
var v2 = verts[i*3+1];
var v3 = verts[i*3+2];
vec3verts[i] = vec3.fromValues(v1, v2, v3);
});
for (var i=0; i<indxs.length; i+=3) {
var a_i = indxs[i+0];
var b_i = indxs[i+1];
var c_i = indxs[i+2];
var a = vec3verts[a_i];
var b = vec3verts[b_i];
var c = vec3verts[c_i];
var e1 = vec3.create();
var e2 = vec3.create();
var norm = vec3.create();
vec3.sub(e1, b, a);
vec3.sub(e2, c, a);
vec3.cross(norm, e1, e2);
vec3.add(vec3norms[a_i], vec3norms[a_i], norm);
vec3.add(vec3norms[b_i], vec3norms[b_i], norm);
vec3.add(vec3norms[c_i], vec3norms[c_i], norm);
}
var norms = [];
vec3norms.forEach(function (vec3norm) {
var normalized = vec3.normalize(vec3norm, vec3norm);
norms.push(normalized[0]);
norms.push(normalized[1]);
norms.push(normalized[2]);
});
return norms;
}
function createProgram(gl, vertFile, fragFile) {
var vert = fetchFile(vertFile);
var frag = fetchFile(fragFile);
var vertShader = compileShader(gl, vert, gl.VERTEX_SHADER);
var fragShader = compileShader(gl, frag, gl.FRAGMENT_SHADER);
var prog = gl.createProgram();
gl.attachShader(prog, vertShader);
gl.attachShader(prog, fragShader);
gl.linkProgram(prog);
if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) {
console.error(gl.getProgramInfoLog(prog));
return null;
}
return prog;
}
function loadImage(gl, src) {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var texId = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texId);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_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);
var img = document.createElement('img');
img.src = src;
img.onload = function() {
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var data = Uint8Array.from(imgData.data);
gl.bindTexture(gl.TEXTURE_2D, texId);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, imgData.width, imgData.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
};
return texId;
}