Changeset d9f99b2 in opengl-game for new-game.cpp


Ignore:
Timestamp:
Apr 27, 2018, 2:13:07 AM (7 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
9dd2eb7
Parents:
07ed460
git-author:
Dmitry Portnoy <dmp1488@…> (04/27/18 01:48:57)
git-committer:
Dmitry Portnoy <dmp1488@…> (04/27/18 02:13:07)
Message:

Remove all instances of ObjectFace and just reference the points vector in each SceneObject for face info.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    r07ed460 rd9f99b2  
    3030#define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
    3131
    32 /*
    33  * If I use one array to store the points for all the object faces in the scene, I'll probably remove the ObjectFace object,
    34  * and store the start and end indices of a given object's point coordinates in that array in the SceneObject.
    35  *
    36  * Should probably do something similar with colors and texture coordinates, once I figure out the best way to store tex coords
    37  * for all objects in one array.
    38  */
    39 
    40 
    41 // might also want to store the shader to be used for the object
    4232struct SceneObject {
     33   unsigned int id;
    4334   mat4 model_mat;
    4435   GLuint shader_program;
     
    5243};
    5344
    54 struct ObjectFace {
    55    unsigned int object_id;
    56    array<vec3, 3> points;
    57 };
    58 
    5945const bool FULLSCREEN = false;
    6046int width = 640;
     
    6753
    6854vector<SceneObject> objects;
    69 vector<ObjectFace> faces;
    7055
    7156SceneObject* clickedObject = NULL;
     
    7459double fps;
    7560
    76 bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point);
     61bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point);
    7762bool insideTriangle(vec3 p, array<vec3, 3> triangle_points);
    7863
     
    11398      vec4 click_point;
    11499      vec3 closest_point = vec3(0.0f, 0.0f, -FAR_CLIP); // Any valid point will be closer than the far clipping plane, so initial value to that
    115       int closest_face_id = -1;
    116 
    117       for (int i = 0; i<faces.size(); i++) {
    118          if (faceClicked(&faces[i], ray_world, cam_pos_temp, click_point)) {
    119             click_point = view_mat * click_point;
    120 
    121             if (-NEAR_CLIP >= click_point.z && click_point.z > -FAR_CLIP && click_point.z > closest_point.z) {
    122                closest_point = click_point.xyz();
    123                closest_face_id = i;
     100      SceneObject* closest_object = NULL;
     101
     102      SceneObject* obj;
     103      for (vector<SceneObject>::iterator it = objects.begin(); it != objects.end(); it++) {
     104         obj = &*it;
     105
     106         for (int p_idx = 0; p_idx < it->points.size(); p_idx += 9) {
     107            if (faceClicked({
     108               vec3(it->points[p_idx], it->points[p_idx + 1], it->points[p_idx + 2]),
     109               vec3(it->points[p_idx + 3], it->points[p_idx + 4], it->points[p_idx + 5]),
     110               vec3(it->points[p_idx + 6], it->points[p_idx + 7], it->points[p_idx + 8]),
     111            },
     112            obj, ray_world, cam_pos_temp, click_point)) {
     113               click_point = view_mat * click_point;
     114
     115               if (-NEAR_CLIP >= click_point.z && click_point.z > -FAR_CLIP && click_point.z > closest_point.z) {
     116                  closest_point = click_point.xyz();
     117                  closest_object = obj;
     118               }
    124119            }
    125120         }
    126121      }
    127122
    128       if (closest_face_id == -1) {
     123      if (closest_object == NULL) {
    129124         cout << "No object was clicked" << endl;
    130125      } else {
    131          clickedObject = &objects[faces[closest_face_id].object_id];
    132          cout << "Clicked object: " << faces[closest_face_id].object_id << endl;
     126         clickedObject = closest_object;
     127         cout << "Clicked object: " << clickedObject->id << endl;
    133128      }
    134129   }
     
    217212   // triangle
    218213   objects.push_back(SceneObject());
     214   objects[0].id = 0;
    219215   objects[0].shader_program = 0;
    220216   objects[0].vertex_vbo_offset = (GLvoid*) (0 * sizeof(float) * 3);
     
    260256   // square
    261257   objects.push_back(SceneObject());
     258   objects[1].id = 1;
    262259   objects[1].shader_program = 0;
    263260   objects[1].vertex_vbo_offset = (GLvoid*) (6 * sizeof(float) * 3);
     
    307304   GLsizeiptr textures_buffer_size = 0;
    308305
    309    faces.clear();
    310 
    311    unsigned int object_id = 0;
    312306   for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
    313307      points_buffer_size += obj_it->points.size() * sizeof(GLfloat);
    314308      textures_buffer_size += obj_it->texcoords.size() * sizeof(GLfloat);
    315 
    316       for (int p_idx = 0; p_idx < obj_it->points.size(); p_idx += 9) {
    317          faces.push_back(ObjectFace());
    318          faces.back().object_id = object_id;
    319          faces.back().points = {
    320             vec3(obj_it->points[p_idx], obj_it->points[p_idx + 1], obj_it->points[p_idx + 2]),
    321             vec3(obj_it->points[p_idx + 3], obj_it->points[p_idx + 4], obj_it->points[p_idx + 5]),
    322             vec3(obj_it->points[p_idx + 6], obj_it->points[p_idx + 7], obj_it->points[p_idx + 8]),
    323          };
    324       }
    325 
    326       object_id++;
    327309   }
    328310
     
    701683}
    702684
    703 bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point) {
     685bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point) {
    704686   // LINE EQUATION:            P = O + Dt
    705687   // O = cam
     
    711693
    712694   // Take the cross-product of two vectors on the plane to get the normal
    713    vec3 v1 = face->points[1] - face->points[0];
    714    vec3 v2 = face->points[2] - face->points[0];
     695   vec3 v1 = points[1] - points[0];
     696   vec3 v2 = points[2] - points[0];
    715697
    716698   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);
     
    718700   print4DVector("Full world ray", world_ray);
    719701
    720    SceneObject* obj = &objects[face->object_id];
    721702   vec3 local_ray = (inverse(obj->model_mat) * world_ray).xyz();
    722703   vec3 local_cam = (inverse(obj->model_mat) * cam).xyz();
     
    724705   local_ray = local_ray - local_cam;
    725706
    726    float d = -glm::dot(face->points[0], normal);
     707   float d = -glm::dot(points[0], normal);
    727708   cout << "d: " << d << endl;
    728709
     
    733714   printVector("Intersection", intersection);
    734715
    735    if (insideTriangle(intersection, face->points)) {
     716   if (insideTriangle(intersection, points)) {
    736717      click_point = obj->model_mat * vec4(intersection, 1.0f);
    737718      return true;
     
    740721   }
    741722}
    742 
    743723bool insideTriangle(vec3 p, array<vec3, 3> triangle_points) {
    744    vec3 v21 = triangle_points[1]- triangle_points[0];
    745    vec3 v31 = triangle_points[2]- triangle_points[0];
    746    vec3 pv1 = p- triangle_points[0];
     724   vec3 v21 = triangle_points[1] - triangle_points[0];
     725   vec3 v31 = triangle_points[2] - triangle_points[0];
     726   vec3 pv1 = p - triangle_points[0];
    747727
    748728   float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
    749729   float x = (pv1.x-y*v31.x) / v21.x;
    750730
    751    cout << "(" << x << ", " << y << ")" << endl;
    752 
    753731   return x > 0.0f && y > 0.0f && x+y < 1.0f;
    754732}
Note: See TracChangeset for help on using the changeset viewer.