Changeset 4f3262f in opengl-game
- Timestamp:
- May 25, 2018, 1:37:25 AM (7 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- f9a242b
- Parents:
- 14ff67c
- git-author:
- Dmitry Portnoy <dmp1488@…> (05/25/18 01:37:08)
- git-committer:
- Dmitry Portnoy <dmp1488@…> (05/25/18 01:37:25)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
r14ff67c r4f3262f 91 91 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); 92 92 93 void glfw_error_callback(int error, const char* description); 94 95 void mouse_button_callback(GLFWwindow* window, int button, int action, int mods); 96 93 97 bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point); 94 98 bool insideTriangle(vec3 p, array<vec3, 3> triangle_points); … … 111 115 SceneObject* selectedObject); 112 116 void renderSceneGui(); 113 114 void glfw_error_callback(int error, const char* description) {115 gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);116 }117 118 void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {119 double mouse_x, mouse_y;120 glfwGetCursorPos(window, &mouse_x, &mouse_y);121 122 if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {123 cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;124 selectedObject = NULL;125 126 float x = (2.0f*mouse_x) / width - 1.0f;127 float y = 1.0f - (2.0f*mouse_y) / height;128 129 cout << "x: " << x << ", y: " << y << endl;130 131 vec4 ray_clip = vec4(x, y, -1.0f, 1.0f);132 vec4 ray_eye = inverse(proj_mat) * ray_clip;133 ray_eye = vec4(ray_eye.xy(), -1.0f, 1.0f);134 vec4 ray_world = inverse(view_mat) * ray_eye;135 136 vec4 cam_pos_temp = vec4(cam_pos, 1.0f);137 138 vec4 click_point;139 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 that140 SceneObject* closest_object = NULL;141 142 SceneObject* obj;143 for (vector<SceneObject>::iterator it = objects.begin(); it != objects.end(); it++) {144 obj = &*it;145 146 for (unsigned int p_idx = 0; p_idx < it->points.size(); p_idx += 9) {147 if (faceClicked({148 vec3(it->points[p_idx], it->points[p_idx + 1], it->points[p_idx + 2]),149 vec3(it->points[p_idx + 3], it->points[p_idx + 4], it->points[p_idx + 5]),150 vec3(it->points[p_idx + 6], it->points[p_idx + 7], it->points[p_idx + 8]),151 },152 obj, ray_world, cam_pos_temp, click_point)) {153 click_point = view_mat * click_point;154 155 if (-NEAR_CLIP >= click_point.z && click_point.z > -FAR_CLIP && click_point.z > closest_point.z) {156 closest_point = click_point.xyz();157 closest_object = obj;158 }159 }160 }161 }162 163 if (closest_object == NULL) {164 cout << "No object was clicked" << endl;165 } else {166 clickedObject = closest_object;167 cout << "Clicked object: " << clickedObject->id << endl;168 }169 }170 }171 117 172 118 int main(int argc, char* argv[]) { … … 761 707 } 762 708 709 void glfw_error_callback(int error, const char* description) { 710 gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description); 711 } 712 713 void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) { 714 double mouse_x, mouse_y; 715 glfwGetCursorPos(window, &mouse_x, &mouse_y); 716 717 if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) { 718 cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl; 719 selectedObject = NULL; 720 721 float x = (2.0f*mouse_x) / width - 1.0f; 722 float y = 1.0f - (2.0f*mouse_y) / height; 723 724 cout << "x: " << x << ", y: " << y << endl; 725 726 vec4 ray_clip = vec4(x, y, -1.0f, 1.0f); 727 vec4 ray_eye = inverse(proj_mat) * ray_clip; 728 ray_eye = vec4(ray_eye.xy(), -1.0f, 1.0f); 729 vec4 ray_world = inverse(view_mat) * ray_eye; 730 731 vec4 cam_pos_temp = vec4(cam_pos, 1.0f); 732 733 vec4 click_point; 734 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 735 SceneObject* closest_object = NULL; 736 737 SceneObject* obj; 738 for (vector<SceneObject>::iterator it = objects.begin(); it != objects.end(); it++) { 739 obj = &*it; 740 741 for (unsigned int p_idx = 0; p_idx < it->points.size(); p_idx += 9) { 742 if (faceClicked({ 743 vec3(it->points[p_idx], it->points[p_idx + 1], it->points[p_idx + 2]), 744 vec3(it->points[p_idx + 3], it->points[p_idx + 4], it->points[p_idx + 5]), 745 vec3(it->points[p_idx + 6], it->points[p_idx + 7], it->points[p_idx + 8]), 746 }, 747 obj, ray_world, cam_pos_temp, click_point)) { 748 click_point = view_mat * click_point; 749 750 if (-NEAR_CLIP >= click_point.z && click_point.z > -FAR_CLIP && click_point.z > closest_point.z) { 751 closest_point = click_point.xyz(); 752 closest_object = obj; 753 } 754 } 755 } 756 } 757 758 if (closest_object == NULL) { 759 cout << "No object was clicked" << endl; 760 } 761 else { 762 clickedObject = closest_object; 763 cout << "Clicked object: " << clickedObject->id << endl; 764 } 765 } 766 } 767 763 768 GLuint loadShader(GLenum type, string file) { 764 769 cout << "Loading shader from file " << file << endl;
Note:
See TracChangeset
for help on using the changeset viewer.