| // for the current mouse position on the screen, where does that correspond to in the world? | |
| glm::vec2 Game::world_position_for_mouse(glm::vec2 const & position, | |
| glm::mat4 const & projection_matrix, | |
| glm::mat4 const & view_matrix) const | |
| { | |
| auto window_dimensions = impl->main_window->get_dimensions(); | |
| // std::cout << fmt::format("computing world position for mouse from {} {}", mouse_x, mouse_y) << std::endl; | |
| float normalized_mouse_x = (2.0f * position.x) / window_dimensions.width - 1.0f; | |
| float normalized_mouse_y = 1.0f - (2.0f * position.y) / window_dimensions.height; | |
| glm::vec3 normalized_mouse_vector = glm::vec3(normalized_mouse_x, normalized_mouse_y, 1.0f); | |
| glm::vec4 ray_clip = glm::vec4(normalized_mouse_vector.xy(), -1.0, 1.0); | |
| glm::vec4 ray_eye = glm::inverse(projection_matrix) * ray_clip; | |
| ray_eye = glm::vec4(ray_eye.xy(), -1.0, 0.0); | |
| glm::vec3 ray_world = (glm::inverse(view_matrix) * ray_eye).xyz(); | |
| ray_world = glm::normalize(ray_world); | |
| float l = -(impl->camera.z / ray_world.z); | |
| return {impl->camera.x + l * ray_world.x, impl->camera.y + l * ray_world.y}; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment