ColorAttribute.h

00001 /***************************************************************************
00002  *   Copyright (c) 2008   Art Tevs                                         *
00003  *                                                                         *
00004  *   This library is free software; you can redistribute it and/or modify  *
00005  *   it under the terms of the GNU Lesser General Public License as        *
00006  *   published by the Free Software Foundation; either version 3 of        *
00007  *   the License, or (at your option) any later version.                   *
00008  *                                                                         *
00009  *   This library is distributed in the hope that it will be useful,       *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00012  *   GNU Lesse General Public License for more details.                    *
00013  *                                                                         *
00014  *   The full license is in LICENSE file included with this distribution.  *
00015  ***************************************************************************/
00016 
00017 #ifndef _C_COLORATTRIBUTE_H_
00018 #define _C_COLORATTRIBUTE_H_
00019 
00020 
00021 //-------------------------------------------------------------------------
00022 // Includes
00023 //-------------------------------------------------------------------------
00024 #include <osg/StateAttribute>
00025 #include <osg/Vec4>
00026 #include <osg/BlendFunc>
00027 #include <osg/NodeVisitor>
00028 
00029 #include <osgPPU/Export.h>
00030 
00031 namespace osgPPU
00032 {
00033 
00034     //! Stateattribute to work with blending. Can be used for animated blending operations (fadein, fadeout)
00035     /**
00036     * This is a class derived from StateAttirbute. It allows to specify blending colors based
00037     * on the reference time provided with the corresponding node visitor. This class is also 
00038     * used to setup any unit's geometry to default color (1,1,1,1) which is required for 
00039     * proper rendering.
00040     **/
00041     class OSGPPU_EXPORT ColorAttribute : public osg::StateAttribute
00042     {
00043         public:
00044             META_StateAttribute(osgPPU, ColorAttribute, MATERIAL);
00045 
00046             ColorAttribute();
00047             ColorAttribute(const ColorAttribute& bm, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
00048             ~ColorAttribute();
00049 
00050             /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
00051             int compare(const osg::StateAttribute& sa) const
00052             {
00053                 // Check for equal types, then create the rhs variable
00054                 // used by the COMPARE_StateAttribute_Paramter macros below.
00055                 COMPARE_StateAttribute_Types(ColorAttribute, sa)
00056     
00057                 // Compare each parameter in turn against the rhs.
00058                 COMPARE_StateAttribute_Parameter(mColor)
00059                 COMPARE_StateAttribute_Parameter(mStartColor)
00060                 COMPARE_StateAttribute_Parameter(mEndColor)
00061                 COMPARE_StateAttribute_Parameter(mStartTime)
00062                 COMPARE_StateAttribute_Parameter(mEndTime)
00063                 COMPARE_StateAttribute_Parameter(mTime)
00064     
00065                 return 0; // Passed all the above comparison macros, so must be equal.
00066             }
00067 
00068             /**
00069             * Apply attribute to the state. This will just call the glColor() function with appropriate
00070             * values. The corresponding time is updated during the update traversion.
00071             **/
00072             void apply(osg::State& state) const;
00073 
00074             /**
00075             * Set to use GL_BLEND mode, because it is required for this attribute
00076             **/
00077             bool getModeUsage(osg::StateAttribute::ModeUsage& usage) const
00078             {
00079                 usage.usesMode(GL_BLEND);
00080                 return true;
00081             }
00082 
00083             /** Set start time for the interpolationm operation **/
00084             void setStartTime(double time) { mStartTime = time; }
00085 
00086             /** Get start interpolation time **/
00087             double getStartTime() const { return mStartTime; }
00088 
00089             /** Set end time for the interpolationm operation **/
00090             void setEndTime(double time) { mEndTime = time; }
00091 
00092             /** Get end interpolation time **/
00093             double getEndTime() const { return mEndTime; }
00094 
00095             /** Set start value of the interpolationm operation **/
00096             void setStartColor(const osg::Vec4& color) { mStartColor = color; }
00097 
00098             /** Get start interpolation time **/
00099             const osg::Vec4& getStartColor() const { return mStartColor; }
00100 
00101             /** Set end value of the interpolationm operation **/
00102             void setEndColor(const osg::Vec4& color) { mEndColor = color; }
00103 
00104             /** Get end interpolation time **/
00105             const osg::Vec4& getEndColor() const { return mEndColor; }
00106 
00107         private:
00108 
00109             /**
00110             * Update method to update the color based on the data
00111             **/
00112             class UpdateCallback : public osg::StateAttribute::Callback
00113             {
00114                 void operator()(osg::StateAttribute* sa, osg::NodeVisitor* nv);
00115             };
00116 
00117             //! Current color in use (default 1,1,1,1)
00118             osg::Vec4 mColor;
00119 
00120             //! Current time 
00121             double    mTime;
00122             
00123             //! Start time of the alpha transition animation
00124             double    mStartTime;
00125 
00126             //! End time of the alpha transition animation
00127             double    mEndTime;
00128 
00129             //! Start value of the color animation
00130             osg::Vec4 mStartColor;
00131 
00132             //! End color of the color transition animation
00133             osg::Vec4 mEndColor;
00134 
00135     };
00136 
00137 };
00138 
00139 #endif
00140 

Back to Homepage of osgPPU

Copyright (C) 2008 by Art Tevs (LGPL)