1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
17#include <osg/StateAttribute>
22/** Encapsulate OpenGL glViewport. */
23class OSG_EXPORT Viewport : public StateAttribute
28 typedef int value_type;
30 typedef double value_type;
34 Viewport(value_type x,value_type y,value_type width,value_type height):
41 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
42 Viewport(const Viewport& vp,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
43 StateAttribute(vp,copyop),
47 _height(vp._height) {}
49 META_StateAttribute(osg, Viewport,VIEWPORT);
51 /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
52 virtual int compare(const StateAttribute& sa) const
54 // check the types are equal and then create the rhs variable
55 // used by the COMPARE_StateAttribute_Parameter macros below.
56 COMPARE_StateAttribute_Types(Viewport,sa)
58 // compare each parameter in turn against the rhs.
59 COMPARE_StateAttribute_Parameter(_x)
60 COMPARE_StateAttribute_Parameter(_y)
61 COMPARE_StateAttribute_Parameter(_width)
62 COMPARE_StateAttribute_Parameter(_height)
64 return 0; // passed all the above comparison macros, must be equal.
67 Viewport& operator = (const Viewport& rhs)
69 if (&rhs==this) return *this;
74 _height = rhs._height;
79 inline void setViewport(value_type x,value_type y,value_type width,value_type height)
88 void getViewport(int& x,int& y,int& width,int& height) const
96 void getViewport(double& x,double& y,double& width,double& height) const
104 inline value_type& x() { return _x; }
105 inline value_type x() const { return _x; }
107 inline value_type& y() { return _y; }
108 inline value_type y() const { return _y; }
110 inline value_type& width() { return _width; }
111 inline value_type width() const { return _width; }
113 inline value_type& height() { return _height; }
114 inline value_type height() const { return _height; }
116 inline bool valid() const { return _width>0 && _height>0; }
118 /** Return the aspectRatio of the viewport, which is equal to width/height.
119 * If height is zero, the potential division by zero is avoided by simply returning 1.0f.
121 inline double aspectRatio() const { if (_height!=0) return (double)_width/(double)_height; else return 1.0; }
123 /** Compute the Window Matrix which takes projected coords into Window coordinates.
124 * To convert local coordinates into window coordinates use v_window = v_local * MVPW matrix,
125 * where the MVPW matrix is ModelViewMatrix * ProjectionMatrix * WindowMatrix, the latter supplied by
126 * Viewport::computeWindowMatrix(), the ModelView and Projection Matrix can either be sourced from the
127 * current osg::State object, via osgUtil::SceneView or CullVisitor.
129 inline const osg::Matrix computeWindowMatrix() const
131 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);
134 virtual void apply(State& state) const;