OgreStructs.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
  34. #include "OgreStructs.h"
  35. #include "TinyFormatter.h"
  36. namespace Assimp
  37. {
  38. namespace Ogre
  39. {
  40. // VertexElement
  41. VertexElement::VertexElement() :
  42. index(0),
  43. source(0),
  44. offset(0),
  45. type(VET_FLOAT1),
  46. semantic(VES_POSITION)
  47. {
  48. }
  49. size_t VertexElement::Size() const
  50. {
  51. return TypeSize(type);
  52. }
  53. size_t VertexElement::ComponentCount() const
  54. {
  55. return ComponentCount(type);
  56. }
  57. size_t VertexElement::ComponentCount(Type type)
  58. {
  59. switch(type)
  60. {
  61. case VET_COLOUR:
  62. case VET_COLOUR_ABGR:
  63. case VET_COLOUR_ARGB:
  64. case VET_FLOAT1:
  65. case VET_DOUBLE1:
  66. case VET_SHORT1:
  67. case VET_USHORT1:
  68. case VET_INT1:
  69. case VET_UINT1:
  70. return 1;
  71. case VET_FLOAT2:
  72. case VET_DOUBLE2:
  73. case VET_SHORT2:
  74. case VET_USHORT2:
  75. case VET_INT2:
  76. case VET_UINT2:
  77. return 2;
  78. case VET_FLOAT3:
  79. case VET_DOUBLE3:
  80. case VET_SHORT3:
  81. case VET_USHORT3:
  82. case VET_INT3:
  83. case VET_UINT3:
  84. return 3;
  85. case VET_FLOAT4:
  86. case VET_DOUBLE4:
  87. case VET_SHORT4:
  88. case VET_USHORT4:
  89. case VET_INT4:
  90. case VET_UINT4:
  91. case VET_UBYTE4:
  92. return 4;
  93. }
  94. return 0;
  95. }
  96. size_t VertexElement::TypeSize(Type type)
  97. {
  98. switch(type)
  99. {
  100. case VET_COLOUR:
  101. case VET_COLOUR_ABGR:
  102. case VET_COLOUR_ARGB:
  103. return sizeof(unsigned int);
  104. case VET_FLOAT1:
  105. return sizeof(float);
  106. case VET_FLOAT2:
  107. return sizeof(float)*2;
  108. case VET_FLOAT3:
  109. return sizeof(float)*3;
  110. case VET_FLOAT4:
  111. return sizeof(float)*4;
  112. case VET_DOUBLE1:
  113. return sizeof(double);
  114. case VET_DOUBLE2:
  115. return sizeof(double)*2;
  116. case VET_DOUBLE3:
  117. return sizeof(double)*3;
  118. case VET_DOUBLE4:
  119. return sizeof(double)*4;
  120. case VET_SHORT1:
  121. return sizeof(short);
  122. case VET_SHORT2:
  123. return sizeof(short)*2;
  124. case VET_SHORT3:
  125. return sizeof(short)*3;
  126. case VET_SHORT4:
  127. return sizeof(short)*4;
  128. case VET_USHORT1:
  129. return sizeof(unsigned short);
  130. case VET_USHORT2:
  131. return sizeof(unsigned short)*2;
  132. case VET_USHORT3:
  133. return sizeof(unsigned short)*3;
  134. case VET_USHORT4:
  135. return sizeof(unsigned short)*4;
  136. case VET_INT1:
  137. return sizeof(int);
  138. case VET_INT2:
  139. return sizeof(int)*2;
  140. case VET_INT3:
  141. return sizeof(int)*3;
  142. case VET_INT4:
  143. return sizeof(int)*4;
  144. case VET_UINT1:
  145. return sizeof(unsigned int);
  146. case VET_UINT2:
  147. return sizeof(unsigned int)*2;
  148. case VET_UINT3:
  149. return sizeof(unsigned int)*3;
  150. case VET_UINT4:
  151. return sizeof(unsigned int)*4;
  152. case VET_UBYTE4:
  153. return sizeof(unsigned char)*4;
  154. }
  155. return 0;
  156. }
  157. std::string VertexElement::TypeToString()
  158. {
  159. return TypeToString(type);
  160. }
  161. std::string VertexElement::TypeToString(Type type)
  162. {
  163. switch(type)
  164. {
  165. case VET_COLOUR: return "COLOUR";
  166. case VET_COLOUR_ABGR: return "COLOUR_ABGR";
  167. case VET_COLOUR_ARGB: return "COLOUR_ARGB";
  168. case VET_FLOAT1: return "FLOAT1";
  169. case VET_FLOAT2: return "FLOAT2";
  170. case VET_FLOAT3: return "FLOAT3";
  171. case VET_FLOAT4: return "FLOAT4";
  172. case VET_DOUBLE1: return "DOUBLE1";
  173. case VET_DOUBLE2: return "DOUBLE2";
  174. case VET_DOUBLE3: return "DOUBLE3";
  175. case VET_DOUBLE4: return "DOUBLE4";
  176. case VET_SHORT1: return "SHORT1";
  177. case VET_SHORT2: return "SHORT2";
  178. case VET_SHORT3: return "SHORT3";
  179. case VET_SHORT4: return "SHORT4";
  180. case VET_USHORT1: return "USHORT1";
  181. case VET_USHORT2: return "USHORT2";
  182. case VET_USHORT3: return "USHORT3";
  183. case VET_USHORT4: return "USHORT4";
  184. case VET_INT1: return "INT1";
  185. case VET_INT2: return "INT2";
  186. case VET_INT3: return "INT3";
  187. case VET_INT4: return "INT4";
  188. case VET_UINT1: return "UINT1";
  189. case VET_UINT2: return "UINT2";
  190. case VET_UINT3: return "UINT3";
  191. case VET_UINT4: return "UINT4";
  192. case VET_UBYTE4: return "UBYTE4";
  193. }
  194. return "Uknown_VertexElement::Type";
  195. }
  196. std::string VertexElement::SemanticToString()
  197. {
  198. return SemanticToString(semantic);
  199. }
  200. std::string VertexElement::SemanticToString(Semantic semantic)
  201. {
  202. switch(semantic)
  203. {
  204. case VES_POSITION: return "POSITION";
  205. case VES_BLEND_WEIGHTS: return "BLEND_WEIGHTS";
  206. case VES_BLEND_INDICES: return "BLEND_INDICES";
  207. case VES_NORMAL: return "NORMAL";
  208. case VES_DIFFUSE: return "DIFFUSE";
  209. case VES_SPECULAR: return "SPECULAR";
  210. case VES_TEXTURE_COORDINATES: return "TEXTURE_COORDINATES";
  211. case VES_BINORMAL: return "BINORMAL";
  212. case VES_TANGENT: return "TANGENT";
  213. }
  214. return "Uknown_VertexElement::Semantic";
  215. }
  216. // VertexData
  217. VertexData::VertexData() :
  218. count(0)
  219. {
  220. }
  221. VertexData::~VertexData()
  222. {
  223. Reset();
  224. }
  225. void VertexData::Reset()
  226. {
  227. // Releases shared ptr memory streams.
  228. vertexBindings.clear();
  229. vertexElements.clear();
  230. }
  231. uint32_t VertexData::VertexSize(uint16_t source) const
  232. {
  233. uint32_t size = 0;
  234. for(VertexElementList::const_iterator iter=vertexElements.begin(), end=vertexElements.end(); iter != end; ++iter)
  235. {
  236. if (iter->source == source)
  237. size += iter->Size();
  238. }
  239. return size;
  240. }
  241. MemoryStream *VertexData::VertexBuffer(uint16_t source)
  242. {
  243. if (vertexBindings.find(source) != vertexBindings.end())
  244. return vertexBindings[source];
  245. return 0;
  246. }
  247. VertexElement *VertexData::GetVertexElement(VertexElement::Semantic semantic, uint16_t index)
  248. {
  249. for(VertexElementList::iterator iter=vertexElements.begin(), end=vertexElements.end(); iter != end; ++iter)
  250. {
  251. VertexElement &element = (*iter);
  252. if (element.semantic == semantic && element.index == index)
  253. return &element;
  254. }
  255. return 0;
  256. }
  257. // IndexData
  258. IndexData::IndexData() :
  259. count(0),
  260. faceCount(0),
  261. is32bit(false)
  262. {
  263. }
  264. IndexData::~IndexData()
  265. {
  266. Reset();
  267. }
  268. void IndexData::Reset()
  269. {
  270. // Release shared ptr memory stream.
  271. buffer.reset();
  272. }
  273. size_t IndexData::IndexSize() const
  274. {
  275. return (is32bit ? sizeof(uint32_t) : sizeof(uint16_t));
  276. }
  277. size_t IndexData::FaceSize() const
  278. {
  279. return IndexSize() * 3;
  280. }
  281. // Mesh
  282. Mesh::Mesh() :
  283. sharedVertexData(0),
  284. hasSkeletalAnimations(false)
  285. {
  286. }
  287. Mesh::~Mesh()
  288. {
  289. Reset();
  290. }
  291. void Mesh::Reset()
  292. {
  293. OGRE_SAFE_DELETE(sharedVertexData)
  294. for(size_t i=0, len=subMeshes.size(); i<len; ++i) {
  295. OGRE_SAFE_DELETE(subMeshes[i])
  296. }
  297. subMeshes.clear();
  298. for(size_t i=0, len=animations.size(); i<len; ++i) {
  299. OGRE_SAFE_DELETE(animations[i])
  300. }
  301. animations.clear();
  302. for(size_t i=0, len=poses.size(); i<len; ++i) {
  303. OGRE_SAFE_DELETE(poses[i])
  304. }
  305. poses.clear();
  306. }
  307. size_t Mesh::NumSubMeshes() const
  308. {
  309. return subMeshes.size();
  310. }
  311. SubMesh2 *Mesh::SubMesh(uint16_t index) const
  312. {
  313. for(size_t i=0; i<subMeshes.size(); ++i)
  314. if (subMeshes[i]->index == index)
  315. return subMeshes[i];
  316. return 0;
  317. }
  318. void Mesh::ConvertToAssimpScene(aiScene* dest)
  319. {
  320. // Export meshes
  321. dest->mNumMeshes = NumSubMeshes();
  322. dest->mMeshes = new aiMesh*[dest->mNumMeshes];
  323. // Create root node
  324. dest->mRootNode = new aiNode();
  325. dest->mRootNode->mNumMeshes = dest->mNumMeshes;
  326. dest->mRootNode->mMeshes = new unsigned int[dest->mRootNode->mNumMeshes];
  327. for(size_t i=0; i<dest->mNumMeshes; ++i) {
  328. dest->mMeshes[i] = subMeshes[i]->ConvertToAssimpMesh(this);
  329. dest->mRootNode->mMeshes[i] = i;
  330. }
  331. }
  332. // SubMesh2
  333. SubMesh2::SubMesh2() :
  334. index(0),
  335. vertexData(0),
  336. indexData(new IndexData()),
  337. usesSharedVertexData(false),
  338. operationType(OT_POINT_LIST),
  339. materialIndex(-1)
  340. {
  341. }
  342. SubMesh2::~SubMesh2()
  343. {
  344. Reset();
  345. }
  346. void SubMesh2::Reset()
  347. {
  348. OGRE_SAFE_DELETE(vertexData)
  349. OGRE_SAFE_DELETE(indexData)
  350. }
  351. aiMesh *SubMesh2::ConvertToAssimpMesh(Mesh *parent)
  352. {
  353. if (operationType != OT_TRIANGLE_LIST) {
  354. throw DeadlyImportError(Formatter::format() << "Only mesh operation type OT_TRIANGLE_LIST is supported. Found " << operationType);
  355. }
  356. aiMesh *dest = new aiMesh();
  357. dest->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  358. if (!name.empty())
  359. dest->mName = name;
  360. // Material index
  361. if (materialIndex != -1)
  362. dest->mMaterialIndex = materialIndex;
  363. // Pick source vertex data from shader geometry or from internal geometry.
  364. VertexData *src = (!usesSharedVertexData ? vertexData : parent->sharedVertexData);
  365. VertexElement *positionsElement = src->GetVertexElement(VertexElement::VES_POSITION);
  366. VertexElement *normalsElement = src->GetVertexElement(VertexElement::VES_NORMAL);
  367. VertexElement *uv1Element = src->GetVertexElement(VertexElement::VES_TEXTURE_COORDINATES, 0);
  368. VertexElement *uv2Element = src->GetVertexElement(VertexElement::VES_TEXTURE_COORDINATES, 1);
  369. // Sanity checks
  370. if (!positionsElement) {
  371. throw DeadlyImportError("Failed to import Ogre VertexElement::VES_POSITION. Mesh does not have vertex positions!");
  372. } else if (positionsElement->type != VertexElement::VET_FLOAT3) {
  373. throw DeadlyImportError("Ogre Mesh position vertex element type != VertexElement::VET_FLOAT3. This is not supported.");
  374. } else if (normalsElement && normalsElement->type != VertexElement::VET_FLOAT3) {
  375. throw DeadlyImportError("Ogre Mesh normal vertex element type != VertexElement::VET_FLOAT3. This is not supported.");
  376. }
  377. // Faces
  378. dest->mNumFaces = indexData->faceCount;
  379. dest->mFaces = new aiFace[dest->mNumFaces];
  380. // Assimp required unique vertices, we need to convert from Ogres shared indexing.
  381. size_t uniqueVertexCount = dest->mNumFaces * 3;
  382. dest->mNumVertices = uniqueVertexCount;
  383. dest->mVertices = new aiVector3D[dest->mNumVertices];
  384. // Source streams
  385. MemoryStream *positions = src->VertexBuffer(positionsElement->source);
  386. MemoryStream *normals = (normalsElement ? src->VertexBuffer(normalsElement->source) : 0);
  387. MemoryStream *uv1 = (uv1Element ? src->VertexBuffer(uv1Element->source) : 0);
  388. MemoryStream *uv2 = (uv2Element ? src->VertexBuffer(uv2Element->source) : 0);
  389. // Element size
  390. const size_t sizePosition = positionsElement->Size();
  391. const size_t sizeNormal = (normalsElement ? normalsElement->Size() : 0);
  392. const size_t sizeUv1 = (uv1Element ? uv1Element->Size() : 0);
  393. const size_t sizeUv2 = (uv2Element ? uv2Element->Size() : 0);
  394. // Vertex width
  395. const size_t vWidthPosition = src->VertexSize(positionsElement->source);
  396. const size_t vWidthNormal = (normalsElement ? src->VertexSize(normalsElement->source) : 0);
  397. const size_t vWidthUv1 = (uv1Element ? src->VertexSize(uv1Element->source) : 0);
  398. const size_t vWidthUv2 = (uv2Element ? src->VertexSize(uv2Element->source) : 0);
  399. // Prepare normals
  400. if (normals)
  401. dest->mNormals = new aiVector3D[dest->mNumVertices];
  402. // Prepare UVs, ignoring incompatible UVs.
  403. if (uv1)
  404. {
  405. if (uv1Element->type == VertexElement::VET_FLOAT2 || uv1Element->type == VertexElement::VET_FLOAT3)
  406. {
  407. dest->mNumUVComponents[0] = uv1Element->ComponentCount();
  408. dest->mTextureCoords[0] = new aiVector3D[dest->mNumVertices];
  409. }
  410. else
  411. {
  412. DefaultLogger::get()->warn(Formatter::format() << "Ogre imported UV0 type " << uv1Element->TypeToString() << " is not compatible with Assimp. Ignoring UV.");
  413. uv1 = 0;
  414. }
  415. }
  416. if (uv2)
  417. {
  418. if (uv2Element->type == VertexElement::VET_FLOAT2 || uv2Element->type == VertexElement::VET_FLOAT3)
  419. {
  420. dest->mNumUVComponents[1] = uv2Element->ComponentCount();
  421. dest->mTextureCoords[1] = new aiVector3D[dest->mNumVertices];
  422. }
  423. else
  424. {
  425. DefaultLogger::get()->warn(Formatter::format() << "Ogre imported UV0 type " << uv2Element->TypeToString() << " is not compatible with Assimp. Ignoring UV.");
  426. uv2 = 0;
  427. }
  428. }
  429. aiVector3D *uv1Dest = (uv1 ? dest->mTextureCoords[0] : 0);
  430. aiVector3D *uv2Dest = (uv2 ? dest->mTextureCoords[1] : 0);
  431. MemoryStream *faces = indexData->buffer.get();
  432. for (size_t fi=0, isize=indexData->IndexSize(), fsize=indexData->FaceSize();
  433. fi<dest->mNumFaces; ++fi)
  434. {
  435. // Source Ogre face
  436. aiFace ogreFace;
  437. ogreFace.mNumIndices = 3;
  438. ogreFace.mIndices = new unsigned int[3];
  439. faces->Seek(fi * fsize, aiOrigin_SET);
  440. if (indexData->is32bit)
  441. {
  442. faces->Read(&ogreFace.mIndices[0], isize, 3);
  443. }
  444. else
  445. {
  446. uint16_t iout = 0;
  447. for (size_t ii=0; ii<3; ++ii)
  448. {
  449. faces->Read(&iout, isize, 1);
  450. ogreFace.mIndices[ii] = static_cast<unsigned int>(iout);
  451. }
  452. }
  453. // Destination Assimp face
  454. aiFace &face = dest->mFaces[fi];
  455. face.mNumIndices = 3;
  456. face.mIndices = new unsigned int[3];
  457. const size_t pos = fi * 3;
  458. for (size_t v=0; v<3; ++v)
  459. {
  460. const size_t newIndex = pos + v;
  461. // Write face index
  462. face.mIndices[v] = newIndex;
  463. // Ogres vertex index to ref into the source buffers.
  464. const size_t ogreVertexIndex = ogreFace.mIndices[v];
  465. // Position
  466. positions->Seek((vWidthPosition * ogreVertexIndex) + positionsElement->offset, aiOrigin_SET);
  467. positions->Read(&dest->mVertices[newIndex], sizePosition, 1);
  468. // Normal
  469. if (normals)
  470. {
  471. normals->Seek((vWidthNormal * ogreVertexIndex) + normalsElement->offset, aiOrigin_SET);
  472. normals->Read(&dest->mNormals[newIndex], sizeNormal, 1);
  473. }
  474. // UV0
  475. if (uv1 && uv1Dest)
  476. {
  477. uv1->Seek((vWidthUv1 * ogreVertexIndex) + uv1Element->offset, aiOrigin_SET);
  478. uv1->Read(&uv1Dest[newIndex], sizeUv1, 1);
  479. }
  480. // UV1
  481. if (uv2 && uv2Dest)
  482. {
  483. uv2->Seek((vWidthUv2 * ogreVertexIndex) + uv2Element->offset, aiOrigin_SET);
  484. uv2->Read(&uv2Dest[newIndex], sizeUv2, 1);
  485. }
  486. /// @todo Bones and bone weights.
  487. }
  488. }
  489. return dest;
  490. }
  491. // Animation2
  492. Animation2::Animation2(Mesh *_parentMesh) :
  493. parentMesh(_parentMesh),
  494. length(0.0f),
  495. baseTime(-1.0f)
  496. {
  497. }
  498. VertexData *Animation2::AssociatedVertexData(VertexAnimationTrack *track) const
  499. {
  500. bool sharedGeom = (track->target == 0);
  501. if (sharedGeom)
  502. return parentMesh->sharedVertexData;
  503. else
  504. return parentMesh->SubMesh(track->target-1)->vertexData;
  505. }
  506. } // Ogre
  507. } // Assimp
  508. #endif // ASSIMP_BUILD_NO_OGRE_IMPORTER