Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // for the current mouse position on the screen, where does that correspond to in the world?
- glm::vec2 World::world_position_for_mouse(const glm::vec2 mouse_position,
- const glm::mat4 projection_matrix,
- const glm::mat4 view_matrix)
- {
- int window_width;
- int window_height;
- this->graphics.get_window_dimensions(window_width, window_height);
- const int mouse_x = mouse_position[0];
- const int mouse_y = mouse_position[1];
- GLfloat normalized_mouse_x = (2.0f * mouse_x) / window_width - 1.0f;
- float normalized_mouse_y = 1.0f - (2.0f * mouse_y) / window_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 = -(camera.z / ray_world.z);
- return {camera.x + l * ray_world.x, camera.y + l * ray_world.y};
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement