Viewport 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSG_VIEWPORT
  14. #define OSG_VIEWPORT 1
  15. #include <osg/StateAttribute>
  16. #include <osg/Matrix>
  17. namespace osg {
  18. /** Encapsulate OpenGL glViewport. */
  19. class OSG_EXPORT Viewport : public StateAttribute
  20. {
  21. public :
  22. #if 0
  23. typedef int value_type;
  24. #else
  25. typedef double value_type;
  26. #endif
  27. Viewport();
  28. Viewport(value_type x,value_type y,value_type width,value_type height):
  29. _x(x),
  30. _y(y),
  31. _width(width),
  32. _height(height) {}
  33. /** Copy constructor using CopyOp to manage deep vs shallow copy. */
  34. Viewport(const Viewport& vp,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  35. StateAttribute(vp,copyop),
  36. _x(vp._x),
  37. _y(vp._y),
  38. _width(vp._width),
  39. _height(vp._height) {}
  40. META_StateAttribute(osg, Viewport,VIEWPORT);
  41. /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
  42. virtual int compare(const StateAttribute& sa) const
  43. {
  44. // check the types are equal and then create the rhs variable
  45. // used by the COMPARE_StateAttribute_Parameter macros below.
  46. COMPARE_StateAttribute_Types(Viewport,sa)
  47. // compare each parameter in turn against the rhs.
  48. COMPARE_StateAttribute_Parameter(_x)
  49. COMPARE_StateAttribute_Parameter(_y)
  50. COMPARE_StateAttribute_Parameter(_width)
  51. COMPARE_StateAttribute_Parameter(_height)
  52. return 0; // passed all the above comparison macros, must be equal.
  53. }
  54. Viewport& operator = (const Viewport& rhs)
  55. {
  56. if (&rhs==this) return *this;
  57. _x = rhs._x;
  58. _y = rhs._y;
  59. _width = rhs._width;
  60. _height = rhs._height;
  61. return *this;
  62. }
  63. inline void setViewport(value_type x,value_type y,value_type width,value_type height)
  64. {
  65. _x = x;
  66. _y = y;
  67. _width = width;
  68. _height = height;
  69. }
  70. #if 0
  71. void getViewport(int& x,int& y,int& width,int& height) const
  72. {
  73. x = _x;
  74. y = _y;
  75. width = _width;
  76. height = _height;
  77. }
  78. void getViewport(double& x,double& y,double& width,double& height) const
  79. {
  80. x = _x;
  81. y = _y;
  82. width = _width;
  83. height = _height;
  84. }
  85. #endif
  86. inline value_type& x() { return _x; }
  87. inline value_type x() const { return _x; }
  88. inline value_type& y() { return _y; }
  89. inline value_type y() const { return _y; }
  90. inline value_type& width() { return _width; }
  91. inline value_type width() const { return _width; }
  92. inline value_type& height() { return _height; }
  93. inline value_type height() const { return _height; }
  94. inline bool valid() const { return _width>0 && _height>0; }
  95. /** Return the aspectRatio of the viewport, which is equal to width/height.
  96. * If height is zero, the potential division by zero is avoided by simply returning 1.0f.
  97. */
  98. inline double aspectRatio() const { if (_height!=0) return (double)_width/(double)_height; else return 1.0; }
  99. /** Compute the Window Matrix which takes projected coords into Window coordinates.
  100. * To convert local coordinates into window coordinates use v_window = v_local * MVPW matrix,
  101. * where the MVPW matrix is ModelViewMatrix * ProjectionMatrix * WindowMatrix, the latter supplied by
  102. * Viewport::computeWindowMatrix(), the ModelView and Projection Matrix can either be sourced from the
  103. * current osg::State object, via osgUtil::SceneView or CullVisitor.
  104. */
  105. inline const osg::Matrix computeWindowMatrix() const
  106. {
  107. return osg::Matrix::translate(1.0,1.0,1.0)*osg::Matrix::scale(0.5*width(),0.5*height(),0.5f)*osg::Matrix::translate(x(),y(),0.0f);
  108. }
  109. virtual void apply(State& state) const;
  110. protected:
  111. virtual ~Viewport();
  112. value_type _x;
  113. value_type _y;
  114. value_type _width;
  115. value_type _height;
  116. };
  117. }
  118. #endif