OgreImporterUtils.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include <Urho3D/Graphics/Animation.h>
  24. #include <Urho3D/Math/BoundingBox.h>
  25. #include <Urho3D/Graphics/Graphics.h>
  26. #include <Urho3D/Graphics/VertexBuffer.h>
  27. #include <Urho3D/IO/Serializer.h>
  28. #include <Urho3D/Math/Matrix3x4.h>
  29. using namespace Urho3D;
  30. struct Triangle
  31. {
  32. Triangle(unsigned v0, unsigned v1, unsigned v2) :
  33. v0_{v0},
  34. v1_{v1},
  35. v2_{v2}
  36. {
  37. }
  38. unsigned v0_;
  39. unsigned v1_;
  40. unsigned v2_;
  41. };
  42. struct ModelBone
  43. {
  44. String name_;
  45. unsigned parentIndex_;
  46. Vector3 bindPosition_;
  47. Quaternion bindRotation_;
  48. Vector3 bindScale_;
  49. Vector3 derivedPosition_;
  50. Quaternion derivedRotation_;
  51. Vector3 derivedScale_;
  52. Matrix3x4 worldTransform_;
  53. Matrix3x4 inverseWorldTransform_;
  54. unsigned char collisionMask_;
  55. float radius_;
  56. BoundingBox boundingBox_;
  57. };
  58. struct ModelAnimation
  59. {
  60. String name_;
  61. float length_;
  62. Vector<AnimationTrack> tracks_;
  63. };
  64. struct BoneWeightAssignment
  65. {
  66. BoneWeightAssignment(unsigned char boneIndex, float weight) :
  67. boneIndex_{boneIndex},
  68. weight_{weight}
  69. {
  70. }
  71. unsigned char boneIndex_;
  72. float weight_;
  73. };
  74. bool CompareWeights(const BoneWeightAssignment& lhs, const BoneWeightAssignment& rhs)
  75. {
  76. return lhs.weight_ > rhs.weight_;
  77. }
  78. bool CompareKeyFrames(const AnimationKeyFrame& lhs, const AnimationKeyFrame& rhs)
  79. {
  80. return lhs.time_ < rhs.time_;
  81. }
  82. struct ModelVertex
  83. {
  84. Vector3 position_;
  85. Vector3 normal_;
  86. Color color_;
  87. Vector2 texCoord1_;
  88. Vector2 texCoord2_;
  89. Vector3 cubeTexCoord1_;
  90. Vector3 cubeTexCoord2_;
  91. Vector4 tangent_;
  92. float blendWeights_[4]{};
  93. unsigned char blendIndices_[4]{};
  94. bool hasBlendWeights_{};
  95. unsigned useCount_{};
  96. int cachePosition_{};
  97. float score_{};
  98. };
  99. struct ModelVertexBuffer
  100. {
  101. VertexMaskFlags elementMask_;
  102. unsigned morphStart_;
  103. unsigned morphCount_;
  104. Vector<ModelVertex> vertices_;
  105. ModelVertexBuffer() :
  106. elementMask_(MASK_NONE),
  107. morphStart_(0),
  108. morphCount_(0)
  109. {
  110. }
  111. void WriteData(Serializer& dest)
  112. {
  113. dest.WriteUInt(vertices_.Size());
  114. PODVector<VertexElement> elements = VertexBuffer::GetElements(elementMask_);
  115. dest.WriteUInt(elements.Size());
  116. for (unsigned j = 0; j < elements.Size(); ++j)
  117. {
  118. unsigned elementDesc = ((unsigned)elements[j].type_) |
  119. (((unsigned)elements[j].semantic_) << 8u) |
  120. (((unsigned)elements[j].index_) << 16u);
  121. dest.WriteUInt(elementDesc);
  122. }
  123. dest.WriteUInt(morphStart_);
  124. dest.WriteUInt(morphCount_);
  125. for (unsigned i = 0; i < vertices_.Size(); ++i)
  126. {
  127. if (elementMask_ & MASK_POSITION)
  128. dest.WriteVector3(vertices_[i].position_);
  129. if (elementMask_ & MASK_NORMAL)
  130. dest.WriteVector3(vertices_[i].normal_);
  131. if (elementMask_ & MASK_COLOR)
  132. dest.WriteUInt(vertices_[i].color_.ToUInt());
  133. if (elementMask_ & MASK_TEXCOORD1)
  134. dest.WriteVector2(vertices_[i].texCoord1_);
  135. if (elementMask_ & MASK_TEXCOORD2)
  136. dest.WriteVector2(vertices_[i].texCoord2_);
  137. if (elementMask_ & MASK_CUBETEXCOORD1)
  138. dest.WriteVector3(vertices_[i].cubeTexCoord1_);
  139. if (elementMask_ & MASK_CUBETEXCOORD2)
  140. dest.WriteVector3(vertices_[i].cubeTexCoord2_);
  141. if (elementMask_ & MASK_TANGENT)
  142. dest.WriteVector4(vertices_[i].tangent_);
  143. if (elementMask_ & MASK_BLENDWEIGHTS)
  144. dest.Write(&vertices_[i].blendWeights_[0], 4 * sizeof(float));
  145. if (elementMask_ & MASK_BLENDINDICES)
  146. dest.Write(&vertices_[i].blendIndices_[0], 4 * sizeof(unsigned char));
  147. }
  148. }
  149. };
  150. struct ModelMorphBuffer
  151. {
  152. unsigned vertexBuffer_;
  153. unsigned elementMask_;
  154. Vector<Pair<unsigned, ModelVertex> > vertices_;
  155. };
  156. struct ModelMorph
  157. {
  158. String name_;
  159. Vector<ModelMorphBuffer> buffers_;
  160. void WriteData(Serializer& dest)
  161. {
  162. dest.WriteString(name_);
  163. dest.WriteUInt(buffers_.Size());
  164. for (unsigned i = 0; i < buffers_.Size(); ++i)
  165. {
  166. dest.WriteUInt(buffers_[i].vertexBuffer_);
  167. dest.WriteUInt(buffers_[i].elementMask_);
  168. dest.WriteUInt(buffers_[i].vertices_.Size());
  169. unsigned elementMask = buffers_[i].elementMask_;
  170. for (Vector<Pair<unsigned, ModelVertex> >::Iterator j = buffers_[i].vertices_.Begin();
  171. j != buffers_[i].vertices_.End(); ++j)
  172. {
  173. dest.WriteUInt(j->first_);
  174. if (elementMask & MASK_POSITION)
  175. dest.WriteVector3(j->second_.position_);
  176. if (elementMask & MASK_NORMAL)
  177. dest.WriteVector3(j->second_.normal_);
  178. if (elementMask & MASK_TANGENT)
  179. dest.WriteVector3(Vector3(j->second_.tangent_.x_, j->second_.tangent_.y_, j->second_.tangent_.z_));
  180. }
  181. }
  182. }
  183. };
  184. struct ModelIndexBuffer
  185. {
  186. unsigned indexSize_;
  187. PODVector<unsigned> indices_;
  188. ModelIndexBuffer() :
  189. indexSize_(sizeof(unsigned short))
  190. {
  191. }
  192. void WriteData(Serializer& dest)
  193. {
  194. dest.WriteUInt(indices_.Size());
  195. dest.WriteUInt(indexSize_);
  196. for (unsigned i = 0; i < indices_.Size(); ++i)
  197. {
  198. if (indexSize_ == sizeof(unsigned short))
  199. dest.WriteUShort(indices_[i]);
  200. else
  201. dest.WriteUInt(indices_[i]);
  202. }
  203. }
  204. };
  205. struct ModelSubGeometryLodLevel
  206. {
  207. float distance_{};
  208. PrimitiveType primitiveType_{TRIANGLE_LIST};
  209. unsigned vertexBuffer_{};
  210. unsigned indexBuffer_{};
  211. unsigned indexStart_{};
  212. unsigned indexCount_{};
  213. HashMap<unsigned, PODVector<BoneWeightAssignment> > boneWeights_;
  214. PODVector<unsigned> boneMapping_;
  215. };