Changeset f7d35da in opengl-game for new-game.cpp
- Timestamp:
- Jun 10, 2018, 9:31:48 PM (6 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 5c403fe
- Parents:
- 0d5c100
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
r0d5c100 rf7d35da 36 36 using namespace std; 37 37 using namespace glm; 38 39 #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.01744444440 38 41 39 struct SceneObject { … … 63 61 }; 64 62 63 #define NUM_KEYS (512) 64 #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444 (maybe make this a const instead) 65 66 const int KEY_STATE_UNCHANGED = -1; 65 67 const bool FULLSCREEN = false; 66 const bool SHOW_FPS = true; 67 const bool DISABLE_VSYNC = true; // disable vsync to see real framerate 68 unsigned int MAX_UNIFORMS = 0; // Requires OpenGL constants only available at runtime 68 const bool SHOW_FPS = false; 69 const bool DISABLE_VSYNC = false; // disable vsync to see real framerate 70 unsigned int MAX_UNIFORMS = 0; // Requires OpenGL constants only available at runtime, so it can't be const 71 72 int key_state[NUM_KEYS]; 73 bool key_pressed[NUM_KEYS]; 69 74 70 75 int width = 640; … … 95 100 96 101 void mouse_button_callback(GLFWwindow* window, int button, int action, int mods); 102 void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); 97 103 98 104 bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point); … … 212 218 213 219 glfwSetMouseButtonCallback(window, mouse_button_callback); 220 glfwSetKeyCallback(window, key_callback); 214 221 215 222 const GLubyte* renderer = glGetString(GL_RENDERER); … … 388 395 1.0f, 1.0f, 389 396 0.0f, 0.0f, 390 1.0f, 0.0f 397 1.0f, 0.0f, 391 398 }; 392 399 obj.selected_colors = { … … 399 406 }; 400 407 401 T_model = translate(mat4(), vec3(0.0f, -0. 65f, 0.0f));408 T_model = translate(mat4(), vec3(0.0f, -0.9f, 0.0f)); 402 409 R_model = rotate(mat4(), -1.0f, vec3(1.0f, 0.0f, 0.0f)); 403 obj.model_mat = T_model * R_model;410 obj.model_mat = T_model; //T_model * R_model; 404 411 405 412 addObjectToScene(obj); … … 463 470 glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, 0); 464 471 465 float speed = 1.0f;466 float last_position = 0.0f;467 468 472 float cam_speed = 1.0f; 469 473 float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD; … … 553 557 } 554 558 555 if (fabs(last_position) > 1.0f) { 556 speed = -speed; 557 } 558 559 // Handle events (Ideally, move all event-handling code 560 // before the render code) 559 // Handle events 561 560 562 561 clickedObject = NULL; 562 563 // reset the all key states to KEY_STATE_UNCHANGED (something the GLFW key callback can never return) 564 // so that GLFW_PRESS and GLFW_RELEASE are only detected once 565 // TODO: Change this if we ever need to act on GLFW_REPEAT (which is when a key is held down continuously) 566 fill(key_state, key_state + NUM_KEYS, KEY_STATE_UNCHANGED); 567 563 568 glfwPollEvents(); 564 569 … … 585 590 selectedObject = &objects[1]; 586 591 } 587 } 588 589 /* 590 model[12] = last_position + speed*elapsed_seconds; 591 last_position = model[12]; 592 */ 592 593 /* 594 if (key_state[GLFW_KEY_SPACE] == GLFW_PRESS) { 595 //transformObject(objects[1], translate(mat4(), vec3(0.3f, 0.0f, 0.0f)), ubo); 596 } 597 if (key_pressed[GLFW_KEY_RIGHT]) { 598 transformObject(objects[2], translate(mat4(), vec3(0.01f, 0.0f, 0.0f)), ubo); 599 } 600 if (key_pressed[GLFW_KEY_LEFT]) { 601 transformObject(objects[2], translate(mat4(), vec3(-0.01f, 0.0f, 0.0f)), ubo); 602 } 603 */ 604 } 605 606 // Render scene 593 607 594 608 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); … … 621 635 cam_pos.x -= cos(cam_yaw)*dist; 622 636 cam_pos.z += sin(cam_yaw)*dist; 637 623 638 cam_moved = true; 624 639 } … … 626 641 cam_pos.x += cos(cam_yaw)*dist; 627 642 cam_pos.z -= sin(cam_yaw)*dist; 643 628 644 cam_moved = true; 629 645 } … … 631 647 cam_pos.x -= sin(cam_yaw)*dist; 632 648 cam_pos.z -= cos(cam_yaw)*dist; 649 633 650 cam_moved = true; 634 651 } … … 636 653 cam_pos.x += sin(cam_yaw)*dist; 637 654 cam_pos.z += cos(cam_yaw)*dist; 655 638 656 cam_moved = true; 639 657 } … … 649 667 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z)); 650 668 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f)); 669 651 670 view_mat = R*T; 652 671 … … 720 739 if (closest_object == NULL) { 721 740 cout << "No object was clicked" << endl; 722 } 723 else { 741 } else { 724 742 clickedObject = closest_object; 725 743 cout << "Clicked object: " << clickedObject->id << endl; … … 727 745 } 728 746 } 747 748 void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { 749 key_state[key] = action; 750 751 // should be true for GLFW_PRESS and GLFW_REPEAT 752 key_pressed[key] = (action != GLFW_RELEASE); 753 } 754 729 755 730 756 GLuint loadShader(GLenum type, string file) { … … 819 845 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); 820 846 821 print4DVector("Full world ray", world_ray);822 823 847 vec3 local_ray = (inverse(obj->model_mat) * world_ray).xyz(); 824 848 vec3 local_cam = (inverse(obj->model_mat) * cam).xyz(); … … 827 851 828 852 float d = -glm::dot(points[0], normal); 829 cout << "d: " << d << endl;830 831 853 float t = -(glm::dot(local_cam, normal) + d) / glm::dot(local_ray, normal); 832 cout << "t: " << t << endl;833 854 834 855 vec3 intersection = local_cam + t*local_ray; 835 printVector("Intersection", intersection);836 856 837 857 if (insideTriangle(intersection, points)) { … … 842 862 } 843 863 } 864 844 865 bool insideTriangle(vec3 p, array<vec3, 3> triangle_points) { 845 866 vec3 v21 = triangle_points[1] - triangle_points[0];
Note:
See TracChangeset
for help on using the changeset viewer.