Alphabetical Class Index  Class Hierarchy   File Members   Compound Members   File List  

HMaterialLibrary.h
Go to the documentation of this file.
00001 //
00002 // Copyright (c) 2010 by Tech Soft 3D, LLC.
00003 // The information contained herein is confidential and proprietary to
00004 // Tech Soft 3D, LLC., and considered a trade secret as defined under
00005 // civil and criminal statutes.  Tech Soft 3D shall pursue its civil
00006 // and criminal remedies in the event of unauthorized use or misappropriation
00007 // of its trade secrets.  Use of this information by anyone other than
00008 // authorized employees of Tech Soft 3D, LLC. is granted only under a
00009 // written non-disclosure agreement, expressly prescribing the scope and
00010 // manner of such use.
00011 //
00012 // $Id: ed062a1e951a32c600733d58cdea323a16258224 $
00013 //
00017 #ifndef _HMATERIALLIBRARY_H
00018 #define _HMATERIALLIBRARY_H
00019 
00020 #ifdef H_PACK_8
00021 #pragma pack(push)
00022 #pragma pack(8)
00023 #endif
00024 
00025 #include "HTools.h"
00026 #include "BStream.h"
00027 
00028 
00029 class HTK_One_Material;
00030 
00034 class MVO_API HMaterialLibrary {
00035 
00036 public:
00037     HMaterialLibrary();
00038     virtual ~HMaterialLibrary();
00039 
00040     /*
00041      * basic usage
00042      */
00043 
00050     bool RegisterMaterials (const char *directory);
00054     bool RegisterMaterials (const wchar_t *directory);
00060     bool ApplyStyles();
00063     const char *GetContainerSegment () const;
00076     const char *GetMaterialList(char *buffer = 0, int buffer_length = -1);
00080     bool IsValidMaterial (const char *name) const;
00085     bool ApplyMaterial (const char *name);
00092     bool ReloadMaterial (const char *name);  
00093 
00094     /*
00095      * for run-time tweaks
00096      */
00102     bool CloneMaterial (const char *old_material_name, char *new_material_name);
00107     bool RenameMaterial (const char *old_material_name, const char *new_material_name);
00112     bool GetTweakablesCount (const char *material_name, int *count);
00119     bool GetTweakableByIndex (const char *material_name, int index, char *tweakable_name, char *type);
00126     bool GetTweakableByName (const char *material_name, char const *tweakable_name, int *index, char *type);
00130     int CountFromType (const char *type);
00136     bool GetTweakableDefaults (const char *material_name, int index, float *vals);
00142     bool Tweak (const char *material_name, int index, const float *vals);
00151     bool Tweak (const char *material_name, int index, float val0, float val1=0, float val2=0, float val3=0 );
00152 
00153     /*
00154      * environment texture set up
00155      */
00157     const char *GetEnvironmentList(char *buffer = 0, int buffer_length = -1);
00161     bool IsValidEnvironment (const char *name) const;
00166     bool SetEnvironment (const char *name);
00167 
00172     bool EnsureMaterialInitialized (const char *name);
00177     bool EnsureEnvironmentInitialized (const char *name);
00178 
00179     bool FindReferencedMaterials (HC_KEY segment_tree, char *list, int list_size);
00180     bool InitWrite (const char *list, int *size);
00181     bool InitRead (int version);
00182     TK_Status ParseBuffer (char const *buffer, int size);
00183     const char *GetCurrentEnvironment(char *buffer = 0, int buffer_length = -1);
00184 
00189     bool Compile (const char *name);
00190 
00193     enum Read_Flags {
00194         Read_Flags_Default          = 0x0000, 
00195         Prefer_Embedded             = 0x0001, 
00196         Prefer_Highest_Resolution   = 0x0002, 
00197     };
00198     void SetReadFlags (Read_Flags flags) { m_ReadFlags = flags; }; 
00199     Read_Flags GetReadFlags () { return m_ReadFlags; }; 
00200 
00203     enum Write_Flags {
00204         Write_Flags_Default         = 0x0000, 
00205         Embedded                    = 0x0001, 
00206         //Baked                     = 0x0002, //!< write plain attributes instead of materials so that the receiving side can view without any special handling (not yet implemented)
00207         //Downsample                = 0x0004, //!< embed material data, but choose a variant with low resolution (not yet implemented)
00208     };
00209     void SetWriteFlags (Write_Flags flags) { m_WriteFlags = flags; }; 
00210     Write_Flags GetWriteFlags () { return m_WriteFlags; }; 
00211 
00212 protected:
00213     const wchar_t *m_MaterialDirectory;     
00214     struct vhash_s *m_MaterialHash;         
00215     struct vhash_s *m_EnvironmentHash;      
00216     const char *m_MaterialList;             
00217     const char *m_EnvironmentList;          
00218     const char *m_CurrentEnvironment;       
00219     HC_KEY m_MaterialSegmentKey;            
00220     HC_KEY m_CurrentEnvironmentKey;         
00221     Read_Flags m_ReadFlags;                 
00222     Write_Flags m_WriteFlags;               
00223 
00230     bool Initialize (const char *name, bool environment = false);
00231 
00236     virtual void Error (const char *message, int severity = 1);
00242     bool GetTweakablesString (const char *material_name, char *string);
00250     bool GetShaderSource (const char *material_name, char *shader_source, char *options = 0);
00251 
00256     const char *FindValidMaterial (const char *name) const;
00257 
00261     bool Compile (const char *name, void alter *bufferlist);
00262 
00263 private:
00264     struct vlist_s *m_SerializationBufferList;
00265     BStreamFileToolkit *m_tk;
00266 
00267     enum ErrorCode {
00268         HMatOK = 1,
00269         HMatParseError = -1,
00270         HMatFileError  = -2,
00271     };
00272 
00273     void Init();
00274     void RecordHSFMaterial (const wchar_t *path, const wchar_t *filename);
00275     void FindHSFMaterials (const wchar_t *path);
00276     bool HasIndexFile (const wchar_t *path, const wchar_t *subpath, const wchar_t *subdir, const wchar_t *index_file);
00277     void FindSubdirectories (const wchar_t *path, const wchar_t *subpath);
00278     bool GenerateSerializationBufferList (const char *materials);
00279     const char *HashToString (struct vhash_s *hash, char *buffer = 0, int buffer_size = -1) const;
00280     ErrorCode slurp_dat_file(                  
00281             const wchar_t *index_file, 
00282             const wchar_t *subdir, 
00283             const wchar_t *path, 
00284             void *datlist);
00285     friend class HTK_One_Material;
00286     friend class HTK_Material_Composite;
00287 };
00288 
00289 
00290 
00291 
00292 class MVO_API HTK_Material_Composite : public TK_Material
00293 {
00294 public:
00295     HTK_Material_Composite (HMaterialLibrary *hmat, HC_KEY key) : TK_Material() {
00296         H_ASSERT (hmat != NULL);
00297         H_ASSERT (key != -1);
00298         m_Root = key;
00299         m_HMat = hmat;
00300     }
00301 
00302     HTK_Material_Composite (HMaterialLibrary *hmat) : TK_Material() {
00303         H_ASSERT (hmat != NULL);
00304         m_Root = -1;
00305         m_HMat = hmat;
00306     }
00307 
00308     TK_Status Interpret(BStreamFileToolkit &tk, ID_Key key, int variant);
00309     TK_Status Execute(BStreamFileToolkit & tk);
00310 
00311 protected:
00312     HC_KEY m_Root;
00313     HMaterialLibrary *m_HMat;
00314 };
00315 
00316 
00317 class MVO_API HTK_One_Material : public BBaseOpcodeHandler
00318 {
00319 public:
00320     HTK_One_Material ();
00321     HTK_One_Material (HMaterialLibrary *hmat);
00322     ~HTK_One_Material ();
00323 
00324     TK_Status Interpret(BStreamFileToolkit &tk, HMaterialLibrary *hmat, const char *name);
00325     TK_Status Execute(BStreamFileToolkit & tk);
00326     TK_Status Read (BStreamFileToolkit & tk) alter;
00327     TK_Status Write (BStreamFileToolkit & tk) alter;
00328     void      Reset () alter;
00329 
00332     enum Subop_Flags {
00333         OMF_Environment = 0x0001,
00334         OMF_Instancee   = 0x0002,
00335         OMF_Tweaks      = 0x0004,
00336         OMF_Embedded    = 0x0008,
00337         OMF_Height_Field= 0x0010,
00338     };
00341     enum Tweak_Type {
00342         OMT_Float1 = 0x1,
00343         OMT_Float2 = 0x2,
00344         OMT_Float3 = 0x3,
00345         OMT_Float4 = 0x4,
00346     };
00347 
00348 protected:
00349     HMaterialLibrary *m_HMat;           //not locally owned
00350     void *m_MaterialDescriptor;         //not locally owned
00351     char *m_Name;                       
00352     int m_NameLength;
00353     char *m_Instancee;
00354     int m_InstanceeLength;
00355     int m_TweakCount;
00356     int *m_TweakNameLengths;    
00357     char **m_TweakNames;    
00358     char *m_TweakTypes;
00359     float **m_Tweaks;
00360     float m_HeightFieldScale;
00361     unsigned int m_subop;
00362     int m_substage;
00363     int m_substage2;
00364 
00365     //the rest of the variables are all used for embedding hsf data
00366 #define ONEMATBUFSIZE 512
00367     void *m_subtk;
00368     FILE *m_HSF;
00369     int m_EmbeddedLength;
00370     unsigned char m_EmbeddingBuffer[ONEMATBUFSIZE];
00371     int m_EmbeddingBufferUsed;
00372 
00373     void Init ();
00374 };
00375 
00376 
00377 
00378 
00379 
00380 #ifdef H_PACK_8
00381 #pragma pack(pop)
00382 #endif
00383 
00384 #endif