viewport.h

00001 
00002 /* Copyright (c) 2006-2009, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #ifndef EQ_VIEWPORT_H
00019 #define EQ_VIEWPORT_H
00020 
00021 #include <eq/base/base.h>
00022 #include <eq/base/debug.h>
00023 #include <eq/client/types.h>
00024 
00025 #include <vector>         // WAR: vector4.h does not include these headers. Fix 
00026 #include <limits> //      vector4.h after vmmlib upgrade and remove them
00027 
00028 #include <iostream>
00029 
00030 namespace eq
00031 {
00032     class PixelViewport;
00033     class Viewport;
00034     std::ostream& operator << ( std::ostream& os, const Viewport& vp );
00035 
00039     class Viewport 
00040     {
00041     public:
00046         Viewport() : x(0.0f), y(0.0f), w(1.0f), h(1.0f)  {}
00047 
00048         Viewport( const float x_, const float y_, const float w_,const float h_)
00049                 : x(x_), y(y_), w(w_), h(h_)  {}
00051 
00052         void invalidate() { x=0.0f; y=0.0f; w=-1.0f; h=-1.0f; }
00053         void apply ( const Viewport& rhs )
00054             {
00055                 EQASSERTINFO( isValid(), *this);
00056                 EQASSERTINFO( rhs.isValid(), rhs );                
00057                 x += rhs.x * w;
00058                 y += rhs.y * h;
00059                 w *= rhs.w;
00060                 h *= rhs.h;
00061             }
00062             
00063         void transform ( const Viewport& rhs )
00064             {
00065                 w = w / rhs.w;
00066                 h = h / rhs.h;
00067                 x = ( x - rhs.x ) / rhs.w;
00068                 y = ( y - rhs.y ) / rhs.h;
00069             }
00070 
00071         bool operator == ( const Viewport& rhs ) const 
00072             { 
00073                 return ( x==rhs.x && y==rhs.y && w==rhs.w && h==rhs.h);
00074             }
00075 
00076         bool operator != ( const Viewport& rhs ) const 
00077             { 
00078                 return ( x!=rhs.x || x!=rhs.y || w!=rhs.w || h!=rhs.h);
00079             }
00080 
00085         bool isValid() const 
00086             { return ( x>=0.0f && y>=0.0f && w>=0.0f && h>=0.0f ); }
00087         
00092         bool hasArea() const { return (w>0.0f && h>0.0f); }
00093 
00095         float getArea() const { return w*h; }
00096 
00098         float getXEnd() const { return x+w; }
00099 
00101         float getYEnd() const { return y+h; }
00102 
00104         void intersect( const Viewport& rhs )
00105             {
00106                 if( *this == rhs )
00107                     return;
00108 
00109                 if( !rhs.isValid() || !isValid() )
00110                 {
00111                     invalidate();
00112                     return;
00113                 }
00114                 
00115                 if( !rhs.hasArea() || !hasArea() )
00116                 {
00117                     x = 0;
00118                     y = 0;
00119                     w = 0;
00120                     h = 0;
00121                     return;
00122                 }
00123                 
00124                 const float sEx = static_cast< float >(     x +     w );
00125                 const float sEy = static_cast< float >(     y +     h );
00126                 const float dEx = static_cast< float >( rhs.x + rhs.w );
00127                 const float dEy = static_cast< float >( rhs.y + rhs.h );
00128                     
00129                 x = EQ_MAX( x, rhs.x );
00130                 y = EQ_MAX( y, rhs.y );
00131                 w = EQ_MIN( sEx, dEx ) - x;
00132                 h = EQ_MIN( sEy, dEy ) - y;
00133             }
00134 
00136         Viewport getCoverage( const Viewport& with ) const
00137             {
00138                 Viewport coverage( with );
00139                 coverage.intersect( *this ); // intersection
00140                 coverage.transform( *this ); // in our coordinate system
00141 
00142                 return coverage;
00143             }
00144 
00146         EQ_EXPORT void applyView( const Viewport& segmentVP, 
00147                                   const Viewport& viewVP,
00148                                   const PixelViewport& pvp, 
00149                                   const Vector4i& overdraw );
00150 
00151         float x;
00152         float y;
00153         float w;
00154         float h;
00155 
00156         EQ_EXPORT static const Viewport FULL;
00157     };
00158 
00159     inline std::ostream& operator << ( std::ostream& os, const Viewport& vp )
00160     {
00161         os << "[ " << vp.x << " " << vp.y << " " << vp.w << " " << vp.h << " ]";
00162         return os;
00163     }
00164 }
00165 
00166 #endif // EQ_VIEWPORT_H
Generated on Mon Aug 10 18:58:41 2009 for Equalizer 0.9 by  doxygen 1.5.8