Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. // for the current mouse position on the screen, where does that correspond to in the world?
  2. glm::vec2 World::world_position_for_mouse(const glm::vec2 mouse_position,
  3. const glm::mat4 projection_matrix,
  4. const glm::mat4 view_matrix)
  5. {
  6. int window_width;
  7. int window_height;
  8. this->graphics.get_window_dimensions(window_width, window_height);
  9.  
  10. const int mouse_x = mouse_position[0];
  11. const int mouse_y = mouse_position[1];
  12.  
  13. GLfloat normalized_mouse_x = (2.0f * mouse_x) / window_width - 1.0f;
  14. float normalized_mouse_y = 1.0f - (2.0f * mouse_y) / window_height;
  15.  
  16.  
  17. glm::vec3 normalized_mouse_vector = glm::vec3(normalized_mouse_x, normalized_mouse_y, 1.0f);
  18.  
  19. glm::vec4 ray_clip = glm::vec4(normalized_mouse_vector.xy(), -1.0, 1.0);
  20.  
  21. glm::vec4 ray_eye = glm::inverse(projection_matrix) * ray_clip;
  22. ray_eye = glm::vec4(ray_eye.xy(), -1.0, 0.0);
  23.  
  24. glm::vec3 ray_world = (glm::inverse(view_matrix) * ray_eye).xyz();
  25.  
  26. ray_world = glm::normalize(ray_world);
  27.  
  28. float l = -(camera.z / ray_world.z);
  29.  
  30.  
  31. return {camera.x + l * ray_world.x, camera.y + l * ray_world.y};
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement