OgreImporterUtils.h 6.8 KB

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