Changeset c62eee6 in opengl-game


Ignore:
Timestamp:
Aug 25, 2017, 3:10:04 AM (7 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
33a9664
Parents:
485424b
Message:

Use glm mat types instead of arrays and mat swizzle operators (e.g. pos.x) instead of array indices more often, add a mouse click event handler, and start writing code to detect which 3d object the user clicked on.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    r485424b rc62eee6  
    33#include "stb_image.h"
    44
    5 #include <glm/mat4x4.hpp> // glm::mat4
     5#define GLM_SWIZZLE
     6#include <glm/mat4x4.hpp>
    67#include <glm/gtc/matrix_transform.hpp>
    78#include <glm/gtc/type_ptr.hpp>
     
    2122
    2223#define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
     24
    2325const bool FULLSCREEN = false;
     26int width = 640;
     27int height = 480;
     28
     29vec3 cam_pos;
     30
     31vec3 face_point1, face_point2, face_point3;
     32
     33mat4 view_mat;
     34mat4 proj_mat;
    2435
    2536GLuint loadShader(GLenum type, string file);
     
    2940void glfw_error_callback(int error, const char* description) {
    3041   gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
     42}
     43
     44void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
     45   double mouse_x, mouse_y;
     46   glfwGetCursorPos(window, &mouse_x, &mouse_y);
     47
     48   if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
     49      cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;
     50
     51      float x = (2.0f*mouse_x) / width - 1.0f;
     52      float y = 1.0f - (2.0f*mouse_y) / height;
     53
     54      vec4 ray_clip = vec4(x, y, -1.0f, 1.0f);
     55      vec4 ray_eye = inverse(proj_mat) * ray_clip;
     56      ray_eye = vec4(ray_eye.xy(), -1.0f, 0.0f);
     57      vec3 ray_world = normalize((inverse(view_mat) * ray_eye).xyz());
     58
     59      vec3 click_point = cam_pos + ray_world;
     60
     61      /* Now, we need to generate the constants for the equations describing
     62       * a 3D line:
     63       *   (x - x0) / a = (y - y0) / b = (z - z0) / c
     64       *
     65       * The line goes through the camera position, so
     66       * cam_pos = <x0, y0, z0>
     67       */
     68
     69      cout << "Converted -> (" << ray_world.x << "," << ray_world.y << "," << ray_world.z << ")" << endl << endl;;
     70      cout << "Camera -> (" << cam_pos.x << "," << cam_pos.y << "," << cam_pos.z << ")" << endl;
     71      cout << "Click point -> (" << click_point.x << "," << click_point.y << "," << click_point.z << ")" << endl;
     72
     73      float a = 1.0f;
     74      float b = a * (click_point.y - cam_pos.y) / (click_point.x - cam_pos.x);
     75      float c = a * (click_point.z - cam_pos.z) / (click_point.x - cam_pos.x);
     76
     77      cout << "(x - " << cam_pos.x << ") / " << a << " = ";
     78      cout << "(y - " << cam_pos.y << ") / " << b << " = ";
     79      cout << "(z - " << cam_pos.z << ") / " << c << endl;;
     80
     81      /* Now, we need to generate the constants for the equations describing
     82       * a 3D plane:
     83       * dx + ey +fz +g = 0
     84       */
     85
     86      cout << "Points on the plane" << endl;
     87      cout << "(" << face_point1.x << "," << face_point1.y << "," << face_point1.z << ")" << endl;
     88      cout << "(" << face_point2.x << "," << face_point2.y << "," << face_point2.z << ")" << endl;
     89      cout << "(" << face_point3.x << "," << face_point3.y << "," << face_point3.z << ")" << endl;
     90
     91      // get intersection
     92   }
    3193}
    3294
     
    54116   GLFWwindow* window = NULL;
    55117
    56    int width = 640;
    57    int height = 480;
    58 
    59118   if (FULLSCREEN) {
    60119      GLFWmonitor* mon = glfwGetPrimaryMonitor();
     
    75134      return 1;
    76135   }
     136
     137   glfwSetMouseButtonCallback(window, mouse_button_callback);
     138
    77139   glfwMakeContextCurrent(window);
    78140   glewExperimental = GL_TRUE;
     
    122184   };
    123185
     186   // initialize global variables for click intersection test
     187   face_point1 = vec3(points[0], points[1], points[2]);
     188   face_point2 = vec3(points[3], points[4], points[5]);
     189   face_point3 = vec3(points[6], points[7], points[8]);
     190
    124191   GLfloat colors[] = {
    125192     1.0, 0.0, 0.0,
     
    166233   int numPoints2 = (sizeof(points2) / sizeof(float)) / 3;
    167234
     235   /*
    168236   mat4 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f));
    169237   mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f));
     238   */
     239   mat4 T_model = translate(mat4(), vec3(0.0f, 0.0f, 0.0f));
     240   mat4 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
    170241   mat4 model_mat = T_model*R_model;
    171242
     
    234305   float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
    235306
    236    float cam_pos[] = {0.0f, 0.0f, 2.0f};
     307   cam_pos = vec3(0.0f, 0.0f, 2.0f);
    237308   float cam_yaw = 0.0f;
    238309
    239    mat4 T = translate(mat4(), vec3(-cam_pos[0], -cam_pos[1], -cam_pos[2]));
     310   mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
    240311   mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
    241    mat4 view_mat = R*T;
     312   view_mat = R*T;
    242313
    243314   float near = 0.1f;
     
    252323   float Pz = -(2.0f * far * near) / (far - near);
    253324
    254    float proj_mat[] = {
     325   float proj_arr[] = {
    255326     Sx, 0.0f, 0.0f, 0.0f,
    256327     0.0f, Sy, 0.0f, 0.0f,
     
    258329     0.0f, 0.0f, Pz, 0.0f,
    259330   };
     331   proj_mat = make_mat4(proj_arr);
    260332
    261333   GLint model_mat_loc = glGetUniformLocation(shader_program2, "model");
     
    269341   glUseProgram(shader_program);
    270342   glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat));
    271    glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, proj_mat);
     343   glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
    272344
    273345   glUseProgram(shader_program2);
    274346   glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat2));
    275    glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, proj_mat);
     347   glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
    276348
    277349   // glUniform1i(tex_loc, 0);
     
    319391      float dist = cam_speed * elapsed_seconds;
    320392      if (glfwGetKey(window, GLFW_KEY_A)) {
    321          cam_pos[0] -= cos(cam_yaw)*dist;
    322          cam_pos[2] += sin(cam_yaw)*dist;
     393         cam_pos.x -= cos(cam_yaw)*dist;
     394         cam_pos.z += sin(cam_yaw)*dist;
    323395         cam_moved = true;
    324396      }
    325397      if (glfwGetKey(window, GLFW_KEY_D)) {
    326          cam_pos[0] += cos(cam_yaw)*dist;
    327          cam_pos[2] -= sin(cam_yaw)*dist;
     398         cam_pos.x += cos(cam_yaw)*dist;
     399         cam_pos.z -= sin(cam_yaw)*dist;
    328400         cam_moved = true;
    329401      }
    330402      if (glfwGetKey(window, GLFW_KEY_W)) {
    331          cam_pos[0] -= sin(cam_yaw)*dist;
    332          cam_pos[2] -= cos(cam_yaw)*dist;
     403         cam_pos.x -= sin(cam_yaw)*dist;
     404         cam_pos.z -= cos(cam_yaw)*dist;
    333405         cam_moved = true;
    334406      }
    335407      if (glfwGetKey(window, GLFW_KEY_S)) {
    336          cam_pos[0] += sin(cam_yaw)*dist;
    337          cam_pos[2] += cos(cam_yaw)*dist;
     408         cam_pos.x += sin(cam_yaw)*dist;
     409         cam_pos.z += cos(cam_yaw)*dist;
    338410         cam_moved = true;
    339411      }
     
    347419      }
    348420      if (cam_moved) {
    349          T = translate(mat4(), vec3(-cam_pos[0], -cam_pos[1], -cam_pos[2]));
     421         T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
    350422         R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
    351423         view_mat = R*T;
Note: See TracChangeset for help on using the changeset viewer.