From f5a466774e2378af862c859ea7445e701a835848 Mon Sep 17 00:00:00 2001
From: "ottavio.buonomo" <ottavio.buonomo@etu.hesge.ch>
Date: Thu, 17 Dec 2020 21:06:56 +0100
Subject: [PATCH] Lab4

---
 lab4/lib/cuon-matrix.js  | 741 +++++++++++++++++++++++++++++++++++++++
 lab4/src/lab4.html       |  17 +-
 lab4/src/lab4.js         | 601 ++++++++++++++++++++++++++++++-
 lab4/src/pyramidModel.js |  90 +++++
 4 files changed, 1433 insertions(+), 16 deletions(-)
 create mode 100644 lab4/lib/cuon-matrix.js
 create mode 100644 lab4/src/pyramidModel.js

diff --git a/lab4/lib/cuon-matrix.js b/lab4/lib/cuon-matrix.js
new file mode 100644
index 0000000..b67a5dd
--- /dev/null
+++ b/lab4/lib/cuon-matrix.js
@@ -0,0 +1,741 @@
+// cuon-matrix.js (c) 2012 kanda and matsuda
+/** 
+ * This is a class treating 4x4 matrix.
+ * This class contains the function that is equivalent to OpenGL matrix stack.
+ * The matrix after conversion is calculated by multiplying a conversion matrix from the right.
+ * The matrix is replaced by the calculated result.
+ */
+
+/**
+ * Constructor of Matrix4
+ * If opt_src is specified, new matrix is initialized by opt_src.
+ * Otherwise, new matrix is initialized by identity matrix.
+ * @param opt_src source matrix(option)
+ */
+var Matrix4 = function(opt_src) {
+  var i, s, d;
+  if (opt_src && typeof opt_src === 'object' && opt_src.hasOwnProperty('elements')) {
+    s = opt_src.elements;
+    d = new Float32Array(16);
+    for (i = 0; i < 16; ++i) {
+      d[i] = s[i];
+    }
+    this.elements = d;
+  } else {
+    this.elements = new Float32Array([1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1]);
+  }
+};
+
+/**
+ * Set the identity matrix.
+ * @return this
+ */
+Matrix4.prototype.setIdentity = function() {
+  var e = this.elements;
+  e[0] = 1;   e[4] = 0;   e[8]  = 0;   e[12] = 0;
+  e[1] = 0;   e[5] = 1;   e[9]  = 0;   e[13] = 0;
+  e[2] = 0;   e[6] = 0;   e[10] = 1;   e[14] = 0;
+  e[3] = 0;   e[7] = 0;   e[11] = 0;   e[15] = 1;
+  return this;
+};
+
+/**
+ * Copy matrix.
+ * @param src source matrix
+ * @return this
+ */
+Matrix4.prototype.set = function(src) {
+  var i, s, d;
+
+  s = src.elements;
+  d = this.elements;
+
+  if (s === d) {
+    return;
+  }
+    
+  for (i = 0; i < 16; ++i) {
+    d[i] = s[i];
+  }
+
+  return this;
+};
+
+/**
+ * Multiply the matrix from the right.
+ * @param other The multiply matrix
+ * @return this
+ */
+Matrix4.prototype.concat = function(other) {
+  var i, e, a, b, ai0, ai1, ai2, ai3;
+  
+  // Calculate e = a * b
+  e = this.elements;
+  a = this.elements;
+  b = other.elements;
+  
+  // If e equals b, copy b to temporary matrix.
+  if (e === b) {
+    b = new Float32Array(16);
+    for (i = 0; i < 16; ++i) {
+      b[i] = e[i];
+    }
+  }
+  
+  for (i = 0; i < 4; i++) {
+    ai0=a[i];  ai1=a[i+4];  ai2=a[i+8];  ai3=a[i+12];
+    e[i]    = ai0 * b[0]  + ai1 * b[1]  + ai2 * b[2]  + ai3 * b[3];
+    e[i+4]  = ai0 * b[4]  + ai1 * b[5]  + ai2 * b[6]  + ai3 * b[7];
+    e[i+8]  = ai0 * b[8]  + ai1 * b[9]  + ai2 * b[10] + ai3 * b[11];
+    e[i+12] = ai0 * b[12] + ai1 * b[13] + ai2 * b[14] + ai3 * b[15];
+  }
+  
+  return this;
+};
+Matrix4.prototype.multiply = Matrix4.prototype.concat;
+
+/**
+ * Multiply the three-dimensional vector.
+ * @param pos  The multiply vector
+ * @return The result of multiplication(Float32Array)
+ */
+Matrix4.prototype.multiplyVector3 = function(pos) {
+  var e = this.elements;
+  var p = pos.elements;
+  var v = new Vector3();
+  var result = v.elements;
+
+  result[0] = p[0] * e[0] + p[1] * e[4] + p[2] * e[ 8] + e[12];
+  result[1] = p[0] * e[1] + p[1] * e[5] + p[2] * e[ 9] + e[13];
+  result[2] = p[0] * e[2] + p[1] * e[6] + p[2] * e[10] + e[14];
+
+  return v;
+};
+
+/**
+ * Multiply the four-dimensional vector.
+ * @param pos  The multiply vector
+ * @return The result of multiplication(Float32Array)
+ */
+Matrix4.prototype.multiplyVector4 = function(pos) {
+  var e = this.elements;
+  var p = pos.elements;
+  var v = new Vector4();
+  var result = v.elements;
+
+  result[0] = p[0] * e[0] + p[1] * e[4] + p[2] * e[ 8] + p[3] * e[12];
+  result[1] = p[0] * e[1] + p[1] * e[5] + p[2] * e[ 9] + p[3] * e[13];
+  result[2] = p[0] * e[2] + p[1] * e[6] + p[2] * e[10] + p[3] * e[14];
+  result[3] = p[0] * e[3] + p[1] * e[7] + p[2] * e[11] + p[3] * e[15];
+
+  return v;
+};
+
+/**
+ * Transpose the matrix.
+ * @return this
+ */
+Matrix4.prototype.transpose = function() {
+  var e, t;
+
+  e = this.elements;
+
+  t = e[ 1];  e[ 1] = e[ 4];  e[ 4] = t;
+  t = e[ 2];  e[ 2] = e[ 8];  e[ 8] = t;
+  t = e[ 3];  e[ 3] = e[12];  e[12] = t;
+  t = e[ 6];  e[ 6] = e[ 9];  e[ 9] = t;
+  t = e[ 7];  e[ 7] = e[13];  e[13] = t;
+  t = e[11];  e[11] = e[14];  e[14] = t;
+
+  return this;
+};
+
+/**
+ * Calculate the inverse matrix of specified matrix, and set to this.
+ * @param other The source matrix
+ * @return this
+ */
+Matrix4.prototype.setInverseOf = function(other) {
+  var i, s, d, inv, det;
+
+  s = other.elements;
+  d = this.elements;
+  inv = new Float32Array(16);
+
+  inv[0]  =   s[5]*s[10]*s[15] - s[5] *s[11]*s[14] - s[9] *s[6]*s[15]
+            + s[9]*s[7] *s[14] + s[13]*s[6] *s[11] - s[13]*s[7]*s[10];
+  inv[4]  = - s[4]*s[10]*s[15] + s[4] *s[11]*s[14] + s[8] *s[6]*s[15]
+            - s[8]*s[7] *s[14] - s[12]*s[6] *s[11] + s[12]*s[7]*s[10];
+  inv[8]  =   s[4]*s[9] *s[15] - s[4] *s[11]*s[13] - s[8] *s[5]*s[15]
+            + s[8]*s[7] *s[13] + s[12]*s[5] *s[11] - s[12]*s[7]*s[9];
+  inv[12] = - s[4]*s[9] *s[14] + s[4] *s[10]*s[13] + s[8] *s[5]*s[14]
+            - s[8]*s[6] *s[13] - s[12]*s[5] *s[10] + s[12]*s[6]*s[9];
+
+  inv[1]  = - s[1]*s[10]*s[15] + s[1] *s[11]*s[14] + s[9] *s[2]*s[15]
+            - s[9]*s[3] *s[14] - s[13]*s[2] *s[11] + s[13]*s[3]*s[10];
+  inv[5]  =   s[0]*s[10]*s[15] - s[0] *s[11]*s[14] - s[8] *s[2]*s[15]
+            + s[8]*s[3] *s[14] + s[12]*s[2] *s[11] - s[12]*s[3]*s[10];
+  inv[9]  = - s[0]*s[9] *s[15] + s[0] *s[11]*s[13] + s[8] *s[1]*s[15]
+            - s[8]*s[3] *s[13] - s[12]*s[1] *s[11] + s[12]*s[3]*s[9];
+  inv[13] =   s[0]*s[9] *s[14] - s[0] *s[10]*s[13] - s[8] *s[1]*s[14]
+            + s[8]*s[2] *s[13] + s[12]*s[1] *s[10] - s[12]*s[2]*s[9];
+
+  inv[2]  =   s[1]*s[6]*s[15] - s[1] *s[7]*s[14] - s[5] *s[2]*s[15]
+            + s[5]*s[3]*s[14] + s[13]*s[2]*s[7]  - s[13]*s[3]*s[6];
+  inv[6]  = - s[0]*s[6]*s[15] + s[0] *s[7]*s[14] + s[4] *s[2]*s[15]
+            - s[4]*s[3]*s[14] - s[12]*s[2]*s[7]  + s[12]*s[3]*s[6];
+  inv[10] =   s[0]*s[5]*s[15] - s[0] *s[7]*s[13] - s[4] *s[1]*s[15]
+            + s[4]*s[3]*s[13] + s[12]*s[1]*s[7]  - s[12]*s[3]*s[5];
+  inv[14] = - s[0]*s[5]*s[14] + s[0] *s[6]*s[13] + s[4] *s[1]*s[14]
+            - s[4]*s[2]*s[13] - s[12]*s[1]*s[6]  + s[12]*s[2]*s[5];
+
+  inv[3]  = - s[1]*s[6]*s[11] + s[1]*s[7]*s[10] + s[5]*s[2]*s[11]
+            - s[5]*s[3]*s[10] - s[9]*s[2]*s[7]  + s[9]*s[3]*s[6];
+  inv[7]  =   s[0]*s[6]*s[11] - s[0]*s[7]*s[10] - s[4]*s[2]*s[11]
+            + s[4]*s[3]*s[10] + s[8]*s[2]*s[7]  - s[8]*s[3]*s[6];
+  inv[11] = - s[0]*s[5]*s[11] + s[0]*s[7]*s[9]  + s[4]*s[1]*s[11]
+            - s[4]*s[3]*s[9]  - s[8]*s[1]*s[7]  + s[8]*s[3]*s[5];
+  inv[15] =   s[0]*s[5]*s[10] - s[0]*s[6]*s[9]  - s[4]*s[1]*s[10]
+            + s[4]*s[2]*s[9]  + s[8]*s[1]*s[6]  - s[8]*s[2]*s[5];
+
+  det = s[0]*inv[0] + s[1]*inv[4] + s[2]*inv[8] + s[3]*inv[12];
+  if (det === 0) {
+    return this;
+  }
+
+  det = 1 / det;
+  for (i = 0; i < 16; i++) {
+    d[i] = inv[i] * det;
+  }
+
+  return this;
+};
+
+/**
+ * Calculate the inverse matrix of this, and set to this.
+ * @return this
+ */
+Matrix4.prototype.invert = function() {
+  return this.setInverseOf(this);
+};
+
+/**
+ * Set the orthographic projection matrix.
+ * @param left The coordinate of the left of clipping plane.
+ * @param right The coordinate of the right of clipping plane.
+ * @param bottom The coordinate of the bottom of clipping plane.
+ * @param top The coordinate of the top top clipping plane.
+ * @param near The distances to the nearer depth clipping plane. This value is minus if the plane is to be behind the viewer.
+ * @param far The distances to the farther depth clipping plane. This value is minus if the plane is to be behind the viewer.
+ * @return this
+ */
+Matrix4.prototype.setOrtho = function(left, right, bottom, top, near, far) {
+  var e, rw, rh, rd;
+
+  if (left === right || bottom === top || near === far) {
+    throw 'null frustum';
+  }
+
+  rw = 1 / (right - left);
+  rh = 1 / (top - bottom);
+  rd = 1 / (far - near);
+
+  e = this.elements;
+
+  e[0]  = 2 * rw;
+  e[1]  = 0;
+  e[2]  = 0;
+  e[3]  = 0;
+
+  e[4]  = 0;
+  e[5]  = 2 * rh;
+  e[6]  = 0;
+  e[7]  = 0;
+
+  e[8]  = 0;
+  e[9]  = 0;
+  e[10] = -2 * rd;
+  e[11] = 0;
+
+  e[12] = -(right + left) * rw;
+  e[13] = -(top + bottom) * rh;
+  e[14] = -(far + near) * rd;
+  e[15] = 1;
+
+  return this;
+};
+
+/**
+ * Multiply the orthographic projection matrix from the right.
+ * @param left The coordinate of the left of clipping plane.
+ * @param right The coordinate of the right of clipping plane.
+ * @param bottom The coordinate of the bottom of clipping plane.
+ * @param top The coordinate of the top top clipping plane.
+ * @param near The distances to the nearer depth clipping plane. This value is minus if the plane is to be behind the viewer.
+ * @param far The distances to the farther depth clipping plane. This value is minus if the plane is to be behind the viewer.
+ * @return this
+ */
+Matrix4.prototype.ortho = function(left, right, bottom, top, near, far) {
+  return this.concat(new Matrix4().setOrtho(left, right, bottom, top, near, far));
+};
+
+/**
+ * Set the perspective projection matrix.
+ * @param left The coordinate of the left of clipping plane.
+ * @param right The coordinate of the right of clipping plane.
+ * @param bottom The coordinate of the bottom of clipping plane.
+ * @param top The coordinate of the top top clipping plane.
+ * @param near The distances to the nearer depth clipping plane. This value must be plus value.
+ * @param far The distances to the farther depth clipping plane. This value must be plus value.
+ * @return this
+ */
+Matrix4.prototype.setFrustum = function(left, right, bottom, top, near, far) {
+  var e, rw, rh, rd;
+
+  if (left === right || top === bottom || near === far) {
+    throw 'null frustum';
+  }
+  if (near <= 0) {
+    throw 'near <= 0';
+  }
+  if (far <= 0) {
+    throw 'far <= 0';
+  }
+
+  rw = 1 / (right - left);
+  rh = 1 / (top - bottom);
+  rd = 1 / (far - near);
+
+  e = this.elements;
+
+  e[ 0] = 2 * near * rw;
+  e[ 1] = 0;
+  e[ 2] = 0;
+  e[ 3] = 0;
+
+  e[ 4] = 0;
+  e[ 5] = 2 * near * rh;
+  e[ 6] = 0;
+  e[ 7] = 0;
+
+  e[ 8] = (right + left) * rw;
+  e[ 9] = (top + bottom) * rh;
+  e[10] = -(far + near) * rd;
+  e[11] = -1;
+
+  e[12] = 0;
+  e[13] = 0;
+  e[14] = -2 * near * far * rd;
+  e[15] = 0;
+
+  return this;
+};
+
+/**
+ * Multiply the perspective projection matrix from the right.
+ * @param left The coordinate of the left of clipping plane.
+ * @param right The coordinate of the right of clipping plane.
+ * @param bottom The coordinate of the bottom of clipping plane.
+ * @param top The coordinate of the top top clipping plane.
+ * @param near The distances to the nearer depth clipping plane. This value must be plus value.
+ * @param far The distances to the farther depth clipping plane. This value must be plus value.
+ * @return this
+ */
+Matrix4.prototype.frustum = function(left, right, bottom, top, near, far) {
+  return this.concat(new Matrix4().setFrustum(left, right, bottom, top, near, far));
+};
+
+/**
+ * Set the perspective projection matrix by fovy and aspect.
+ * @param fovy The angle between the upper and lower sides of the frustum.
+ * @param aspect The aspect ratio of the frustum. (width/height)
+ * @param near The distances to the nearer depth clipping plane. This value must be plus value.
+ * @param far The distances to the farther depth clipping plane. This value must be plus value.
+ * @return this
+ */
+Matrix4.prototype.setPerspective = function(fovy, aspect, near, far) {
+  var e, rd, s, ct;
+
+  if (near === far || aspect === 0) {
+    throw 'null frustum';
+  }
+  if (near <= 0) {
+    throw 'near <= 0';
+  }
+  if (far <= 0) {
+    throw 'far <= 0';
+  }
+
+  fovy = Math.PI * fovy / 180 / 2;
+  s = Math.sin(fovy);
+  if (s === 0) {
+    throw 'null frustum';
+  }
+
+  rd = 1 / (far - near);
+  ct = Math.cos(fovy) / s;
+
+  e = this.elements;
+
+  e[0]  = ct / aspect;
+  e[1]  = 0;
+  e[2]  = 0;
+  e[3]  = 0;
+
+  e[4]  = 0;
+  e[5]  = ct;
+  e[6]  = 0;
+  e[7]  = 0;
+
+  e[8]  = 0;
+  e[9]  = 0;
+  e[10] = -(far + near) * rd;
+  e[11] = -1;
+
+  e[12] = 0;
+  e[13] = 0;
+  e[14] = -2 * near * far * rd;
+  e[15] = 0;
+
+  return this;
+};
+
+/**
+ * Multiply the perspective projection matrix from the right.
+ * @param fovy The angle between the upper and lower sides of the frustum.
+ * @param aspect The aspect ratio of the frustum. (width/height)
+ * @param near The distances to the nearer depth clipping plane. This value must be plus value.
+ * @param far The distances to the farther depth clipping plane. This value must be plus value.
+ * @return this
+ */
+Matrix4.prototype.perspective = function(fovy, aspect, near, far) {
+  return this.concat(new Matrix4().setPerspective(fovy, aspect, near, far));
+};
+
+/**
+ * Set the matrix for scaling.
+ * @param x The scale factor along the X axis
+ * @param y The scale factor along the Y axis
+ * @param z The scale factor along the Z axis
+ * @return this
+ */
+Matrix4.prototype.setScale = function(x, y, z) {
+  var e = this.elements;
+  e[0] = x;  e[4] = 0;  e[8]  = 0;  e[12] = 0;
+  e[1] = 0;  e[5] = y;  e[9]  = 0;  e[13] = 0;
+  e[2] = 0;  e[6] = 0;  e[10] = z;  e[14] = 0;
+  e[3] = 0;  e[7] = 0;  e[11] = 0;  e[15] = 1;
+  return this;
+};
+
+/**
+ * Multiply the matrix for scaling from the right.
+ * @param x The scale factor along the X axis
+ * @param y The scale factor along the Y axis
+ * @param z The scale factor along the Z axis
+ * @return this
+ */
+Matrix4.prototype.scale = function(x, y, z) {
+  var e = this.elements;
+  e[0] *= x;  e[4] *= y;  e[8]  *= z;
+  e[1] *= x;  e[5] *= y;  e[9]  *= z;
+  e[2] *= x;  e[6] *= y;  e[10] *= z;
+  e[3] *= x;  e[7] *= y;  e[11] *= z;
+  return this;
+};
+
+/**
+ * Set the matrix for translation.
+ * @param x The X value of a translation.
+ * @param y The Y value of a translation.
+ * @param z The Z value of a translation.
+ * @return this
+ */
+Matrix4.prototype.setTranslate = function(x, y, z) {
+  var e = this.elements;
+  e[0] = 1;  e[4] = 0;  e[8]  = 0;  e[12] = x;
+  e[1] = 0;  e[5] = 1;  e[9]  = 0;  e[13] = y;
+  e[2] = 0;  e[6] = 0;  e[10] = 1;  e[14] = z;
+  e[3] = 0;  e[7] = 0;  e[11] = 0;  e[15] = 1;
+  return this;
+};
+
+/**
+ * Multiply the matrix for translation from the right.
+ * @param x The X value of a translation.
+ * @param y The Y value of a translation.
+ * @param z The Z value of a translation.
+ * @return this
+ */
+Matrix4.prototype.translate = function(x, y, z) {
+  var e = this.elements;
+  e[12] += e[0] * x + e[4] * y + e[8]  * z;
+  e[13] += e[1] * x + e[5] * y + e[9]  * z;
+  e[14] += e[2] * x + e[6] * y + e[10] * z;
+  e[15] += e[3] * x + e[7] * y + e[11] * z;
+  return this;
+};
+
+/**
+ * Set the matrix for rotation.
+ * The vector of rotation axis may not be normalized.
+ * @param angle The angle of rotation (degrees)
+ * @param x The X coordinate of vector of rotation axis.
+ * @param y The Y coordinate of vector of rotation axis.
+ * @param z The Z coordinate of vector of rotation axis.
+ * @return this
+ */
+Matrix4.prototype.setRotate = function(angle, x, y, z) {
+  var e, s, c, len, rlen, nc, xy, yz, zx, xs, ys, zs;
+
+  angle = Math.PI * angle / 180;
+  e = this.elements;
+
+  s = Math.sin(angle);
+  c = Math.cos(angle);
+
+  if (0 !== x && 0 === y && 0 === z) {
+    // Rotation around X axis
+    if (x < 0) {
+      s = -s;
+    }
+    e[0] = 1;  e[4] = 0;  e[ 8] = 0;  e[12] = 0;
+    e[1] = 0;  e[5] = c;  e[ 9] =-s;  e[13] = 0;
+    e[2] = 0;  e[6] = s;  e[10] = c;  e[14] = 0;
+    e[3] = 0;  e[7] = 0;  e[11] = 0;  e[15] = 1;
+  } else if (0 === x && 0 !== y && 0 === z) {
+    // Rotation around Y axis
+    if (y < 0) {
+      s = -s;
+    }
+    e[0] = c;  e[4] = 0;  e[ 8] = s;  e[12] = 0;
+    e[1] = 0;  e[5] = 1;  e[ 9] = 0;  e[13] = 0;
+    e[2] =-s;  e[6] = 0;  e[10] = c;  e[14] = 0;
+    e[3] = 0;  e[7] = 0;  e[11] = 0;  e[15] = 1;
+  } else if (0 === x && 0 === y && 0 !== z) {
+    // Rotation around Z axis
+    if (z < 0) {
+      s = -s;
+    }
+    e[0] = c;  e[4] =-s;  e[ 8] = 0;  e[12] = 0;
+    e[1] = s;  e[5] = c;  e[ 9] = 0;  e[13] = 0;
+    e[2] = 0;  e[6] = 0;  e[10] = 1;  e[14] = 0;
+    e[3] = 0;  e[7] = 0;  e[11] = 0;  e[15] = 1;
+  } else {
+    // Rotation around another axis
+    len = Math.sqrt(x*x + y*y + z*z);
+    if (len !== 1) {
+      rlen = 1 / len;
+      x *= rlen;
+      y *= rlen;
+      z *= rlen;
+    }
+    nc = 1 - c;
+    xy = x * y;
+    yz = y * z;
+    zx = z * x;
+    xs = x * s;
+    ys = y * s;
+    zs = z * s;
+
+    e[ 0] = x*x*nc +  c;
+    e[ 1] = xy *nc + zs;
+    e[ 2] = zx *nc - ys;
+    e[ 3] = 0;
+
+    e[ 4] = xy *nc - zs;
+    e[ 5] = y*y*nc +  c;
+    e[ 6] = yz *nc + xs;
+    e[ 7] = 0;
+
+    e[ 8] = zx *nc + ys;
+    e[ 9] = yz *nc - xs;
+    e[10] = z*z*nc +  c;
+    e[11] = 0;
+
+    e[12] = 0;
+    e[13] = 0;
+    e[14] = 0;
+    e[15] = 1;
+  }
+
+  return this;
+};
+
+/**
+ * Multiply the matrix for rotation from the right.
+ * The vector of rotation axis may not be normalized.
+ * @param angle The angle of rotation (degrees)
+ * @param x The X coordinate of vector of rotation axis.
+ * @param y The Y coordinate of vector of rotation axis.
+ * @param z The Z coordinate of vector of rotation axis.
+ * @return this
+ */
+Matrix4.prototype.rotate = function(angle, x, y, z) {
+  return this.concat(new Matrix4().setRotate(angle, x, y, z));
+};
+
+/**
+ * Set the viewing matrix.
+ * @param eyeX, eyeY, eyeZ The position of the eye point.
+ * @param centerX, centerY, centerZ The position of the reference point.
+ * @param upX, upY, upZ The direction of the up vector.
+ * @return this
+ */
+Matrix4.prototype.setLookAt = function(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ) {
+  var e, fx, fy, fz, rlf, sx, sy, sz, rls, ux, uy, uz;
+
+  fx = centerX - eyeX;
+  fy = centerY - eyeY;
+  fz = centerZ - eyeZ;
+
+  // Normalize f.
+  rlf = 1 / Math.sqrt(fx*fx + fy*fy + fz*fz);
+  fx *= rlf;
+  fy *= rlf;
+  fz *= rlf;
+
+  // Calculate cross product of f and up.
+  sx = fy * upZ - fz * upY;
+  sy = fz * upX - fx * upZ;
+  sz = fx * upY - fy * upX;
+
+  // Normalize s.
+  rls = 1 / Math.sqrt(sx*sx + sy*sy + sz*sz);
+  sx *= rls;
+  sy *= rls;
+  sz *= rls;
+
+  // Calculate cross product of s and f.
+  ux = sy * fz - sz * fy;
+  uy = sz * fx - sx * fz;
+  uz = sx * fy - sy * fx;
+
+  // Set to this.
+  e = this.elements;
+  e[0] = sx;
+  e[1] = ux;
+  e[2] = -fx;
+  e[3] = 0;
+
+  e[4] = sy;
+  e[5] = uy;
+  e[6] = -fy;
+  e[7] = 0;
+
+  e[8] = sz;
+  e[9] = uz;
+  e[10] = -fz;
+  e[11] = 0;
+
+  e[12] = 0;
+  e[13] = 0;
+  e[14] = 0;
+  e[15] = 1;
+
+  // Translate.
+  return this.translate(-eyeX, -eyeY, -eyeZ);
+};
+
+/**
+ * Multiply the viewing matrix from the right.
+ * @param eyeX, eyeY, eyeZ The position of the eye point.
+ * @param centerX, centerY, centerZ The position of the reference point.
+ * @param upX, upY, upZ The direction of the up vector.
+ * @return this
+ */
+Matrix4.prototype.lookAt = function(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ) {
+  return this.concat(new Matrix4().setLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ));
+};
+
+/**
+ * Multiply the matrix for project vertex to plane from the right.
+ * @param plane The array[A, B, C, D] of the equation of plane "Ax + By + Cz + D = 0".
+ * @param light The array which stored coordinates of the light. if light[3]=0, treated as parallel light.
+ * @return this
+ */
+Matrix4.prototype.dropShadow = function(plane, light) {
+  var mat = new Matrix4();
+  var e = mat.elements;
+
+  var dot = plane[0] * light[0] + plane[1] * light[1] + plane[2] * light[2] + plane[3] * light[3];
+
+  e[ 0] = dot - light[0] * plane[0];
+  e[ 1] =     - light[1] * plane[0];
+  e[ 2] =     - light[2] * plane[0];
+  e[ 3] =     - light[3] * plane[0];
+
+  e[ 4] =     - light[0] * plane[1];
+  e[ 5] = dot - light[1] * plane[1];
+  e[ 6] =     - light[2] * plane[1];
+  e[ 7] =     - light[3] * plane[1];
+
+  e[ 8] =     - light[0] * plane[2];
+  e[ 9] =     - light[1] * plane[2];
+  e[10] = dot - light[2] * plane[2];
+  e[11] =     - light[3] * plane[2];
+
+  e[12] =     - light[0] * plane[3];
+  e[13] =     - light[1] * plane[3];
+  e[14] =     - light[2] * plane[3];
+  e[15] = dot - light[3] * plane[3];
+
+  return this.concat(mat);
+}
+
+/**
+ * Multiply the matrix for project vertex to plane from the right.(Projected by parallel light.)
+ * @param normX, normY, normZ The normal vector of the plane.(Not necessary to be normalized.)
+ * @param planeX, planeY, planeZ The coordinate of arbitrary points on a plane.
+ * @param lightX, lightY, lightZ The vector of the direction of light.(Not necessary to be normalized.)
+ * @return this
+ */
+Matrix4.prototype.dropShadowDirectionally = function(normX, normY, normZ, planeX, planeY, planeZ, lightX, lightY, lightZ) {
+  var a = planeX * normX + planeY * normY + planeZ * normZ;
+  return this.dropShadow([normX, normY, normZ, -a], [lightX, lightY, lightZ, 0]);
+};
+
+/**
+ * Constructor of Vector3
+ * If opt_src is specified, new vector is initialized by opt_src.
+ * @param opt_src source vector(option)
+ */
+var Vector3 = function(opt_src) {
+  var v = new Float32Array(3);
+  if (opt_src && typeof opt_src === 'object') {
+    v[0] = opt_src[0]; v[1] = opt_src[1]; v[2] = opt_src[2];
+  } 
+  this.elements = v;
+}
+
+/**
+  * Normalize.
+  * @return this
+  */
+Vector3.prototype.normalize = function() {
+  var v = this.elements;
+  var c = v[0], d = v[1], e = v[2], g = Math.sqrt(c*c+d*d+e*e);
+  if(g){
+    if(g == 1)
+        return this;
+   } else {
+     v[0] = 0; v[1] = 0; v[2] = 0;
+     return this;
+   }
+   g = 1/g;
+   v[0] = c*g; v[1] = d*g; v[2] = e*g;
+   return this;
+};
+
+/**
+ * Constructor of Vector4
+ * If opt_src is specified, new vector is initialized by opt_src.
+ * @param opt_src source vector(option)
+ */
+var Vector4 = function(opt_src) {
+  var v = new Float32Array(4);
+  if (opt_src && typeof opt_src === 'object') {
+    v[0] = opt_src[0]; v[1] = opt_src[1]; v[2] = opt_src[2]; v[3] = opt_src[3];
+  } 
+  this.elements = v;
+}
diff --git a/lab4/src/lab4.html b/lab4/src/lab4.html
index 2bb37bc..4e1320d 100644
--- a/lab4/src/lab4.html
+++ b/lab4/src/lab4.html
@@ -5,12 +5,27 @@
   <title>Lab 4</title>
 </head>
 <body onload="main()">
