csgfx/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 "ivideo/rndbuf.h" 00036 00037 struct iTextureHandle; 00038 struct iTextureWrapper; 00039 struct csShaderVariableWrapper; 00040 00041 class csShaderVariable; 00042 00043 SCF_VERSION (iShaderVariableAccessor, 0, 0, 1); 00049 struct iShaderVariableAccessor : public iBase 00050 { 00052 virtual void PreGetValue (csShaderVariable *variable) = 0; 00053 }; 00054 00055 00059 class CS_CSGFX_EXPORT csShaderVariable : public csRefCount 00060 { 00061 public: 00063 enum VariableType 00064 { 00065 INT = 1, 00066 FLOAT, 00067 COLOR, 00068 TEXTURE, 00069 RENDERBUFFER, 00070 VECTOR2, 00071 VECTOR3, 00072 VECTOR4, 00073 MATRIX 00074 }; 00075 00076 private: 00077 00078 VariableType Type; 00079 00080 int Int; 00081 csRef<iTextureHandle> TextureHandValue; 00082 csRef<iTextureWrapper> TextureWrapValue; 00083 csRef<iRenderBuffer> RenderBuffer; 00084 csVector4 VectorValue; 00085 csMatrix3* MatrixValuePtr; 00086 00087 csRef<iShaderVariableAccessor> accessor; 00088 00089 public: 00090 csStringID Name; 00091 00093 csShaderVariable (csStringID name); 00094 virtual ~csShaderVariable () 00095 { 00096 delete MatrixValuePtr; 00097 } 00098 00099 csShaderVariable& operator= (csShaderVariable& copyFrom); 00100 00102 VariableType GetType() const { return Type; } 00104 void SetType (VariableType t) { Type = t; } 00105 00107 void SetAccessor (iShaderVariableAccessor* a) { accessor = a;} 00108 00110 csStringID GetName () const { return Name; } 00111 00113 bool GetValue (int& value) 00114 { 00115 if (accessor) accessor->PreGetValue (this); 00116 value = Int; 00117 return true; 00118 } 00119 00121 bool GetValue (float& value) 00122 { 00123 if (accessor) accessor->PreGetValue (this); 00124 value = VectorValue.x; 00125 return true; 00126 } 00127 00129 bool GetValue (csRGBpixel& value) 00130 { 00131 if (accessor) accessor->PreGetValue (this); 00132 value.red = (char) VectorValue.x; 00133 value.green = (char) VectorValue.y; 00134 value.blue = (char) VectorValue.z; 00135 value.alpha = (char) VectorValue.w; 00136 return true; 00137 } 00138 00140 bool GetValue (iTextureHandle*& value) 00141 { 00142 if (accessor) accessor->PreGetValue (this); 00143 value = TextureHandValue; 00144 if (!value && TextureWrapValue) 00145 value = TextureHandValue = TextureWrapValue->GetTextureHandle (); 00146 return true; 00147 } 00148 00150 bool GetValue (iTextureWrapper*& value) 00151 { 00152 if (accessor) accessor->PreGetValue (this); 00153 value = TextureWrapValue; 00154 return true; 00155 } 00156 00158 bool GetValue (iRenderBuffer*& value) 00159 { 00160 if (accessor) accessor->PreGetValue (this); 00161 value = RenderBuffer; 00162 return true; 00163 } 00164 00166 bool GetValue (csVector2& value) 00167 { 00168 if (accessor) accessor->PreGetValue (this); 00169 value.Set (VectorValue.x, VectorValue.y); 00170 return true; 00171 } 00172 00174 bool GetValue (csVector3& value) 00175 { 00176 if (accessor) accessor->PreGetValue (this); 00177 value.Set (VectorValue.x, VectorValue.y, VectorValue.z); 00178 return true; 00179 } 00180 00182 bool GetValue (csVector4& value) 00183 { 00184 if (accessor) accessor->PreGetValue (this); 00185 value = VectorValue; 00186 return true; 00187 } 00188 00190 bool GetValue (csMatrix3& value) 00191 { 00192 if (accessor) accessor->PreGetValue (this); 00193 if (MatrixValuePtr) 00194 { 00195 value = *MatrixValuePtr; 00196 return true; 00197 } 00198 else 00199 { 00200 value = csMatrix3(); 00201 } 00202 return false; 00203 } 00204 00205 00207 bool SetValue (int value) 00208 { 00209 Type = INT; 00210 Int = value; 00211 float f = (float)value; 00212 VectorValue.Set (f, f, f, f); 00213 return true; 00214 } 00215 00217 bool SetValue (float value) 00218 { 00219 Type = FLOAT; 00220 Int = (int)value; 00221 VectorValue.Set (value, value, value, value); 00222 return true; 00223 } 00224 00226 bool SetValue (const csRGBpixel &value) 00227 { 00228 Type = COLOR; 00229 VectorValue.x = (float) value.red; 00230 VectorValue.y = (float) value.green; 00231 VectorValue.z = (float) value.blue; 00232 VectorValue.w = (float) value.alpha; 00233 return true; 00234 } 00235 00237 bool SetValue (iTextureHandle* value) 00238 { 00239 Type = TEXTURE; 00240 TextureHandValue = value; 00241 return true; 00242 } 00243 00245 bool SetValue (iTextureWrapper* value) 00246 { 00247 Type = TEXTURE; 00248 TextureWrapValue = value; 00249 return true; 00250 } 00251 00253 bool SetValue (iRenderBuffer* value) 00254 { 00255 Type = RENDERBUFFER; 00256 RenderBuffer = value; 00257 return true; 00258 } 00259 00261 bool SetValue (const csVector2 &value) 00262 { 00263 Type = VECTOR2; 00264 VectorValue.Set (value.x, value.y, 0.0f, 1.0f); 00265 Int = (int)value.x; 00266 return true; 00267 } 00268 00270 bool SetValue (const csVector3 &value) 00271 { 00272 Type = VECTOR3; 00273 VectorValue.Set (value.x, value.y, value.z, 1.0f); 00274 Int = (int)value.x; 00275 return true; 00276 } 00277 00279 bool SetValue (const csVector4 &value) 00280 { 00281 Type = VECTOR4; 00282 VectorValue.Set (value.x, value.y, value.z, value.w); 00283 Int = (int)value.x; 00284 return true; 00285 } 00286 00288 bool SetValue (const csMatrix3 &value) 00289 { 00290 Type = MATRIX; 00291 if (MatrixValuePtr) 00292 { 00293 *MatrixValuePtr = value; 00294 } 00295 else 00296 { 00297 MatrixValuePtr = new csMatrix3 (value); 00298 } 00299 return true; 00300 } 00301 }; 00302 00303 #endif
Generated for Crystal Space by doxygen 1.2.18