Skip to content
Snippets Groups Projects
Commit 08efe0b9 authored by julien.dasilva's avatar julien.dasilva
Browse files

modif normals lab3

parent b71bb1dc
No related branches found
No related tags found
No related merge requests found
...@@ -158,6 +158,31 @@ function getStorageLocation(gl, name) { ...@@ -158,6 +158,31 @@ function getStorageLocation(gl, name) {
return storageLocation return storageLocation
} }
function subtractArrays(array1, array2) {
result = []
for (let i = 0; i < array1.length; i++){
result.push(array1[i] - array2[i]);
}
return result;
}
function calcNormal(vertices, normals, indices) {
for (let i = 0; i < indices.length; i += 3) {
let A = [vertices[indices[i] * 3], vertices[indices[i] * 3 + 1], vertices[indices[i] * 3 + 2]];
let B = [vertices[indices[i + 1] * 3], vertices[indices[i + 1] * 3 + 1], vertices[indices[i + 1] * 3 + 2]];
let C = [vertices[indices[i + 2] * 3], vertices[indices[i + 2] * 3 + 1], vertices[indices[i + 2] * 3 + 2]];
let U = subtractArrays(B, A);
let V = subtractArrays(C, A);
for (let j = 0; j < 3; j++) {
normals[indices[i + j] * 3] = (U[1] * V[2] - U[2] * V[1]);
normals[indices[i + j] * 3 + 1] = (U[2] * V[0] - U[0] * V[2]);
normals[indices[i + j] * 3 + 2] = (U[0] * V[1] - U[1] * V[0]);
}
}
}
function initVertexBuffers(gl) { function initVertexBuffers(gl) {
// This is the model // This is the model
let vertices = new Float32Array([ let vertices = new Float32Array([
...@@ -258,79 +283,81 @@ function initVertexBuffers(gl) { ...@@ -258,79 +283,81 @@ function initVertexBuffers(gl) {
let normals = new Float32Array([ let normals = new Float32Array([
//Grande base //Grande base
0.0, -1.0, 0.0, 0.0, 0.0, 0.0,
0.0, -1.0, 0.0, 0.0, 0.0, 0.0,
0.0, -1.0, 0.0, 0.0, 0.0, 0.0,
0.0, -1.0, 0.0, 0.0, 0.0, 0.0,
//Petite base //Petite base
0.0, 1.0, 0.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 0.0, 0.0,
//Face arrière //Face arrière
0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
//Face gauche //Face gauche
-1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
-1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
-1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
-1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
-1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
//Face droite //Face droite
1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
//Face avant //Face avant
0.0, 0.0, -1.0, 0.0, 0.0, 0.0,
0.0, 0.0, -1.0, 0.0, 0.0, 0.0,
0.0, 0.0, -1.0, 0.0, 0.0, 0.0,
0.0, 0.0, -1.0, 0.0, 0.0, 0.0,
0.0, 0.0, -1.0, 0.0, 0.0, 0.0,
//Plain //Plain
0.0, 1.0, 0.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0 0.0, 0.0, 0.0
]); ]);
let indices = new Uint8Array([ let indices = new Uint8Array([
0, 1, 2, //Grande base 0, 2, 1, //Grande base
1, 2, 3, 2, 3, 1,
4, 5, 6, //Petite base 4, 5, 6, //Petite base
5, 6, 7, 5, 7, 6,
8, 9, 10, //Face arrière 8, 10, 9, //Face arrière
9, 10, 11, 10, 11, 9,
10, 11, 12, 10, 12, 11,
13, 14, 15, //Face gauche 13, 14, 15, //Face gauche
14, 15, 16, 14, 16, 15,
15, 16, 17, 16, 17, 15,
18, 19, 20, //Face droite 18, 19, 20, //Face droite
19, 20, 21, 19, 21, 20,
20, 21, 22, 21, 22, 20,
23, 24, 25, //Face avant 23, 24, 25, //Face avant
24, 25, 26, 24, 26, 25,
25, 26, 27, 26, 27, 25,
28, 29, 30, //Plain 28, 29, 30, //Plain
29, 30, 31 29, 31, 30
]); ]);
calcNormal(vertices, normals, indices);
// Create a buffer object // Create a buffer object
const vertexBuffer = gl.createBuffer(); const vertexBuffer = gl.createBuffer();
if (!vertexBuffer) { if (!vertexBuffer) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment