diff --git a/lab3/src/lab3.js b/lab3/src/lab3.js index 78b0fc35b4898ec2e4aff5dd99716f67e5eb5a7f..173b18566dcad8831552430d38dcbb85f6625731 100644 --- a/lab3/src/lab3.js +++ b/lab3/src/lab3.js @@ -158,6 +158,31 @@ function getStorageLocation(gl, name) { 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) { // This is the model let vertices = new Float32Array([ @@ -258,79 +283,81 @@ function initVertexBuffers(gl) { let normals = new Float32Array([ //Grande base - 0.0, -1.0, 0.0, - 0.0, -1.0, 0.0, - 0.0, -1.0, 0.0, - 0.0, -1.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, //Petite base - 0.0, 1.0, 0.0, - 0.0, 1.0, 0.0, - 0.0, 1.0, 0.0, - 0.0, 1.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, //Face arrière - 0.0, 0.0, 1.0, - 0.0, 0.0, 1.0, - 0.0, 0.0, 1.0, - 0.0, 0.0, 1.0, - 0.0, 0.0, 1.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, //Face gauche - -1.0, 0.0, 0.0, - -1.0, 0.0, 0.0, - -1.0, 0.0, 0.0, - -1.0, 0.0, 0.0, - -1.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, //Face droite - 1.0, 0.0, 0.0, - 1.0, 0.0, 0.0, - 1.0, 0.0, 0.0, - 1.0, 0.0, 0.0, - 1.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, //Face avant - 0.0, 0.0, -1.0, - 0.0, 0.0, -1.0, - 0.0, 0.0, -1.0, - 0.0, 0.0, -1.0, - 0.0, 0.0, -1.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, //Plain - 0.0, 1.0, 0.0, - 0.0, 1.0, 0.0, - 0.0, 1.0, 0.0, - 0.0, 1.0, 0.0 + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0 ]); let indices = new Uint8Array([ - 0, 1, 2, //Grande base - 1, 2, 3, + 0, 2, 1, //Grande base + 2, 3, 1, 4, 5, 6, //Petite base - 5, 6, 7, + 5, 7, 6, - 8, 9, 10, //Face arrière - 9, 10, 11, - 10, 11, 12, + 8, 10, 9, //Face arrière + 10, 11, 9, + 10, 12, 11, 13, 14, 15, //Face gauche - 14, 15, 16, - 15, 16, 17, + 14, 16, 15, + 16, 17, 15, 18, 19, 20, //Face droite - 19, 20, 21, - 20, 21, 22, - + 19, 21, 20, + 21, 22, 20, + 23, 24, 25, //Face avant - 24, 25, 26, - 25, 26, 27, + 24, 26, 25, + 26, 27, 25, 28, 29, 30, //Plain - 29, 30, 31 + 29, 31, 30 ]); + calcNormal(vertices, normals, indices); + // Create a buffer object const vertexBuffer = gl.createBuffer(); if (!vertexBuffer) {