Viewport.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  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. /// @private
  13. // Simple Viewport class for an opengl context. Handles reshaping and mouse.
  14. struct Viewport
  15. {
  16. int x,y,width,height;
  17. // Constructors
  18. Viewport(
  19. const int x=0,
  20. const int y=0,
  21. const int width=0,
  22. const int height=0):
  23. x(x),
  24. y(y),
  25. width(width),
  26. height(height)
  27. {
  28. };
  29. virtual ~Viewport(){}
  30. void reshape(
  31. const int x,
  32. const int y,
  33. const int width,
  34. const int height)
  35. {
  36. this->x = x;
  37. this->y = y;
  38. this->width = width;
  39. this->height = height;
  40. };
  41. // Given mouse_x,mouse_y on the entire window return mouse_x, mouse_y in
  42. // this viewport.
  43. //
  44. // Inputs:
  45. // my mouse y-coordinate
  46. // wh window height
  47. // Returns y-coordinate in viewport
  48. int mouse_y(const int my,const int wh)
  49. {
  50. return my - (wh - height - y);
  51. }
  52. // Inputs:
  53. // mx mouse x-coordinate
  54. // Returns x-coordinate in viewport
  55. int mouse_x(const int mx)
  56. {
  57. return mx - x;
  58. }
  59. // Returns whether point (mx,my) is in extend of Viewport
  60. bool inside(const int mx, const int my) const
  61. {
  62. return
  63. mx >= x && my >= y &&
  64. mx < x+width && my < y+height;
  65. }
  66. };
  67. }
  68. #endif