Index: new-game.cpp
===================================================================
--- new-game.cpp	(revision 6f73e0c72a1e29e068ebe12351386dee41978f0e)
+++ new-game.cpp	(revision d12d00359e0f8264e69f3a877fb290373acf06cf)
@@ -43,4 +43,9 @@
 GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath);
 unsigned char* loadImage(string file_name, int* x, int* y);
+
+void printVector(string label, vec3 v);
+
+float NEAR_CLIP = 0.1f;
+float FAR_CLIP = 100.0f;
 
 void glfw_error_callback(int error, const char* description) {
@@ -139,10 +144,6 @@
       cout << "(" << i.x << "," << i.y << "," << i.z << ")" << endl;
 
-      bool hit = insideTriangle(i, fp1, fp2, fp3);
-      cout << (hit ? "true" : "false")  << endl;
-
-      if (hit) {
-         clicked = true;
-      }
+      clicked = insideTriangle(i, fp1, fp2, fp3);
+      cout << (clicked ? "true" : "false")  << endl;
    }
 }
@@ -157,4 +158,11 @@
       float x = (2.0f*mouse_x) / width - 1.0f;
       float y = 1.0f - (2.0f*mouse_y) / height;
+
+      //x = -.1f;
+      x = -.25f;
+      //x = -.5f;
+
+      y = .1f;
+
       cout << "x: " << x << ", y: " << y << endl;
 
@@ -172,6 +180,8 @@
       // vec3 ray_world = normalize((inverse(view_mat) * ray_eye).xyz());
 
-      vec4 ray_clip = vec4(x, y, 1.0f, 1.0f); // this should have a z equal to the near clipping plane
-      vec3 ray_world = (inverse(view_mat) * ray_clip).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, 0.0f, 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();
 
       /* LATEST NOTES:
@@ -184,35 +194,20 @@
        */
 
-      // since ray_world is the end result we want anyway, we probably don't need to add cam_pos to
-      // it, only to subtract it later
-
-      vec3 click_point = cam_pos + ray_world;
-
-      /* Now, we need to generate the constants for the equations describing
-       * a 3D line:
-       *   (x - x0) / a = (y - y0) / b = (z - z0) / c
-       *
-       * The line goes through the camera position, so
-       * cam_pos = <x0, y0, z0>
-       */
+      printVector("Initial world ray:", ray_world);
+
+      // Theoretically, I should be able to change the z of the camera to whatever I want.
+      // Figure out why I can't.
+      // Remember that ray_world has to be a ray along the click line, not any point along it
+      // vec3 cam_pos_temp = ray_world;
+      // ray_world = vec3(0.0f, 0.0f, 1.0f);
+
+      vec3 cam_pos_temp = vec3(ray_world.xy(), 0.0f);
+      ray_world = ray_world-cam_pos_temp;
 
       // upper right corner is 1, 1 in opengl
 
-      cout << "Converted -> (" << ray_world.x << "," << ray_world.y << "," << ray_world.z << ")" << endl << endl;;
-      cout << "Camera -> (" << cam_pos.x << "," << cam_pos.y << "," << cam_pos.z << ")" << endl;
-      cout << "Click point -> (" << click_point.x << "," << click_point.y << "," << click_point.z << ")" << endl;
-
-      float a = 1.0f;
-      float b = a * (click_point.y - cam_pos.y) / (click_point.x - cam_pos.x);
-      float c = a * (click_point.z - cam_pos.z) / (click_point.x - cam_pos.x);
-
-      cout << "(x - " << cam_pos.x << ") / " << a << " = ";
-      cout << "(y - " << cam_pos.y << ") / " << b << " = ";
-      cout << "(z - " << cam_pos.z << ") / " << c << endl;;
-
-      /* Now, we need to generate the constants for the equations describing
-       * a 3D plane:
-       * dx + ey +fz +g = 0
-       */
+      cout << "Ray clip -> (" << ray_clip.x << "," << ray_clip.y << "," << ray_clip.z << ")" << endl << endl;;
+      cout << "Ray world -> (" << ray_world.x << "," << ray_world.y << "," << ray_world.z << ")" << endl << endl;;
+      cout << "Camera -> (" << cam_pos_temp.x << "," << cam_pos_temp.y << "," << cam_pos_temp.z << ")" << endl;
 
       vec3 fp1 = face_point1;
@@ -225,29 +220,32 @@
       cout << "(" << fp3.x << ", " << fp3.y << ", " << fp3.z << ")" << endl;
 
-      float pa = (fp2.y-fp1.y)*(fp3.z-fp1.z) - (fp3.y-fp1.y)*(fp2.z-fp1.z);
-      float pb = (fp2.z-fp1.z)*(fp3.x-fp1.x) - (fp3.z-fp1.z)*(fp2.x-fp1.x);
-      float pc = (fp2.x-fp1.x)*(fp3.y-fp1.y) - (fp3.x-fp1.x)*(fp2.y-fp1.y);
-      float pd = -(pa*fp1.x+pb*fp1.y+pc*fp1.z);
-
-      cout << pa << "x+" << pb << "y+" << pc << "z+" << pd << "=0" << endl;
-
-      // get intersection
-
-      // the intersection this computes is incorrect
-      // it doesn't match the equation of the plane
-      vec3 i;
-      i.z = -cam_pos.z - pc*pd/(pa*a+pb*b);
-      i.x = cam_pos.x + a * (i.z-cam_pos.z) / c;
-      i.y = cam_pos.y + b * (i.z-cam_pos.z) / c;
-
-      cout << "The holy grail?" << endl;
-      cout << "(" << i.x << "," << i.y << "," << i.z << ")" << endl;
-
-      bool hit = insideTriangle(i, fp1, fp2, fp3);
-      cout << (hit ? "true" : "false")  << endl;
-
-      if (hit) {
-         clicked = true;
-      }
+      // 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);
+
+      clicked = insideTriangle(intersection, fp1, fp2, fp3);
+      cout << (clicked ? "true" : "false") << endl;
    }
 }
