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