Index: new-game.cpp
===================================================================
--- new-game.cpp	(revision 07ed460186797228b4f4cd8aa9d995371846395f)
+++ new-game.cpp	(revision d9f99b2bd20ab9dfaaeeb06cf3ea011c3142f431)
@@ -30,15 +30,6 @@
 #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
 
-/*
- * If I use one array to store the points for all the object faces in the scene, I'll probably remove the ObjectFace object,
- * and store the start and end indices of a given object's point coordinates in that array in the SceneObject.
- *
- * Should probably do something similar with colors and texture coordinates, once I figure out the best way to store tex coords
- * for all objects in one array.
- */
-
-
-// might also want to store the shader to be used for the object
 struct SceneObject {
+   unsigned int id;
    mat4 model_mat;
    GLuint shader_program;
@@ -52,9 +43,4 @@
 };
 
-struct ObjectFace {
-   unsigned int object_id;
-   array<vec3, 3> points;
-};
-
 const bool FULLSCREEN = false;
 int width = 640;
@@ -67,5 +53,4 @@
 
 vector<SceneObject> objects;
-vector<ObjectFace> faces;
 
 SceneObject* clickedObject = NULL;
@@ -74,5 +59,5 @@
 double fps;
 
-bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point);
+bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point);
 bool insideTriangle(vec3 p, array<vec3, 3> triangle_points);
 
@@ -113,22 +98,32 @@
       vec4 click_point;
       vec3 closest_point = vec3(0.0f, 0.0f, -FAR_CLIP); // Any valid point will be closer than the far clipping plane, so initial value to that
-      int closest_face_id = -1;
-
-      for (int i = 0; i<faces.size(); i++) {
-         if (faceClicked(&faces[i], ray_world, cam_pos_temp, click_point)) {
-            click_point = view_mat * click_point;
-
-            if (-NEAR_CLIP >= click_point.z && click_point.z > -FAR_CLIP && click_point.z > closest_point.z) {
-               closest_point = click_point.xyz();
-               closest_face_id = i;
+      SceneObject* closest_object = NULL;
+
+      SceneObject* obj;
+      for (vector<SceneObject>::iterator it = objects.begin(); it != objects.end(); it++) {
+         obj = &*it;
+
+         for (int p_idx = 0; p_idx < it->points.size(); p_idx += 9) {
+            if (faceClicked({
+               vec3(it->points[p_idx], it->points[p_idx + 1], it->points[p_idx + 2]),
+               vec3(it->points[p_idx + 3], it->points[p_idx + 4], it->points[p_idx + 5]),
+               vec3(it->points[p_idx + 6], it->points[p_idx + 7], it->points[p_idx + 8]),
+            },
+            obj, ray_world, cam_pos_temp, click_point)) {
+               click_point = view_mat * click_point;
+
+               if (-NEAR_CLIP >= click_point.z && click_point.z > -FAR_CLIP && click_point.z > closest_point.z) {
+                  closest_point = click_point.xyz();
+                  closest_object = obj;
+               }
             }
          }
       }
 
-      if (closest_face_id == -1) {
+      if (closest_object == NULL) {
          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;
+         clickedObject = closest_object;
+         cout << "Clicked object: " << clickedObject->id << endl;
       }
    }
@@ -217,4 +212,5 @@
    // triangle
    objects.push_back(SceneObject());
+   objects[0].id = 0;
    objects[0].shader_program = 0;
    objects[0].vertex_vbo_offset = (GLvoid*) (0 * sizeof(float) * 3);
@@ -260,4 +256,5 @@
    // square
    objects.push_back(SceneObject());
+   objects[1].id = 1;
    objects[1].shader_program = 0;
    objects[1].vertex_vbo_offset = (GLvoid*) (6 * sizeof(float) * 3);
@@ -307,22 +304,7 @@
    GLsizeiptr textures_buffer_size = 0;
 
-   faces.clear();
-
-   unsigned int object_id = 0;
    for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
       points_buffer_size += obj_it->points.size() * sizeof(GLfloat);
       textures_buffer_size += obj_it->texcoords.size() * sizeof(GLfloat);
-
-      for (int p_idx = 0; p_idx < obj_it->points.size(); p_idx += 9) {
-         faces.push_back(ObjectFace());
-         faces.back().object_id = object_id;
-         faces.back().points = {
-            vec3(obj_it->points[p_idx], obj_it->points[p_idx + 1], obj_it->points[p_idx + 2]),
-            vec3(obj_it->points[p_idx + 3], obj_it->points[p_idx + 4], obj_it->points[p_idx + 5]),
-            vec3(obj_it->points[p_idx + 6], obj_it->points[p_idx + 7], obj_it->points[p_idx + 8]),
-         };
-      }
-
-      object_id++;
    }
 
@@ -701,5 +683,5 @@
 }
 
-bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point) {
+bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point) {
    // LINE EQUATION:		P = O + Dt
    // O = cam
@@ -711,6 +693,6 @@
 
    // 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 v1 = points[1] - points[0];
+   vec3 v2 = points[2] - 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);
@@ -718,5 +700,4 @@
    print4DVector("Full world ray", world_ray);
 
-   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();
@@ -724,5 +705,5 @@
    local_ray = local_ray - local_cam;
 
-   float d = -glm::dot(face->points[0], normal);
+   float d = -glm::dot(points[0], normal);
    cout << "d: " << d << endl;
 
@@ -733,5 +714,5 @@
    printVector("Intersection", intersection);
 
-   if (insideTriangle(intersection, face->points)) {
+   if (insideTriangle(intersection, points)) {
       click_point = obj->model_mat * vec4(intersection, 1.0f);
       return true;
@@ -740,15 +721,12 @@
    }
 }
-
 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];
+   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);
    float x = (pv1.x-y*v31.x) / v21.x;
 
-   cout << "(" << x << ", " << y << ")" << endl;
-
    return x > 0.0f && y > 0.0f && x+y < 1.0f;
 }
