Index: new-game.cpp
===================================================================
--- new-game.cpp	(revision 64a70f4ed82805ac7679c4d094945bbf3be13f44)
+++ new-game.cpp	(revision 19c93388b7ef3df32ba98d9fe3532b13307c592b)
@@ -18,4 +18,5 @@
 #include <cmath>
 #include <string>
+#include <array>
 
 using namespace std;
@@ -30,5 +31,9 @@
 vec3 cam_pos;
 
-vec3 face_point1, face_point2, face_point3;
+array<vec3, 3> triangle_face;
+
+array<vec3,3> colored_triangle;
+array<vec3, 3> square_triangle1;
+array<vec3, 3> square_triangle2;
 
 bool clicked = false;
@@ -37,8 +42,11 @@
 bool clicked_square = false;
 
+mat4 model_mat;
+mat4 model_mat2;
+
 mat4 view_mat;
 mat4 proj_mat;
 
-bool insideTriangle(vec3 p, vec3 v1, vec3 v2, vec3 v3);
+bool insideTriangle(vec3 p, array<vec3, 3> triangle);
 
 GLuint loadShader(GLenum type, string file);
@@ -118,7 +126,7 @@
        */
 
-      vec3 fp1 = face_point1;
-      vec3 fp2 = face_point2;
-      vec3 fp3 = face_point3;
+      vec3 fp1 = triangle_face[0];
+      vec3 fp2 = triangle_face[1];
+      vec3 fp3 = triangle_face[2];
 
       cout << "Points on the plane" << endl;
@@ -146,8 +154,31 @@
       cout << "(" << i.x << "," << i.y << "," << i.z << ")" << endl;
 
-      clicked = insideTriangle(i, fp1, fp2, fp3);
+      clicked = insideTriangle(i, triangle_face);
       cout << (clicked ? "true" : "false")  << endl;
    }
 }
+
+/* REFACTORING PLAN:
+ *
+ * Have an array of object structs
+ * Each object struct has:
+ *    -a model matrix
+ *    -a selected boolean
+ * Eventually, maybe also want to store a reference to the correct shader
+ * or whatever other info I need to properly render it
+ *
+ * Have an array of face structs
+ * Each face struct has
+ *    -an object index indicating which object it is a part of
+ *    -an array of three points
+ *
+ * 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:
+ *       -Probably a world ray created from the mouse click coordinates
+ *       -An array of 3 points representing the face
+ *       -The object struct represnting the object the face is a part of
+ */
 
 void mouse_button_callback_new(GLFWwindow* window, int button, int action, int mods) {
@@ -161,13 +192,5 @@
       float y = 1.0f - (2.0f*mouse_y) / height;
 
-      //x = -.1f;
-      //x = -.25f;
-      //x = -.5f;
-
-      //y = .1f;
-
       cout << "x: " << x << ", y: " << y << endl;
-
-      // CHECK: Looks good up to here
 
       // Since the projection matrix gets applied before the view matrix,
@@ -182,8 +205,7 @@
       // vec3 ray_world = normalize((inverse(view_mat) * ray_eye).xyz());
 
-      //vec4 ray_clip = vec4(0.0f, 0.0f, NEAR_CLIP, 1.0f); // this should have a z equal to the near clipping plane
       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(view_mat) * ray_eye).xyz();
+      vec3 ray_world = (inverse(model_mat) * inverse(view_mat) * ray_eye).xyz();
 
       /* LATEST NOTES:
@@ -191,4 +213,6 @@
        * Normalizing the world ray caused issues, although it should make sense with the projection
        * matrix, since the z coordinate has meaning there.
+       * Plus, we really want to normalize it only once we recompute it below as the difference of two points,
+       * although doing so shouldn't effect the results. Check the book to see if there is a good reason for doing so.
        */
 
@@ -196,5 +220,5 @@
 
       vec4 cam_pos_origin = vec4(x, y, 0.0f, 1.0f);
-      vec3 cam_pos_temp = (inverse(view_mat) * cam_pos_origin).xyz();
+      vec3 cam_pos_temp = (inverse(model_mat) * inverse(view_mat) * cam_pos_origin).xyz();
 
       ray_world = ray_world-cam_pos_temp;
@@ -204,7 +228,7 @@
       cout << "Camera -> (" << cam_pos_temp.x << "," << cam_pos_temp.y << "," << cam_pos_temp.z << ")" << endl;
 
-      vec3 fp1 = face_point1;
-      vec3 fp2 = face_point2;
-      vec3 fp3 = face_point3;
+      vec3 fp1 = triangle_face[0];
+      vec3 fp2 = triangle_face[1];
+      vec3 fp3 = triangle_face[2];
 
       cout << "Points on the plane" << endl;
@@ -239,5 +263,5 @@
       printVector("Intersection", intersection);
 
-      clicked = insideTriangle(intersection, fp1, fp2, fp3);
+      clicked = insideTriangle(intersection, triangle_face);
       cout << (clicked ? "true" : "false") << endl;
 
