Alphabetical Class Index  Class Hierarchy   File Members   Compound Members   File List  

HMaterialLibrary.h
Go to the documentation of this file.
1 // Copyright (c) 1998-2014 by 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);
249  bool GetShaderSource (const char *material_name, char *shader_source, char *options = 0);
250 
255  const char *FindValidMaterial (const char *name) const;
256 
260  bool Compile (const char *name, void *bufferlist);
261 
262 private:
263  struct vlist_s *m_SerializationBufferList;
264  BStreamFileToolkit *m_tk;
265 
266  enum ErrorCode {
267  HMatOK = 1,
268  HMatParseError = -1,
269  HMatFileError = -2,
270  };
271 
272  void Init();
273  void RecordHSFMaterial (const wchar_t *path, const wchar_t *filename);
274  void FindHSFMaterials (const wchar_t *path);
275  bool HasIndexFile (const wchar_t *path, const wchar_t *subpath, const wchar_t *subdir, const wchar_t *index_file);
276  void FindSubdirectories (const wchar_t *path, const wchar_t *subpath);
277  bool GenerateSerializationBufferList (const char *materials);
278  const char *HashToString (struct vhash_s *hash, char *buffer = 0, int buffer_size = -1) const;
279  ErrorCode slurp_dat_file(
280  const wchar_t *index_file,
281  const wchar_t *subdir,
282  const wchar_t *path,
283  void *datlist);
284  friend class HTK_One_Material;
285  friend class HTK_Material_Composite;
286 };
287 
288 
289 
290 
291 class MVO_API HTK_Material_Composite : public TK_Material
292 {
293 public:
294  HTK_Material_Composite (HMaterialLibrary *hmat, HC_KEY key) : TK_Material() {
295  H_ASSERT (hmat != NULL);
296  H_ASSERT (key != -1);
297  m_Root = key;
298  m_HMat = hmat;
299  }
300 
301  HTK_Material_Composite (HMaterialLibrary *hmat) : TK_Material() {
302  H_ASSERT (hmat != NULL);
303  m_Root = -1;
304  m_HMat = hmat;
305  }
306 
307  TK_Status Interpret(BStreamFileToolkit &tk, ID_Key key, int variant);
308  TK_Status Execute(BStreamFileToolkit & tk);
309 
310 protected:
311  HC_KEY m_Root;
312  HMaterialLibrary *m_HMat;
313 };
314 
315 
316 class MVO_API HTK_One_Material : public BBaseOpcodeHandler
317 {
318 public:
319  HTK_One_Material ();
321  ~HTK_One_Material ();
322 
323  TK_Status Interpret(BStreamFileToolkit &tk, HMaterialLibrary *hmat, const char *name);
324  TK_Status Execute(BStreamFileToolkit & tk);
325  TK_Status Read (BStreamFileToolkit & tk);
326  TK_Status Write (BStreamFileToolkit & tk);
327  void Reset ();
328 
331  enum Subop_Flags {
332  OMF_Environment = 0x0001,
333  OMF_Instancee = 0x0002,
334  OMF_Tweaks = 0x0004,
335  OMF_Embedded = 0x0008,
336  OMF_Height_Field= 0x0010,
337  };
340  enum Tweak_Type {
341  OMT_Float1 = 0x1,
342  OMT_Float2 = 0x2,
343  OMT_Float3 = 0x3,
344  OMT_Float4 = 0x4,
345  };
346 
347 protected:
348  HMaterialLibrary *m_HMat; //not locally owned
349  void *m_MaterialDescriptor; //not locally owned
350  char *m_Name;
351  int m_NameLength;
352  char *m_Instancee;
353  int m_InstanceeLength;
354  int m_TweakCount;
355  int *m_TweakNameLengths;
356  char **m_TweakNames;
357  char *m_TweakTypes;
358  float **m_Tweaks;
359  float m_HeightFieldScale;
360  unsigned int m_subop;
361  int m_substage;
362  int m_substage2;
363 
364  //the rest of the variables are all used for embedding hsf data
365 #define ONEMATBUFSIZE 512
366  void *m_subtk;
367  FILE *m_HSF;
368  int m_EmbeddedLength;
369  unsigned char m_EmbeddingBuffer[ONEMATBUFSIZE];
370  int m_EmbeddingBufferUsed;
371 
372  void Init ();
373 };
374 
375 
376 
377 
378 
379 #ifdef H_PACK_8
380 #pragma pack(pop)
381 #endif
382 
383 #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
#define HC_KEY
Read_Flags
see documentation on HMaterialLibrary::Read_Flags
Definition: HMaterialLibrary.h:191
const char * m_MaterialList
the material list
Definition: HMaterialLibrary.h:214
Write_Flags m_WriteFlags
see documentation on HMaterialLibrary::Write_Flags
Definition: HMaterialLibrary.h:220
bool IsValid() const
Definition: HMaterialLibrary.h:52
Tweak_Type
Definition: HMaterialLibrary.h:340
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:316
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:331
Read_Flags m_ReadFlags
see documentation on HMaterialLibrary::Read_Flags
Definition: HMaterialLibrary.h:219
Definition: HMaterialLibrary.h:291