Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

BlenderModifier.cpp 11 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. /** @file BlenderModifier.cpp
  34. * @brief Implementation of some blender modifiers (i.e subdivision, mirror).
  35. */
  36. #include "AssimpPCH.h"
  37. #ifndef ASSIMP_BUILD_NO_BLEND_IMPORTER
  38. #include "BlenderModifier.h"
  39. #include "SceneCombiner.h"
  40. #include "Subdivision.h"
  41. #include <functional>
  42. using namespace Assimp;
  43. using namespace Assimp::Blender;
  44. template <typename T> BlenderModifier* god() {
  45. return new T();
  46. }
  47. // add all available modifiers here
  48. typedef BlenderModifier* (*fpCreateModifier)();
  49. static const fpCreateModifier creators[] = {
  50. &god<BlenderModifier_Mirror>,
  51. &god<BlenderModifier_Subdivision>,
  52. NULL // sentinel
  53. };
  54. // ------------------------------------------------------------------------------------------------
  55. // just testing out some new macros to simplify logging
  56. #define ASSIMP_LOG_WARN_F(string,...)\
  57. DefaultLogger::get()->warn((Formatter::format(string),__VA_ARGS__))
  58. #define ASSIMP_LOG_ERROR_F(string,...)\
  59. DefaultLogger::get()->error((Formatter::format(string),__VA_ARGS__))
  60. #define ASSIMP_LOG_DEBUG_F(string,...)\
  61. DefaultLogger::get()->debug((Formatter::format(string),__VA_ARGS__))
  62. #define ASSIMP_LOG_INFO_F(string,...)\
  63. DefaultLogger::get()->info((Formatter::format(string),__VA_ARGS__))
  64. #define ASSIMP_LOG_WARN(string)\
  65. DefaultLogger::get()->warn(string)
  66. #define ASSIMP_LOG_ERROR(string)\
  67. DefaultLogger::get()->error(string)
  68. #define ASSIMP_LOG_DEBUG(string)\
  69. DefaultLogger::get()->debug(string)
  70. #define ASSIMP_LOG_INFO(string)\
  71. DefaultLogger::get()->info(string)
  72. // ------------------------------------------------------------------------------------------------
  73. struct SharedModifierData : ElemBase
  74. {
  75. ModifierData modifier;
  76. };
  77. // ------------------------------------------------------------------------------------------------
  78. void BlenderModifierShowcase::ApplyModifiers(aiNode& out, ConversionData& conv_data, const Scene& in, const Object& orig_object )
  79. {
  80. size_t cnt = 0u, ful = 0u;
  81. // NOTE: this cast is potentially unsafe by design, so we need to perform type checks before
  82. // we're allowed to dereference the pointers without risking to crash. We might still be
  83. // invoking UB btw - we're assuming that the ModifierData member of the respective modifier
  84. // structures is at offset sizeof(vftable) with no padding.
  85. const SharedModifierData* cur = boost::static_pointer_cast<const SharedModifierData> ( orig_object.modifiers.first.get() );
  86. for (; cur; cur = boost::static_pointer_cast<const SharedModifierData> ( cur->modifier.next.get() ), ++ful) {
  87. ai_assert(cur->dna_type);
  88. const Structure* s = conv_data.db.dna.Get( cur->dna_type );
  89. if (!s) {
  90. ASSIMP_LOG_WARN_F("BlendModifier: could not resolve DNA name: ",cur->dna_type);
  91. continue;
  92. }
  93. // this is a common trait of all XXXMirrorData structures in BlenderDNA
  94. const Field* f = s->Get("modifier");
  95. if (!f || f->offset != 0) {
  96. ASSIMP_LOG_WARN("BlendModifier: expected a `modifier` member at offset 0");
  97. continue;
  98. }
  99. s = conv_data.db.dna.Get( f->type );
  100. if (!s || s->name != "ModifierData") {
  101. ASSIMP_LOG_WARN("BlendModifier: expected a ModifierData structure as first member");
  102. continue;
  103. }
  104. // now, we can be sure that we should be fine to dereference *cur* as
  105. // ModifierData (with the above note).
  106. const ModifierData& dat = cur->modifier;
  107. const fpCreateModifier* curgod = creators;
  108. std::vector< BlenderModifier* >::iterator curmod = cached_modifiers->begin(), endmod = cached_modifiers->end();
  109. for (;*curgod;++curgod,++curmod) { // allocate modifiers on the fly
  110. if (curmod == endmod) {
  111. cached_modifiers->push_back((*curgod)());
  112. endmod = cached_modifiers->end();
  113. curmod = endmod-1;
  114. }
  115. BlenderModifier* const modifier = *curmod;
  116. if(modifier->IsActive(dat)) {
  117. modifier->DoIt(out,conv_data,*boost::static_pointer_cast<const ElemBase>(cur),in,orig_object);
  118. cnt++;
  119. curgod = NULL;
  120. break;
  121. }
  122. }
  123. if (curgod) {
  124. ASSIMP_LOG_WARN_F("Couldn't find a handler for modifier: ",dat.name);
  125. }
  126. }
  127. // Even though we managed to resolve some or all of the modifiers on this
  128. // object, we still can't say whether our modifier implementations were
  129. // able to fully do their job.
  130. if (ful) {
  131. ASSIMP_LOG_DEBUG_F("BlendModifier: found handlers for ",cnt," of ",ful," modifiers on `",orig_object.id.name,
  132. "`, check log messages above for errors");
  133. }
  134. }
  135. // ------------------------------------------------------------------------------------------------
  136. bool BlenderModifier_Mirror :: IsActive (const ModifierData& modin)
  137. {
  138. return modin.type == ModifierData::eModifierType_Mirror;
  139. }
  140. // ------------------------------------------------------------------------------------------------
  141. void BlenderModifier_Mirror :: DoIt(aiNode& out, ConversionData& conv_data, const ElemBase& orig_modifier,
  142. const Scene& /*in*/,
  143. const Object& orig_object )
  144. {
  145. // hijacking the ABI, see the big note in BlenderModifierShowcase::ApplyModifiers()
  146. const MirrorModifierData& mir = static_cast<const MirrorModifierData&>(orig_modifier);
  147. ai_assert(mir.modifier.type == ModifierData::eModifierType_Mirror);
  148. conv_data.meshes->reserve(conv_data.meshes->size() + out.mNumMeshes);
  149. // XXX not entirely correct, mirroring on two axes results in 4 distinct objects in blender ...
  150. // take all input meshes and clone them
  151. for (unsigned int i = 0; i < out.mNumMeshes; ++i) {
  152. aiMesh* mesh;
  153. SceneCombiner::Copy(&mesh,conv_data.meshes[out.mMeshes[i]]);
  154. const float xs = mir.flag & MirrorModifierData::Flags_AXIS_X ? -1.f : 1.f;
  155. const float ys = mir.flag & MirrorModifierData::Flags_AXIS_Y ? -1.f : 1.f;
  156. const float zs = mir.flag & MirrorModifierData::Flags_AXIS_Z ? -1.f : 1.f;
  157. if (mir.mirror_ob) {
  158. const aiVector3D center( mir.mirror_ob->obmat[3][0],mir.mirror_ob->obmat[3][1],mir.mirror_ob->obmat[3][2] );
  159. for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
  160. aiVector3D& v = mesh->mVertices[i];
  161. v.x = center.x + xs*(center.x - v.x);
  162. v.y = center.y + ys*(center.y - v.y);
  163. v.z = center.z + zs*(center.z - v.z);
  164. }
  165. }
  166. else {
  167. for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
  168. aiVector3D& v = mesh->mVertices[i];
  169. v.x *= xs;v.y *= ys;v.z *= zs;
  170. }
  171. }
  172. if (mesh->mNormals) {
  173. for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
  174. aiVector3D& v = mesh->mNormals[i];
  175. v.x *= xs;v.y *= ys;v.z *= zs;
  176. }
  177. }
  178. if (mesh->mTangents) {
  179. for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
  180. aiVector3D& v = mesh->mTangents[i];
  181. v.x *= xs;v.y *= ys;v.z *= zs;
  182. }
  183. }
  184. if (mesh->mBitangents) {
  185. for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
  186. aiVector3D& v = mesh->mBitangents[i];
  187. v.x *= xs;v.y *= ys;v.z *= zs;
  188. }
  189. }
  190. const float us = mir.flag & MirrorModifierData::Flags_MIRROR_U ? -1.f : 1.f;
  191. const float vs = mir.flag & MirrorModifierData::Flags_MIRROR_V ? -1.f : 1.f;
  192. for (unsigned int n = 0; mesh->HasTextureCoords(n); ++n) {
  193. for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
  194. aiVector3D& v = mesh->mTextureCoords[n][i];
  195. v.x *= us;v.y *= vs;
  196. }
  197. }
  198. // Only reverse the winding order if an odd number of axes were mirrored.
  199. if (xs * ys * zs < 0) {
  200. for( unsigned int i = 0; i < mesh->mNumFaces; i++) {
  201. aiFace& face = mesh->mFaces[i];
  202. for( unsigned int fi = 0; fi < face.mNumIndices / 2; ++fi)
  203. std::swap( face.mIndices[fi], face.mIndices[face.mNumIndices - 1 - fi]);
  204. }
  205. }
  206. conv_data.meshes->push_back(mesh);
  207. }
  208. unsigned int* nind = new unsigned int[out.mNumMeshes*2];
  209. std::copy(out.mMeshes,out.mMeshes+out.mNumMeshes,nind);
  210. std::transform(out.mMeshes,out.mMeshes+out.mNumMeshes,nind+out.mNumMeshes,
  211. std::bind1st(std::plus< unsigned int >(),out.mNumMeshes));
  212. delete[] out.mMeshes;
  213. out.mMeshes = nind;
  214. out.mNumMeshes *= 2;
  215. ASSIMP_LOG_INFO_F("BlendModifier: Applied the `Mirror` modifier to `",
  216. orig_object.id.name,"`");
  217. }
  218. // ------------------------------------------------------------------------------------------------
  219. bool BlenderModifier_Subdivision :: IsActive (const ModifierData& modin)
  220. {
  221. return modin.type == ModifierData::eModifierType_Subsurf;
  222. }
  223. // ------------------------------------------------------------------------------------------------
  224. void BlenderModifier_Subdivision :: DoIt(aiNode& out, ConversionData& conv_data, const ElemBase& orig_modifier,
  225. const Scene& /*in*/,
  226. const Object& orig_object )
  227. {
  228. // hijacking the ABI, see the big note in BlenderModifierShowcase::ApplyModifiers()
  229. const SubsurfModifierData& mir = static_cast<const SubsurfModifierData&>(orig_modifier);
  230. ai_assert(mir.modifier.type == ModifierData::eModifierType_Subsurf);
  231. Subdivider::Algorithm algo;
  232. switch (mir.subdivType)
  233. {
  234. case SubsurfModifierData::TYPE_CatmullClarke:
  235. algo = Subdivider::CATMULL_CLARKE;
  236. break;
  237. case SubsurfModifierData::TYPE_Simple:
  238. ASSIMP_LOG_WARN("BlendModifier: The `SIMPLE` subdivision algorithm is not currently implemented, using Catmull-Clarke");
  239. algo = Subdivider::CATMULL_CLARKE;
  240. break;
  241. default:
  242. ASSIMP_LOG_WARN_F("BlendModifier: Unrecognized subdivision algorithm: ",mir.subdivType);
  243. return;
  244. };
  245. boost::scoped_ptr<Subdivider> subd(Subdivider::Create(algo));
  246. ai_assert(subd);
  247. aiMesh** const meshes = &conv_data.meshes[conv_data.meshes->size() - out.mNumMeshes];
  248. boost::scoped_array<aiMesh*> tempmeshes(new aiMesh*[out.mNumMeshes]());
  249. subd->Subdivide(meshes,out.mNumMeshes,tempmeshes.get(),std::max( mir.renderLevels, mir.levels ),true);
  250. std::copy(tempmeshes.get(),tempmeshes.get()+out.mNumMeshes,meshes);
  251. ASSIMP_LOG_INFO_F("BlendModifier: Applied the `Subdivision` modifier to `",
  252. orig_object.id.name,"`");
  253. }
  254. #endif