csgfx/shadervar.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 2003 by Mat Sutcliffe <[email protected]> 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 UNKNOWN = 0, 00084 INT = 1, 00086 FLOAT, 00088 COLOR, 00090 TEXTURE, 00092 RENDERBUFFER, 00094 VECTOR2, 00096 VECTOR3, 00098 VECTOR4, 00100 MATRIX, 00102 TRANSFORM, 00104 ARRAY 00105 }; 00106 00107 //CS_LEAKGUARD_DECLARE (csShaderVariable); 00108 private: 00109 00110 VariableType Type; 00111 00112 csRef<iTextureHandle> TextureHandValue; 00113 csRef<iTextureWrapper> TextureWrapValue; 00114 csRef<iRenderBuffer> RenderBuffer; 00115 csVector4 VectorValue; 00116 00117 int Int; 00118 csMatrix3* MatrixValuePtr; 00119 csReversibleTransform* TransformPtr; 00120 00121 csRef<iShaderVariableAccessor> accessor; 00122 00123 csRefArray<csShaderVariable> *array; 00124 00125 csStringID Name; 00126 public: 00127 00132 csShaderVariable (); 00134 csShaderVariable (csStringID name); 00135 csShaderVariable (const csShaderVariable& other) : csRefCount(), 00136 MatrixValuePtr(0), TransformPtr (0), array(0) { *this = other; } 00137 virtual ~csShaderVariable () 00138 { 00139 delete MatrixValuePtr; 00140 delete TransformPtr; 00141 delete array; 00142 } 00143 00144 csShaderVariable& operator= (const csShaderVariable& copyFrom); 00145 00147 VariableType GetType() 00148 { 00149 /* The accessor should be called at least once so the var has a proper 00150 * type set */ 00151 if ((Type == UNKNOWN) && accessor) accessor->PreGetValue (this); 00152 return Type; 00153 } 00155 void SetType (VariableType t) { Type = t; } 00156 00158 void SetAccessor (iShaderVariableAccessor* a) { accessor = a;} 00159 00165 void SetName (csStringID newName) { Name = newName; } 00166 00168 csStringID GetName () const { return Name; } 00169 00171 bool GetValue (int& value) 00172 { 00173 if (accessor) accessor->PreGetValue (this); 00174 value = Int; 00175 return true; 00176 } 00177 00179 bool GetValue (float& value) 00180 { 00181 if (accessor) accessor->PreGetValue (this); 00182 value = VectorValue.x; 00183 return true; 00184 } 00185 00187 bool GetValue (csRGBpixel& value) 00188 { 00189 if (accessor) accessor->PreGetValue (this); 00190 value.red = (char) VectorValue.x; 00191 value.green = (char) VectorValue.y; 00192 value.blue = (char) VectorValue.z; 00193 value.alpha = (char) VectorValue.w; 00194 return true; 00195 } 00196 00198 bool GetValue (iTextureHandle*& value) 00199 { 00200 if (accessor) accessor->PreGetValue (this); 00201 value = TextureHandValue; 00202 if (!value && TextureWrapValue) 00203 value = TextureHandValue = TextureWrapValue->GetTextureHandle (); 00204 return true; 00205 } 00206 00208 bool GetValue (iTextureWrapper*& value) 00209 { 00210 if (accessor) accessor->PreGetValue (this); 00211 value = TextureWrapValue; 00212 return true; 00213 } 00214 00216 bool GetValue (iRenderBuffer*& value) 00217 { 00218 if (accessor) accessor->PreGetValue (this); 00219 value = RenderBuffer; 00220 return true; 00221 } 00222 00224 bool GetValue (csVector2& value) 00225 { 00226 if (accessor) accessor->PreGetValue (this); 00227 value.Set (VectorValue.x, VectorValue.y); 00228 return true; 00229 } 00230 00232 bool GetValue (csVector3& 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 (csColor& value) 00241 { 00242 if (accessor) accessor->PreGetValue (this); 00243 value.Set (VectorValue.x, VectorValue.y, VectorValue.z); 00244 return true; 00245 } 00246 00248 bool GetValue (csVector4& value) 00249 { 00250 if (accessor) accessor->PreGetValue (this); 00251 value = VectorValue; 00252 return true; 00253 } 00254 00256 bool GetValue (csMatrix3& value) 00257 { 00258 if (accessor) accessor->PreGetValue (this); 00259 if (MatrixValuePtr) 00260 { 00261 value = *MatrixValuePtr; 00262 return true; 00263 } 00264 else 00265 { 00266 value = csMatrix3(); 00267 } 00268 return false; 00269 } 00270 00272 bool GetValue (csReversibleTransform& value) 00273 { 00274 if (accessor) accessor->PreGetValue (this); 00275 if (TransformPtr) 00276 { 00277 value = *TransformPtr; 00278 return true; 00279 } 00280 else 00281 { 00282 value = csReversibleTransform(); 00283 } 00284 return false; 00285 } 00286 00287 00289 bool SetValue (int value) 00290 { 00291 Type = INT; 00292 Int = value; 00293 float f = (float)value; 00294 VectorValue.Set (f, f, f, f); 00295 return true; 00296 } 00297 00299 bool SetValue (float value) 00300 { 00301 Type = FLOAT; 00302 Int = (int)value; 00303 VectorValue.Set (value, value, value, value); 00304 return true; 00305 } 00306 00308 bool SetValue (const csRGBpixel &value) 00309 { 00310 Type = COLOR; 00311 VectorValue.x = (float) value.red; 00312 VectorValue.y = (float) value.green; 00313 VectorValue.z = (float) value.blue; 00314 VectorValue.w = (float) value.alpha; 00315 return true; 00316 } 00317 00319 bool SetValue (iTextureHandle* value) 00320 { 00321 Type = TEXTURE; 00322 TextureHandValue = value; 00323 return true; 00324 } 00325 00327 bool SetValue (iTextureWrapper* value) 00328 { 00329 Type = TEXTURE; 00330 TextureWrapValue = value; 00331 return true; 00332 } 00333 00335 bool SetValue (iRenderBuffer* value) 00336 { 00337 Type = RENDERBUFFER; 00338 RenderBuffer = value; 00339 return true; 00340 } 00341 00343 bool SetValue (const csVector2 &value) 00344 { 00345 Type = VECTOR2; 00346 VectorValue.Set (value.x, value.y, 0.0f, 1.0f); 00347 Int = (int)value.x; 00348 return true; 00349 } 00350 00352 bool SetValue (const csVector3 &value) 00353 { 00354 Type = VECTOR3; 00355 VectorValue.Set (value.x, value.y, value.z, 1.0f); 00356 Int = (int)value.x; 00357 return true; 00358 } 00359 00361 bool SetValue (const csColor& value) 00362 { 00363 Type = VECTOR3; 00364 VectorValue.Set (value.red, value.green, value.blue, 1.0f); 00365 Int = (int)value.red; 00366 return true; 00367 } 00368 00370 bool SetValue (const csVector4 &value) 00371 { 00372 Type = VECTOR4; 00373 VectorValue.Set (value.x, value.y, value.z, value.w); 00374 Int = (int)value.x; 00375 return true; 00376 } 00377 00379 bool SetValue (const csMatrix3 &value) 00380 { 00381 Type = MATRIX; 00382 if (MatrixValuePtr) 00383 { 00384 *MatrixValuePtr = value; 00385 } 00386 else 00387 { 00388 MatrixValuePtr = new csMatrix3 (value); 00389 } 00390 return true; 00391 } 00392 00394 bool SetValue (const csReversibleTransform &value) 00395 { 00396 Type = TRANSFORM; 00397 if (TransformPtr) 00398 { 00399 *TransformPtr = value; 00400 } 00401 else 00402 { 00403 TransformPtr = new csReversibleTransform (value); 00404 } 00405 return true; 00406 } 00407 00408 void AddVariableToArray (csShaderVariable *variable) 00409 { 00410 if (array) array->Push (variable); 00411 } 00412 00413 void RemoveFromArray (size_t element) 00414 { 00415 if (array) array->DeleteIndex (element); 00416 } 00417 00419 void SetArraySize (size_t size) 00420 { 00421 if (array == 0) 00422 { 00423 array = new csRefArray<csShaderVariable>; 00424 } 00425 array->SetSize (size); 00426 } 00427 00429 size_t GetArraySize () 00430 { 00431 if (array == 0) 00432 return 0; 00433 else 00434 return array->Length (); 00435 } 00436 00442 csShaderVariable *GetArrayElement (size_t element) 00443 { 00444 if (array != 0 && element<array->Length ()) 00445 { 00446 return array->Get (element); 00447 } 00448 return 0; 00449 } 00450 00454 void SetArrayElement (size_t element, csShaderVariable *variable) 00455 { 00456 array->Put (element, variable); 00457 } 00458 }; 00459 00460 namespace CS 00461 { 00463 struct ShaderVarName 00464 { 00465 csStringID name; 00466 00467 ShaderVarName() : name (csInvalidStringID) {} 00468 ShaderVarName (iStringSet* strings, const char* name) 00469 { this->name = strings->Request (name); } 00470 00471 operator csStringID () const { return name; } 00472 }; 00473 00474 } // namespace CS 00475 00478 #endif
Generated for Crystal Space by doxygen 1.4.7