Alphabetical Class Index  Class Hierarchy   File Members   Compound Members   File List  

HMaterialLibrary.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2010 by Tech Soft 3D, LLC.
3 // The information contained herein is confidential and proprietary to
4 // Tech Soft 3D, LLC., and considered a trade secret as defined under
5 // civil and criminal statutes. Tech Soft 3D shall pursue its civil
6 // and criminal remedies in the event of unauthorized use or misappropriation
7 // of its trade secrets. Use of this information by anyone other than
8 // authorized employees of Tech Soft 3D, LLC. is granted only under a
9 // written non-disclosure agreement, expressly prescribing the scope and
10 // manner of such use.
11 //
12 // $Id: ed062a1e951a32c600733d58cdea323a16258224 $
13 //
17 #ifndef _HMATERIALLIBRARY_H
18 #define _HMATERIALLIBRARY_H
19 
20 #ifdef H_PACK_8
21 #pragma pack(push)
22 #pragma pack(8)
23 #endif
24 
25 #include "HTools.h"
26 #include "BStream.h"
27 
28 
29 class HTK_One_Material;
30 
34 class MVO_API HMaterialLibrary {
35 
36 public:
38  virtual ~HMaterialLibrary();
39 
40  /*
41  * basic usage
42  */
43 
50  bool RegisterMaterials (const char *directory);
54  bool RegisterMaterials (const wchar_t *directory);
60  bool ApplyStyles();
63  const char *GetContainerSegment () const;
76  const char *GetMaterialList(char *buffer = 0, int buffer_length = -1);
80  bool IsValidMaterial (const char *name) const;
85  bool ApplyMaterial (const char *name);
92  bool ReloadMaterial (const char *name);
93 
94  /*
95  * for run-time tweaks
96  */
102  bool CloneMaterial (const char *old_material_name, char *new_material_name);
107  bool RenameMaterial (const char *old_material_name, const char *new_material_name);
112  bool GetTweakablesCount (const char *material_name, int *count);
119  bool GetTweakableByIndex (const char *material_name, int index, char *tweakable_name, char *type);
126  bool GetTweakableByName (const char *material_name, char const *tweakable_name, int *index, char *type);
130  int CountFromType (const char *type);
136  bool GetTweakableDefaults (const char *material_name, int index, float *vals);
142  bool Tweak (const char *material_name, int index, const float *vals);
151  bool Tweak (const char *material_name, int index, float val0, float val1=0, float val2=0, float val3=0 );
152 
153  /*
154  * environment texture set up
155  */
157  const char *GetEnvironmentList(char *buffer = 0, int buffer_length = -1);
161  bool IsValidEnvironment (const char *name) const;
166  bool SetEnvironment (const char *name);
167 
172  bool EnsureMaterialInitialized (const char *name);
177  bool EnsureEnvironmentInitialized (const char *name);
178 
179  bool FindReferencedMaterials (HC_KEY segment_tree, char *list, int list_size);
180  bool InitWrite (const char *list, int *size);
181  bool InitRead (int version);
182  TK_Status ParseBuffer (char const *buffer, int size);
183  const char *GetCurrentEnvironment(char *buffer = 0, int buffer_length = -1);
184 
189  bool Compile (const char *name);
190 
193  enum Read_Flags {
194  Read_Flags_Default = 0x0000,
195  Prefer_Embedded = 0x0001,
196  Prefer_Highest_Resolution = 0x0002,
197  };
198  void SetReadFlags (Read_Flags flags) { m_ReadFlags = flags; };
199  Read_Flags GetReadFlags () { return m_ReadFlags; };
200 
203  enum Write_Flags {
204  Write_Flags_Default = 0x0000,
205  Embedded = 0x0001,
206  //Baked = 0x0002, //!< write plain attributes instead of materials so that the receiving side can view without any special handling (not yet implemented)
207  //Downsample = 0x0004, //!< embed material data, but choose a variant with low resolution (not yet implemented)
208  };
209  void SetWriteFlags (Write_Flags flags) { m_WriteFlags = flags; };
210  Write_Flags GetWriteFlags () { return m_WriteFlags; };
211 
212 protected:
213  const wchar_t *m_MaterialDirectory;
214  struct vhash_s *m_MaterialHash;
215  struct vhash_s *m_EnvironmentHash;
216  const char *m_MaterialList;
217  const char *m_EnvironmentList;
218  const char *m_CurrentEnvironment;
223 
230  bool Initialize (const char *name, bool environment = false);
231 
236  virtual void Error (const char *message, int severity = 1);
242  bool GetTweakablesString (const char *material_name, char *string);
250  bool GetShaderSource (const char *material_name, char *shader_source, char *options = 0);
251 
256  const char *FindValidMaterial (const char *name) const;
257 
261  bool Compile (const char *name, void alter *bufferlist);
262 
263 private:
264  struct vlist_s *m_SerializationBufferList;
265  BStreamFileToolkit *m_tk;
266 
267  enum ErrorCode {
268  HMatOK = 1,
269  HMatParseError = -1,
270  HMatFileError = -2,
271  };
272 
273  void Init();
274  void RecordHSFMaterial (const wchar_t *path, const wchar_t *filename);
275  void FindHSFMaterials (const wchar_t *path);
276  bool HasIndexFile (const wchar_t *path, const wchar_t *subpath, const wchar_t *subdir, const wchar_t *index_file);
277  void FindSubdirectories (const wchar_t *path, const wchar_t *subpath);
278  bool GenerateSerializationBufferList (const char *materials);
279  const char *HashToString (struct vhash_s *hash, char *buffer = 0, int buffer_size = -1) const;
280  ErrorCode slurp_dat_file(
281  const wchar_t *index_file,
282  const wchar_t *subdir,
283  const wchar_t *path,
284  void *datlist);
285  friend class HTK_One_Material;
286  friend class HTK_Material_Composite;
287 };
288 
289 
290 
291 
292 class MVO_API HTK_Material_Composite : public TK_Material
293 {
294 public:
295  HTK_Material_Composite (HMaterialLibrary *hmat, HC_KEY key) : TK_Material() {
296  H_ASSERT (hmat != NULL);
297  H_ASSERT (key != -1);
298  m_Root = key;
299  m_HMat = hmat;
300  }
301 
302  HTK_Material_Composite (HMaterialLibrary *hmat) : TK_Material() {
303  H_ASSERT (hmat != NULL);
304  m_Root = -1;
305  m_HMat = hmat;
306  }
307 
308  TK_Status Interpret(BStreamFileToolkit &tk, ID_Key key, int variant);
309  TK_Status Execute(BStreamFileToolkit & tk);
310 
311 protected:
312  HC_KEY m_Root;
313  HMaterialLibrary *m_HMat;
314 };
315 
316 
317 class MVO_API HTK_One_Material : public BBaseOpcodeHandler
318 {
319 public:
320  HTK_One_Material ();
322  ~HTK_One_Material ();
323 
324  TK_Status Interpret(BStreamFileToolkit &tk, HMaterialLibrary *hmat, const char *name);
325  TK_Status Execute(BStreamFileToolkit & tk);
326  TK_Status Read (BStreamFileToolkit & tk) alter;
327  TK_Status Write (BStreamFileToolkit & tk) alter;
328  void Reset () alter;
329 
332  enum Subop_Flags {
333  OMF_Environment = 0x0001,
334  OMF_Instancee = 0x0002,
335  OMF_Tweaks = 0x0004,
336  OMF_Embedded = 0x0008,
337  OMF_Height_Field= 0x0010,
338  };
341  enum Tweak_Type {
342  OMT_Float1 = 0x1,
343  OMT_Float2 = 0x2,
344  OMT_Float3 = 0x3,
345  OMT_Float4 = 0x4,
346  };
347 
348 protected:
349  HMaterialLibrary *m_HMat; //not locally owned
350  void *m_MaterialDescriptor; //not locally owned
351  char *m_Name;
352  int m_NameLength;
353  char *m_Instancee;
354  int m_InstanceeLength;
355  int m_TweakCount;
356  int *m_TweakNameLengths;
357  char **m_TweakNames;
358  char *m_TweakTypes;
359  float **m_Tweaks;
360  float m_HeightFieldScale;
361  unsigned int m_subop;
362  int m_substage;
363  int m_substage2;
364 
365  //the rest of the variables are all used for embedding hsf data
366 #define ONEMATBUFSIZE 512
367  void *m_subtk;
368  FILE *m_HSF;
369  int m_EmbeddedLength;
370  unsigned char m_EmbeddingBuffer[ONEMATBUFSIZE];
371  int m_EmbeddingBufferUsed;
372 
373  void Init ();
374 };
375 
376 
377 
378 
379 
380 #ifdef H_PACK_8
381 #pragma pack(pop)
382 #endif
383 
384 #endif
HC_KEY m_CurrentEnvironmentKey
the currently active environment
Definition: HMaterialLibrary.h:220
Definition: HMaterialLibrary.h:34
const char * m_EnvironmentList
the environment list
Definition: HMaterialLibrary.h:217
#define HC_KEY
Read_Flags
see documentation on HMaterialLibrary::Read_Flags
Definition: HMaterialLibrary.h:193
const char * m_MaterialList
the material list
Definition: HMaterialLibrary.h:216
Write_Flags m_WriteFlags
see documentation on HMaterialLibrary::Write_Flags
Definition: HMaterialLibrary.h:222
Tweak_Type
Definition: HMaterialLibrary.h:341
struct vhash_s * m_MaterialHash
maps material names to the compiled material hsf file from which it came
Definition: HMaterialLibrary.h:214
struct vhash_s * m_EnvironmentHash
maps environment names to the compiled material hsf file from which it came
Definition: HMaterialLibrary.h:215
Read_Flags GetReadFlags()
see documentation on HMaterialLibrary::Read_Flags
Definition: HMaterialLibrary.h:199
HC_KEY m_MaterialSegmentKey
the segment containing the materials and environments
Definition: HMaterialLibrary.h:219
Definition: HMaterialLibrary.h:317
const char * m_CurrentEnvironment
the name of the environment most recently set with SetEnvironment
Definition: HMaterialLibrary.h:218
Write_Flags
Definition: HMaterialLibrary.h:203
Subop_Flags
Definition: HMaterialLibrary.h:332
Read_Flags m_ReadFlags
see documentation on HMaterialLibrary::Read_Flags
Definition: HMaterialLibrary.h:221
Definition: HMaterialLibrary.h:292