openscenegraph
Viewport
Go to the documentation of this file.
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
14#ifndef OSG_VIEWPORT
15#define OSG_VIEWPORT 1
16
17#include <osg/StateAttribute>
18#include <osg/Matrix>
19
20namespace osg {
21
22/** Encapsulate OpenGL glViewport. */
23class OSG_EXPORT Viewport : public StateAttribute
24{
25 public :
26
27#if 0
28 typedef int value_type;
29#else
30 typedef double value_type;
31#endif
32 Viewport();
33
34 Viewport(value_type x,value_type y,value_type width,value_type height):
35 _x(x),
36 _y(y),
37 _width(width),
38 _height(height) {}
39
40
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),
44 _x(vp._x),
45 _y(vp._y),
46 _width(vp._width),
47 _height(vp._height) {}
48
49 META_StateAttribute(osg, Viewport,VIEWPORT);
50
51 /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
52 virtual int compare(const StateAttribute& sa) const
53 {
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)
57
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)
63
64 return 0; // passed all the above comparison macros, must be equal.
65 }
66
67 Viewport& operator = (const Viewport& rhs)
68 {
69 if (&rhs==this) return *this;
70
71 _x = rhs._x;
72 _y = rhs._y;
73 _width = rhs._width;
74 _height = rhs._height;
75
76 return *this;
77 }
78
79 inline void setViewport(value_type x,value_type y,value_type width,value_type height)
80 {
81 _x = x;
82 _y = y;
83 _width = width;
84 _height = height;
85 }
86
87#if 0
88 void getViewport(int& x,int& y,int& width,int& height) const
89 {
90 x = _x;
91 y = _y;
92 width = _width;
93 height = _height;
94 }
95
96 void getViewport(double& x,double& y,double& width,double& height) const
97 {
98 x = _x;
99 y = _y;
100 width = _width;
101 height = _height;
102 }
103#endif
104 inline value_type& x() { return _x; }
105 inline value_type x() const { return _x; }
106
107 inline value_type& y() { return _y; }
108 inline value_type y() const { return _y; }
109
110 inline value_type& width() { return _width; }
111 inline value_type width() const { return _width; }
112
113 inline value_type& height() { return _height; }
114 inline value_type height() const { return _height; }
115
116 inline bool valid() const { return _width>0 && _height>0; }
117
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.
120 */
121 inline double aspectRatio() const { if (_height!=0) return (double)_width/(double)_height; else return 1.0; }
122
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.
128 */
129 inline const osg::Matrix computeWindowMatrix() const
130 {
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);
132 }
133
134 virtual void apply(State& state) const;
135
136 protected:
137
138 virtual ~Viewport();
139
140 value_type _x;
141 value_type _y;
142 value_type _width;
143 value_type _height;
144
145};
146
147}
148
149#endif