Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Serialize and Unserialize Wavefront OBJ <-> DME Data
  4. //
  5. //=============================================================================
  6. #ifndef DMOBJSERIALIZER_H
  7. #define DMOBJSERIALIZER_H
  8. #if defined( _WIN32 )
  9. #pragma once
  10. #endif
  11. #include "datamodel/idatamodel.h"
  12. #include "tier1/utlbuffer.h"
  13. #include "tier1/utlstring.h"
  14. #include "tier1/utlvector.h"
  15. #include "tier1/UtlStringMap.h"
  16. class CDmeMesh;
  17. class CDmeDag;
  18. class CDmeVertexDeltaData;
  19. class CDmeCombinationOperator;
  20. //-----------------------------------------------------------------------------
  21. // Serialization class for OBJ files
  22. //-----------------------------------------------------------------------------
  23. class CDmObjSerializer : public IDmSerializer
  24. {
  25. public:
  26. // Inherited from IDMSerializer
  27. virtual const char *GetName() const { return "obj"; }
  28. virtual const char *GetDescription() const { return "Wavefront OBJ"; }
  29. virtual bool IsBinaryFormat() const { return false; }
  30. virtual bool StoresVersionInFile() const { return false; }
  31. virtual int GetCurrentVersion() const { return 0; } // doesn't store a version
  32. virtual bool Serialize( CUtlBuffer &buf, CDmElement *pRoot );
  33. virtual bool Unserialize( CUtlBuffer &buf, const char *pEncodingName, int nEncodingVersion,
  34. const char *pSourceFormatName, int nSourceFormatVersion,
  35. DmFileId_t fileid, DmConflictResolution_t idConflictResolution, CDmElement **ppRoot );
  36. virtual const char *GetImportedFormat() const { return NULL; }
  37. virtual int GetImportedVersion() const { return 1; }
  38. CDmElement *ReadOBJ( const char *pFilename, CDmeMesh **ppCreatedMesh = NULL, bool bLoadAllDeltas = true, bool bAbsolute = true );
  39. bool WriteOBJ( const char *pFilename, CDmElement *pRoot, bool bWriteDeltas, const char *pDeltaName = NULL, bool absolute = true );
  40. void MeshToObj( CUtlBuffer &b, const matrix3x4_t &parentWorldMatrix, CDmeMesh *pMesh, const char *pDeltaName = NULL, bool absolute = true );
  41. CDmeVertexDeltaData *GetDelta( const char *pDeltaName, bool bAbsolute );
  42. private:
  43. CDmElement *ReadOBJ(
  44. CUtlBuffer &buf,
  45. DmFileId_t dmFileId,
  46. const char *pName,
  47. const char *pFilename = NULL,
  48. CDmeMesh *pBaseMesh = NULL,
  49. CDmeMesh **ppCreatedMesh = NULL,
  50. bool bAbsolute = true );
  51. static int OutputVectors( CUtlBuffer &b, const char *pPrefix, const CUtlVector< Vector > &vData, const matrix3x4_t &matrix );
  52. static int OutputVectors( CUtlBuffer &b, const char *pPrefix, const CUtlVector< Vector2D > &vData );
  53. static void DeltaToObj( CUtlBuffer &b, const matrix3x4_t &parentWorldMatrix, CDmeMesh *pMesh, const char *pDeltaName = NULL );
  54. void ParseMtlLib( CUtlBuffer &buf );
  55. const char *FindMtlEntry( const char *pTgaName );
  56. static bool ParseVertex( CUtlBuffer& bufParse, characterset_t &breakSet, int &v, int &t, int &n );
  57. static const char *SkipSpace( const char *pBuf );
  58. void DagToObj( CUtlBuffer &b, const matrix3x4_t &parentWorldMatrix, CDmeDag *pDag, const char *pDeltaName = NULL, bool absolute = true );
  59. static void FindDeltaMeshes( CDmeDag *pDag, CUtlVector< CDmeMesh * > &meshes );
  60. bool LoadDependentDeltas( const char *pDeltaName );
  61. struct MtlInfo_t
  62. {
  63. CUtlString m_MtlName;
  64. CUtlString m_TgaName;
  65. };
  66. CUtlVector< MtlInfo_t > m_mtlLib;
  67. CUtlString m_objDirectory;
  68. struct DeltaInfo_t
  69. {
  70. DeltaInfo_t()
  71. : m_pComboOp( NULL )
  72. , m_pMesh( NULL )
  73. , m_pDeltaData( NULL )
  74. {}
  75. CUtlString m_filename;
  76. CDmeMesh *m_pMesh;
  77. CDmeCombinationOperator *m_pComboOp;
  78. CDmeVertexDeltaData *m_pDeltaData;
  79. };
  80. CUtlStringMap< DeltaInfo_t > m_deltas;
  81. int m_nPositionOffset;
  82. int m_nTextureOffset;
  83. int m_nNormalOffset;
  84. };
  85. #endif // DMOBJSERIALIZER_H