-<canvas width="400" height="600" id="my-canvas">
+<canvas width="700" height="800" id="my-canvas" style="float: left;">
   Please use a browser that supports "canvas"
 </canvas>
+<div style="float: left; margin-left: 15px;">
+  <p id="control" style="color: red;">Control on : directional light</p>
+  <p id="light_x">Light X : </p>
+  <p id="light_y">Light Y : </p>
+  <p id="light_z">Light Z : </p>
+  <p id="light_x_point">Light X : </p>
+  <p id="light_y_point">Light Y : </p>
+  <p id="light_z_point">Light Z : </p>
+</div>
+<script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.8.1/gl-matrix-min.js"
+            integrity="sha512-zhHQR0/H5SEBL3Wn6yYSaTTZej12z0hVZKOv3TwCUXT1z5qeqGcXJLLrbERYRScEDDpYIJhPC1fk31gqR783iQ=="
+            crossorigin="anonymous" defer>
+    </script>
 <script src="../lib/webgl-utils.js"></script>
 <script src="../lib/webgl-debug.js"></script>
 <script src="../lib/cuon-utils.js"></script>
+<script src="../lib/cuon-matrix.js"></script>
+<script src="pyramidModel.js"></script>
 <script src="lab4.js"></script>
 </body>
 </html>
