CrystalSpace

Public API Reference

csgfx/shadervar.h

Go to the documentation of this file.
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 
00027 #include "csextern.h"
00028 
00029 #include "csgeom/transfrm.h"
00030 #include "csgeom/vector2.h"
00031 #include "csgeom/vector3.h"
00032 #include "csgeom/vector4.h"
00033 #include "csgfx/rgbpixel.h"
00034 #include "csutil/cscolor.h"
00035 #include "csutil/leakguard.h"
00036 #include "csutil/refarr.h"
00037 #include "csutil/refcount.h"
00038 #include "csutil/strset.h"
00039 
00040 #include "iengine/texture.h"
00041 #include "ivideo/texture.h"
00042 #include "ivideo/rndbuf.h"
00043 
00044 struct iTextureHandle;
00045 struct iTextureWrapper;
00046 struct csShaderVariableWrapper;
00047 
00048 class csShaderVariable;
00049 
00058 struct iShaderVariableAccessor : public virtual iBase
00059 {
00060   SCF_INTERFACE (iShaderVariableAccessor, 2, 0, 0);
00061 
00063   virtual void PreGetValue (csShaderVariable *variable) = 0;
00064 };
00065 
00071 class CS_CRYSTALSPACE_EXPORT csShaderVariable : public csRefCount
00072 {
00073 public:
00079   enum VariableType
00080   {
00082     INT = 1,
00084     FLOAT,
00086     COLOR,
00088     TEXTURE,
00090     RENDERBUFFER,
00092     VECTOR2,
00094     VECTOR3,
00096     VECTOR4,
00098     MATRIX,
00100     TRANSFORM,
00102     ARRAY
00103   };
00104 
00105   //CS_LEAKGUARD_DECLARE (csShaderVariable);
00106 private:
00107 
00108   VariableType Type;
00109 
00110   csRef<iTextureHandle> TextureHandValue;
00111   csRef<iTextureWrapper> TextureWrapValue;
00112   csRef<iRenderBuffer> RenderBuffer;
00113   csVector4 VectorValue;
00114 
00115   int Int;
00116   csMatrix3* MatrixValuePtr;
00117   csReversibleTransform* TransformPtr;
00118 
00119   csRef<iShaderVariableAccessor> accessor;
00120 
00121   csRefArray<csShaderVariable> *array;
00122 
00123   csStringID Name;
00124 public:
00125 
00130   csShaderVariable ();
00132   csShaderVariable (csStringID name);
00133   csShaderVariable (const csShaderVariable& other) : csRefCount(),
00134     MatrixValuePtr(0), TransformPtr (0), array(0) { *this = other; }
00135   virtual ~csShaderVariable ()
00136   {
00137     delete MatrixValuePtr;
00138     delete TransformPtr;
00139     delete array;
00140   }
00141 
00142   csShaderVariable& operator= (const csShaderVariable& copyFrom);
00143 
00145   VariableType GetType() const { return Type; }
00147   void SetType (VariableType t) { Type = t; }
00148 
00150   void SetAccessor (iShaderVariableAccessor* a) { accessor = a;}
00151 
00157   void SetName (csStringID newName) { Name = newName; }
00158   
00160   csStringID GetName () const { return Name; }
00161 
00163   bool GetValue (int& value)
00164   { 
00165     if (accessor) accessor->PreGetValue (this);
00166     value = Int; 
00167     return true; 
00168   }
00169 
00171   bool GetValue (float& value)
00172   { 
00173     if (accessor) accessor->PreGetValue (this);
00174     value = VectorValue.x; 
00175     return true; 
00176   }
00177 
00179   bool GetValue (csRGBpixel& value)
00180   {
00181     if (accessor) accessor->PreGetValue (this);
00182     value.red = (char) VectorValue.x;
00183     value.green = (char) VectorValue.y;
00184     value.blue = (char) VectorValue.z;
00185     value.alpha = (char) VectorValue.w;
00186     return true;
00187   }
00188 
00190   bool GetValue (iTextureHandle*& value)
00191   {
00192     if (accessor) accessor->PreGetValue (this);
00193     value = TextureHandValue;
00194     if (!value && TextureWrapValue)
00195       value = TextureHandValue = TextureWrapValue->GetTextureHandle ();
00196     return true;
00197   }
00198 
00200   bool GetValue (iTextureWrapper*& value)
00201   {
00202     if (accessor) accessor->PreGetValue (this);
00203     value = TextureWrapValue;
00204     return true;
00205   }
00206 
00208   bool GetValue (iRenderBuffer*& value)
00209   {
00210     if (accessor) accessor->PreGetValue (this);
00211     value = RenderBuffer;
00212     return true;
00213   }
00214 
00216   bool GetValue (csVector2& value)
00217   {
00218     if (accessor) accessor->PreGetValue (this);
00219     value.Set (VectorValue.x, VectorValue.y);
00220     return true;
00221   }
00222 
00224   bool GetValue (csVector3& value)
00225   { 
00226     if (accessor) accessor->PreGetValue (this);
00227     value.Set (VectorValue.x, VectorValue.y, VectorValue.z);
00228     return true; 
00229   }
00230 
00232   bool GetValue (csColor& value)
00233   { 
00234     if (accessor) accessor->PreGetValue (this);
00235     value.Set (VectorValue.x, VectorValue.y, VectorValue.z);
00236     return true; 
00237   }
00238 
00240   bool GetValue (csVector4& value)
00241   { 
00242     if (accessor) accessor->PreGetValue (this);
00243     value = VectorValue; 
00244     return true; 
00245   }
00246 
00248   bool GetValue (csMatrix3& value)
00249   {
00250     if (accessor) accessor->PreGetValue (this);
00251     if (MatrixValuePtr)
00252     {
00253       value = *MatrixValuePtr;
00254       return true;
00255     }
00256     else
00257     {
00258       value = csMatrix3();
00259     }
00260     return false;
00261   }
00262 
00264   bool GetValue (csReversibleTransform& value)
00265   {
00266     if (accessor) accessor->PreGetValue (this);
00267     if (TransformPtr)
00268     {
00269       value = *TransformPtr;
00270       return true;
00271     }
00272     else
00273     {
00274       value = csReversibleTransform();
00275     }
00276     return false;
00277   }
00278 
00279 
00281   bool SetValue (int value) 
00282   { 
00283     Type = INT; 
00284     Int = value; 
00285     float f = (float)value;
00286     VectorValue.Set (f, f, f, f);
00287     return true; 
00288   }
00289 
00291   bool SetValue (float value)
00292   { 
00293     Type = FLOAT; 
00294     Int = (int)value;
00295     VectorValue.Set (value, value, value, value);
00296     return true; 
00297   }
00298 
00300   bool SetValue (const csRGBpixel &value)
00301   {
00302     Type = COLOR;
00303     VectorValue.x = (float) value.red;
00304     VectorValue.y = (float) value.green;
00305     VectorValue.z = (float) value.blue;
00306     VectorValue.w = (float) value.alpha;
00307     return true;
00308   }
00309 
00311   bool SetValue (iTextureHandle* value)
00312   {
00313     Type = TEXTURE;
00314     TextureHandValue = value;
00315     return true;
00316   }
00317 
00319   bool SetValue (iTextureWrapper* value)
00320   {
00321     Type = TEXTURE;
00322     TextureWrapValue = value;
00323     return true;
00324   }
00325 
00327   bool SetValue (iRenderBuffer* value)
00328   {
00329     Type = RENDERBUFFER;
00330     RenderBuffer = value;
00331     return true;
00332   }
00333 
00335   bool SetValue (const csVector2 &value)
00336   {
00337     Type = VECTOR2;
00338     VectorValue.Set (value.x, value.y, 0.0f, 1.0f);
00339     Int = (int)value.x;
00340     return true;
00341   }
00342 
00344   bool SetValue (const csVector3 &value)
00345   { 
00346     Type = VECTOR3; 
00347     VectorValue.Set (value.x, value.y, value.z, 1.0f);
00348     Int = (int)value.x;
00349     return true; 
00350   }
00351 
00353   bool SetValue (const csColor& value)
00354   { 
00355     Type = VECTOR3; 
00356     VectorValue.Set (value.red, value.green, value.blue, 1.0f);
00357     Int = (int)value.red;
00358     return true; 
00359   }
00360 
00362   bool SetValue (const csVector4 &value)
00363   { 
00364     Type = VECTOR4; 
00365     VectorValue.Set (value.x, value.y, value.z, value.w);
00366     Int = (int)value.x;
00367     return true; 
00368   }
00369 
00371   bool SetValue (const csMatrix3 &value)
00372   {
00373     Type = MATRIX;
00374     if (MatrixValuePtr)
00375     {
00376       *MatrixValuePtr = value;
00377     }
00378     else
00379     {
00380       MatrixValuePtr = new csMatrix3 (value);
00381     }
00382     return true;
00383   }
00384 
00386   bool SetValue (const csReversibleTransform &value)
00387   {
00388     Type = TRANSFORM;
00389     if (TransformPtr)
00390     {
00391       *TransformPtr = value;
00392     }
00393     else
00394     {
00395       TransformPtr = new csReversibleTransform (value);
00396     }
00397     return true;
00398   }
00399 
00401   void SetArraySize (size_t size)
00402   {
00403     if (array == 0)
00404     {
00405       array = new csRefArray<csShaderVariable>;
00406     }
00407     array->SetSize (size);
00408   }
00409 
00411   size_t GetArraySize ()
00412   {
00413     if (array == 0)
00414       return 0;
00415     else
00416       return array->Length ();
00417   }
00418 
00424   csShaderVariable *GetArrayElement (size_t element)
00425   {
00426     if (array != 0 && element<array->Length ())
00427     {
00428       return array->Get (element);
00429     }
00430     return 0;
00431   }
00432 
00436   void SetArrayElement (size_t element, csShaderVariable *variable)
00437   {
00438     array->Put (element, variable);
00439   }
00440 };
00441 
00444 #endif

Generated for Crystal Space by doxygen 1.4.6