Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | 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 "csgeom/transfrm.h" 00026 #include "csgeom/vector2.h" 00027 #include "csgeom/vector3.h" 00028 #include "csgeom/vector4.h" 00029 #include "csgfx/rgbpixel.h" 00030 #include "csutil/cscolor.h" 00031 #include "csutil/leakguard.h" 00032 #include "csutil/refarr.h" 00033 #include "csutil/refcount.h" 00034 #include "csutil/strset.h" 00035 00036 #include "iengine/texture.h" 00037 #include "ivideo/texture.h" 00038 #include "ivideo/rndbuf.h" 00039 00040 struct iTextureHandle; 00041 struct iTextureWrapper; 00042 struct csShaderVariableWrapper; 00043 00044 class csShaderVariable; 00045 00046 SCF_VERSION (iShaderVariableAccessor, 0, 0, 1); 00047 00053 struct iShaderVariableAccessor : public iBase 00054 { 00056 virtual void PreGetValue (csShaderVariable *variable) = 0; 00057 }; 00058 00064 class CS_CRYSTALSPACE_EXPORT csShaderVariable : public csRefCount 00065 { 00066 public: 00072 enum VariableType 00073 { 00075 INT = 1, 00077 FLOAT, 00079 COLOR, 00081 TEXTURE, 00083 RENDERBUFFER, 00085 VECTOR2, 00087 VECTOR3, 00089 VECTOR4, 00091 MATRIX, 00093 TRANSFORM, 00095 ARRAY 00096 }; 00097 00098 //CS_LEAKGUARD_DECLARE (csShaderVariable); 00099 private: 00100 00101 VariableType Type; 00102 00103 csRef<iTextureHandle> TextureHandValue; 00104 csRef<iTextureWrapper> TextureWrapValue; 00105 csRef<iRenderBuffer> RenderBuffer; 00106 csVector4 VectorValue; 00107 00108 int Int; 00109 csMatrix3* MatrixValuePtr; 00110 csReversibleTransform* TransformPtr; 00111 00112 csRef<iShaderVariableAccessor> accessor; 00113 00114 csRefArray<csShaderVariable> *array; 00115 00116 csStringID Name; 00117 public: 00118 00123 csShaderVariable (); 00125 csShaderVariable (csStringID name); 00126 csShaderVariable (const csShaderVariable& other) : csRefCount(), 00127 MatrixValuePtr(0), TransformPtr (0), array(0) { *this = other; } 00128 virtual ~csShaderVariable () 00129 { 00130 delete MatrixValuePtr; 00131 delete TransformPtr; 00132 delete array; 00133 } 00134 00135 csShaderVariable& operator= (const csShaderVariable& copyFrom); 00136 00138 VariableType GetType() const { return Type; } 00140 void SetType (VariableType t) { Type = t; } 00141 00143 void SetAccessor (iShaderVariableAccessor* a) { accessor = a;} 00144 00150 void SetName (csStringID newName) { Name = newName; } 00151 00153 csStringID GetName () const { return Name; } 00154 00156 bool GetValue (int& value) 00157 { 00158 if (accessor) accessor->PreGetValue (this); 00159 value = Int; 00160 return true; 00161 } 00162 00164 bool GetValue (float& value) 00165 { 00166 if (accessor) accessor->PreGetValue (this); 00167 value = VectorValue.x; 00168 return true; 00169 } 00170 00172 bool GetValue (csRGBpixel& value) 00173 { 00174 if (accessor) accessor->PreGetValue (this); 00175 value.red = (char) VectorValue.x; 00176 value.green = (char) VectorValue.y; 00177 value.blue = (char) VectorValue.z; 00178 value.alpha = (char) VectorValue.w; 00179 return true; 00180 } 00181 00183 bool GetValue (iTextureHandle*& value) 00184 { 00185 if (accessor) accessor->PreGetValue (this); 00186 value = TextureHandValue; 00187 if (!value && TextureWrapValue) 00188 value = TextureHandValue = TextureWrapValue->GetTextureHandle (); 00189 return true; 00190 } 00191 00193 bool GetValue (iTextureWrapper*& value) 00194 { 00195 if (accessor) accessor->PreGetValue (this); 00196 value = TextureWrapValue; 00197 return true; 00198 } 00199 00201 bool GetValue (iRenderBuffer*& value) 00202 { 00203 if (accessor) accessor->PreGetValue (this); 00204 value = RenderBuffer; 00205 return true; 00206 } 00207 00209 bool GetValue (csVector2& value) 00210 { 00211 if (accessor) accessor->PreGetValue (this); 00212 value.Set (VectorValue.x, VectorValue.y); 00213 return true; 00214 } 00215 00217 bool GetValue (csVector3& value) 00218 { 00219 if (accessor) accessor->PreGetValue (this); 00220 value.Set (VectorValue.x, VectorValue.y, VectorValue.z); 00221 return true; 00222 } 00223 00225 bool GetValue (csColor& value) 00226 { 00227 if (accessor) accessor->PreGetValue (this); 00228 value.Set (VectorValue.x, VectorValue.y, VectorValue.z); 00229 return true; 00230 } 00231 00233 bool GetValue (csVector4& value) 00234 { 00235 if (accessor) accessor->PreGetValue (this); 00236 value = VectorValue; 00237 return true; 00238 } 00239 00241 bool GetValue (csMatrix3& value) 00242 { 00243 if (accessor) accessor->PreGetValue (this); 00244 if (MatrixValuePtr) 00245 { 00246 value = *MatrixValuePtr; 00247 return true; 00248 } 00249 else 00250 { 00251 value = csMatrix3(); 00252 } 00253 return false; 00254 } 00255 00257 bool GetValue (csReversibleTransform& value) 00258 { 00259 if (accessor) accessor->PreGetValue (this); 00260 if (TransformPtr) 00261 { 00262 value = *TransformPtr; 00263 return true; 00264 } 00265 else 00266 { 00267 value = csReversibleTransform(); 00268 } 00269 return false; 00270 } 00271 00272 00274 bool SetValue (int value) 00275 { 00276 Type = INT; 00277 Int = value; 00278 float f = (float)value; 00279 VectorValue.Set (f, f, f, f); 00280 return true; 00281 } 00282 00284 bool SetValue (float value) 00285 { 00286 Type = FLOAT; 00287 Int = (int)value; 00288 VectorValue.Set (value, value, value, value); 00289 return true; 00290 } 00291 00293 bool SetValue (const csRGBpixel &value) 00294 { 00295 Type = COLOR; 00296 VectorValue.x = (float) value.red; 00297 VectorValue.y = (float) value.green; 00298 VectorValue.z = (float) value.blue; 00299 VectorValue.w = (float) value.alpha; 00300 return true; 00301 } 00302 00304 bool SetValue (iTextureHandle* value) 00305 { 00306 Type = TEXTURE; 00307 TextureHandValue = value; 00308 return true; 00309 } 00310 00312 bool SetValue (iTextureWrapper* value) 00313 { 00314 Type = TEXTURE; 00315 TextureWrapValue = value; 00316 return true; 00317 } 00318 00320 bool SetValue (iRenderBuffer* value) 00321 { 00322 Type = RENDERBUFFER; 00323 RenderBuffer = value; 00324 return true; 00325 } 00326 00328 bool SetValue (const csVector2 &value) 00329 { 00330 Type = VECTOR2; 00331 VectorValue.Set (value.x, value.y, 0.0f, 1.0f); 00332 Int = (int)value.x; 00333 return true; 00334 } 00335 00337 bool SetValue (const csVector3 &value) 00338 { 00339 Type = VECTOR3; 00340 VectorValue.Set (value.x, value.y, value.z, 1.0f); 00341 Int = (int)value.x; 00342 return true; 00343 } 00344 00346 bool SetValue (const csColor& value) 00347 { 00348 Type = VECTOR3; 00349 VectorValue.Set (value.red, value.green, value.blue, 1.0f); 00350 Int = (int)value.red; 00351 return true; 00352 } 00353 00355 bool SetValue (const csVector4 &value) 00356 { 00357 Type = VECTOR4; 00358 VectorValue.Set (value.x, value.y, value.z, value.w); 00359 Int = (int)value.x; 00360 return true; 00361 } 00362 00364 bool SetValue (const csMatrix3 &value) 00365 { 00366 Type = MATRIX; 00367 if (MatrixValuePtr) 00368 { 00369 *MatrixValuePtr = value; 00370 } 00371 else 00372 { 00373 MatrixValuePtr = new csMatrix3 (value); 00374 } 00375 return true; 00376 } 00377 00379 bool SetValue (const csReversibleTransform &value) 00380 { 00381 Type = TRANSFORM; 00382 if (TransformPtr) 00383 { 00384 *TransformPtr = value; 00385 } 00386 else 00387 { 00388 TransformPtr = new csReversibleTransform (value); 00389 } 00390 return true; 00391 } 00392 00394 void SetArraySize (size_t size) 00395 { 00396 if (array == 0) 00397 { 00398 array = new csRefArray<csShaderVariable>; 00399 } 00400 array->SetSize (size); 00401 } 00402 00404 size_t GetArraySize () 00405 { 00406 if (array == 0) 00407 return 0; 00408 else 00409 return array->Length (); 00410 } 00411 00417 csShaderVariable *GetArrayElement (size_t element) 00418 { 00419 if (array != 0 && element<array->Length ()) 00420 { 00421 return array->Get (element); 00422 } 00423 return 0; 00424 } 00425 00429 void SetArrayElement (size_t element, csShaderVariable *variable) 00430 { 00431 array->Put (element, variable); 00432 } 00433 }; 00434 00435 #endif
Generated for Crystal Space by doxygen 1.4.4