My Project
BMaxParser.h
1 #pragma once
2 #include "BlockEngine/BlockCoordinate.h"
3 #include "math/ShapeAABB.h"
4 #include "BMaxFrameNode.h"
5 #include "ParaXModel/ParaXModel.h"
6 #include "Rectangle.h"
7 
8 class TiXmlDocument;
9 class TiXmlElement;
10 class TiXmlNode;
11 typedef TiXmlDocument BMaxXMLDocument;
12 typedef TiXmlElement BMaxXMLElement;
13 typedef TiXmlNode BMaxXMLNode;
14 
15 namespace ParaEngine
16 {
17  class CParaXModel;
18  class BlockModel;
19  class Bone;
20  class BMaxParser;
21  struct ModelAnimation;
22  struct ModelVertex;
23  class BMaxAnimGenerator;
24 
27  class BMaxParser
28  {
29  public:
32  {
33  // transparent block (cob web) will transmit bone weight, but will not be rendered.
34  TransparentBlockId = 118,
35  // bones
36  BoneBlockId = 253,
37  // block model are extracted.
38  BlockModelBlockId = 254,
39  };
40 
41  BMaxParser(const char* pBuffer, int32 nSize, const char* filename = NULL, BMaxParser* pParent = NULL);
42  virtual ~BMaxParser(void);
43  CParaXModel* ParseParaXModel();
44  CParaXModel* ParseParaXModel(uint32 nMaxTriangleCount);
45 
46  const std::string& GetFilename() const;
47  void SetFilename(const std::string& val);
48  protected:
50  bool IsFileNameRecursiveLoaded(const std::string& filename);
51  void AddAnimation(const ModelAnimation& anim);
52  int GetAnimationsCount();
53  BMaxParser* GetParent() const { return m_pParent; }
54  void SetParent(BMaxParser* val) { m_pParent = val; }
55 
56  void SetAutoScale(bool value);
57  bool IsAutoScale();
58  const Vector3& GetCenterPos() const;
59 
60  void MergeCoplanerBlockFace();
61  bool IsCoplaneNode(BMaxNode* node1, BMaxNode* node2, int nFaceIndex);
62  void FindCoplanerFace(BMaxNode* node, uint32 nFaceIndex);
63  void FindNeighbourFace(Rectangle *rectangle, uint32 i, uint32 nFaceIndex);
64 
65  void CalculateLod(uint32 nMaxTriangleCount);
66  void GetLodTable(uint32 faceCount, vector<uint32>&lodTable);
67  void PerformLod();
68  void CalculateAABB(vector<BMaxNodePtr>&nodes);
69  void CalculateLodNode(map<int64, BMaxNodePtr> &nodeMap, int x, int y, int z);
70 
71  inline uint64 GetNodeIndex(uint16 x, uint16 y, uint16 z)
72  {
73  return (uint64)x + ((uint64)z << 16) + ((uint64)y << 32);
74  }
75  /* same as GetNode except that it checks for boundary condition. */
76  BMaxNode* GetBMaxNode(int x, int y, int z);
77 
78  inline BMaxNode* GetNode(uint16 x, uint16 y, uint16 z)
79  {
80  uint64 index = GetNodeIndex(x, y, z);
81  auto iter = m_nodes.find(index);
82  return (iter != m_nodes.end()) ? iter->second.get() : NULL;
83  }
84  inline BMaxNode* GetNodeByIndex(int64 index)
85  {
86  auto iter = m_nodes.find(index);
87  return (iter != m_nodes.end()) ? iter->second.get() : NULL;
88  }
90  int64 InsertNode(BMaxNodePtr& nodePtr);
91  void Load(const char* pBuffer, int32 nSize);
92  void ParseBlocks(BMaxXMLDocument& doc);
93  void ParseBlocks_Internal(const char* value);
94 
95  void ParseVisibleBlocks();
96  void ParseHead(BMaxXMLDocument& doc);
97  // parse bones
98  void ParseBlockFrames();
99 
100  void CreateDefaultAnimations();
101 
102  void AutoAddWalkAnimation(int nAnimID, int nStartTime, int nEndTime, float fMoveSpeed=4.f, bool bMoveForward = true);
103 
104  BMaxFrameNode* GetFrameNode(int16 x, int16 y, int16 z);
105 
106  // bone kins
107  void CalculateBoneWeights();
108 
109  void CalculateBoneWeightFromNeighbours(BMaxNode* node);
110 
111  void CalculateBoneSkin(BMaxFrameNode* pBoneNode);
115  void CalculateBoneWeightForBlock(BMaxFrameNode* pBoneNode, BMaxNode* node, bool bMustBeSameColor = true);
119  int FindRootBoneIndex();
120  // animations
121  void ParseBlockAnimations(BMaxXMLDocument& doc);
122  void ParseBlockAnimationSet(BMaxXMLElement* node);
123  void ParseBlockAnimation(BMaxXMLElement* node);
124  void ParseBlockAnimationKeys(uint16 x, uint16 y, uint16 z, BMaxXMLElement* node, int nIndex);
125  void ParseBlockAnimationKey(BMaxXMLElement* node,Bone* bone,const std::string propertyType);
126 
127  void ClearModel();
128  void FillParaXModelData(CParaXModel *pMesh);
129  void FillVerticesAndIndices();
130  //void ProcessBoneNodes();
131  //int CreateBoneIndex(uint16 x, uint16 y, uint16 z, int parentIndex);
132  int GetBoneIndex(uint16 x, uint16 y, uint16 z);
133  void ScaleModels();
134  float CalculateScale(float length);
135  int64 GetIndexFromXmlElement(BMaxXMLElement* node, const char* name, int& x, int& y, int& z);
136 
137  bool ReadValue(BMaxXMLElement* node, const char* name, int32_t& value);
138  bool ReadValue(BMaxXMLElement* node, const char* name, float& value);
139  bool ReadValue(BMaxXMLElement* node, const char* name, Vector3& value);
140  bool ReadValue(BMaxXMLElement* node, const char* name, std::string& value);
141  bool ReadValue(BMaxXMLElement* node, const char* name, Vector4& value);
142 
144  int GetNameAppearanceCount(const std::string& name, bool bAdd = true);
145 
147  CParaXModel* CreateGetRefModel(const std::string& sFilename);
148 
149  ModelGeoset* AddGeoset();
150  ModelRenderPass* AddRenderPass();
151 
152  /* create or get the default animation generator */
153  BMaxAnimGenerator* GetAnimGenerator();
154  protected:
155  BMaxParser* m_pParent;
156  std::string m_filename;
157  std::vector<BlockModel*> m_blockModels;
158  std::map<uint64, BMaxNodePtr> m_nodes;
159  std::map<std::string, ref_ptr<CParaXModel> > m_refModels;
160  /*std::vector<RectanglePtr>m_originRectangles;
161  std::map<uint16, vector<RectanglePtr>>m_lodRectangles;*/
162 
164  vector<BMaxFrameNodePtr> m_bones;
165  // how many times that a given name has appeared.
166  std::map<std::string, int> m_name_occurances;
167  bool m_bAutoScale;
168  bool m_bHasBoneBlock;
169  float m_fScale;
170  map<BlockModel*, BMaxNode*> m_blockModelsMapping;
171  vector<ModelAnimation> m_anims;
172  bool m_bHasAnimation;
173 
174  vector<ModelVertex> m_vertices;
175  vector<RectanglePtr>m_rectangles;
176  vector<uint16> m_indices;
177  std::vector<ModelGeoset> m_geosets;
178  std::vector<ModelRenderPass> m_renderPasses;
183  // AABB in model space
184  Vector3 m_minExtent;
185  Vector3 m_maxExtent;
186 
189  int m_nLodLevel;
190  BMaxAnimGenerator* m_pAnimGenerator;
191 
192  static const int MaxBoneLengthHorizontal;
193  static const int MaxBoneLengthVertical;
194  friend struct BMaxNode;
195  friend struct BMaxFrameNode;
196  friend struct BMaxBlockModelNode;
197  friend class BMaxAnimGenerator;
198  };
199 }
int m_nHelperBlockId
the block id used to extend AABB.
Definition: BMaxParser.h:188
CShapeBox m_blockAABB
aabb in block world coordinate
Definition: BMaxParser.h:180
4-dimensional homogeneous vector.
Definition: ParaVector4.h:10
Vector3 m_centerPos
the center point in block world coordinate
Definition: BMaxParser.h:182
Definition: Rectangle.h:7
different physics engine has different winding order.
Definition: EventBinding.h:32
BMax Animation generator.
Definition: BMaxAnimGenerator.h:78
a single animated bone, it contains both the bone instance data and all animation data of the bone...
Definition: ParaXBone.h:15
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
block model node in bmax
Definition: BMaxBlockModelNode.h:9
One material + render operation.
Definition: modelheaders.h:247
base class for a block in bmax model
Definition: BMaxNode.h:14
bool IsFileNameRecursiveLoaded(const std::string &filename)
check if the given filename belongs to one of its parent&#39;s filename
Definition: BMaxParser.cpp:1477
vector< BMaxFrameNodePtr > m_bones
array of bone nodes, array index is the bone index.
Definition: BMaxParser.h:164
int FindRootBoneIndex()
find the first root bone index.
Definition: BMaxParser.cpp:1390
CParaXModel * CreateGetRefModel(const std::string &sFilename)
filename is relative to current world directory.
Definition: BMaxParser.cpp:1543
Block Max file format parser.
Definition: BMaxParser.h:27
BlockIDNum
block id
Definition: BMaxParser.h:31
Definition: enum_maker.hpp:46
int64 InsertNode(BMaxNodePtr &nodePtr)
return node index
Definition: BMaxParser.cpp:329
void CalculateBoneWeightForBlock(BMaxFrameNode *pBoneNode, BMaxNode *node, bool bMustBeSameColor=true)
Definition: BMaxParser.cpp:1345
int GetNameAppearanceCount(const std::string &name, bool bAdd=true)
how many times that a given name has appeared.
Definition: BMaxParser.cpp:1160
ParaX model is the model file for character animation, etc in the game world.
Definition: ParaXModel.h:30
Definition: ModelRenderPass.h:9
bone node in bmax
Definition: BMaxFrameNode.h:9
Definition: modelheaders.h:165
a min max box.
Definition: ShapeAABB.h:190