@@ -328,5 +352,4 @@
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 
-   /*
    GLfloat points[] = {
        0.0f,  0.5f,  0.0f,
@@ -337,19 +360,4 @@
        0.0f,  0.5f,  0.0f,
    };
-   */
-
-   GLfloat points[] = {
-       0.5f,  0.5f,  0.0f,
-       0.0f, -0.5f,  0.0f,
-       1.0f, -0.5f,  0.0f,
-       1.0f, -0.5f,  0.0f,
-       0.0f, -0.5f,  0.0f,
-       0.5f,  0.5f,  0.0f,
-   };
-
-   // initialize global variables for click intersection test
-   face_point1 = vec3(points[0], points[1], points[2]);
-   face_point2 = vec3(points[3], points[4], points[5]);
-   face_point3 = vec3(points[6], points[7], points[8]);
 
    GLfloat colors[] = {
@@ -374,5 +382,4 @@
    int numPoints = (sizeof(points) / sizeof(float)) / 3;
 
-   /*
    GLfloat points2[] = {
       0.5f,  0.5f,  0.0f,
@@ -383,14 +390,4 @@
       0.5f, -0.5f,  0.0f,
    };
-   */
-
-   GLfloat points2[] = {
-       0.0f,  0.5f,  0.0f,
-      -1.0f,  0.5f,  0.0f,
-      -1.0f, -0.5f,  0.0f,
-       0.0f,  0.5f,  0.0f,
-      -1.0f, -0.5f,  0.0f,
-       0.0f, -0.5f,  0.0f,
-   };
 
    GLfloat colors2[] = {
@@ -415,16 +412,37 @@
    int numPoints2 = (sizeof(points2) / sizeof(float)) / 3;
 
+   // initialize global variables for click intersection tests
+
+   colored_triangle = {
+      vec3(points[0], points[1], points[2]),
+      vec3(points[3], points[4], points[5]),
+      vec3(points[6], points[7], points[8]),
+   };
+
+   square_triangle1 = {
+      vec3(points2[0], points2[1], points2[2]),
+      vec3(points2[3], points2[4], points2[5]),
+      vec3(points2[6], points2[7], points2[8]),
+   };
+
+   square_triangle2 = {
+      vec3(points2[9], points2[10], points2[11]),
+      vec3(points2[12], points2[13], points2[14]),
+      vec3(points2[15], points2[16], points2[17]),
+   };
+
+   triangle_face = colored_triangle;
+
    /*
-   mat4 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f));
    mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f));
    */
-   mat4 T_model = translate(mat4(), vec3(0.0f, 0.0f, 0.0f));
+   mat4 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f));
    mat4 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
-   mat4 model_mat = T_model*R_model;
+   model_mat = T_model*R_model;
 
    // mat4 T_model2 = translate(mat4(), vec3(-1.0f, 0.0f, 0.0f));
-   mat4 T_model2 = translate(mat4(), vec3(0.0f, 0.0f, 0.0f));
+   mat4 T_model2 = translate(mat4(), vec3(-0.5f, 0.0f, 0.0f));
    mat4 R_model2 = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
-   mat4 model_mat2 = T_model2*R_model2;
+   model_mat2 = T_model2*R_model2;
 
    GLuint points_vbo = 0;
@@ -523,21 +541,21 @@
    proj_mat = make_mat4(proj_arr);
 
+   GLint model_test_loc = glGetUniformLocation(shader_program, "model");
+   GLint view_test_loc = glGetUniformLocation(shader_program, "view");
+   GLint proj_test_loc = glGetUniformLocation(shader_program, "proj");
+
    GLint model_mat_loc = glGetUniformLocation(shader_program2, "model");
    GLint view_mat_loc = glGetUniformLocation(shader_program2, "view");
    GLint proj_mat_loc = glGetUniformLocation(shader_program2, "proj");
 
-   GLint model_test_loc = glGetUniformLocation(shader_program, "model");
-   GLint view_test_loc = glGetUniformLocation(shader_program, "view");
-   GLint proj_test_loc = glGetUniformLocation(shader_program, "proj");
-
    glUseProgram(shader_program);
    glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat));
+   glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
    glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
 
    glUseProgram(shader_program2);
    glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat2));
+   glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
-
-   // glUniform1i(tex_loc, 0);
 
    bool cam_moved = false;
@@ -575,5 +593,8 @@
 
       glUseProgram(shader_program);
-      glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
+
+      // this is temporary.
+      // It's needed to offset the code for the recoloring of the square working during click detection
+      glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat));
 
       glBindVertexArray(vao);
@@ -583,5 +604,8 @@
       if (clicked_square) {
          glUseProgram(shader_program);
-         glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
+
+         // this is temporary.
+         // It's needed to get the recoloring of the square working during click detection
+         glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat2));
 
          glBindVertexArray(vao2);
@@ -591,5 +615,4 @@
       } else {
          glUseProgram(shader_program2);
-         glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
 
          glBindVertexArray(vao2);
@@ -701,8 +724,8 @@
 }
 
-bool insideTriangle(vec3 p, vec3 v1, vec3 v2, vec3 v3) {
-   vec3 v21 = v2-v1;
-   vec3 v31 = v3-v1;
-   vec3 pv1 = p-v1;
+bool insideTriangle(vec3 p, array<vec3,3> triangle) {
+   vec3 v21 = triangle[1]-triangle[0];
+   vec3 v31 = triangle[2]-triangle[0];
+   vec3 pv1 = p-triangle[0];
 
    float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