@@ -295,5 +293,5 @@
    }
 
-   glfwSetMouseButtonCallback(window, mouse_button_callback);
+   glfwSetMouseButtonCallback(window, mouse_button_callback_new);
 
    glfwMakeContextCurrent(window);
@@ -336,19 +334,19 @@
 
    GLfloat points[] = {
-      0.0f,  0.5f, -0.001f,
-     -0.5f, -0.5f,  0.0f,
-      0.5f, -0.5f,  0.0f,
-      0.5f, -0.5f,  0.0f,
-     -0.5f, -0.5f,  0.0f,
-      0.0f,  0.5f, -0.001f,
+       0.0f,  0.5f,  0.0f,
+      -0.5f, -0.5f,  0.0f,
+       0.5f, -0.5f,  0.0f,
+       0.5f, -0.5f,  0.0f,
+      -0.5f, -0.5f,  0.0f,
+       0.0f,  0.5f,  0.0f,
    };
    /*
    GLfloat points[] = {
-      0.0f,  1.0f,  0.0f,
-     -1.0f,  0.0f,  0.0f,
-      1.0f,  0.0f,  0.0f,
-      1.0f,  0.0f,  0.0f,
-     -1.0f,  0.0f,  0.0f,
-      0.0f,  1.0f,  0.0f,
+       0.0f,  1.0f,  0.0f,
+      -1.0f,  0.0f,  0.0f,
+       1.0f,  0.0f,  0.0f,
+       1.0f,  0.0f,  0.0f,
+      -1.0f,  0.0f,  0.0f,
+       0.0f,  1.0f,  0.0f,
    };
    */
@@ -381,10 +379,10 @@
 
    GLfloat points2[] = {
-      0.5f,  0.5f,  0.0f,
-     -0.5f,  0.5f,  0.0f,
-     -0.5f, -0.5f,  0.0f,
-      0.5f,  0.5f,  0.0f,
-     -0.5f, -0.5f,  0.0f,
-      0.5f, -0.5f,  0.0f,
+       0.5f,  0.5f,  0.0f,
+      -0.5f,  0.5f,  0.0f,
+      -0.5f, -0.5f,  0.0f,
+       0.5f,  0.5f,  0.0f,
+      -0.5f, -0.5f,  0.0f,
+       0.5f, -0.5f,  0.0f,
    };
 
@@ -485,7 +483,7 @@
    float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
 
-   cam_pos = vec3(0.0f, 0.0f, 2.0f);
-   //cam_pos = vec3(0.0f, 0.0f, 0.0f);
-   float cam_yaw = 0.0f;
+   //cam_pos = vec3(0.0f, 0.0f, 2.0f);
+   cam_pos = vec3(0.0f, 0.0f, 0.0f);
+   float cam_yaw = 45.0f * 2.0f * 3.14159f / 360.0f;
 
    mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
@@ -497,15 +495,14 @@
    view_mat = R*T;
 
-   float near = 0.1f;
-   float far = 100.0f;
    float fov = 67.0f * ONE_DEG_IN_RAD;
    float aspect = (float)width / (float)height;
 
-   float range = tan(fov * 0.5f) * near;
-   float Sx = near / (range * aspect);
-   float Sy = near / range;
-   float Sz = -(far + near) / (far - near);
-   float Pz = -(2.0f * far * near) / (far - near);
-
+   float range = tan(fov * 0.5f) * NEAR_CLIP;
+   float Sx = NEAR_CLIP / (range * aspect);
+   float Sy = NEAR_CLIP / range;
+   float Sz = -(FAR_CLIP + NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
+   float Pz = -(2.0f * FAR_CLIP * NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
+
+   /*
    float proj_arr[] = {
      Sx, 0.0f, 0.0f, 0.0f,
@@ -514,5 +511,5 @@
      0.0f, 0.0f, Pz, 0.0f,
    };
-   /*
+   */
    float proj_arr[] = {
      1.0f, 0.0f, 0.0f, 0.0f,
@@ -521,5 +518,4 @@
      0.0f, 0.0f, 0.0f, 1.0f,
    };
-   */
    proj_mat = make_mat4(proj_arr);
 
@@ -536,7 +532,9 @@
    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(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
+   */
 
    // glUniform1i(tex_loc, 0);
@@ -703,2 +701,6 @@
    return x > 0.0f && y > 0.0f && x+y < 1.0f;
 }
+
+void printVector(string label, vec3 v) {
+   cout << label << " -> (" << v.x << "," << v.y << "," << v.z << ")" << endl;
+}
