OpenWalnut  1.4.0
WGEUniformTypeTraits.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WGEUNIFORMTYPETRAITS_H
00026 #define WGEUNIFORMTYPETRAITS_H
00027 
00028 #include <osg/Vec3>
00029 #include <osg/Vec2>
00030 
00031 #include "../../common/math/linearAlgebra/WVectorFixed.h"
00032 #include "../../common/math/WInterval.h"
00033 
00034 class WItemSelector;
00035 
00036 namespace wge
00037 {
00038     /**
00039      * Class helping to adapt types specified as template parameter into the best matching osg::Uniform (GLSL) type. This is useful especially for
00040      * property-types to uniform type conversion.
00041      * \note: bool map to bool, int to int, unsigned int to unsigned int. Unallowed types like std::string will then cause compilation errors as
00042      * osg::Uniform does not offer proper constructors/setters for these types.
00043      */
00044     template< typename T >
00045     class UniformType
00046     {
00047     public:
00048         /**
00049          * The best matching GLSL uniform type for the specified template parameter.
00050          */
00051         typedef T Type;
00052     };
00053 
00054     /**
00055      * Maps doubles to floats as only floats are allowed in uniforms.
00056      */
00057     template<>
00058     class UniformType< double >
00059     {
00060     public:
00061         /**
00062          * The best matching GLSL uniform type for the specified template parameter.
00063          */
00064         typedef float Type;
00065     };
00066 
00067     /**
00068      * Maps WVector3d/WPosition to osg::Vec3.
00069      */
00070     template<>
00071     class UniformType< WVector3d >
00072     {
00073     public:
00074         /**
00075          * The best matching GLSL uniform type for the specified template parameter.
00076          */
00077         typedef osg::Vec3 Type;
00078     };
00079 
00080     /**
00081      * Maps Selection Properties to ints.
00082      */
00083     template<>
00084     class UniformType< WItemSelector >
00085     {
00086     public:
00087         /**
00088          * The best matching GLSL uniform type for the specified template parameter.
00089          */
00090         typedef int Type;
00091     };
00092 
00093     /**
00094      * Maps Selection Properties to ints.
00095      */
00096     template<>
00097     class UniformType< WIntervalDouble >
00098     {
00099     public:
00100         /**
00101          * The best matching GLSL uniform type for the specified template parameter.
00102          */
00103         typedef osg::Vec2 Type;
00104     };
00105 
00106     /**
00107      * Create an instance of the uniform target type given some input type.
00108      *
00109      * \tparam InType the input type
00110      * \param in the input value
00111      *
00112      * \return the resulting uniform type
00113      */
00114     template< typename InType >
00115     typename UniformType< InType >::Type toUniformType( const InType& in )
00116     {
00117         return static_cast< typename UniformType< InType >::Type >( in );
00118     }
00119 
00120     /**
00121      * Creates an OSG vec2 when an interval is inserted.
00122      *
00123      * \param in the interval to convert
00124      *
00125      * \return the vector
00126      */
00127     osg::Vec2 toUniformType( const WIntervalDouble& in );
00128 }
00129 
00130 #endif  // WGEUNIFORMTYPETRAITS_H