Index: new-game.cpp
===================================================================
--- new-game.cpp	(revision 5c9d193c19a832248a9576ae056bb29c23ce5c3f)
+++ new-game.cpp	(revision e82692b1725b5a92ab35e332743605d47b7d8ab9)
@@ -35,5 +35,5 @@
 
 struct ObjectFace {
-   unsigned int objectId;
+   unsigned int object_id;
    array<vec3, 3> points;
 };
@@ -54,5 +54,5 @@
 SceneObject* selectedObject = NULL;
 
-bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam);
+bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point);
 bool insideTriangle(vec3 p, array<vec3, 3> triangle_points);
 
@@ -238,16 +238,29 @@
       cout << "Camera -> (" << cam_pos_temp.x << "," << cam_pos_temp.y << "," << cam_pos_temp.z << ")" << endl;
 
+      vec4 click_point;
+      vec3 closest_point;
+      int closest_face_id = -1;
+
       // 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 (faceClicked(&faces[i], ray_world, cam_pos_temp, click_point)) {
+            click_point = view_mat * click_point;
+
+            // Check against clipping planes once I start applying the projection matrix
+            // if (NEAR_CLIP <= click_point.z && click_point.z < FAR_CLIP && ...) {
+            if (-1.0f <= click_point.z && click_point.z < 1.0f && click_point.z < closest_point.z) {
+               closest_point = click_point.xyz();
+               closest_face_id = i;
+            }
          }
       }
 
-      if (clickedObject == NULL) {
+      if (closest_face_id == -1) {
          cout << "No object was clicked" << endl;
+      } else {
+         clickedObject = &objects[faces[closest_face_id].object_id];
+         cout << "Clicked object: " << faces[closest_face_id].object_id << endl;
+         printVector("Click point", closest_point);
       }
    }
@@ -409,10 +422,10 @@
    mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f));
    */
-   T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f));
+   T_model = translate(mat4(), vec3(0.0f, 0.0f, 0.0f));
    R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
    objects[0].model_mat = T_model*R_model;
 
    faces.push_back(ObjectFace());
-   faces[0].objectId = 0;
+   faces[0].object_id = 0;
    faces[0].points = {
       vec3(points[0], points[1], points[2]),
@@ -425,10 +438,10 @@
 
    // mat4 T_model2 = translate(mat4(), vec3(-1.0f, 0.0f, 0.0f));
-   T_model = translate(mat4(), vec3(-0.5f, 0.0f, 0.0f));
+   T_model = translate(mat4(), vec3(-0.5f, 0.0f, 0.1f));
    R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
    objects[1].model_mat = T_model*R_model;
 
    faces.push_back(ObjectFace());
-   faces[1].objectId = 1;
+   faces[1].object_id = 1;
    faces[1].points = {
       vec3(points2[0], points2[1], points2[2]),
@@ -438,5 +451,5 @@
 
    faces.push_back(ObjectFace());
-   faces[2].objectId = 1;
+   faces[2].object_id = 1;
    faces[2].points = {
       vec3(points2[9], points2[10], points2[11]),
@@ -729,5 +742,5 @@
 }
 
-bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam) {
+bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point) {
    cout << "Points on the plane" << endl;
    printVector("fp1", face->points[0]);
@@ -750,5 +763,5 @@
    printVector("Cross", normal);
 
-   SceneObject* obj = &objects[face->objectId];
+   SceneObject* obj = &objects[face->object_id];
    vec3 local_ray = (inverse(obj->model_mat) * world_ray).xyz();
    vec3 local_cam = (inverse(obj->model_mat) * cam).xyz();
@@ -767,5 +780,10 @@
    printVector("Intersection", intersection);
 
-   return insideTriangle(intersection, face->points);
+   if (insideTriangle(intersection, face->points)) {
+      click_point = obj->model_mat * vec4(intersection, 1.0f);
+      return true;
+   } else {
+      return false;
+   }
 }
 
