Index: new-game.cpp
===================================================================
--- new-game.cpp	(revision 147ac6d98bbce1abca735e47d994310e71b0bf00)
+++ new-game.cpp	(revision 5c9d193c19a832248a9576ae056bb29c23ce5c3f)
@@ -5,4 +5,8 @@
 #define _USE_MATH_DEFINES
 #define GLM_SWIZZLE
+
+// This is to fix a non-alignment issue when passing vec4 params.
+// Check if it got fixed in a later version of GLM
+#define GLM_FORCE_PURE
 
 #include <glm/mat4x4.hpp>
@@ -50,5 +54,6 @@
 SceneObject* selectedObject = NULL;
 
-bool insideTriangle(vec3 p, ObjectFace* face);
+bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam);
+bool insideTriangle(vec3 p, array<vec3, 3> triangle_points);
 
 GLuint loadShader(GLenum type, string file);
@@ -178,5 +183,4 @@
  *
  * The mouse button callback will:
- *    -Set all selected flags in the objects array to false
  *    -iterate through the faces array
  *    -For each face, it will call faceClicked() with the following params:
@@ -193,7 +197,4 @@
    glfwGetCursorPos(window, &mouse_x, &mouse_y);
 
-   ObjectFace* face = &faces[0];
-   SceneObject* obj = &objects[face->objectId];
-
    if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
       cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;
@@ -217,6 +218,6 @@
 
       vec4 ray_clip = vec4(x, y, NEAR_CLIP, 1.0f); // this should have a z equal to the near clipping plane
-      vec4 ray_eye = ray_clip;
-      vec3 ray_world = (inverse(obj->model_mat) * inverse(view_mat) * ray_eye).xyz();
+      vec4 ray_eye = ray_clip; // Need to apply the projection matrix here
+      vec4 ray_world = inverse(view_mat) * ray_eye;
 
       /* LATEST NOTES:
@@ -228,10 +229,8 @@
        */
 
-      printVector("Initial world ray:", ray_world);
+      printVector("Initial world ray:", ray_world.xyz());
 
       vec4 cam_pos_origin = vec4(x, y, 0.0f, 1.0f);
-      vec3 cam_pos_temp = (inverse(obj->model_mat) * inverse(view_mat) * cam_pos_origin).xyz();
-
-      ray_world = ray_world-cam_pos_temp;
+      vec4 cam_pos_temp = inverse(view_mat) * cam_pos_origin;
 
       cout << "Ray clip -> (" << ray_clip.x << "," << ray_clip.y << "," << ray_clip.z << ")" << endl << endl;;
@@ -239,43 +238,17 @@
       cout << "Camera -> (" << cam_pos_temp.x << "," << cam_pos_temp.y << "," << cam_pos_temp.z << ")" << endl;
 
-      vec3 fp1 = face->points[0];
-      vec3 fp2 = face->points[1];
-      vec3 fp3 = face->points[2];
-
-      cout << "Points on the plane" << endl;
-      cout << "(" << fp1.x << ", " << fp1.y << ", " << fp1.z << ")" << endl;
-      cout << "(" << fp2.x << ", " << fp2.y << ", " << fp2.z << ")" << endl;
-      cout << "(" << fp3.x << ", " << fp3.y << ", " << fp3.z << ")" << endl;
-
-      // LINE EQUATION:		P = O + Dt
-      // O = cam_pos
-      // D = ray_world
-
-      // PLANE EQUATION:	P dot n + d = 0  (n is the normal vector and d is the offset from the origin)
-
-      // Take the cross-product of two vectors on the plane to get the normal
-      vec3 v1 = fp2 - fp1;
-      vec3 v2 = fp3 - fp1;
-
-      vec3 normal = vec3(v1.y*v2.z-v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x);
-      printVector("v1", v1);
-      printVector("v2", v2);
-      printVector("Cross", normal);
-      cout << "Test theory: " << glm::dot(cam_pos_temp, normal) << endl;
-      cout << "Test 2: " << glm::dot(ray_world, normal) << endl;
-
-      float d = -glm::dot(fp1, normal);
-      cout << "d: " << d << endl;
-
-      float t = - (glm::dot(cam_pos_temp, normal) + d) / glm::dot(ray_world, normal);
-      cout << "t: " << t << endl;
-
-      vec3 intersection = cam_pos_temp+t*ray_world;
-      printVector("Intersection", intersection);
-
-      if (insideTriangle(intersection, face)) {
-         clickedObject = obj;
-      }
-      cout << (obj == clickedObject ? "true" : "false") << endl;
+      // Need to account for faces that are behind one another
+      // Using an iterator for the loop makes it difficult to get a reference to each face (for the faceClicked function)
+      for (int i = 0; i<faces.size(); i++) {
+         if (faceClicked(&faces[i], ray_world, cam_pos_temp)) {
+            clickedObject = &objects[faces[i].objectId];
+            cout << "Clicked object: " << faces[i].objectId << endl;
+            break;
+         }
+      }
+
+      if (clickedObject == NULL) {
+         cout << "No object was clicked" << endl;
+      }
    }
 }
@@ -756,8 +729,49 @@
 }
 
-bool insideTriangle(vec3 p, ObjectFace* face) {
-   vec3 v21 = face->points[1]- face->points[0];
-   vec3 v31 = face->points[2]- face->points[0];
-   vec3 pv1 = p- face->points[0];
+bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam) {
+   cout << "Points on the plane" << endl;
+   printVector("fp1", face->points[0]);
+   printVector("fp2", face->points[1]);
+   printVector("fp3", face->points[2]);
+
+   // LINE EQUATION:		P = O + Dt
+   // O = cam_pos
+   // D = ray_world
+
+   // PLANE EQUATION:	P dot n + d = 0  (n is the normal vector and d is the offset from the origin)
+
+   // Take the cross-product of two vectors on the plane to get the normal
+   vec3 v1 = face->points[1] - face->points[0];
+   vec3 v2 = face->points[2] - face->points[0];
+
+   vec3 normal = vec3(v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x);
+   printVector("v1", v1);
+   printVector("v2", v2);
+   printVector("Cross", normal);
+
+   SceneObject* obj = &objects[face->objectId];
+   vec3 local_ray = (inverse(obj->model_mat) * world_ray).xyz();
+   vec3 local_cam = (inverse(obj->model_mat) * cam).xyz();
+   local_ray = local_ray - local_cam;
+
+   cout << "Test theory: " << glm::dot(local_cam, normal) << endl;
+   cout << "Test 2: " << glm::dot(local_ray, normal) << endl;
+
+   float d = -glm::dot(face->points[0], normal);
+   cout << "d: " << d << endl;
+
+   float t = -(glm::dot(local_cam, normal) + d) / glm::dot(local_ray, normal);
+   cout << "t: " << t << endl;
+
+   vec3 intersection = local_cam + t*local_ray;
+   printVector("Intersection", intersection);
+
+   return insideTriangle(intersection, face->points);
+}
+
+bool insideTriangle(vec3 p, array<vec3, 3> triangle_points) {
+   vec3 v21 = triangle_points[1]- triangle_points[0];
+   vec3 v31 = triangle_points[2]- triangle_points[0];
+   vec3 pv1 = p- triangle_points[0];
 
    float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