\ No newline at end of file
diff --git a/lab4/src/lab4.js b/lab4/src/lab4.js
index 105a227..1ef5d8a 100644
--- a/lab4/src/lab4.js
+++ b/lab4/src/lab4.js
@@ -1,18 +1,589 @@
-// Vertex shader program
-const VSHADER_SOURCE =
-  '\n' +
-  // TODO: Implement your vertex shader code here
-  '\n';
-
-// Fragment shader program
-const FSHADER_SOURCE =
-  '\n' +
-  // TODO: Implement your fragment shader code here
-  '\n';
+/*
+ * Projet : Lab 4
+ * Auteur : Ottavio Buonomo
+ * Date   : 04.12.2020
+ * Version : 1.0
+*/
+
+let x_coord_point = 0.0;
+let y_coord_point = 0.0;
+let z_coord_point = 1.5;
+
+let x_coord_direct = 1.0;
+let y_coord_direct = 3.0;
+let z_coord_direct = 5.0;
+
+let x_coord_cam = 0.0;
+let y_coord_cam = 6.0;
+let z_coord_cam = 20.0;
+
+let lightSelector = true
+
+let currentAngle = 0.0;
+
+var indices = new Uint8Array([
+    0, 1, 2, 0, 2, 3,    // front
+    4, 5, 6, 4, 6, 7,    // right
+    8, 9, 10, 8, 10, 11,    // up
+    12, 13, 14, 12, 14, 15,   // left
+    16, 17, 18, 16, 18, 19,   // down
+    20, 21, 22, 20, 22, 23,   // back
+]);
+
+var indicesGround = new Uint8Array([
+    0, 1, 3, 0, 2, 3,   // ground
+]);
+
+var OFFSCREEN_WIDTH = 2048, OFFSCREEN_HEIGHT = 2048;
+
+var SHADOW_VSHADER_SOURCE = 
+  `
+  attribute vec4 a_Position;
+  uniform mat4 u_MvpMatrix;
+
+  void main() {
+    gl_Position = u_MvpMatrix * a_Position;
+  }`
+
+var SHADOW_FSHADER_SOURCE = 
+  `
+  #ifdef GL_ES
+  precision mediump float;
+  #endif
+
+  void main() {
+    gl_FragColor = vec4(gl_FragCoord.z, 0.0, 0.0, 0.0);
+  }`
+
+var VSHADER_SOURCE =
+    `precision mediump float;
+  attribute vec4 a_Position;
+  attribute vec4 a_Color;
+  attribute vec4 a_Normal;
+  uniform mat4 u_MvpMatrix;
+  uniform mat4 u_ModelMatrix;
+  uniform mat4 u_NormalMatrix;
+  varying vec4 v_Color;
+  varying vec3 v_Normal;
+  varying vec3 v_Position;
+  uniform vec3 u_LightDirection;
+
+  uniform mat4 u_MvpMatrixFromLight;
+  varying vec4 v_PositionFromLight;
+
+  void main() {
+    gl_Position = u_MvpMatrix * a_Position;
+    v_PositionFromLight = vec4(u_MvpMatrixFromLight * a_Position);
+    v_Position = vec3(u_ModelMatrix * a_Position);
+    v_Normal = normalize(vec3(u_NormalMatrix * a_Normal));
+    v_Color = a_Color;
+  }`
+
+var FSHADER_SOURCE =
+    `#ifdef GL_ES
+    precision mediump float;
+    #endif
+    uniform vec3 u_LightColor;
+    uniform vec3 u_LightPosition;
+    uniform vec3 u_LightDirection;
+
+    uniform int u_SwitchLight;
+    uniform bool u_Picked;
+
+    varying vec3 v_Normal;
+    varying vec3 v_Position;
+    varying vec4 v_Color;
+
+    vec4 finalColor;
+    uniform vec3 u_AmbientLight;
+
+    uniform sampler2D u_ShadowMap;
+    varying vec4 v_PositionFromLight;
+
+    void main() {
+        vec3 shadowCoord = (v_PositionFromLight.xyz / v_PositionFromLight.w) / 2.0 + 0.5;
+        vec4 rgbaDepth = texture2D(u_ShadowMap, shadowCoord.xy);
+        float depth = rgbaDepth.r;
+        float visibility = (shadowCoord.z > depth + 0.005) ? 0.7 : 1.0;
+        vec3 normal = normalize(v_Normal);
+        vec3 lightDirection = normalize(u_LightPosition - v_Position);
+        float nDotL;
+        if (u_SwitchLight == 1) {
+            nDotL = max(dot(lightDirection, normal), 0.0);
+        } else {
+            nDotL = max(dot(u_LightDirection, normal), 0.0);;
+        }
+        vec3 diffuse = u_LightColor * v_Color.rgb * visibility * nDotL;
+        vec3 ambient = u_AmbientLight * v_Color.rgb;
+        if (u_Picked) {
+            finalColor = vec4(1.0, 0.0, 0.0, 1.0);
+        } else {
+            finalColor = vec4(diffuse + ambient, v_Color.a);
+        }
+        gl_FragColor = finalColor;
+    }`
+
+var OFFSCREEN_WIDTH = 2048, OFFSCREEN_HEIGHT = 2048;
+var LIGHT_X = 1, LIGHT_Y = 3, LIGHT_Z = 5;
 
 function main() {
-  // Retrieve <canvas> element
-  const canvas = document.getElementById('my-canvas');
+    // Retrieve <canvas> element
+    var canvas = document.getElementById('my-canvas');
+
+    // Get the rendering context for WebGL
+    var gl = getWebGLContext(canvas);
+    if (!gl) {
+        console.log('Failed to get the rendering context for WebGL');
+        return;
+    }
+
+    // Contrôle de la caméra
+    document.addEventListener('keydown', e => {
+        switch (e.keyCode) {
+            case 16:
+                lightSelector = !lightSelector
+                gl.uniform1i(programInfo.uniformLocations.u_SwitchLight, !lightSelector)
+                document.getElementById("control").textContent = lightSelector ? "Control on (SHIFT) : directional" : "Control on (SHIFT) : point light"
+                break;
+            case 38: // Flèche haut
+                if (lightSelector) {
+                    y_coord_direct += 0.1
+                } else {
+                    y_coord_point += 0.1
+                }
+                break;
+            case 39: // Flèche droite
+                if (lightSelector) {
+                    x_coord_direct += 0.1
+                } else {
+                    x_coord_point += 0.1
+                }
+                break;
+            case 40: // Flèche bas
+                if (lightSelector) {
+                    y_coord_direct -= 0.1
+                } else {
+                    y_coord_point -= 0.1
+                }
+                break;
+            case 37: // Flèche gauche
+                if (lightSelector) {
+                    x_coord_direct -= 0.1
+                } else {
+                    x_coord_point -= 0.1
+                }
+                break;
+            case 87: // W
+                if (lightSelector) {
+                    z_coord_direct -= 0.1
+                } else {
+                    z_coord_point -= 0.1
+                }
+                break;
+            case 83: // S
+                if (lightSelector) {
+                    z_coord_direct += 0.1
+                } else {
+                    z_coord_point += 0.1
+                }
+                break;
+            case 65: // A
+                x_coord_cam -= 0.1
+                break;
+            case 68: // D
+                x_coord_cam += 0.1
+                break;
+            case 69: // E
+                y_coord_cam += 0.1
+                break;
+            case 81: // Q
+                y_coord_cam -= 0.1
+                break;
+            case 89: // Y
+                z_coord_cam -= 0.1
+                break;
+            case 67: // C
+                z_coord_cam += 0.1
+                break;
+            default:
+                break;
+        }
+    });
+
+    var shadowProgram = createProgram(gl, SHADOW_VSHADER_SOURCE, SHADOW_FSHADER_SOURCE);
+    const shadowProgramInfo = {
+        attributes: {
+            a_Position: gl.getAttribLocation(shadowProgram, 'a_Position')
+        },
+        uniformLocations: {
+            u_MvpMatrix: gl.getUniformLocation(shadowProgram, 'u_MvpMatrix')
+        }
+    }
+
+    var shapesProgram = createProgram(gl, VSHADER_SOURCE, FSHADER_SOURCE);
+    const programInfo = {
+        attributes: {
+            a_Position: gl.getAttribLocation(shapesProgram, 'a_Position'),
+            a_Color: gl.getAttribLocation(shapesProgram, 'a_Color'),
+            a_Normals: gl.getAttribLocation(shapesProgram, 'a_Normal'),
+        },
+        uniformLocations: {
+            u_ModelMatrix: gl.getUniformLocation(shapesProgram, 'u_ModelMatrix'),
+            u_MvpMatrix: gl.getUniformLocation(shapesProgram, 'u_MvpMatrix'),
+            u_NormalMatrix: gl.getUniformLocation(shapesProgram, 'u_NormalMatrix'),
+            u_LightColor: gl.getUniformLocation(shapesProgram, 'u_LightColor'),
+            u_LightPosition: gl.getUniformLocation(shapesProgram, 'u_LightPosition'),
+            u_LightDirection: gl.getUniformLocation(shapesProgram, 'u_LightDirection'),
+            u_SwitchLight: gl.getUniformLocation(shapesProgram, 'u_SwitchLight'),
+            u_Picked: gl.getUniformLocation(shapesProgram, 'u_Picked'),
+            u_AmbientLight: gl.getUniformLocation(shapesProgram, 'u_AmbientLight'),
+            u_MvpMatrixFromLight: gl.getUniformLocation(shapesProgram, 'u_MvpMatrixFromLight'),
+            u_ShadowMap: gl.getUniformLocation(shapesProgram, 'u_ShadowMap'),
+        }
+    }
+
+    var vertices = pyramid()
+    let normals = []
+    for (let i = 0; i < vertices.length - 12; i += 12) {
+        let n = normalizedCrossWith3Points(
+            vertices[i],
+            vertices[i + 1],
+            vertices[i + 2],
+            vertices[i + 3],
+            vertices[i + 4],
+            vertices[i + 5],
+            vertices[i + 6],
+            vertices[i + 7],
+            vertices[i + 8]
+        )
+        for (let j = 0; j < 4; j++) {
+            normals.push(n[0]);
+            normals.push(n[1]);
+            normals.push(n[2]);
+        }
+    }
+
+    var normalsGround = [0.0, 1.0, 0.0,
+        0.0, 1.0, 0.0,
+        0.0, 1.0, 0.0,
+        0.0, 1.0, 0.0]
+
+    var ground = initVertexBuffersGround(gl, groundVertices(), groundColors(), indicesGround, new Float32Array(normalsGround));
+    var py = initVertexBuffersPyramid(gl, pyramid(), pyramidColors(), indices, new Float32Array(normals));
+    
+    gl.clearColor(0.0, 0.0, 0.0, 1.0);
+    gl.enable(gl.DEPTH_TEST);
+
+    // var fbo = initFramebufferObject(gl);
+    // if (!fbo) {
+    //     console.log('Failed to initialize frame buffer object');
+    //     return;
+    // }
+    
+    // gl.activeTexture(gl.TEXTURE0);
+    // gl.bindTexture(gl.TEXTURE_2D, fbo.texture);
+
+    
+    gl.clearColor(0, 0, 0, 1);
+    gl.enable(gl.DEPTH_TEST);
+
+    var modelMatrix = new Matrix4();
+    var mvpMatrix = new Matrix4();
+    var normalMatrix = new Matrix4();
+    var viewPortMatrix = new Matrix4();
+    
+    var viewPortLightMatrix = new Matrix4();
+    viewPortLightMatrix.setPerspective(70, OFFSCREEN_WIDTH/OFFSCREEN_HEIGHT, 1, 100);
+    viewPortLightMatrix.lookAt(LIGHT_X, LIGHT_Y, LIGHT_Z, 0, 0, 0, 0, 1, 0);
+
+    normalMatrix.setInverseOf(modelMatrix);
+    normalMatrix.transpose();
+
+    mvpMatrix.multiply(modelMatrix);
+
+    var picked = false;
+
+    canvas.onmousedown = function (ev) {
+        var oldValuePicked = picked
+        var x = ev.clientX
+        var y = ev.clientY
+        var rect = ev.target.getBoundingClientRect()
+        if (rect.left <= x && x < rect.right && rect.top <= y && y < rect.bottom) {
+            var xCanvas = x - rect.left
+            var yCanvas = rect.bottom - y
+            picked = check(gl, xCanvas, yCanvas, shapesProgram, programInfo, py, modelMatrix, viewPortMatrix, mvpMatrix, normalMatrix);
+            if (picked !== oldValuePicked) {
+                py = initVertexBuffersPyramid(gl, pyramid(), pyramidColors2(), indices, new Float32Array(normals));
+                runAnimation = true
+            } else {
+                py = initVertexBuffersPyramid(gl, pyramid(), pyramidColors(), indices, new Float32Array(normals));
+                picked = false
+            }
+        }
+    }
+    
+    let runAnimation = false
+    var mvpMatrixFromLight_py = new Matrix4();
+    var mvpMatrixFromLight_gr = new Matrix4();
+    let render = () => {
+        if (runAnimation) {
+            currentAngle = animate(currentAngle);
+        }
+        if (Math.ceil(currentAngle) % 180 === 0) {
+            runAnimation = false
+        }
+        // gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
+        // gl.viewport(0, 0, OFFSCREEN_WIDTH, OFFSCREEN_HEIGHT);
+        // gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
+
+        // gl.useProgram(shadowProgram);
+        // drawHourglass(gl, shadowProgram, shadowProgramInfo, py, modelMatrix, viewPortLightMatrix, mvpMatrix, normalMatrix)
+        // mvpMatrixFromLight_py.set(mvpMatrix);
+        // drawGround(gl, shadowProgram, shadowProgramInfo, ground, modelMatrix, viewPortLightMatrix, mvpMatrix, normalMatrix)
+        // mvpMatrixFromLight_gr.set(mvpMatrix);
+
+        // gl.bindFramebuffer(gl.FRAMEBUFFER, null);           
+        // gl.viewport(0, 0, canvas.width, canvas.height);
+        // gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
+
+
+        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
+        gl.useProgram(shapesProgram)
+        gl.uniform1i(programInfo.uniformLocations.u_ShadowMap, 0);  
+        
+        
+        viewPortMatrix.setPerspective(30, canvas.width / canvas.height, 1, 30);
+        viewPortMatrix.lookAt(x_coord_cam, y_coord_cam, z_coord_cam, 0, 0, 0, 0, 1, 0);
+        
+        // gl.uniformMatrix4fv(programInfo.uniformLocations.u_MvpMatrixFromLight, false, mvpMatrixFromLight_py.elements);
+        drawHourglass(gl, shapesProgram, programInfo, py, modelMatrix, viewPortMatrix, mvpMatrix, normalMatrix, currentAngle)
+        // gl.uniformMatrix4fv(programInfo.uniformLocations.u_MvpMatrixFromLight, false, mvpMatrixFromLight_gr.elements);
+        drawGround(gl, shapesProgram, programInfo, ground, modelMatrix, viewPortMatrix, mvpMatrix, normalMatrix)
+        requestAnimationFrame(render)
+    }
+    requestAnimationFrame(render)
+}
+
+function check(gl, x, y, prg, programInfo, buffer, modelMatrix, viewPortMatrix, mvpMatrix, normalMatrix) {
+    var picked = false;
+    gl.uniform1i(programInfo.uniformLocations.u_Picked, 1);
+    drawHourglass(gl, prg, programInfo, buffer, modelMatrix, mvpMatrix, viewPortMatrix, normalMatrix, currentAngle)
+    var pixels = new Uint8Array(4);
+    gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
+    if (pixels[0] == 255) {
+        picked = true;
+    }
+    gl.uniform1i(programInfo.uniformLocations.u_Picked, 0);
+    drawHourglass(gl, prg, programInfo, buffer, modelMatrix, mvpMatrix, viewPortMatrix, normalMatrix, currentAngle)
+    return picked;
+}
+
+function drawHourglass(gl, prg, programInfo, py, modelMatrix, viewPortMatrix, mvpMatrix, normalMatrix, angle) {
+    // Bas
+    modelMatrix.setRotate(0 + angle, 0, 0, 1);
+    modelMatrix.translate(0.0, -1.5, 0.0);
+    mvpMatrix.set(viewPortMatrix).multiply(modelMatrix);
+    normalMatrix.rotate(0, 0, 0, 1);
+    normalMatrix.setInverseOf(modelMatrix);
+    normalMatrix.transpose();
+    draw(gl, prg, programInfo, py, modelMatrix, mvpMatrix, viewPortMatrix, normalMatrix)
+
+    // Haut
+    modelMatrix.setRotate(180 + angle, 0, 0, 1);
+    modelMatrix.translate(0, -1.5, 0);
+    mvpMatrix.set(viewPortMatrix).multiply(modelMatrix);
+    normalMatrix.rotate(180, 0, 0, 1);
+    normalMatrix.transpose();
+    draw(gl, prg, programInfo, py, modelMatrix, mvpMatrix, viewPortMatrix, normalMatrix)
+}
+
+function drawGround(gl, prg, programInfo, ground, modelMatrix, viewPortMatrix, mvpMatrix, normalMatrix) {
+    modelMatrix.setRotate(0, 0, 0, 1);
+    modelMatrix.translate(0, -2.0, 0);
+    mvpMatrix.set(viewPortMatrix).multiply(modelMatrix);
+    normalMatrix.rotate(180, 0, 0, 1);
+    normalMatrix.transpose();
+    draw(gl, prg, programInfo, ground, modelMatrix, mvpMatrix, viewPortMatrix, normalMatrix)
+}
+
+function draw(gl, prg, program, o, modelMatrix, mvpMatrix, viewPortMatrix, normalMatrix) {
+    initAttributeVariable(gl, program.attributes.a_Position, o.vertexBuffer);
+    initAttributeVariable(gl, program.attributes.a_Color, o.colorBuffer);
+    initAttributeVariable(gl, program.attributes.a_Normals, o.normalsBuffer);
+
+    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, o.indexBuffer);
+
+    mvpMatrix.set(viewPortMatrix).multiply(modelMatrix);
+    // Lights
+    gl.uniform3f(program.uniformLocations.u_AmbientLight, 0.5, 0.5, 0.5);
+    gl.uniform3f(program.uniformLocations.u_LightColor, 1.0, 1.0, 1.0); // Light color
+    gl.uniform3f(program.uniformLocations.u_LightPosition, x_coord_point, y_coord_point, z_coord_point);
+    
+    // var lightDirection = new Vector3([x_coord_direct, y_coord_direct, z_coord_direct]);
+    // lightDirection.normalize();     // Normalize
+    // gl.uniform3fv(program.uniformLocations.u_LightDirection, lightDirection.elements);
+    
+    if (gl.getUniform(prg, program.uniformLocations.u_SwitchLight.valueOf()) === 1) {
+        gl.uniform3f(program.uniformLocations.u_LightPosition, x_coord_point, y_coord_point, z_coord_point);
+    } else {
+        var lightDirection = new Vector3([x_coord_direct, y_coord_direct, z_coord_direct]);
+        lightDirection.normalize();     // Normalize
+        gl.uniform3fv(program.uniformLocations.u_LightDirection, lightDirection.elements);
+    }
+
+    document.getElementById("light_x").textContent = "Light X directionnelle : " + x_coord_direct
+    document.getElementById("light_y").textContent = "Light Y directionnelle : " + y_coord_direct
+    document.getElementById("light_z").textContent = "Light Z directionnelle : " + z_coord_direct
+
+    document.getElementById("light_x_point").textContent = "Light X ponctuelle : " + x_coord_point
+    document.getElementById("light_y_point").textContent = "Light Y ponctuelle : " + y_coord_point
+    document.getElementById("light_z_point").textContent = "Light Z ponctuelle : " + z_coord_point
+    
+    
+    gl.uniformMatrix4fv(program.uniformLocations.u_MvpMatrix, false, mvpMatrix.elements);
+    gl.uniformMatrix4fv(program.uniformLocations.u_ModelMatrix, false, modelMatrix.elements);
+    gl.uniformMatrix4fv(program.uniformLocations.u_NormalMatrix, false, normalMatrix.elements);
+
+    gl.drawElements(gl.TRIANGLES, o.numIndices, gl.UNSIGNED_BYTE, 0);
+}
+
+function initAttributeVariable(gl, a_attribute, buffer) {
+    gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
+    gl.vertexAttribPointer(a_attribute, buffer.num, buffer.type, false, 0, 0);
+    gl.enableVertexAttribArray(a_attribute);
+}
+
+function initVertexBuffersGround(gl, vertices, colors, indices, normals) {
+  var g = new Object()
+  g.vertexBuffer = initBuffer(gl, vertices, 3, gl.FLOAT);
+  g.colorBuffer = initBuffer(gl, colors, 3, gl.FLOAT);
+  g.normalsBuffer = initBuffer(gl, normals, 3, gl.FLOAT);
+  g.indexBuffer = initElementArrayBuffer(gl, indices, gl.UNSIGNED_BYTE);
+  if (!g.vertexBuffer || !g.colorBuffer || !g.indexBuffer) 
+    return null; 
+
+  g.numIndices = indices.length;
+  gl.bindBuffer(gl.ARRAY_BUFFER, null);
+  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
+
+  return g;
+}
+
+function initVertexBuffersPyramid(gl, vertices, colors, indices, normals) {
+  var p = new Object()
+  p.vertexBuffer = initBuffer(gl, vertices, 3, gl.FLOAT);
+  p.colorBuffer = initBuffer(gl, colors, 3, gl.FLOAT);
+  p.normalsBuffer = initBuffer(gl, normals, 3, gl.FLOAT);
+  p.indexBuffer = initElementArrayBuffer(gl, indices, gl.UNSIGNED_BYTE);
+  if (!p.vertexBuffer || !p.colorBuffer || !p.indexBuffer) 
+    return null; 
+
+  p.numIndices = indices.length;
+  gl.bindBuffer(gl.ARRAY_BUFFER, null);
+  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
+
+  return p;
+}
+
+function initBuffer(gl, data, num, type) {
+    var buffer = gl.createBuffer();
+    if (!buffer) {
+        console.log('Failed to create the buffer object');
+        return null;
+    }
+    
+    gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
+    gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
+    buffer.num = num;
+    buffer.type = type;
+
+    return buffer;
+}
+
+function initElementArrayBuffer(gl, data, type) {
+    var buffer = gl.createBuffer();
+    if (!buffer) {
+        console.log('Failed to create the buffer object');
+        return null;
+    }
+
+    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
+    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, gl.STATIC_DRAW);
+    buffer.type = type;
+
+    return buffer;
+}
+
+function normalizedCrossWith3Points(a0, a1, a2, b0, b1, b2, c0, c1, c2) {
+    const x = 0;
+    const y = 1;
+    const z = 2;
+
+    const u = [b0 - a0, b1 - a1, b2 - a2];
+    const v = [c0 - a0, c1 - a1, c2 - a2];
+    return [
+        u[y] * v[z] - u[z] * v[y],
+        u[z] * v[x] - u[x] * v[z],
+        u[x] * v[y] - u[y] * v[x]
+    ];
+}
+
+const ANGLE_STEP = 40;
+const SPEED = 50;
+
+function animate(angle) {
+  var newAngle = angle + (ANGLE_STEP * SPEED) / 1000.0;
+  return newAngle % 360;
+}
 
