Changeset d9f99b2 in opengl-game for new-game.cpp
- Timestamp:
- Apr 27, 2018, 2:13:07 AM (7 years ago)
- 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)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
r07ed460 rd9f99b2 30 30 #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444 31 31 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 coords37 * for all objects in one array.38 */39 40 41 // might also want to store the shader to be used for the object42 32 struct SceneObject { 33 unsigned int id; 43 34 mat4 model_mat; 44 35 GLuint shader_program; … … 52 43 }; 53 44 54 struct ObjectFace {55 unsigned int object_id;56 array<vec3, 3> points;57 };58 59 45 const bool FULLSCREEN = false; 60 46 int width = 640; … … 67 53 68 54 vector<SceneObject> objects; 69 vector<ObjectFace> faces;70 55 71 56 SceneObject* clickedObject = NULL; … … 74 59 double fps; 75 60 76 bool faceClicked( ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point);61 bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point); 77 62 bool insideTriangle(vec3 p, array<vec3, 3> triangle_points); 78 63 … … 113 98 vec4 click_point; 114 99 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 } 124 119 } 125 120 } 126 121 } 127 122 128 if (closest_ face_id == -1) {123 if (closest_object == NULL) { 129 124 cout << "No object was clicked" << endl; 130 125 } 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; 133 128 } 134 129 } … … 217 212 // triangle 218 213 objects.push_back(SceneObject()); 214 objects[0].id = 0; 219 215 objects[0].shader_program = 0; 220 216 objects[0].vertex_vbo_offset = (GLvoid*) (0 * sizeof(float) * 3); … … 260 256 // square 261 257 objects.push_back(SceneObject()); 258 objects[1].id = 1; 262 259 objects[1].shader_program = 0; 263 260 objects[1].vertex_vbo_offset = (GLvoid*) (6 * sizeof(float) * 3); … … 307 304 GLsizeiptr textures_buffer_size = 0; 308 305 309 faces.clear();310 311 unsigned int object_id = 0;312 306 for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) { 313 307 points_buffer_size += obj_it->points.size() * sizeof(GLfloat); 314 308 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++;327 309 } 328 310 … … 701 683 } 702 684 703 bool faceClicked( ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point) {685 bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point) { 704 686 // LINE EQUATION: P = O + Dt 705 687 // O = cam … … 711 693 712 694 // 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]; 715 697 716 698 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); … … 718 700 print4DVector("Full world ray", world_ray); 719 701 720 SceneObject* obj = &objects[face->object_id];721 702 vec3 local_ray = (inverse(obj->model_mat) * world_ray).xyz(); 722 703 vec3 local_cam = (inverse(obj->model_mat) * cam).xyz(); … … 724 705 local_ray = local_ray - local_cam; 725 706 726 float d = -glm::dot( face->points[0], normal);707 float d = -glm::dot(points[0], normal); 727 708 cout << "d: " << d << endl; 728 709 … … 733 714 printVector("Intersection", intersection); 734 715 735 if (insideTriangle(intersection, face->points)) {716 if (insideTriangle(intersection, points)) { 736 717 click_point = obj->model_mat * vec4(intersection, 1.0f); 737 718 return true; … … 740 721 } 741 722 } 742 743 723 bool 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]; 747 727 748 728 float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y); 749 729 float x = (pv1.x-y*v31.x) / v21.x; 750 730 751 cout << "(" << x << ", " << y << ")" << endl;752 753 731 return x > 0.0f && y > 0.0f && x+y < 1.0f; 754 732 }
Note:
See TracChangeset
for help on using the changeset viewer.