Viewport.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef IGL_VIEWPORT_H
  9. #define IGL_VIEWPORT_H
  10. namespace igl
  11. {
  12. struct Viewport
  13. {
  14. int x,y,width,height;
  15. // Constructors
  16. Viewport(
  17. const int x=0,
  18. const int y=0,
  19. const int width=0,
  20. const int height=0):
  21. x(x),
  22. y(y),
  23. width(width),
  24. height(height)
  25. {
  26. };
  27. virtual ~Viewport(){}
  28. void reshape(
  29. const int x,
  30. const int y,
  31. const int width,
  32. const int height)
  33. {
  34. this->x = x;
  35. this->y = y;
  36. this->width = width;
  37. this->height = height;
  38. };
  39. // Given mouse_x,mouse_y on the entire window return mouse_x, mouse_y in
  40. // this viewport.
  41. //
  42. // Inputs:
  43. // my mouse y-coordinate
  44. // wh window height
  45. // Returns y-coordinate in viewport
  46. int mouse_y(const int my,const int wh)
  47. {
  48. return my - (wh - height - y);
  49. }
  50. // Inputs:
  51. // mx mouse x-coordinate
  52. // Returns x-coordinate in viewport
  53. int mouse_x(const int mx)
  54. {
  55. return mx - x;
  56. }
  57. // Returns whether point (mx,my) is in extend of Viewport
  58. bool inside(const int mx, const int my) const
  59. {
  60. return
  61. mx >= x && my >= y &&
  62. mx < x+width && my < y+height;
  63. }
  64. };
  65. }
  66. #endif