Changeset e82692b in opengl-game
- Timestamp:
- Mar 23, 2018, 4:06:56 AM (7 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- b73cb3b
- Parents:
- 5c9d193
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
r5c9d193 re82692b 35 35 36 36 struct ObjectFace { 37 unsigned int object Id;37 unsigned int object_id; 38 38 array<vec3, 3> points; 39 39 }; … … 54 54 SceneObject* selectedObject = NULL; 55 55 56 bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam );56 bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point); 57 57 bool insideTriangle(vec3 p, array<vec3, 3> triangle_points); 58 58 … … 238 238 cout << "Camera -> (" << cam_pos_temp.x << "," << cam_pos_temp.y << "," << cam_pos_temp.z << ")" << endl; 239 239 240 vec4 click_point; 241 vec3 closest_point; 242 int closest_face_id = -1; 243 240 244 // Need to account for faces that are behind one another 241 245 // Using an iterator for the loop makes it difficult to get a reference to each face (for the faceClicked function) 242 246 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 } 247 256 } 248 257 } 249 258 250 if (cl ickedObject == NULL) {259 if (closest_face_id == -1) { 251 260 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); 252 265 } 253 266 } … … 409 422 mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f)); 410 423 */ 411 T_model = translate(mat4(), vec3(0. 5f, 0.0f, 0.0f));424 T_model = translate(mat4(), vec3(0.0f, 0.0f, 0.0f)); 412 425 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f)); 413 426 objects[0].model_mat = T_model*R_model; 414 427 415 428 faces.push_back(ObjectFace()); 416 faces[0].object Id = 0;429 faces[0].object_id = 0; 417 430 faces[0].points = { 418 431 vec3(points[0], points[1], points[2]), … … 425 438 426 439 // 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)); 428 441 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f)); 429 442 objects[1].model_mat = T_model*R_model; 430 443 431 444 faces.push_back(ObjectFace()); 432 faces[1].object Id = 1;445 faces[1].object_id = 1; 433 446 faces[1].points = { 434 447 vec3(points2[0], points2[1], points2[2]), … … 438 451 439 452 faces.push_back(ObjectFace()); 440 faces[2].object Id = 1;453 faces[2].object_id = 1; 441 454 faces[2].points = { 442 455 vec3(points2[9], points2[10], points2[11]), … … 729 742 } 730 743 731 bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam ) {744 bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point) { 732 745 cout << "Points on the plane" << endl; 733 746 printVector("fp1", face->points[0]); … … 750 763 printVector("Cross", normal); 751 764 752 SceneObject* obj = &objects[face->object Id];765 SceneObject* obj = &objects[face->object_id]; 753 766 vec3 local_ray = (inverse(obj->model_mat) * world_ray).xyz(); 754 767 vec3 local_cam = (inverse(obj->model_mat) * cam).xyz(); … … 767 780 printVector("Intersection", intersection); 768 781 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 } 770 788 } 771 789
Note:
See TracChangeset
for help on using the changeset viewer.