Index: new-game.cpp
===================================================================
--- new-game.cpp	(revision 485424b115047116d3e57c15d4a4583a1018a048)
+++ new-game.cpp	(revision c62eee67d1b1bb7ea638fbbb17aa6b5bf8a110a6)
@@ -3,5 +3,6 @@
 #include "stb_image.h"
 
-#include <glm/mat4x4.hpp> // glm::mat4
+#define GLM_SWIZZLE
+#include <glm/mat4x4.hpp>
 #include <glm/gtc/matrix_transform.hpp>
 #include <glm/gtc/type_ptr.hpp>
@@ -21,5 +22,15 @@
 
 #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
+
 const bool FULLSCREEN = false;
+int width = 640;
+int height = 480;
+
+vec3 cam_pos;
+
+vec3 face_point1, face_point2, face_point3;
+
+mat4 view_mat;
+mat4 proj_mat;
 
 GLuint loadShader(GLenum type, string file);
@@ -29,4 +40,55 @@
 void glfw_error_callback(int error, const char* description) {
    gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
+}
+
+void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
+   double mouse_x, mouse_y;
+   glfwGetCursorPos(window, &mouse_x, &mouse_y);
+
+   if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
+      cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;
+
+      float x = (2.0f*mouse_x) / width - 1.0f;
+      float y = 1.0f - (2.0f*mouse_y) / height;
+
+      vec4 ray_clip = vec4(x, y, -1.0f, 1.0f);
+      vec4 ray_eye = inverse(proj_mat) * ray_clip;
+      ray_eye = vec4(ray_eye.xy(), -1.0f, 0.0f);
+      vec3 ray_world = normalize((inverse(view_mat) * ray_eye).xyz());
+
+      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>
+       */
+
+      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 << "Points on the plane" << endl;
+      cout << "(" << face_point1.x << "," << face_point1.y << "," << face_point1.z << ")" << endl;
+      cout << "(" << face_point2.x << "," << face_point2.y << "," << face_point2.z << ")" << endl;
+      cout << "(" << face_point3.x << "," << face_point3.y << "," << face_point3.z << ")" << endl;
+
+      // get intersection
+   }
 }
 
@@ -54,7 +116,4 @@
    GLFWwindow* window = NULL;
 
-   int width = 640;
-   int height = 480;
-
    if (FULLSCREEN) {
       GLFWmonitor* mon = glfwGetPrimaryMonitor();
@@ -75,4 +134,7 @@
       return 1;
    }
+
+   glfwSetMouseButtonCallback(window, mouse_button_callback);
+
    glfwMakeContextCurrent(window);
    glewExperimental = GL_TRUE;
@@ -122,4 +184,9 @@
    };
 
+   // 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[] = {
      1.0, 0.0, 0.0,
@@ -166,6 +233,10 @@
    int numPoints2 = (sizeof(points2) / sizeof(float)) / 3;
 
+   /*
    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 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
    mat4 model_mat = T_model*R_model;
 
@@ -234,10 +305,10 @@
    float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
 
-   float cam_pos[] = {0.0f, 0.0f, 2.0f};
+   cam_pos = vec3(0.0f, 0.0f, 2.0f);
    float cam_yaw = 0.0f;
 
-   mat4 T = translate(mat4(), vec3(-cam_pos[0], -cam_pos[1], -cam_pos[2]));
+   mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
    mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
-   mat4 view_mat = R*T;
+   view_mat = R*T;
 
    float near = 0.1f;
@@ -252,5 +323,5 @@
    float Pz = -(2.0f * far * near) / (far - near);
 
-   float proj_mat[] = {
+   float proj_arr[] = {
      Sx, 0.0f, 0.0f, 0.0f,
      0.0f, Sy, 0.0f, 0.0f,
@@ -258,4 +329,5 @@
      0.0f, 0.0f, Pz, 0.0f,
    };
+   proj_mat = make_mat4(proj_arr);
 
    GLint model_mat_loc = glGetUniformLocation(shader_program2, "model");
@@ -269,9 +341,9 @@
    glUseProgram(shader_program);
    glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat));
-   glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, proj_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(proj_mat_loc, 1, GL_FALSE, proj_mat);
+   glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
 
    // glUniform1i(tex_loc, 0);
@@ -319,21 +391,21 @@
       float dist = cam_speed * elapsed_seconds;
       if (glfwGetKey(window, GLFW_KEY_A)) {
-         cam_pos[0] -= cos(cam_yaw)*dist;
-         cam_pos[2] += sin(cam_yaw)*dist;
+         cam_pos.x -= cos(cam_yaw)*dist;
+         cam_pos.z += sin(cam_yaw)*dist;
          cam_moved = true;
       }
       if (glfwGetKey(window, GLFW_KEY_D)) {
-         cam_pos[0] += cos(cam_yaw)*dist;
-         cam_pos[2] -= sin(cam_yaw)*dist;
+         cam_pos.x += cos(cam_yaw)*dist;
+         cam_pos.z -= sin(cam_yaw)*dist;
          cam_moved = true;
       }
       if (glfwGetKey(window, GLFW_KEY_W)) {
-         cam_pos[0] -= sin(cam_yaw)*dist;
-         cam_pos[2] -= cos(cam_yaw)*dist;
+         cam_pos.x -= sin(cam_yaw)*dist;
+         cam_pos.z -= cos(cam_yaw)*dist;
          cam_moved = true;
       }
       if (glfwGetKey(window, GLFW_KEY_S)) {
-         cam_pos[0] += sin(cam_yaw)*dist;
-         cam_pos[2] += cos(cam_yaw)*dist;
+         cam_pos.x += sin(cam_yaw)*dist;
+         cam_pos.z += cos(cam_yaw)*dist;
          cam_moved = true;
       }
@@ -347,5 +419,5 @@
       }
       if (cam_moved) {
-         T = translate(mat4(), vec3(-cam_pos[0], -cam_pos[1], -cam_pos[2]));
+         T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
          R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
          view_mat = R*T;
