Viewport.h 1.6 KB

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