OgreImporterUtils.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // Copyright (c) 2008-2017 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. unsigned v0_;
  33. unsigned v1_;
  34. unsigned v2_;
  35. };
  36. struct ModelBone
  37. {
  38. String name_;
  39. unsigned parentIndex_;
  40. Vector3 bindPosition_;
  41. Quaternion bindRotation_;
  42. Vector3 bindScale_;
  43. Vector3 derivedPosition_;
  44. Quaternion derivedRotation_;
  45. Vector3 derivedScale_;
  46. Matrix3x4 worldTransform_;
  47. Matrix3x4 inverseWorldTransform_;
  48. unsigned char collisionMask_;
  49. float radius_;
  50. BoundingBox boundingBox_;
  51. };
  52. struct ModelAnimation
  53. {
  54. String name_;
  55. float length_;
  56. Vector<AnimationTrack> tracks_;
  57. };
  58. struct BoneWeightAssignment
  59. {
  60. unsigned char boneIndex_;
  61. float weight_;
  62. };
  63. bool CompareWeights(const BoneWeightAssignment& lhs, const BoneWeightAssignment& rhs)
  64. {
  65. return lhs.weight_ > rhs.weight_;
  66. }
  67. bool CompareKeyFrames(const AnimationKeyFrame& lhs, const AnimationKeyFrame& rhs)
  68. {
  69. return lhs.time_ < rhs.time_;
  70. }
  71. struct ModelVertex
  72. {
  73. ModelVertex() :
  74. hasBlendWeights_(false)
  75. {
  76. }
  77. Vector3 position_;
  78. Vector3 normal_;
  79. Color color_;
  80. Vector2 texCoord1_;
  81. Vector2 texCoord2_;
  82. Vector3 cubeTexCoord1_;
  83. Vector3 cubeTexCoord2_;
  84. Vector4 tangent_;
  85. float blendWeights_[4];
  86. unsigned char blendIndices_[4];
  87. bool hasBlendWeights_;
  88. unsigned useCount_;
  89. int cachePosition_;
  90. float score_;
  91. };
  92. struct ModelVertexBuffer
  93. {
  94. unsigned elementMask_;
  95. unsigned morphStart_;
  96. unsigned morphCount_;
  97. Vector<ModelVertex> vertices_;
  98. ModelVertexBuffer() :
  99. elementMask_(0),
  100. morphStart_(0),
  101. morphCount_(0)
  102. {
  103. }
  104. void WriteData(Serializer& dest)
  105. {
  106. dest.WriteUInt(vertices_.Size());
  107. PODVector<VertexElement> elements = VertexBuffer::GetElements(elementMask_);
  108. dest.WriteUInt(elements.Size());
  109. for (unsigned j = 0; j < elements.Size(); ++j)
  110. {
  111. unsigned elementDesc = ((unsigned)elements[j].type_) |
  112. (((unsigned)elements[j].semantic_) << 8) |
  113. (((unsigned)elements[j].index_) << 16);
  114. dest.WriteUInt(elementDesc);
  115. }
  116. dest.WriteUInt(morphStart_);
  117. dest.WriteUInt(morphCount_);
  118. for (unsigned i = 0; i < vertices_.Size(); ++i)
  119. {
  120. if (elementMask_ & MASK_POSITION)
  121. dest.WriteVector3(vertices_[i].position_);
  122. if (elementMask_ & MASK_NORMAL)
  123. dest.WriteVector3(vertices_[i].normal_);
  124. if (elementMask_ & MASK_COLOR)
  125. dest.WriteUInt(vertices_[i].color_.ToUInt());
  126. if (elementMask_ & MASK_TEXCOORD1)
  127. dest.WriteVector2(vertices_[i].texCoord1_);
  128. if (elementMask_ & MASK_TEXCOORD2)
  129. dest.WriteVector2(vertices_[i].texCoord2_);
  130. if (elementMask_ & MASK_CUBETEXCOORD1)
  131. dest.WriteVector3(vertices_[i].cubeTexCoord1_);
  132. if (elementMask_ & MASK_CUBETEXCOORD2)
  133. dest.WriteVector3(vertices_[i].cubeTexCoord2_);
  134. if (elementMask_ & MASK_TANGENT)
  135. dest.WriteVector4(vertices_[i].tangent_);
  136. if (elementMask_ & MASK_BLENDWEIGHTS)
  137. dest.Write(&vertices_[i].blendWeights_[0], 4 * sizeof(float));
  138. if (elementMask_ & MASK_BLENDINDICES)
  139. dest.Write(&vertices_[i].blendIndices_[0], 4 * sizeof(unsigned char));
  140. }
  141. }
  142. };
  143. struct ModelMorphBuffer
  144. {
  145. unsigned vertexBuffer_;
  146. unsigned elementMask_;
  147. Vector<Pair<unsigned, ModelVertex> > vertices_;
  148. };
  149. struct ModelMorph
  150. {
  151. String name_;
  152. Vector<ModelMorphBuffer> buffers_;
  153. void WriteData(Serializer& dest)
  154. {
  155. dest.WriteString(name_);
  156. dest.WriteUInt(buffers_.Size());
  157. for (unsigned i = 0; i < buffers_.Size(); ++i)
  158. {
  159. dest.WriteUInt(buffers_[i].vertexBuffer_);
  160. dest.WriteUInt(buffers_[i].elementMask_);
  161. dest.WriteUInt(buffers_[i].vertices_.Size());
  162. unsigned elementMask = buffers_[i].elementMask_;
  163. for (Vector<Pair<unsigned, ModelVertex> >::Iterator j = buffers_[i].vertices_.Begin();
  164. j != buffers_[i].vertices_.End(); ++j)
  165. {
  166. dest.WriteUInt(j->first_);
  167. if (elementMask & MASK_POSITION)
  168. dest.WriteVector3(j->second_.position_);
  169. if (elementMask & MASK_NORMAL)
  170. dest.WriteVector3(j->second_.normal_);
  171. if (elementMask & MASK_TANGENT)
  172. dest.WriteVector3(Vector3(j->second_.tangent_.x_, j->second_.tangent_.y_, j->second_.tangent_.z_));
  173. }
  174. }
  175. }
  176. };
  177. struct ModelIndexBuffer
  178. {
  179. unsigned indexSize_;
  180. PODVector<unsigned> indices_;
  181. ModelIndexBuffer() :
  182. indexSize_(sizeof(unsigned short))
  183. {
  184. }
  185. void WriteData(Serializer& dest)
  186. {
  187. dest.WriteUInt(indices_.Size());
  188. dest.WriteUInt(indexSize_);
  189. for (unsigned i = 0; i < indices_.Size(); ++i)
  190. {
  191. if (indexSize_ == sizeof(unsigned short))
  192. dest.WriteUShort(indices_[i]);
  193. else
  194. dest.WriteUInt(indices_[i]);
  195. }
  196. }
  197. };
  198. struct ModelSubGeometryLodLevel
  199. {
  200. float distance_;
  201. PrimitiveType primitiveType_;
  202. unsigned vertexBuffer_;
  203. unsigned indexBuffer_;
  204. unsigned indexStart_;
  205. unsigned indexCount_;
  206. HashMap<unsigned, PODVector<BoneWeightAssignment> > boneWeights_;
  207. PODVector<unsigned> boneMapping_;
  208. ModelSubGeometryLodLevel() :
  209. distance_(0.0f),
  210. primitiveType_(TRIANGLE_LIST),
  211. indexBuffer_(0),
  212. indexStart_(0),
  213. indexCount_(0)
  214. {
  215. }
  216. };