| 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 | |
|---|
| 20 | namespace osg { |
|---|
| 21 | |
|---|
| 22 | /** Encapsulate OpenGL glViewport. */ |
|---|
| 23 | class 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 | inline void setViewport(value_type x,value_type y,value_type width,value_type height) |
|---|
| 68 | { |
|---|
| 69 | _x = x; |
|---|
| 70 | _y = y; |
|---|
| 71 | _width = width; |
|---|
| 72 | _height = height; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | #if 0 |
|---|
| 76 | void getViewport(int& x,int& y,int& width,int& height) const |
|---|
| 77 | { |
|---|
| 78 | x = _x; |
|---|
| 79 | y = _y; |
|---|
| 80 | width = _width; |
|---|
| 81 | height = _height; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | void getViewport(double& x,double& y,double& width,double& height) const |
|---|
| 85 | { |
|---|
| 86 | x = _x; |
|---|
| 87 | y = _y; |
|---|
| 88 | width = _width; |
|---|
| 89 | height = _height; |
|---|
| 90 | } |
|---|
| 91 | #endif |
|---|
| 92 | inline value_type& x() { return _x; } |
|---|
| 93 | inline value_type x() const { return _x; } |
|---|
| 94 | |
|---|
| 95 | inline value_type& y() { return _y; } |
|---|
| 96 | inline value_type y() const { return _y; } |
|---|
| 97 | |
|---|
| 98 | inline value_type& width() { return _width; } |
|---|
| 99 | inline value_type width() const { return _width; } |
|---|
| 100 | |
|---|
| 101 | inline value_type& height() { return _height; } |
|---|
| 102 | inline value_type height() const { return _height; } |
|---|
| 103 | |
|---|
| 104 | inline bool valid() const { return _width>0 && _height>0; } |
|---|
| 105 | |
|---|
| 106 | /** Return the aspectRatio of the viewport, which is equal to width/height. |
|---|
| 107 | * If height is zero, the potential division by zero is avoided by simply returning 1.0f. |
|---|
| 108 | */ |
|---|
| 109 | inline double aspectRatio() const { if (_height!=0) return (double)_width/(double)_height; else return 1.0; } |
|---|
| 110 | |
|---|
| 111 | /** Compute the Window Matrix which takes projected coords into Window coordinates. |
|---|
| 112 | * To convert local coordinates into window coordinates use v_window = v_local * MVPW matrix, |
|---|
| 113 | * where the MVPW matrix is ModelViewMatrix * ProjectionMatrix * WindowMatrix, the latter supplied by |
|---|
| 114 | * Viewport::computeWindowMatrix(), the ModelView and Projection Matrix can either be sourced from the |
|---|
| 115 | * current osg::State object, via osgUtil::SceneView or CullVisitor. |
|---|
| 116 | */ |
|---|
| 117 | inline const osg::Matrix computeWindowMatrix() const |
|---|
| 118 | { |
|---|
| 119 | 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); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | virtual void apply(State& state) const; |
|---|
| 123 | |
|---|
| 124 | protected: |
|---|
| 125 | |
|---|
| 126 | virtual ~Viewport(); |
|---|
| 127 | |
|---|
| 128 | value_type _x; |
|---|
| 129 | value_type _y; |
|---|
| 130 | value_type _width; |
|---|
| 131 | value_type _height; |
|---|
| 132 | |
|---|
| 133 | }; |
|---|
| 134 | |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | #endif |
|---|