-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
294 lines (247 loc) · 9.04 KB
/
utils.js
File metadata and controls
294 lines (247 loc) · 9.04 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//Raccoglie diverse funzioni come il caricamento di una texture da un'immqgine,
// la crezione di una texture da applicarea allo skybox,
// e tutto ciò che riguarda il caricamento di una mesh da un Obj
function degToRad(d) {
return d * Math.PI / 180;
}
function radToDeg(r) {
return r * 180 / Math.PI;
}
function isPowerOf2(value) {
return (value & (value - 1)) == 0;
}
//Funzione per creare la texture dello skybox
function loadSkyboxTexture() {
const texture = gl.createTexture()
gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture)
/* const faceInfos = [{target: gl.TEXTURE_CUBE_MAP_POSITIVE_X, url: 'resources/images/prova.jpg',},
{target: gl.TEXTURE_CUBE_MAP_NEGATIVE_X, url: 'resources/images/prova.jpg',},
{target: gl.TEXTURE_CUBE_MAP_POSITIVE_Y, url: 'resources/images/prova.jpg',},
{target: gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, url: 'resources/images/prova.jpg',},
{target: gl.TEXTURE_CUBE_MAP_POSITIVE_Z, url: 'resources/images/prova.jpg',},
{target: gl.TEXTURE_CUBE_MAP_NEGATIVE_Z, url: 'resources/images/prova.jpg',},
];*/
const faceInfos = [{target: gl.TEXTURE_CUBE_MAP_POSITIVE_X, url: 'resources/images/matrix.jpg',},
{target: gl.TEXTURE_CUBE_MAP_NEGATIVE_X, url: 'resources/images/matrix.jpg',},
{target: gl.TEXTURE_CUBE_MAP_POSITIVE_Y, url: 'resources/images/matrix.jpg',},
{target: gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, url: 'resources/images/matrix.jpg',},
{target: gl.TEXTURE_CUBE_MAP_POSITIVE_Z, url: 'resources/images/matrix.jpg',},
{target: gl.TEXTURE_CUBE_MAP_NEGATIVE_Z, url: 'resources/images/matrix.jpg',},
];
faceInfos.forEach((faceInfo) => {
const {target, url} = faceInfo;
// Upload the canvas to the cubemap face.
const level = 0;
const internalFormat = gl.RGBA;
const width = 1024;
const height = 1024;
const format = gl.RGBA;
const type = gl.UNSIGNED_BYTE;
// setup each face so it's immediately renderable
gl.texImage2D(target, level, internalFormat, width, height, 0, format, type, null);
// Asynchronously load an image
const image = new Image();
image.src = url;
image.addEventListener('load', function() {
// Now that the image has loaded make copy it to the texture.
gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
gl.texImage2D(target, level, internalFormat, format, type, image);
gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
});
});
gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
return texture
}
//Funzone per caricare una texture
function loadTextureFromImg(imageSrc) {
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
// Fill the texture with a 1x1 blue pixel.
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE,
new Uint8Array([0, 0, 255, 255]));
// Asynchronously load an image
var textureImage = new Image();
textureImage.src = imageSrc;
textureImage.addEventListener('load', function() {
// Now that the image has loaded make copy it to the texture.
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA,gl.UNSIGNED_BYTE, textureImage);
// Check if the image is a power of 2 in both dimensions.
if (isPowerOf2(textureImage.width) && isPowerOf2(textureImage.height)) {
// Yes, it's a power of 2. Generate mips.
gl.generateMipmap(gl.TEXTURE_2D);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST );
} else {
// No, it's not a power of 2. Turn off mips and set wrapping to clamp to edge
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); //tell WebGL to not repeat the texture in S direction
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); //tell WebGL to not repeat the texture in T direction
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
}
});
return texture;
}
var depthFramebuffer, depthTextureSize, depthTexture, unusedTexture;
//funzione per creare depth framebuffer
function createTextureLight(){
depthTexture = gl.createTexture();
depthTextureSize = 1024;
gl.bindTexture(gl.TEXTURE_2D, depthTexture);
gl.texImage2D(
gl.TEXTURE_2D, // target
0, // mip level
gl.DEPTH_COMPONENT, // internal format
depthTextureSize, // width
depthTextureSize, // height
0, // border
gl.DEPTH_COMPONENT, // format
gl.UNSIGNED_INT, // type
null); // data
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.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
depthFramebuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, depthFramebuffer);
gl.framebufferTexture2D(
gl.FRAMEBUFFER, // target
gl.DEPTH_ATTACHMENT, // attachment point
gl.TEXTURE_2D, // texture target
depthTexture, // texture
0); // mip level
// --------------------------------------------------
// UNUSED TEXTURE
// create a color texture of the same size as the depth texture
// see article why this is needed_
unusedTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, unusedTexture);
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
depthTextureSize,
depthTextureSize,
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
null,
);
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.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
// attach it to the framebuffer
gl.framebufferTexture2D(
gl.FRAMEBUFFER, // target
gl.COLOR_ATTACHMENT0, // attachment point
gl.TEXTURE_2D, // texture target
unusedTexture, // texture
0); // mip level
}
//*********************************************************************************************************************
// MESH.OBJ
var webglVertexData = [
[], // positions
[], // texcoords
[], // normals
];
function getObjToDraw(objsToDraw, name){
return objsToDraw.find(x => x.name === name);
}
function loadObj(url) {
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4) {
parseOBJ(xhttp.responseText);
}
};
xhttp.open("GET", url, false);
xhttp.send(null);
}
function parseOBJ(text) {
webglVertexData = [
[], // positions
[], // texcoords
[], // normals
];
const objPositions = [[0, 0, 0]];
const objTexcoords = [[0, 0]];
const objNormals = [[0, 0, 0]];
const objVertexData = [
objPositions,
objTexcoords,
objNormals,
];
// same order as `f` indices
//f 1/2/3 -> 1 2 3
function addVertex(vert) {
const ptn = vert.split('/');
ptn.forEach((objIndexStr, i) => {
if (!objIndexStr) {
return;
}
const objIndex = parseInt(objIndexStr);
const index = objIndex + (objIndex >= 0 ? 0 : objVertexData[i].length);
//webglVertexData pubblica
//console.log(i);
webglVertexData[i].push(...objVertexData[i][index]);
});
}
const keywords = {
v(parts) {
objPositions.push(parts.map(parseFloat));
},
vn(parts) {
objNormals.push(parts.map(parseFloat));
},
vt(parts) {
// should check for missing v and extra w?
objTexcoords.push(parts.map(parseFloat));
},
f(parts) {
const numTriangles = parts.length - 2;
for (let tri = 0; tri < numTriangles; ++tri) {
addVertex(parts[0]);
addVertex(parts[tri + 1]);
addVertex(parts[tri + 2]);
}
},
};
// \w* = almeno una lettere o un numero
// ?:x = meccia gli spazi singoli bianchi (anche più di uno)
// . = classi di caratteri, meccia ogni singolo carattere tranne i terminatori di linea
const keywordRE = /(\w*)(?: )*(.*)/;
const lines = text.split('\n');
//let identifica una variabile in un determinato blocco di codice
for (let lineNo = 0; lineNo < lines.length; ++lineNo) {
const line = lines[lineNo].trim();
if (line === '' || line.startsWith('#')) {
//la riga è vuota o è un commento
continue;
}
//ritorna la stringa
const m = keywordRE.exec(line);
//console.log(m);
if (!m) {
continue;
}
const [, keyword, unparsedArgs] = m;
const parts = line.split(/\s+/).slice(1);
const handler = keywords[keyword];
//console.log(parts);
if (!handler) {
//console.warn('unhandled keyword:', keyword, 'at line', lineNo + 1);
continue;
}
handler(parts, unparsedArgs); //gestisce gli argomenti che non hai gestito
}
/*
var arrayProva = [];
for (let n = 0; n < objPositions.length; n++){
arrayProva.push(objPositions[n][2]);
}
//console.log(getMaxOfArray(arrayProva));
//console.log(getMinOfArray(arrayProva));
*/
}