Changeset df652d5 in opengl-game
- Timestamp:
- Mar 17, 2018, 2:44:39 AM (7 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 147ac6d
- Parents:
- de1d7f6
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
rde1d7f6 rdf652d5 19 19 #include <string> 20 20 #include <array> 21 #include <vector> 21 22 22 23 using namespace std; … … 24 25 25 26 #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444 27 28 struct SceneObject { 29 mat4 model_mat; 30 bool clicked; 31 }; 32 33 struct ObjectFace { 34 unsigned int objectId; 35 array<vec3, 3> points; 36 }; 26 37 27 38 const bool FULLSCREEN = false; … … 31 42 vec3 cam_pos; 32 43 33 array<vec3, 3> triangle_face;34 35 array<vec3,3> colored_triangle;36 array<vec3, 3> square_triangle1;37 array<vec3, 3> square_triangle2;38 39 bool clicked = false;40 44 int colors_i = 0; 41 42 bool clicked_square = false;43 44 mat4 model_mat;45 mat4 model_mat2;46 45 47 46 mat4 view_mat; 48 47 mat4 proj_mat; 49 48 50 bool insideTriangle(vec3 p, array<vec3, 3> triangle); 49 vector<SceneObject> objects; 50 vector<ObjectFace> faces; 51 52 bool insideTriangle(vec3 p, ObjectFace* face); 51 53 52 54 GLuint loadShader(GLenum type, string file); … … 64 66 65 67 void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) { 68 /* 66 69 double mouse_x, mouse_y; 67 70 glfwGetCursorPos(window, &mouse_x, &mouse_y); … … 85 88 vec3 ray_world = normalize((inverse(view_mat) * ray_eye).xyz()); 86 89 87 / * LATEST NOTES:90 / * LATEST NOTES: 88 91 * 89 92 * Normalizing the world ray caused issues, although it should make sense with the projection … … 92 95 * Now, I need to figure out the correct intersection test in 2D space 93 96 * Also, need to check that the global triangle points are correct 94 * /97 * / 95 98 96 99 // since ray_world is the end result we want anyway, we probably don't need to add cam_pos to … … 99 102 vec3 click_point = cam_pos + ray_world; 100 103 101 / * Now, we need to generate the constants for the equations describing104 / * Now, we need to generate the constants for the equations describing 102 105 * a 3D line: 103 106 * (x - x0) / a = (y - y0) / b = (z - z0) / c … … 105 108 * The line goes through the camera position, so 106 109 * cam_pos = <x0, y0, z0> 107 * /110 * / 108 111 109 112 // upper right corner is 1, 1 in opengl … … 121 124 cout << "(z - " << cam_pos.z << ") / " << c << endl;; 122 125 123 / * Now, we need to generate the constants for the equations describing126 / * Now, we need to generate the constants for the equations describing 124 127 * a 3D plane: 125 128 * dx + ey +fz +g = 0 126 * /129 * / 127 130 128 131 vec3 fp1 = triangle_face[0]; … … 157 160 cout << (clicked ? "true" : "false") << endl; 158 161 } 162 */ 159 163 } 160 164 … … 180 184 * -An array of 3 points representing the face 181 185 * -The object struct represnting the object the face is a part of 186 * 187 * -Really, all I need to pass in are the world ray and an ObjectFace reference 188 * -The world ray will first need to be multiplied by the view and projection matrices before being passed in 182 189 */ 183 190 … … 185 192 double mouse_x, mouse_y; 186 193 glfwGetCursorPos(window, &mouse_x, &mouse_y); 194 195 ObjectFace* face = &faces[0]; 196 SceneObject* obj = &objects[face->objectId]; 187 197 188 198 if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) { … … 207 217 vec4 ray_clip = vec4(x, y, NEAR_CLIP, 1.0f); // this should have a z equal to the near clipping plane 208 218 vec4 ray_eye = ray_clip; 209 vec3 ray_world = (inverse( model_mat) * inverse(view_mat) * ray_eye).xyz();219 vec3 ray_world = (inverse(obj->model_mat) * inverse(view_mat) * ray_eye).xyz(); 210 220 211 221 /* LATEST NOTES: … … 220 230 221 231 vec4 cam_pos_origin = vec4(x, y, 0.0f, 1.0f); 222 vec3 cam_pos_temp = (inverse( model_mat) * inverse(view_mat) * cam_pos_origin).xyz();232 vec3 cam_pos_temp = (inverse(obj->model_mat) * inverse(view_mat) * cam_pos_origin).xyz(); 223 233 224 234 ray_world = ray_world-cam_pos_temp; … … 228 238 cout << "Camera -> (" << cam_pos_temp.x << "," << cam_pos_temp.y << "," << cam_pos_temp.z << ")" << endl; 229 239 230 vec3 fp1 = triangle_face[0];231 vec3 fp2 = triangle_face[1];232 vec3 fp3 = triangle_face[2];240 vec3 fp1 = face->points[0]; 241 vec3 fp2 = face->points[1]; 242 vec3 fp3 = face->points[2]; 233 243 234 244 cout << "Points on the plane" << endl; … … 263 273 printVector("Intersection", intersection); 264 274 265 clicked = insideTriangle(intersection, triangle_face); 266 cout << (clicked ? "true" : "false") << endl; 267 268 clicked_square = !clicked_square; 275 obj->clicked = insideTriangle(intersection, face); 276 cout << (obj->clicked ? "true" : "false") << endl; 269 277 } 270 278 } … … 312 320 } 313 321 322 bool squareSelected = false; 323 314 324 glfwSetMouseButtonCallback(window, mouse_button_callback_new); 315 325 … … 318 328 glewInit(); 319 329 330 // Check if we might ever need this. If not, remove it. 320 331 // glViewport(0, 0, width*2, height*2); 321 332 … … 414 425 // initialize global variables for click intersection tests 415 426 416 colored_triangle = { 427 mat4 T_model, R_model; 428 429 // triangle 430 objects.push_back(SceneObject()); 431 objects[0].clicked = false; 432 433 /* 434 mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f)); 435 */ 436 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f)); 437 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f)); 438 objects[0].model_mat = T_model*R_model; 439 440 faces.push_back(ObjectFace()); 441 faces[0].objectId = 0; 442 faces[0].points = { 417 443 vec3(points[0], points[1], points[2]), 418 444 vec3(points[3], points[4], points[5]), … … 420 446 }; 421 447 422 square_triangle1 = { 448 // square 449 objects.push_back(SceneObject()); 450 objects[1].clicked = false; 451 452 // mat4 T_model2 = translate(mat4(), vec3(-1.0f, 0.0f, 0.0f)); 453 T_model = translate(mat4(), vec3(-0.5f, 0.0f, 0.0f)); 454 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f)); 455 objects[1].model_mat = T_model*R_model; 456 457 faces.push_back(ObjectFace()); 458 faces[1].objectId = 1; 459 faces[1].points = { 423 460 vec3(points2[0], points2[1], points2[2]), 424 461 vec3(points2[3], points2[4], points2[5]), … … 426 463 }; 427 464 428 square_triangle2 = { 465 faces.push_back(ObjectFace()); 466 faces[2].objectId = 1; 467 faces[2].points = { 429 468 vec3(points2[9], points2[10], points2[11]), 430 469 vec3(points2[12], points2[13], points2[14]), 431 470 vec3(points2[15], points2[16], points2[17]), 432 471 }; 433 434 triangle_face = colored_triangle;435 436 /*437 mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f));438 */439 mat4 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f));440 mat4 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));441 model_mat = T_model*R_model;442 443 // mat4 T_model2 = translate(mat4(), vec3(-1.0f, 0.0f, 0.0f));444 mat4 T_model2 = translate(mat4(), vec3(-0.5f, 0.0f, 0.0f));445 mat4 R_model2 = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));446 model_mat2 = T_model2*R_model2;447 472 448 473 GLuint points_vbo = 0; … … 550 575 551 576 glUseProgram(shader_program); 552 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr( model_mat));577 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat)); 553 578 glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat)); 554 579 glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat)); 555 580 556 581 glUseProgram(shader_program2); 557 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr( model_mat2));582 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat)); 558 583 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat)); 559 584 glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat)); … … 571 596 } 572 597 573 if ( clicked) {598 if (objects[0].clicked) { 574 599 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo); 575 600 … … 581 606 colors_i = 0; 582 607 } 583 584 clicked = false;585 608 } 586 609 … … 596 619 // this is temporary. 597 620 // It's needed to offset the code for the recoloring of the square working during click detection 598 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr( model_mat));621 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat)); 599 622 600 623 glBindVertexArray(vao); … … 602 625 glDrawArrays(GL_TRIANGLES, 0, numPoints); 603 626 604 if (clicked_square) { 627 if (objects[1].clicked) { 628 squareSelected = !squareSelected; 629 } 630 631 if (squareSelected) { 605 632 glUseProgram(shader_program); 606 633 607 634 // this is temporary. 608 635 // It's needed to get the recoloring of the square working during click detection 609 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr( model_mat2));636 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat)); 610 637 611 638 glBindVertexArray(vao2); … … 623 650 624 651 glDrawArrays(GL_TRIANGLES, 0, numPoints2); 652 653 for (vector<SceneObject>::iterator it = objects.begin(); it != objects.end(); it++) { 654 it->clicked = false; 655 } 625 656 626 657 glfwPollEvents(); … … 724 755 } 725 756 726 bool insideTriangle(vec3 p, array<vec3,3> triangle) {727 vec3 v21 = triangle[1]-triangle[0];728 vec3 v31 = triangle[2]-triangle[0];729 vec3 pv1 = p- triangle[0];757 bool insideTriangle(vec3 p, ObjectFace* face) { 758 vec3 v21 = face->points[1]- face->points[0]; 759 vec3 v31 = face->points[2]- face->points[0]; 760 vec3 pv1 = p- face->points[0]; 730 761 731 762 float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
Note:
See TracChangeset
for help on using the changeset viewer.