CrystalSpace

Public API Reference

Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

shadervar.h

00001 /*
00002     Copyright (C) 2003 by Mat Sutcliffe <oktal@gmx.co.uk>
00003                           Marten Svanfeldt
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 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     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CS_GFX_SHADERVAR_H__
00021 #define __CS_GFX_SHADERVAR_H__
00022 
00023 #include "csextern.h"
00024 
00025 #include "csutil/refcount.h"
00026 #include "csutil/strhash.h"
00027 #include "iutil/string.h"
00028 #include "csgeom/vector2.h"
00029 #include "csgeom/vector3.h"
00030 #include "csgeom/vector4.h"
00031 #include "csgfx/rgbpixel.h"
00032 #include "iengine/texture.h"
00033 #include "ivideo/texture.h"
00034 #include "csutil/refarr.h"
00035 #include "csutil/leakguard.h"
00036 #include "ivideo/rndbuf.h"
00037 
00038 struct iTextureHandle;
00039 struct iTextureWrapper;
00040 struct csShaderVariableWrapper;
00041 
00042 class csShaderVariable;
00043 
00044 SCF_VERSION (iShaderVariableAccessor, 0, 0, 1);
00045 
00051 struct iShaderVariableAccessor : public iBase
00052 {
00054   virtual void PreGetValue (csShaderVariable *variable) = 0;
00055 };
00056 
00062 class CS_CSGFX_EXPORT csShaderVariable : public csRefCount
00063 {
00064 public:
00070   enum VariableType
00071   {
00073     INT = 1,
00075     FLOAT,
00077     COLOR,
00079     TEXTURE,
00081     RENDERBUFFER,
00083     VECTOR2,
00085     VECTOR3,
00087     VECTOR4,
00089     MATRIX
00090   };
00091 
00092   //CS_LEAKGUARD_DECLARE (csShaderVariable);
00093 private:
00094 
00095   VariableType Type;
00096 
00097   int Int;
00098   csRef<iTextureHandle> TextureHandValue;
00099   csRef<iTextureWrapper> TextureWrapValue;
00100   csRef<iRenderBuffer> RenderBuffer;
00101   csVector4 VectorValue;
00102   csMatrix3* MatrixValuePtr;
00103 
00104   csRef<iShaderVariableAccessor> accessor;
00105 
00106   csStringID Name;
00107 public:
00108 
00110   csShaderVariable (csStringID name);
00111   virtual ~csShaderVariable ()
00112   {
00113     delete MatrixValuePtr;
00114   }
00115 
00116   csShaderVariable& operator= (csShaderVariable& copyFrom);
00117 
00119   VariableType GetType() const { return Type; }
00121   void SetType (VariableType t) { Type = t; }
00122 
00124   void SetAccessor (iShaderVariableAccessor* a) { accessor = a;}
00125 
00127   csStringID GetName () const { return Name; }
00128 
00130   bool GetValue (int& value)
00131   { 
00132     if (accessor) accessor->PreGetValue (this);
00133     value = Int; 
00134     return true; 
00135   }
00136 
00138   bool GetValue (float& value)
00139   { 
00140     if (accessor) accessor->PreGetValue (this);
00141     value = VectorValue.x; 
00142     return true; 
00143   }
00144 
00146   bool GetValue (csRGBpixel& value)
00147   {
00148     if (accessor) accessor->PreGetValue (this);
00149     value.red = (char) VectorValue.x;
00150     value.green = (char) VectorValue.y;
00151     value.blue = (char) VectorValue.z;
00152     value.alpha = (char) VectorValue.w;
00153     return true;
00154   }
00155 
00157   bool GetValue (iTextureHandle*& value)
00158   {
00159     if (accessor) accessor->PreGetValue (this);
00160     value = TextureHandValue;
00161     if (!value && TextureWrapValue)
00162       value = TextureHandValue = TextureWrapValue->GetTextureHandle ();
00163     return true;
00164   }
00165 
00167   bool GetValue (iTextureWrapper*& value)
00168   {
00169     if (accessor) accessor->PreGetValue (this);
00170     value = TextureWrapValue;
00171     return true;
00172   }
00173 
00175   bool GetValue (iRenderBuffer*& value)
00176   {
00177     if (accessor) accessor->PreGetValue (this);
00178     value = RenderBuffer;
00179     return true;
00180   }
00181 
00183   bool GetValue (csVector2& value)
00184   {
00185     if (accessor) accessor->PreGetValue (this);
00186     value.Set (VectorValue.x, VectorValue.y);
00187     return true;
00188   }
00189 
00191   bool GetValue (csVector3& value)
00192   { 
00193     if (accessor) accessor->PreGetValue (this);
00194     value.Set (VectorValue.x, VectorValue.y, VectorValue.z);
00195     return true; 
00196   }
00197 
00199   bool GetValue (csVector4& value)
00200   { 
00201     if (accessor) accessor->PreGetValue (this);
00202     value = VectorValue; 
00203     return true; 
00204   }
00205 
00207   bool GetValue (csMatrix3& value)
00208   {
00209     if (accessor) accessor->PreGetValue (this);
00210     if (MatrixValuePtr)
00211     {
00212       value = *MatrixValuePtr;
00213       return true;
00214     }
00215     else
00216     {
00217       value = csMatrix3();
00218     }
00219     return false;
00220   }
00221 
00222 
00224   bool SetValue (int value) 
00225   { 
00226     Type = INT; 
00227     Int = value; 
00228     float f = (float)value;
00229     VectorValue.Set (f, f, f, f);
00230     return true; 
00231   }
00232 
00234   bool SetValue (float value)
00235   { 
00236     Type = FLOAT; 
00237     Int = (int)value;
00238     VectorValue.Set (value, value, value, value);
00239     return true; 
00240   }
00241 
00243   bool SetValue (const csRGBpixel &value)
00244   {
00245     Type = COLOR;
00246     VectorValue.x = (float) value.red;
00247     VectorValue.y = (float) value.green;
00248     VectorValue.z = (float) value.blue;
00249     VectorValue.w = (float) value.alpha;
00250     return true;
00251   }
00252 
00254   bool SetValue (iTextureHandle* value)
00255   {
00256     Type = TEXTURE;
00257     TextureHandValue = value;
00258     return true;
00259   }
00260 
00262   bool SetValue (iTextureWrapper* value)
00263   {
00264     Type = TEXTURE;
00265     TextureWrapValue = value;
00266     return true;
00267   }
00268 
00270   bool SetValue (iRenderBuffer* value)
00271   {
00272     Type = RENDERBUFFER;
00273     RenderBuffer = value;
00274     return true;
00275   }
00276 
00278   bool SetValue (const csVector2 &value)
00279   {
00280     Type = VECTOR2;
00281     VectorValue.Set (value.x, value.y, 0.0f, 1.0f);
00282     Int = (int)value.x;
00283     return true;
00284   }
00285 
00287   bool SetValue (const csVector3 &value)
00288   { 
00289     Type = VECTOR3; 
00290     VectorValue.Set (value.x, value.y, value.z, 1.0f);
00291     Int = (int)value.x;
00292     return true; 
00293   }
00294 
00296   bool SetValue (const csVector4 &value)
00297   { 
00298     Type = VECTOR4; 
00299     VectorValue.Set (value.x, value.y, value.z, value.w);
00300     Int = (int)value.x;
00301     return true; 
00302   }
00303 
00305   bool SetValue (const csMatrix3 &value)
00306   {
00307     Type = MATRIX;
00308     if (MatrixValuePtr)
00309     {
00310       *MatrixValuePtr = value;
00311     }
00312     else
00313     {
00314       MatrixValuePtr = new csMatrix3 (value);
00315     }
00316     return true;
00317   }
00318 };
00319 
00320 #endif

Generated for Crystal Space by doxygen 1.3.9.1