2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2020-01-20 13:53:12 +00:00
|
|
|
Copyright (c) 2006-2020, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2017-05-09 17:57:36 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
2015-05-19 03:48:29 +00:00
|
|
|
following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2015-05-19 03:52:10 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-05-19 03:48:29 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2015-05-19 03:52:10 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2015-05-19 03:52:10 +00:00
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file BlenderModifier.cpp
|
|
|
|
* @brief Implementation of some blender modifiers (i.e subdivision, mirror).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_BLEND_IMPORTER
|
2017-06-21 12:31:37 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
#include "BlenderModifier.h"
|
2017-06-21 12:31:37 +00:00
|
|
|
#include <assimp/SceneCombiner.h>
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/Subdivision.h>
|
2016-06-06 20:04:29 +00:00
|
|
|
#include <assimp/scene.h>
|
2016-04-05 21:23:53 +00:00
|
|
|
#include <memory>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
using namespace Assimp::Blender;
|
|
|
|
|
2020-03-01 12:15:45 +00:00
|
|
|
template <typename T>
|
|
|
|
BlenderModifier *god() {
|
2015-05-19 03:57:13 +00:00
|
|
|
return new T();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add all available modifiers here
|
2020-03-01 12:15:45 +00:00
|
|
|
typedef BlenderModifier *(*fpCreateModifier)();
|
2015-05-19 03:48:29 +00:00
|
|
|
static const fpCreateModifier creators[] = {
|
2020-03-01 12:15:45 +00:00
|
|
|
&god<BlenderModifier_Mirror>,
|
|
|
|
&god<BlenderModifier_Subdivision>,
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-03-01 12:15:45 +00:00
|
|
|
NULL // sentinel
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-03-01 12:15:45 +00:00
|
|
|
struct SharedModifierData : ElemBase {
|
2015-05-19 03:57:13 +00:00
|
|
|
ModifierData modifier;
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-03-01 12:15:45 +00:00
|
|
|
void BlenderModifierShowcase::ApplyModifiers(aiNode &out, ConversionData &conv_data, const Scene &in, const Object &orig_object) {
|
2015-05-19 03:57:13 +00:00
|
|
|
size_t cnt = 0u, ful = 0u;
|
|
|
|
|
|
|
|
// NOTE: this cast is potentially unsafe by design, so we need to perform type checks before
|
|
|
|
// we're allowed to dereference the pointers without risking to crash. We might still be
|
|
|
|
// invoking UB btw - we're assuming that the ModifierData member of the respective modifier
|
|
|
|
// structures is at offset sizeof(vftable) with no padding.
|
2020-03-01 12:15:45 +00:00
|
|
|
const SharedModifierData *cur = static_cast<const SharedModifierData *>(orig_object.modifiers.first.get());
|
|
|
|
for (; cur; cur = static_cast<const SharedModifierData *>(cur->modifier.next.get()), ++ful) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ai_assert(cur->dna_type);
|
|
|
|
|
2020-03-01 12:15:45 +00:00
|
|
|
const Structure *s = conv_data.db.dna.Get(cur->dna_type);
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!s) {
|
2020-03-01 12:15:45 +00:00
|
|
|
ASSIMP_LOG_WARN_F("BlendModifier: could not resolve DNA name: ", cur->dna_type);
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is a common trait of all XXXMirrorData structures in BlenderDNA
|
2020-03-01 12:15:45 +00:00
|
|
|
const Field *f = s->Get("modifier");
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!f || f->offset != 0) {
|
|
|
|
ASSIMP_LOG_WARN("BlendModifier: expected a `modifier` member at offset 0");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-03-01 12:15:45 +00:00
|
|
|
s = conv_data.db.dna.Get(f->type);
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!s || s->name != "ModifierData") {
|
|
|
|
ASSIMP_LOG_WARN("BlendModifier: expected a ModifierData structure as first member");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now, we can be sure that we should be fine to dereference *cur* as
|
|
|
|
// ModifierData (with the above note).
|
2020-03-01 12:15:45 +00:00
|
|
|
const ModifierData &dat = cur->modifier;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-03-01 12:15:45 +00:00
|
|
|
const fpCreateModifier *curgod = creators;
|
|
|
|
std::vector<BlenderModifier *>::iterator curmod = cached_modifiers->begin(), endmod = cached_modifiers->end();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-03-01 12:15:45 +00:00
|
|
|
for (; *curgod; ++curgod, ++curmod) { // allocate modifiers on the fly
|
2015-05-19 03:57:13 +00:00
|
|
|
if (curmod == endmod) {
|
|
|
|
cached_modifiers->push_back((*curgod)());
|
|
|
|
|
|
|
|
endmod = cached_modifiers->end();
|
2020-03-01 12:15:45 +00:00
|
|
|
curmod = endmod - 1;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 12:15:45 +00:00
|
|
|
BlenderModifier *const modifier = *curmod;
|
|
|
|
if (modifier->IsActive(dat)) {
|
|
|
|
modifier->DoIt(out, conv_data, *static_cast<const ElemBase *>(cur), in, orig_object);
|
2015-05-19 03:57:13 +00:00
|
|
|
cnt++;
|
|
|
|
|
|
|
|
curgod = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (curgod) {
|
2020-03-01 12:15:45 +00:00
|
|
|
ASSIMP_LOG_WARN_F("Couldn't find a handler for modifier: ", dat.name);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Even though we managed to resolve some or all of the modifiers on this
|
|
|
|
// object, we still can't say whether our modifier implementations were
|
|
|
|
// able to fully do their job.
|
|
|
|
if (ful) {
|
2020-03-01 12:15:45 +00:00
|
|
|
ASSIMP_LOG_DEBUG_F("BlendModifier: found handlers for ", cnt, " of ", ful, " modifiers on `", orig_object.id.name,
|
|
|
|
"`, check log messages above for errors");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-03-01 12:15:45 +00:00
|
|
|
bool BlenderModifier_Mirror ::IsActive(const ModifierData &modin) {
|
2015-05-19 03:57:13 +00:00
|
|
|
return modin.type == ModifierData::eModifierType_Mirror;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-03-01 12:15:45 +00:00
|
|
|
void BlenderModifier_Mirror ::DoIt(aiNode &out, ConversionData &conv_data, const ElemBase &orig_modifier,
|
|
|
|
const Scene & /*in*/,
|
|
|
|
const Object &orig_object) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// hijacking the ABI, see the big note in BlenderModifierShowcase::ApplyModifiers()
|
2020-03-01 12:15:45 +00:00
|
|
|
const MirrorModifierData &mir = static_cast<const MirrorModifierData &>(orig_modifier);
|
2015-05-19 03:57:13 +00:00
|
|
|
ai_assert(mir.modifier.type == ModifierData::eModifierType_Mirror);
|
|
|
|
|
|
|
|
conv_data.meshes->reserve(conv_data.meshes->size() + out.mNumMeshes);
|
|
|
|
|
|
|
|
// XXX not entirely correct, mirroring on two axes results in 4 distinct objects in blender ...
|
|
|
|
|
|
|
|
// take all input meshes and clone them
|
|
|
|
for (unsigned int i = 0; i < out.mNumMeshes; ++i) {
|
2020-03-01 12:15:45 +00:00
|
|
|
aiMesh *mesh;
|
|
|
|
SceneCombiner::Copy(&mesh, conv_data.meshes[out.mMeshes[i]]);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
const float xs = mir.flag & MirrorModifierData::Flags_AXIS_X ? -1.f : 1.f;
|
|
|
|
const float ys = mir.flag & MirrorModifierData::Flags_AXIS_Y ? -1.f : 1.f;
|
|
|
|
const float zs = mir.flag & MirrorModifierData::Flags_AXIS_Z ? -1.f : 1.f;
|
|
|
|
|
|
|
|
if (mir.mirror_ob) {
|
2020-03-01 12:15:45 +00:00
|
|
|
const aiVector3D center(mir.mirror_ob->obmat[3][0], mir.mirror_ob->obmat[3][1], mir.mirror_ob->obmat[3][2]);
|
|
|
|
for (unsigned int j = 0; j < mesh->mNumVertices; ++j) {
|
|
|
|
aiVector3D &v = mesh->mVertices[j];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-03-01 12:15:45 +00:00
|
|
|
v.x = center.x + xs * (center.x - v.x);
|
|
|
|
v.y = center.y + ys * (center.y - v.y);
|
|
|
|
v.z = center.z + zs * (center.z - v.z);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-03-01 12:15:45 +00:00
|
|
|
} else {
|
|
|
|
for (unsigned int j = 0; j < mesh->mNumVertices; ++j) {
|
|
|
|
aiVector3D &v = mesh->mVertices[j];
|
|
|
|
v.x *= xs;
|
|
|
|
v.y *= ys;
|
|
|
|
v.z *= zs;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mesh->mNormals) {
|
2020-03-01 12:15:45 +00:00
|
|
|
for (unsigned int j = 0; j < mesh->mNumVertices; ++j) {
|
|
|
|
aiVector3D &v = mesh->mNormals[j];
|
|
|
|
v.x *= xs;
|
|
|
|
v.y *= ys;
|
|
|
|
v.z *= zs;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mesh->mTangents) {
|
2020-03-01 12:15:45 +00:00
|
|
|
for (unsigned int j = 0; j < mesh->mNumVertices; ++j) {
|
|
|
|
aiVector3D &v = mesh->mTangents[j];
|
|
|
|
v.x *= xs;
|
|
|
|
v.y *= ys;
|
|
|
|
v.z *= zs;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mesh->mBitangents) {
|
2020-03-01 12:15:45 +00:00
|
|
|
for (unsigned int j = 0; j < mesh->mNumVertices; ++j) {
|
|
|
|
aiVector3D &v = mesh->mBitangents[j];
|
|
|
|
v.x *= xs;
|
|
|
|
v.y *= ys;
|
|
|
|
v.z *= zs;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const float us = mir.flag & MirrorModifierData::Flags_MIRROR_U ? -1.f : 1.f;
|
|
|
|
const float vs = mir.flag & MirrorModifierData::Flags_MIRROR_V ? -1.f : 1.f;
|
|
|
|
|
|
|
|
for (unsigned int n = 0; mesh->HasTextureCoords(n); ++n) {
|
2020-03-01 12:15:45 +00:00
|
|
|
for (unsigned int j = 0; j < mesh->mNumVertices; ++j) {
|
|
|
|
aiVector3D &v = mesh->mTextureCoords[n][j];
|
|
|
|
v.x *= us;
|
|
|
|
v.y *= vs;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only reverse the winding order if an odd number of axes were mirrored.
|
|
|
|
if (xs * ys * zs < 0) {
|
2020-03-01 12:15:45 +00:00
|
|
|
for (unsigned int j = 0; j < mesh->mNumFaces; ++j ) {
|
|
|
|
aiFace &face = mesh->mFaces[j];
|
|
|
|
for (unsigned int fi = 0; fi < face.mNumIndices / 2; ++fi)
|
|
|
|
std::swap(face.mIndices[fi], face.mIndices[face.mNumIndices - 1 - fi]);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conv_data.meshes->push_back(mesh);
|
|
|
|
}
|
2020-03-01 12:15:45 +00:00
|
|
|
unsigned int *nind = new unsigned int[out.mNumMeshes * 2];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-03-01 12:15:45 +00:00
|
|
|
std::copy(out.mMeshes, out.mMeshes + out.mNumMeshes, nind);
|
|
|
|
std::transform(out.mMeshes, out.mMeshes + out.mNumMeshes, nind + out.mNumMeshes,
|
|
|
|
[&out](unsigned int n) { return out.mNumMeshes + n; });
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
delete[] out.mMeshes;
|
|
|
|
out.mMeshes = nind;
|
|
|
|
out.mNumMeshes *= 2;
|
|
|
|
|
|
|
|
ASSIMP_LOG_INFO_F("BlendModifier: Applied the `Mirror` modifier to `",
|
2020-03-01 12:15:45 +00:00
|
|
|
orig_object.id.name, "`");
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-03-01 12:15:45 +00:00
|
|
|
bool BlenderModifier_Subdivision ::IsActive(const ModifierData &modin) {
|
2015-05-19 03:57:13 +00:00
|
|
|
return modin.type == ModifierData::eModifierType_Subsurf;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-03-01 12:15:45 +00:00
|
|
|
void BlenderModifier_Subdivision ::DoIt(aiNode &out, ConversionData &conv_data, const ElemBase &orig_modifier,
|
|
|
|
const Scene & /*in*/,
|
|
|
|
const Object &orig_object) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// hijacking the ABI, see the big note in BlenderModifierShowcase::ApplyModifiers()
|
2020-03-01 12:15:45 +00:00
|
|
|
const SubsurfModifierData &mir = static_cast<const SubsurfModifierData &>(orig_modifier);
|
2015-05-19 03:57:13 +00:00
|
|
|
ai_assert(mir.modifier.type == ModifierData::eModifierType_Subsurf);
|
|
|
|
|
|
|
|
Subdivider::Algorithm algo;
|
2020-03-01 12:15:45 +00:00
|
|
|
switch (mir.subdivType) {
|
|
|
|
case SubsurfModifierData::TYPE_CatmullClarke:
|
|
|
|
algo = Subdivider::CATMULL_CLARKE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SubsurfModifierData::TYPE_Simple:
|
|
|
|
ASSIMP_LOG_WARN("BlendModifier: The `SIMPLE` subdivision algorithm is not currently implemented, using Catmull-Clarke");
|
|
|
|
algo = Subdivider::CATMULL_CLARKE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ASSIMP_LOG_WARN_F("BlendModifier: Unrecognized subdivision algorithm: ", mir.subdivType);
|
|
|
|
return;
|
2015-05-19 03:57:13 +00:00
|
|
|
};
|
|
|
|
|
2016-04-05 21:23:53 +00:00
|
|
|
std::unique_ptr<Subdivider> subd(Subdivider::Create(algo));
|
2015-05-19 03:57:13 +00:00
|
|
|
ai_assert(subd);
|
2020-03-01 12:15:45 +00:00
|
|
|
if (conv_data.meshes->empty()) {
|
2017-10-31 13:03:03 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-03-01 12:15:45 +00:00
|
|
|
aiMesh **const meshes = &conv_data.meshes[conv_data.meshes->size() - out.mNumMeshes];
|
|
|
|
std::unique_ptr<aiMesh *[]> tempmeshes(new aiMesh *[out.mNumMeshes]());
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-03-01 12:15:45 +00:00
|
|
|
subd->Subdivide(meshes, out.mNumMeshes, tempmeshes.get(), std::max(mir.renderLevels, mir.levels), true);
|
|
|
|
std::copy(tempmeshes.get(), tempmeshes.get() + out.mNumMeshes, meshes);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
ASSIMP_LOG_INFO_F("BlendModifier: Applied the `Subdivision` modifier to `",
|
2020-03-01 12:15:45 +00:00
|
|
|
orig_object.id.name, "`");
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 10:41:49 +00:00
|
|
|
#endif // ASSIMP_BUILD_NO_BLEND_IMPORTER
|