Changeset e82692b in opengl-game


Ignore:
Timestamp:
Mar 23, 2018, 4:06:56 AM (7 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
b73cb3b
Parents:
5c9d193
Message:

Pass a reference to faceClicked that will be set to the clicked point in world space, and fix the click detection algorithm to set the closest visible object to the camera as having been clicked.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    r5c9d193 re82692b  
    3535
    3636struct ObjectFace {
    37    unsigned int objectId;
     37   unsigned int object_id;
    3838   array<vec3, 3> points;
    3939};
     
    5454SceneObject* selectedObject = NULL;
    5555
    56 bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam);
     56bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point);
    5757bool insideTriangle(vec3 p, array<vec3, 3> triangle_points);
    5858
     
    238238      cout << "Camera -> (" << cam_pos_temp.x << "," << cam_pos_temp.y << "," << cam_pos_temp.z << ")" << endl;
    239239
     240      vec4 click_point;
     241      vec3 closest_point;
     242      int closest_face_id = -1;
     243
    240244      // Need to account for faces that are behind one another
    241245      // Using an iterator for the loop makes it difficult to get a reference to each face (for the faceClicked function)
    242246      for (int i = 0; i<faces.size(); i++) {
    243          if (faceClicked(&faces[i], ray_world, cam_pos_temp)) {
    244             clickedObject = &objects[faces[i].objectId];
    245             cout << "Clicked object: " << faces[i].objectId << endl;
    246             break;
     247         if (faceClicked(&faces[i], ray_world, cam_pos_temp, click_point)) {
     248            click_point = view_mat * click_point;
     249
     250            // Check against clipping planes once I start applying the projection matrix
     251            // if (NEAR_CLIP <= click_point.z && click_point.z < FAR_CLIP && ...) {
     252            if (-1.0f <= click_point.z && click_point.z < 1.0f && click_point.z < closest_point.z) {
     253               closest_point = click_point.xyz();
     254               closest_face_id = i;
     255            }
    247256         }
    248257      }
    249258
    250       if (clickedObject == NULL) {
     259      if (closest_face_id == -1) {
    251260         cout << "No object was clicked" << endl;
     261      } else {
     262         clickedObject = &objects[faces[closest_face_id].object_id];
     263         cout << "Clicked object: " << faces[closest_face_id].object_id << endl;
     264         printVector("Click point", closest_point);
    252265      }
    253266   }
     
    409422   mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f));
    410423   */
    411    T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f));
     424   T_model = translate(mat4(), vec3(0.0f, 0.0f, 0.0f));
    412425   R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
    413426   objects[0].model_mat = T_model*R_model;
    414427
    415428   faces.push_back(ObjectFace());
    416    faces[0].objectId = 0;
     429   faces[0].object_id = 0;
    417430   faces[0].points = {
    418431      vec3(points[0], points[1], points[2]),
     
    425438
    426439   // mat4 T_model2 = translate(mat4(), vec3(-1.0f, 0.0f, 0.0f));
    427    T_model = translate(mat4(), vec3(-0.5f, 0.0f, 0.0f));
     440   T_model = translate(mat4(), vec3(-0.5f, 0.0f, 0.1f));
    428441   R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
    429442   objects[1].model_mat = T_model*R_model;
    430443
    431444   faces.push_back(ObjectFace());
    432    faces[1].objectId = 1;
     445   faces[1].object_id = 1;
    433446   faces[1].points = {
    434447      vec3(points2[0], points2[1], points2[2]),
     
    438451
    439452   faces.push_back(ObjectFace());
    440    faces[2].objectId = 1;
     453   faces[2].object_id = 1;
    441454   faces[2].points = {
    442455      vec3(points2[9], points2[10], points2[11]),
     
    729742}
    730743
    731 bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam) {
     744bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point) {
    732745   cout << "Points on the plane" << endl;
    733746   printVector("fp1", face->points[0]);
     
    750763   printVector("Cross", normal);
    751764
    752    SceneObject* obj = &objects[face->objectId];
     765   SceneObject* obj = &objects[face->object_id];
    753766   vec3 local_ray = (inverse(obj->model_mat) * world_ray).xyz();
    754767   vec3 local_cam = (inverse(obj->model_mat) * cam).xyz();
     
    767780   printVector("Intersection", intersection);
    768781
    769    return insideTriangle(intersection, face->points);
     782   if (insideTriangle(intersection, face->points)) {
     783      click_point = obj->model_mat * vec4(intersection, 1.0f);
     784      return true;
     785   } else {
     786      return false;
     787   }
    770788}
    771789
Note: See TracChangeset for help on using the changeset viewer.