AaronCameron.net
I care not for your petty politics.
Not a Member? - Login or Create an Account
Sunday the 20th of May 2012 @ 03:02pm
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cColour.h

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2002  Aaron Cameron
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2.1 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, mC[3]  02111-1307 USA
00018 
00019 A copy of the GNU Lesser General Public License has been provided with
00020 this library in the file 'COPYING'.
00021 
00022 Contact informC[3]tion for the author of this library has been provided
00023 with this library in the file 'AUTHOR'.
00024 ************************************************************************/
00025 #ifndef _n2l4_cColour_H
00026 #define _n2l4_cColour_H
00027 
00028 #include "n2l/n2l.h"
00029 #include "n2l/dynVars.h"
00030 #include <GL/gl.h>
00031 
00032 /******************************************************************************/
00033 namespace n2l
00034 {
00038     class cColour
00039     {
00040     public:
00041         static const cColour White;
00042         static const cColour Red;
00043         static const cColour Blue;
00044         static const cColour Green;
00045         static const cColour Black;
00046     
00047         /**********************************************************************/
00048         cColour()
00049         {
00050             mC[0] = 1.0f;
00051             mC[1] = 1.0f;
00052             mC[2] = 1.0f;
00053             mC[3] = 1.0f;
00054         }
00055 
00056         /**********************************************************************/
00057         cColour( const cColour &iColour )
00058         {
00059             mC[0] = iColour.mC[0];
00060             mC[1] = iColour.mC[1];
00061             mC[2] = iColour.mC[2];
00062             mC[3] = iColour.mC[3];
00063         }
00064 
00065         /**********************************************************************/
00066         cColour( const cDynVar &iDef )
00067         {
00068             mC[0] = iDef.keyValueOr(0,0.0f);
00069             mC[1] = iDef.keyValueOr(1,0.0f);
00070             mC[2] = iDef.keyValueOr(2,0.0f);
00071             mC[3] = iDef.keyValueOr(3,1.0f);
00072         }
00073 
00074         /**********************************************************************/
00075         cColour( const tFloat &iR, const tFloat &iG, const tFloat &iB,
00076             const tFloat &iA = 1.0f )
00077         {
00078             mC[0] = iR; mC[1] = iG;
00079             mC[2] = iB; mC[3] = iA;
00080         }
00081 
00082         /**********************************************************************/
00083         inline void r( const tFloat & iR ) { mC[0] = iR; }
00084 
00085         /**********************************************************************/
00086         inline void g( const tFloat & iG ) { mC[1] = iG; }
00087 
00088         /**********************************************************************/
00089         inline void b( const tFloat & iB ) { mC[2] = iB; }
00090 
00091         /**********************************************************************/
00092         inline void a( const tFloat & iA ) { mC[3] = iA; }
00093 
00094         /**********************************************************************/
00095         inline cColour &set( const tFloat &iR,  const tFloat &iG,
00096             const tFloat &iB, const tFloat &iA = 1.0f )
00097         {
00098             mC[0] = iR; mC[1] = iG; mC[2] = iB; mC[3] = iA;
00099             return *this;
00100         }
00101 
00102         /**********************************************************************/
00109         void normalize()
00110         {
00111             tFloat scale;
00112             // Operate on the largest component
00113             if (mC[0]>mC[1] && mC[0]>mC[2] && mC[0]>mC[3]) {
00114                 scale = 1.0f/mC[0];
00115             } else if (mC[1]>mC[0] && mC[1]>mC[2] && mC[1]>mC[3]) {
00116                 scale = 1.0f/mC[1];
00117             } else if (mC[2]>mC[1] && mC[2]>mC[0] && mC[2]>mC[3]) {
00118                 scale = 1.0f/mC[2];
00119             } else if (mC[3]>mC[1] && mC[3]>mC[2] && mC[3]>mC[0]) {
00120                 scale = 1.0f/mC[3];
00121             } else {
00122                 // else we do nothing?  That seems wrong.
00123                 return;
00124             }
00125             *this *= scale;
00126         }
00127 
00128         /**********************************************************************/
00129         inline const tFloat &r() const { return mC[0]; }
00130 
00131         /**********************************************************************/
00132         inline const tFloat &g() const { return mC[1]; }
00133 
00134         /**********************************************************************/
00135         inline const tFloat &b() const { return mC[2]; }
00136 
00137         /**********************************************************************/
00138         inline const tFloat &a() const { return mC[3]; }
00139 
00140         /**********************************************************************/
00141         inline void get( tFloat &oR, tFloat &oG, tFloat &oB ) const
00142         {
00143             oR = mC[0]; oG = mC[1]; oB = mC[2];
00144         }
00145 
00146         /**********************************************************************/
00147         inline void get( tFloat &oR, tFloat &oG, tFloat &oB, tFloat &oA ) const
00148         {
00149             oR = mC[0]; oG = mC[1]; oB = mC[2]; oA = mC[3];
00150         }
00151 
00152         /**********************************************************************/
00153         inline void operator +=( const cColour &iColour )
00154         {
00155             mC[0]+=iColour.mC[0];
00156             mC[1]+=iColour.mC[1];
00157             mC[2]+=iColour.mC[2];
00158             mC[3]+=iColour.mC[3];
00159         }
00160 
00161         /**********************************************************************/
00162         inline void operator *=( const cColour &iColour )
00163         {
00164             mC[0]*=iColour.mC[0];
00165             mC[1]*=iColour.mC[1];
00166             mC[2]*=iColour.mC[2];
00167             mC[3]*=iColour.mC[3];
00168         }
00169 
00170         /**********************************************************************/
00171         inline const cColour operator -( const cColour &iColour ) const
00172         {
00173             cColour temp( mC[0]-iColour.mC[0], mC[1]-iColour.mC[1], 
00174                 mC[2]-iColour.mC[2], mC[3]-iColour.mC[3] );
00175             return temp;
00176         }
00177 
00178         /**********************************************************************/
00179         inline const cColour operator +( const cColour &iColour ) const
00180         {
00181             cColour temp( mC[0]+iColour.mC[0], mC[1]+iColour.mC[1], 
00182                 mC[2]+iColour.mC[2], mC[3]+iColour.mC[3] );
00183             return temp;
00184         }
00185 
00186         /**********************************************************************/
00187         inline const cColour operator *( const tFloat &iScaler ) const
00188         {
00189             cColour temp( mC[0]*iScaler, mC[1]*iScaler, 
00190                 mC[2]*iScaler, mC[3]*iScaler );
00191             return temp;
00192         }
00193 
00194         /**********************************************************************/
00195         inline void operator *=( const tFloat &iScaler )
00196         {
00197             mC[0]*=iScaler;
00198             mC[1]*=iScaler;
00199             mC[2]*=iScaler;
00200             mC[3]*=iScaler;
00201         }
00202 
00203         /**********************************************************************/
00204         inline void operator -=( const cColour &iColour )
00205         {
00206             mC[0]-=iColour.mC[0];
00207             mC[1]-=iColour.mC[1];
00208             mC[2]-=iColour.mC[2];
00209             mC[3]-=iColour.mC[3];
00210         }
00211 
00212         /**********************************************************************/
00213         cColour &operator =( const cDynVar &iDef )
00214         {
00215             mC[0] = iDef.keyValueOr(0,0.0f);
00216             mC[1] = iDef.keyValueOr(1,0.0f);
00217             mC[2] = iDef.keyValueOr(2,0.0f);
00218             mC[3] = iDef.keyValueOr(3,1.0f);
00219             return *this;
00220         }
00221 
00222         /**********************************************************************/
00223         void clampToZeroOne()
00224         {
00225             mC[0] = n2lMax( n2lMin(mC[0], 1.0f), 0.0f );
00226             mC[1] = n2lMax( n2lMin(mC[1], 1.0f), 0.0f );
00227             mC[2] = n2lMax( n2lMin(mC[2], 1.0f), 0.0f );
00228             mC[3] = n2lMax( n2lMin(mC[3], 1.0f), 0.0f );
00229         }
00230 
00231         /**********************************************************************/
00232         const tString dump() const {
00233             tString str("tColour(");
00234             str += asString(mC[0]);
00235             str += ",";
00236             str += asString(mC[1]);
00237             str += ",";
00238             str += asString(mC[2]);
00239             str += ",";
00240             str += asString(mC[3]);
00241             str += ")";
00242             return str;
00243         }
00244 
00245         /**********************************************************************/
00246         inline void glColor4fv() const { ::glColor4fv( mC );    }
00247 
00248         /**********************************************************************/
00249         inline void glColor4f() const {
00250 			::glColor4f( mC[0],mC[1],mC[2],mC[3] );
00251         }
00252 
00253         /**********************************************************************/
00254         inline void glColor3fv() const { ::glColor3fv( mC ); }
00255 
00256         /**********************************************************************/
00257         inline void glColor3f() const { ::glColor3f( mC[0],mC[1],mC[2] ); }
00258 
00259         /**********************************************************************/
00260         inline void glColour3() const { ::glColor3fv( mC ); }
00261 
00262         /**********************************************************************/
00263         inline void glColour4() const { ::glColor4fv( mC ); }
00264 
00265         /**********************************************************************/
00266         inline void glMaterialfv(   const GLenum iFace,
00267                                     const GLenum iPName ) const {
00268 			::glMaterialfv( iFace, iPName, mC );
00269         }
00270 
00271         /**********************************************************************/
00272         inline void glLightfv(  const GLenum iLight, const GLenum iProp ) const
00273         {
00274 			::glLightfv( iLight, iProp, mC );
00275         }
00276 
00277     private:
00278         tFloat mC[4];
00279 
00280      }; // class 
00281 
00282 } // namespace n2l
00283 
00284 #endif
©2012 Aaron Cameron