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/math.h" 00030 #include "csgeom/quaternion.h" 00031 #include "csgeom/transfrm.h" 00032 #include "csgeom/vector2.h" 00033 #include "csgeom/vector3.h" 00034 #include "csgeom/vector4.h" 00035 #include "csgfx/rgbpixel.h" 00036 #include "csutil/cscolor.h" 00037 #include "csutil/leakguard.h" 00038 #include "csutil/refarr.h" 00039 #include "csutil/refcount.h" 00040 #include "csutil/strset.h" 00041 00042 #include "iengine/texture.h" 00043 #include "ivideo/texture.h" 00044 #include "ivideo/rndbuf.h" 00045 00046 struct iTextureHandle; 00047 struct iTextureWrapper; 00048 struct csShaderVariableWrapper; 00049 00050 class csShaderVariable; 00051 00060 struct iShaderVariableAccessor : public virtual iBase 00061 { 00062 SCF_INTERFACE (iShaderVariableAccessor, 2, 0, 0); 00063 00065 virtual void PreGetValue (csShaderVariable *variable) = 0; 00066 }; 00067 00073 class CS_CRYSTALSPACE_EXPORT csShaderVariable : public csRefCount 00074 { 00075 public: 00081 enum VariableType 00082 { 00084 UNKNOWN = 0, 00086 INT = 1, 00088 FLOAT, 00090 TEXTURE, 00092 RENDERBUFFER, 00094 VECTOR2, 00096 VECTOR3, 00098 VECTOR4, 00100 MATRIX, 00102 TRANSFORM, 00104 ARRAY, 00105 00110 COLOR = VECTOR4 00111 }; 00112 00113 //CS_LEAKGUARD_DECLARE (csShaderVariable); 00114 private: 00115 00116 VariableType Type; 00117 00118 csRef<iTextureHandle> TextureHandValue; 00119 csRef<iTextureWrapper> TextureWrapValue; 00120 csRef<iRenderBuffer> RenderBuffer; 00121 csVector4 VectorValue; 00122 00123 int Int; 00124 csMatrix3* MatrixValuePtr; 00125 csReversibleTransform* TransformPtr; 00126 00127 csRef<iShaderVariableAccessor> accessor; 00128 00129 csRefArray<csShaderVariable> *array; 00130 00131 csStringID Name; 00132 public: 00133 00138 csShaderVariable (); 00140 csShaderVariable (csStringID name); 00141 csShaderVariable (const csShaderVariable& other) : csRefCount(), 00142 MatrixValuePtr(0), TransformPtr (0), array(0) { *this = other; } 00143 virtual ~csShaderVariable () 00144 { 00145 delete MatrixValuePtr; 00146 delete TransformPtr; 00147 delete array; 00148 } 00149 00150 csShaderVariable& operator= (const csShaderVariable& copyFrom); 00151 00153 VariableType GetType() 00154 { 00155 /* The accessor should be called at least once so the var has a proper 00156 * type set */ 00157 if ((Type == UNKNOWN) && accessor) accessor->PreGetValue (this); 00158 return Type; 00159 } 00161 void SetType (VariableType t) { Type = t; } 00162 00164 void SetAccessor (iShaderVariableAccessor* a) { accessor = a;} 00165 00171 void SetName (csStringID newName) { Name = newName; } 00172 00174 csStringID GetName () const { return Name; } 00175 00177 bool GetValue (int& value) 00178 { 00179 if (accessor) accessor->PreGetValue (this); 00180 value = Int; 00181 return true; 00182 } 00183 00185 bool GetValue (float& value) 00186 { 00187 if (accessor) accessor->PreGetValue (this); 00188 value = VectorValue.x; 00189 return true; 00190 } 00191 00193 bool GetValue (csRGBpixel& value) 00194 { 00195 if (accessor) accessor->PreGetValue (this); 00196 value.red = 00197 (unsigned char) csClamp (int (VectorValue.x * 255.0f), 255, 0); 00198 value.green = 00199 (unsigned char) csClamp (int (VectorValue.y * 255.0f), 255, 0); 00200 value.blue = 00201 (unsigned char) csClamp (int (VectorValue.z * 255.0f), 255, 0); 00202 value.alpha = 00203 (unsigned char) csClamp (int (VectorValue.w * 255.0f), 255, 0);; 00204 return true; 00205 } 00206 00208 bool GetValue (iTextureHandle*& value) 00209 { 00210 if (accessor) accessor->PreGetValue (this); 00211 value = TextureHandValue; 00212 if (!value && TextureWrapValue) 00213 value = TextureHandValue = TextureWrapValue->GetTextureHandle (); 00214 return true; 00215 } 00216 00218 bool GetValue (iTextureWrapper*& value) 00219 { 00220 if (accessor) accessor->PreGetValue (this); 00221 value = TextureWrapValue; 00222 return true; 00223 } 00224 00226 bool GetValue (iRenderBuffer*& value) 00227 { 00228 if (accessor) accessor->PreGetValue (this); 00229 value = RenderBuffer; 00230 return true; 00231 } 00232 00234 bool GetValue (csVector2& value) 00235 { 00236 if (accessor) accessor->PreGetValue (this); 00237 value.Set (VectorValue.x, VectorValue.y); 00238 return true; 00239 } 00240 00242 bool GetValue (csVector3& value) 00243 { 00244 if (accessor) accessor->PreGetValue (this); 00245 value.Set (VectorValue.x, VectorValue.y, VectorValue.z); 00246 return true; 00247 } 00248 00250 bool GetValue (csColor& value) 00251 { 00252 if (accessor) accessor->PreGetValue (this); 00253 value.Set (VectorValue.x, VectorValue.y, VectorValue.z); 00254 return true; 00255 } 00256 00258 bool GetValue (csVector4& value) 00259 { 00260 if (accessor) accessor->PreGetValue (this); 00261 value = VectorValue; 00262 return true; 00263 } 00264 00266 bool GetValue (csQuaternion& value) 00267 { 00268 if (accessor) accessor->PreGetValue (this); 00269 value.Set (VectorValue.x, VectorValue.y, VectorValue.z, VectorValue.w); 00270 return true; 00271 } 00272 00274 bool GetValue (csMatrix3& value) 00275 { 00276 if (accessor) accessor->PreGetValue (this); 00277 if (MatrixValuePtr) 00278 { 00279 value = *MatrixValuePtr; 00280 return true; 00281 } 00282 else 00283 { 00284 value = csMatrix3(); 00285 } 00286 return false; 00287 } 00288 00290 bool GetValue (csReversibleTransform& value) 00291 { 00292 if (accessor) accessor->PreGetValue (this); 00293 if (TransformPtr) 00294 { 00295 value = *TransformPtr; 00296 return true; 00297 } 00298 else 00299 { 00300 value = csReversibleTransform(); 00301 } 00302 return false; 00303 } 00304 00305 00307 bool SetValue (int value) 00308 { 00309 Type = INT; 00310 Int = value; 00311 float f = (float)value; 00312 VectorValue.Set (f, f, f, f); 00313 return true; 00314 } 00315 00317 bool SetValue (float value) 00318 { 00319 Type = FLOAT; 00320 Int = (int)value; 00321 VectorValue.Set (value, value, value, value); 00322 return true; 00323 } 00324 00326 bool SetValue (const csRGBpixel &value) 00327 { 00328 Type = COLOR; 00329 VectorValue.x = (float)value.red / 255.0f; 00330 VectorValue.y = (float)value.green / 255.0f; 00331 VectorValue.z = (float)value.blue / 255.0f; 00332 VectorValue.w = (float)value.alpha / 255.0f; 00333 return true; 00334 } 00335 00337 bool SetValue (iTextureHandle* value) 00338 { 00339 Type = TEXTURE; 00340 TextureHandValue = value; 00341 return true; 00342 } 00343 00345 bool SetValue (iTextureWrapper* value) 00346 { 00347 Type = TEXTURE; 00348 TextureWrapValue = value; 00349 return true; 00350 } 00351 00353 bool SetValue (iRenderBuffer* value) 00354 { 00355 Type = RENDERBUFFER; 00356 RenderBuffer = value; 00357 return true; 00358 } 00359 00361 bool SetValue (const csVector2 &value) 00362 { 00363 Type = VECTOR2; 00364 VectorValue.Set (value.x, value.y, 0.0f, 1.0f); 00365 Int = (int)value.x; 00366 return true; 00367 } 00368 00370 bool SetValue (const csVector3 &value) 00371 { 00372 Type = VECTOR3; 00373 VectorValue.Set (value.x, value.y, value.z, 1.0f); 00374 Int = (int)value.x; 00375 return true; 00376 } 00377 00379 bool SetValue (const csColor& value) 00380 { 00381 Type = VECTOR3; 00382 VectorValue.Set (value.red, value.green, value.blue, 1.0f); 00383 Int = (int)value.red; 00384 return true; 00385 } 00386 00388 bool SetValue (const csVector4 &value) 00389 { 00390 Type = VECTOR4; 00391 VectorValue.Set (value.x, value.y, value.z, value.w); 00392 Int = (int)value.x; 00393 return true; 00394 } 00395 00396 bool SetValue (const csQuaternion& value) 00397 { 00398 Type = VECTOR4; 00399 VectorValue.Set (value.v.x, value.v.y, value.v.z, value.w); 00400 return true; 00401 } 00402 00404 bool SetValue (const csMatrix3 &value) 00405 { 00406 Type = MATRIX; 00407 if (MatrixValuePtr) 00408 { 00409 *MatrixValuePtr = value; 00410 } 00411 else 00412 { 00413 MatrixValuePtr = new csMatrix3 (value); 00414 } 00415 return true; 00416 } 00417 00419 bool SetValue (const csReversibleTransform &value) 00420 { 00421 Type = TRANSFORM; 00422 if (TransformPtr) 00423 { 00424 *TransformPtr = value; 00425 } 00426 else 00427 { 00428 TransformPtr = new csReversibleTransform (value); 00429 } 00430 return true; 00431 } 00432 00433 void AddVariableToArray (csShaderVariable *variable) 00434 { 00435 if (array) array->Push (variable); 00436 } 00437 00438 void RemoveFromArray (size_t element) 00439 { 00440 if (array) array->DeleteIndex (element); 00441 } 00442 00444 void SetArraySize (size_t size) 00445 { 00446 if (array == 0) 00447 { 00448 array = new csRefArray<csShaderVariable>; 00449 } 00450 array->SetSize (size); 00451 } 00452 00454 size_t GetArraySize () 00455 { 00456 if (array == 0) 00457 return 0; 00458 else 00459 return array->GetSize (); 00460 } 00461 00467 csShaderVariable *GetArrayElement (size_t element) 00468 { 00469 if (array != 0 && element<array->GetSize ()) 00470 { 00471 return array->Get (element); 00472 } 00473 return 0; 00474 } 00475 00479 void SetArrayElement (size_t element, csShaderVariable *variable) 00480 { 00481 array->Put (element, variable); 00482 } 00483 }; 00484 00485 namespace CS 00486 { 00488 struct ShaderVarName 00489 { 00490 csStringID name; 00491 00492 ShaderVarName() : name (csInvalidStringID) {} 00493 ShaderVarName (iStringSet* strings, const char* name) 00494 { this->name = strings->Request (name); } 00495 00496 operator csStringID () const { return name; } 00497 }; 00498 00499 } // namespace CS 00500 00503 #endif
Generated for Crystal Space 1.2.1 by doxygen 1.5.3