00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __CS_MATERIAL_H__
00021 #define __CS_MATERIAL_H__
00022
00023 #include "csgfx/rgbpixel.h"
00024 #include "csutil/csobject.h"
00025 #include "csutil/nobjvec.h"
00026 #include "ivideo/material.h"
00027 #include "iengine/material.h"
00028 #include "ivideo/effects/efdef.h"
00029
00030 #include "csengine/engine.h"
00031 #include "csutil/symtable.h"
00032
00033 struct iTextureWrapper;
00034 struct iTextureManager;
00035
00036 SCF_VERSION (csMaterialWrapper, 0, 0, 1);
00037
00042 template <class T>
00043 class csRefHash : csHashMap
00044 {
00045 public:
00046 csRefHash ()
00047 { }
00048
00049 ~csRefHash ()
00050 {
00051 csGlobalHashIterator it (this);
00052
00053 while (it.HasNext ())
00054 {
00055 T* obj = (T*)it.Next ();
00056 if (obj != 0) obj->DecRef ();
00057 }
00058 }
00059
00060 void Put (csHashKey key, T* object)
00061 {
00062 T* oldobj = (T*)csHashMap::Get (key);
00063 if (oldobj != 0) oldobj->DecRef ();
00064 Delete (key, (csHashObject)oldobj);
00065 if (object != 0) object->IncRef ();
00066 csHashMap::Put (key, (csHashObject)object);
00067 }
00068
00069 T* Get (csHashKey key) const
00070 {
00071 return (T*)csHashMap::Get (key);
00072 }
00073 };
00074
00078 class csMaterial : public iMaterial
00079 {
00080 private:
00081 friend class csEngine;
00082
00084 csRGBcolor flat_color;
00085 #ifndef CS_USE_NEW_RENDERER
00086
00087 csRef<iTextureWrapper> texture;
00089 int num_texture_layers;
00091 csTextureLayer texture_layers[4];
00093 csRef<iTextureWrapper> texture_layer_wrappers[4];
00094
00096 float diffuse;
00098 float ambient;
00100 float reflection;
00101
00103 iEffectDefinition* effect;
00104 #endif
00105
00107 csHashMap* shaders;
00108 csSymbolTable symtab;
00109 csEngine* engine;
00110
00111 #ifdef CS_USE_NEW_RENDERER
00112 csShaderVariable* GetVar (csStringID name, bool create = false);
00113 #endif
00114
00115 static csStringID nameDiffuseParam;
00116 static csStringID nameAmbientParam;
00117 static csStringID nameReflectParam;
00118 static csStringID nameFlatColorParam;
00119 static csStringID nameDiffuseTexture;
00120
00121 static csStringID nameTextureLayer1;
00122 static csStringID nameTextureLayer2;
00123 static csStringID nameTextureLayer3;
00124 static csStringID nameTextureLayer4;
00125
00126 public:
00130 csMaterial (csEngine* engine);
00134 csMaterial (csEngine* engine,
00135 iTextureWrapper *txt);
00136
00140 virtual ~csMaterial ();
00141
00143 csRGBcolor& GetFlatColor ();
00144
00146 float GetDiffuse ();
00148 void SetDiffuse (float val);
00149
00151 float GetAmbient ();
00153 void SetAmbient (float val);
00154
00156 float GetReflection ();
00158 void SetReflection (float val);
00159
00160 #ifdef CS_USE_NEW_RENDERER
00161
00162 iTextureWrapper* GetTextureWrapper ()
00163 {
00164 return GetTextureWrapper (nameDiffuseTexture);
00165 }
00166 #else
00167
00168 iTextureWrapper* GetTextureWrapper () const { return texture; }
00169 #endif
00170
00171 void SetTextureWrapper (iTextureWrapper* tex);
00172
00173 #ifndef CS_USE_NEW_RENDERER
00174
00175 void AddTextureLayer (iTextureWrapper* txtwrap, uint mode,
00176 float uscale, float vscale, float ushift, float vshift);
00177 #else
00178
00179 void SetTextureWrapper (csStringID name, iTextureWrapper* tex);
00180 #endif
00181
00182 iTextureWrapper* GetTextureWrapper (csStringID name);
00183
00184
00185
00187 virtual void SetShader (csStringID type, iShaderWrapper* shader);
00189 virtual iShaderWrapper* GetShader (csStringID type);
00190
00191 virtual void AddChild (iShaderBranch *c)
00192 {
00193 csRef<iShaderWrapper> w = SCF_QUERY_INTERFACE (c, iShaderWrapper);
00194 if (w) w->SelectMaterial (this);
00195 symtab.AddChild (c->GetSymbolTable ());
00196 }
00197 virtual void AddVariable (csShaderVariable *v)
00198 {
00199 symtab.SetSymbol (v->GetName (), v);
00200 }
00201 virtual csShaderVariable* GetVariable (csStringID name)
00202 {
00203 return (csShaderVariable *) symtab.GetSymbol (name);
00204 }
00205 virtual csSymbolTable* GetSymbolTable () { return & symtab; }
00206 virtual csSymbolTable* GetSymbolTable (int i) { return & symtab; }
00207 virtual void SelectSymbolTable (int i) {}
00208
00209 #ifndef CS_USE_NEW_RENDERER
00210
00211 virtual void SetEffect (iEffectDefinition *ed);
00213 virtual iEffectDefinition *GetEffect ();
00214 #endif
00215
00216 virtual iTextureHandle* GetTexture ();
00220 virtual iTextureHandle *GetTexture (csStringID name);
00221 #ifndef CS_USE_NEW_RENDERER
00222
00223 virtual int GetTextureLayerCount ();
00225 virtual csTextureLayer* GetTextureLayer (int idx);
00226 #endif
00227
00228 virtual void GetFlatColor (csRGBpixel &oColor, bool useTextureMean = true);
00230 virtual void SetFlatColor (const csRGBcolor& col);
00232 virtual void GetReflection (float &oDiffuse, float &oAmbient,
00233 float &oReflection);
00235 virtual void SetReflection (float oDiffuse, float oAmbient,
00236 float oReflection);
00237
00241 void Visit ();
00242 bool IsVisitRequired () const;
00243
00244 SCF_DECLARE_IBASE;
00245
00247 struct MaterialEngine : public iMaterialEngine
00248 {
00249 SCF_DECLARE_EMBEDDED_IBASE (csMaterial);
00250 virtual iTextureWrapper *GetTextureWrapper ()
00251 {
00252 return scfParent->GetTextureWrapper ();
00253 }
00254 virtual iTextureWrapper* GetTextureWrapper (csStringID name)
00255 {
00256 return scfParent->GetTextureWrapper (name);
00257 }
00258 virtual void Visit ()
00259 {
00260 scfParent->Visit ();
00261 }
00262 virtual bool IsVisitRequired () const
00263 {
00264 return scfParent->IsVisitRequired ();
00265 }
00266 } scfiMaterialEngine;
00267 friend struct MaterialEngine;
00268 };
00269
00274 class csMaterialWrapper : public csObject
00275 {
00276 private:
00278 csRef<iMaterial> material;
00280 csRef<iMaterialEngine> matEngine;
00282 csRef<iMaterialHandle> handle;
00283
00284 private:
00286 virtual ~csMaterialWrapper ();
00287
00288 public:
00290 csMaterialWrapper (iMaterial* Image);
00292 csMaterialWrapper (iMaterialHandle *ith);
00294 csMaterialWrapper (csMaterialWrapper &);
00295
00300 void SetMaterialHandle (iMaterialHandle *mat);
00302 iMaterialHandle* GetMaterialHandle () { return handle; }
00303
00308 void SetMaterial (iMaterial* material);
00310 iMaterial* GetMaterial () { return material; }
00311
00313 void Register (iTextureManager *txtmng);
00314
00320 void Visit ();
00321 bool IsVisitRequired () const;
00322
00323 SCF_DECLARE_IBASE_EXT (csObject);
00324
00325
00326 struct MaterialWrapper : public iMaterialWrapper
00327 {
00328 SCF_DECLARE_EMBEDDED_IBASE (csMaterialWrapper);
00329 virtual iObject *QueryObject()
00330 { return scfParent; }
00331 virtual iMaterialWrapper *Clone () const
00332 { return &(new csMaterialWrapper (*scfParent))->scfiMaterialWrapper; }
00333 virtual void SetMaterialHandle (iMaterialHandle *mat)
00334 { scfParent->SetMaterialHandle (mat); }
00335 virtual iMaterialHandle* GetMaterialHandle ()
00336 { return scfParent->GetMaterialHandle (); }
00337 virtual void SetMaterial (iMaterial* material)
00338 { scfParent->SetMaterial (material); }
00339 virtual iMaterial* GetMaterial ()
00340 { return scfParent->GetMaterial (); }
00341 virtual void Register (iTextureManager *txtmng)
00342 { scfParent->Register (txtmng); }
00343 virtual void Visit ()
00344 { scfParent->Visit (); }
00345 virtual bool IsVisitRequired () const
00346 {
00347 return scfParent->IsVisitRequired ();
00348 }
00349 } scfiMaterialWrapper;
00350 friend struct MaterialWrapper;
00351 };
00352
00356 class csMaterialList : public csRefArrayObject<iMaterialWrapper>
00357 {
00358 public:
00360 csMaterialList ();
00361 virtual ~csMaterialList () { }
00362
00364 iMaterialWrapper* NewMaterial (iMaterial* material);
00365
00370 iMaterialWrapper* NewMaterial (iMaterialHandle *ith);
00371
00372 SCF_DECLARE_IBASE;
00373
00374
00375 class MaterialList : public iMaterialList
00376 {
00377 public:
00378 SCF_DECLARE_EMBEDDED_IBASE (csMaterialList);
00379
00380 virtual iMaterialWrapper* NewMaterial (iMaterial* material);
00381 virtual iMaterialWrapper* NewMaterial (iMaterialHandle *ith);
00382 virtual int GetCount () const;
00383 virtual iMaterialWrapper *Get (int n) const;
00384 virtual int Add (iMaterialWrapper *obj);
00385 virtual bool Remove (iMaterialWrapper *obj);
00386 virtual bool Remove (int n);
00387 virtual void RemoveAll ();
00388 virtual int Find (iMaterialWrapper *obj) const;
00389 virtual iMaterialWrapper *FindByName (const char *Name) const;
00390 } scfiMaterialList;
00391 };
00392
00393 #endif // __CS_MATERIAL_H__