-  // TODO: Complete with your code here
-}
\ No newline at end of file
+function initFramebufferObject(gl) {
+    var framebuffer, texture, depthBuffer;
+  
+    var error = function() {
+      if (framebuffer) gl.deleteFramebuffer(framebuffer);
+      if (texture) gl.deleteTexture(texture);
+      if (depthBuffer) gl.deleteRenderbuffer(depthBuffer);
+      return null;
+    }
+  
+    framebuffer = gl.createFramebuffer();
+    if (!framebuffer) {
+      console.log('Failed to create frame buffer object');
+      return error();
+    }
+  
+    texture = gl.createTexture();
+    if (!texture) {
+      console.log('Failed to create texture object');
+      return error();
+    }
+    gl.bindTexture(gl.TEXTURE_2D, texture);
+    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, OFFSCREEN_WIDTH, OFFSCREEN_HEIGHT, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
+    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
+  
+    depthBuffer = gl.createRenderbuffer();
+    if (!depthBuffer) {
+      console.log('Failed to create renderbuffer object');
+      return error();
+    }
+    gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer);
+    gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, OFFSCREEN_WIDTH, OFFSCREEN_HEIGHT);
+  
+    gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
+    gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
+    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthBuffer);
+  
+    var e = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
+    if (gl.FRAMEBUFFER_COMPLETE !== e) {
+      console.log('Frame buffer object is incomplete: ' + e.toString());
+      return error();
+    }
+  
+    framebuffer.texture = texture;
+  
+    gl.bindFramebuffer(gl.FRAMEBUFFER, null);
+    gl.bindTexture(gl.TEXTURE_2D, null);
+    gl.bindRenderbuffer(gl.RENDERBUFFER, null);
+  
+    return framebuffer;
+  }
\ No newline at end of file
diff --git a/lab4/src/pyramidModel.js b/lab4/src/pyramidModel.js
new file mode 100644
index 0000000..73188cf
--- /dev/null
+++ b/lab4/src/pyramidModel.js
@@ -0,0 +1,90 @@
+let pyramid = () => {
+    return new Float32Array([
+  
+        // Devant
+        -1.5, -1.5,  1.5,
+        1.5, -1.5,  1.5,
+        0.75,  1.5,  0.75,
+        -0.75,  1.5,  0.75,
+        
+        // Derrière
+        -1.5, -1.5, -1.5,
+        -0.75,  1.5, -0.75,
+        0.75,  1.5, -0.75,
+        1.5, -1.5, -1.5,
+        
+        // Haut
+        -0.75,  1.5, -0.75,
+        -0.75,  1.5,  0.75,
+        0.75,  1.5,  0.75,
+        0.75,  1.5, -0.75,
+        
+        // Bas
+        -1.5, -1.5, -1.5,
+        1.5, -1.5, -1.5,
+        1.5, -1.5,  1.5,
+        -1.5, -1.5,  1.5,
+        
+        // Droite
+        1.5, -1.5, -1.5,
+        0.75,  1.5, -0.75,
+        0.75,  1.5,  0.75,
+        1.5, -1.5,  1.5,
+        
+        // Gauche
+        -1.5, -1.5, -1.5,
+        -1.5, -1.5,  1.5,
+        -0.75,  1.5,  0.75,
+        -0.75,  1.5, -0.75,
+        
+        -6.0, -1.5, 6.0,  // Bas gauche
+        -6.0, -1.5, -6.0, // Haut gauche
+        6.0, -1.5,  6.0,  // Bas droite
+        6.0, -1.5,  -6.0, // Haut droite
+    ])
+}
+
+let groundVertices = () => {
+    return new Float32Array([
+        -6.0, -1.5, 6.0,  // Bas gauche
+        -6.0, -1.5, -6.0, // Haut gauche
+        6.0, -1.5,  6.0,  // Bas droite
+        6.0, -1.5,  -6.0, // Haut droite
+    ])
+}
+
+let pyramidColors = () => {
+    return new Float32Array([
+        0.5, 0.5, 1.0,   0.5, 0.5, 1.0,   0.5, 0.5, 1.0,  0.5, 0.5, 1.0,
+        0.5, 0.5, 1.0,   0.5, 0.5, 1.0,   0.5, 0.5, 1.0,  0.5, 0.5, 1.0,
+        0.5, 0.5, 1.0,   0.5, 0.5, 1.0,   0.5, 0.5, 1.0,  0.5, 0.5, 1.0,
+        0.5, 0.5, 1.0,   0.5, 0.5, 1.0,   0.5, 0.5, 1.0,  0.5, 0.5, 1.0,
+        0.5, 0.5, 1.0,   0.5, 0.5, 1.0,   0.5, 0.5, 1.0,  0.5, 0.5, 1.0,
+        0.5, 0.5, 1.0,   0.5, 0.5, 1.0,   0.5, 0.5, 1.0,  0.5, 0.5, 1.0,
+        0.5, 0.5, 1.0,   0.5, 0.5, 1.0,   0.5, 0.5, 1.0,  0.5, 0.5, 1.0,
+    ]);
+}
+
+let groundColors = () => {
+    return new Float32Array([
+        1.0, 1.0, 1.0,   1.0, 1.0, 1.0,   1.0, 1.0, 1.0,  1.0, 1.0, 1.0,
+        1.0, 1.0, 1.0,   1.0, 1.0, 1.0,   1.0, 1.0, 1.0,  1.0, 1.0, 1.0,
+        1.0, 1.0, 1.0,   1.0, 1.0, 1.0,   1.0, 1.0, 1.0,  1.0, 1.0, 1.0,
+        1.0, 1.0, 1.0,   1.0, 1.0, 1.0,   1.0, 1.0, 1.0,  1.0, 1.0, 1.0,
+        1.0, 1.0, 1.0,   1.0, 1.0, 1.0,   1.0, 1.0, 1.0,  1.0, 1.0, 1.0,
+        1.0, 1.0, 1.0,   1.0, 1.0, 1.0,   1.0, 1.0, 1.0,  1.0, 1.0, 1.0,
+        1.0, 1.0, 1.0,   1.0, 1.0, 1.0,   1.0, 1.0, 1.0,  1.0, 1.0, 1.0,
+    ]);
+}
+
+let pyramidColors2 = () => {
+    return new Float32Array([
+        0.5, 1.0, 0.5,   0.5, 1.0, 0.5,   0.5, 1.0, 0.5,  0.5, 1.0, 0.5,
+        0.5, 1.0, 0.5,   0.5, 1.0, 0.5,   0.5, 1.0, 0.5,  0.5, 1.0, 0.5,
+        0.5, 1.0, 0.5,   0.5, 1.0, 0.5,   0.5, 1.0, 0.5,  0.5, 1.0, 0.5,
+        0.5, 1.0, 0.5,   0.5, 1.0, 0.5,   0.5, 1.0, 0.5,  0.5, 1.0, 0.5,
+        0.5, 1.0, 0.5,   0.5, 1.0, 0.5,   0.5, 1.0, 0.5,  0.5, 1.0, 0.5,
+        0.5, 1.0, 0.5,   0.5, 1.0, 0.5,   0.5, 1.0, 0.5,  0.5, 1.0, 0.5,
+        0.5, 1.0, 0.5,   0.5, 1.0, 0.5,   0.5, 1.0, 0.5,  0.5, 1.0, 0.5,
+    ]);
+}
\ No newline at end of file
-- 
GitLab