vrshoot

diff libs/assimp/BlenderModifier.cpp @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/assimp/BlenderModifier.cpp	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,315 @@
     1.4 +/*
     1.5 +Open Asset Import Library (assimp)
     1.6 +----------------------------------------------------------------------
     1.7 +
     1.8 +Copyright (c) 2006-2012, assimp team
     1.9 +All rights reserved.
    1.10 +
    1.11 +Redistribution and use of this software in source and binary forms, 
    1.12 +with or without modification, are permitted provided that the 
    1.13 +following conditions are met:
    1.14 +
    1.15 +* Redistributions of source code must retain the above
    1.16 +  copyright notice, this list of conditions and the
    1.17 +  following disclaimer.
    1.18 +
    1.19 +* Redistributions in binary form must reproduce the above
    1.20 +  copyright notice, this list of conditions and the
    1.21 +  following disclaimer in the documentation and/or other
    1.22 +  materials provided with the distribution.
    1.23 +
    1.24 +* Neither the name of the assimp team, nor the names of its
    1.25 +  contributors may be used to endorse or promote products
    1.26 +  derived from this software without specific prior
    1.27 +  written permission of the assimp team.
    1.28 +
    1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
    1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
    1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
    1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
    1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
    1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.40 +
    1.41 +----------------------------------------------------------------------
    1.42 +*/
    1.43 +
    1.44 +/** @file  BlenderModifier.cpp
    1.45 + *  @brief Implementation of some blender modifiers (i.e subdivision, mirror).
    1.46 + */
    1.47 +#include "AssimpPCH.h"
    1.48 +
    1.49 +#ifndef ASSIMP_BUILD_NO_BLEND_IMPORTER
    1.50 +#include "BlenderModifier.h"
    1.51 +#include "SceneCombiner.h"
    1.52 +#include "Subdivision.h"
    1.53 +
    1.54 +#include <functional>
    1.55 +
    1.56 +using namespace Assimp;
    1.57 +using namespace Assimp::Blender;
    1.58 +
    1.59 +template <typename T> BlenderModifier* god() {
    1.60 +	return new T();
    1.61 +}
    1.62 +
    1.63 +// add all available modifiers here
    1.64 +typedef BlenderModifier* (*fpCreateModifier)();
    1.65 +static const fpCreateModifier creators[] = {
    1.66 +		&god<BlenderModifier_Mirror>,
    1.67 +		&god<BlenderModifier_Subdivision>,
    1.68 +
    1.69 +		NULL // sentinel
    1.70 +};
    1.71 +
    1.72 +// ------------------------------------------------------------------------------------------------
    1.73 +// just testing out some new macros to simplify logging
    1.74 +#define ASSIMP_LOG_WARN_F(string,...)\
    1.75 +	DefaultLogger::get()->warn((Formatter::format(string),__VA_ARGS__))
    1.76 +
    1.77 +#define ASSIMP_LOG_ERROR_F(string,...)\
    1.78 +	DefaultLogger::get()->error((Formatter::format(string),__VA_ARGS__))
    1.79 +
    1.80 +#define ASSIMP_LOG_DEBUG_F(string,...)\
    1.81 +	DefaultLogger::get()->debug((Formatter::format(string),__VA_ARGS__))
    1.82 +
    1.83 +#define ASSIMP_LOG_INFO_F(string,...)\
    1.84 +	DefaultLogger::get()->info((Formatter::format(string),__VA_ARGS__))
    1.85 +
    1.86 +
    1.87 +#define ASSIMP_LOG_WARN(string)\
    1.88 +	DefaultLogger::get()->warn(string)
    1.89 +
    1.90 +#define ASSIMP_LOG_ERROR(string)\
    1.91 +	DefaultLogger::get()->error(string)
    1.92 +
    1.93 +#define ASSIMP_LOG_DEBUG(string)\
    1.94 +	DefaultLogger::get()->debug(string)
    1.95 +
    1.96 +#define ASSIMP_LOG_INFO(string)\
    1.97 +	DefaultLogger::get()->info(string)
    1.98 +
    1.99 +
   1.100 +// ------------------------------------------------------------------------------------------------
   1.101 +struct SharedModifierData : ElemBase
   1.102 +{
   1.103 +	ModifierData modifier;
   1.104 +};
   1.105 +
   1.106 +// ------------------------------------------------------------------------------------------------
   1.107 +void BlenderModifierShowcase::ApplyModifiers(aiNode& out, ConversionData& conv_data, const Scene& in, const Object& orig_object ) 
   1.108 +{
   1.109 +	size_t cnt = 0u, ful = 0u;
   1.110 +
   1.111 +	// NOTE: this cast is potentially unsafe by design, so we need to perform type checks before
   1.112 +	// we're allowed to dereference the pointers without risking to crash. We might still be
   1.113 +	// invoking UB btw - we're assuming that the ModifierData member of the respective modifier
   1.114 +	// structures is at offset sizeof(vftable) with no padding.
   1.115 +	const SharedModifierData* cur = boost::static_pointer_cast<const SharedModifierData> ( orig_object.modifiers.first.get() );
   1.116 +	for (; cur; cur =  boost::static_pointer_cast<const SharedModifierData> ( cur->modifier.next.get() ), ++ful) {
   1.117 +		ai_assert(cur->dna_type);
   1.118 +
   1.119 +		const Structure* s = conv_data.db.dna.Get( cur->dna_type );
   1.120 +		if (!s) {
   1.121 +			ASSIMP_LOG_WARN_F("BlendModifier: could not resolve DNA name: ",cur->dna_type);
   1.122 +			continue;
   1.123 +		}
   1.124 +
   1.125 +		// this is a common trait of all XXXMirrorData structures in BlenderDNA
   1.126 +		const Field* f = s->Get("modifier");
   1.127 +		if (!f || f->offset != 0) {
   1.128 +			ASSIMP_LOG_WARN("BlendModifier: expected a `modifier` member at offset 0");
   1.129 +			continue;
   1.130 +		}
   1.131 +
   1.132 +		s = conv_data.db.dna.Get( f->type );
   1.133 +		if (!s || s->name != "ModifierData") {
   1.134 +			ASSIMP_LOG_WARN("BlendModifier: expected a ModifierData structure as first member");
   1.135 +			continue;
   1.136 +		}
   1.137 +
   1.138 +		// now, we can be sure that we should be fine to dereference *cur* as
   1.139 +		// ModifierData (with the above note).
   1.140 +		const ModifierData& dat = cur->modifier;
   1.141 +
   1.142 +		const fpCreateModifier* curgod = creators;
   1.143 +		std::vector< BlenderModifier* >::iterator curmod = cached_modifiers->begin(), endmod = cached_modifiers->end();
   1.144 +
   1.145 +		for (;*curgod;++curgod,++curmod) { // allocate modifiers on the fly
   1.146 +			if (curmod == endmod) {
   1.147 +				cached_modifiers->push_back((*curgod)());
   1.148 +
   1.149 +				endmod = cached_modifiers->end();
   1.150 +				curmod = endmod-1;
   1.151 +			}
   1.152 +
   1.153 +			BlenderModifier* const modifier = *curmod;
   1.154 +			if(modifier->IsActive(dat)) {
   1.155 +				modifier->DoIt(out,conv_data,*boost::static_pointer_cast<const ElemBase>(cur),in,orig_object);
   1.156 +				cnt++;
   1.157 +
   1.158 +				curgod = NULL;
   1.159 +				break;
   1.160 +			}
   1.161 +		}
   1.162 +		if (curgod) {
   1.163 +			ASSIMP_LOG_WARN_F("Couldn't find a handler for modifier: ",dat.name);
   1.164 +		}
   1.165 +	}
   1.166 +
   1.167 +	// Even though we managed to resolve some or all of the modifiers on this
   1.168 +	// object, we still can't say whether our modifier implementations were
   1.169 +	// able to fully do their job.
   1.170 +	if (ful) {
   1.171 +		ASSIMP_LOG_DEBUG_F("BlendModifier: found handlers for ",cnt," of ",ful," modifiers on `",orig_object.id.name,
   1.172 +			"`, check log messages above for errors");
   1.173 +	}
   1.174 +}
   1.175 +
   1.176 +
   1.177 +
   1.178 +// ------------------------------------------------------------------------------------------------
   1.179 +bool BlenderModifier_Mirror :: IsActive (const ModifierData& modin)
   1.180 +{
   1.181 +	return modin.type == ModifierData::eModifierType_Mirror;
   1.182 +}
   1.183 +
   1.184 +// ------------------------------------------------------------------------------------------------
   1.185 +void  BlenderModifier_Mirror :: DoIt(aiNode& out, ConversionData& conv_data,  const ElemBase& orig_modifier, 
   1.186 +	const Scene& /*in*/,
   1.187 +	const Object& orig_object ) 
   1.188 +{
   1.189 +	// hijacking the ABI, see the big note in BlenderModifierShowcase::ApplyModifiers()
   1.190 +	const MirrorModifierData& mir = static_cast<const MirrorModifierData&>(orig_modifier);
   1.191 +	ai_assert(mir.modifier.type == ModifierData::eModifierType_Mirror);
   1.192 +
   1.193 +	conv_data.meshes->reserve(conv_data.meshes->size() + out.mNumMeshes);
   1.194 +
   1.195 +	// XXX not entirely correct, mirroring on two axes results in 4 distinct objects in blender ...
   1.196 +
   1.197 +	// take all input meshes and clone them
   1.198 +	for (unsigned int i = 0; i < out.mNumMeshes; ++i) {
   1.199 +		aiMesh* mesh;
   1.200 +		SceneCombiner::Copy(&mesh,conv_data.meshes[out.mMeshes[i]]);
   1.201 +
   1.202 +		const float xs = mir.flag & MirrorModifierData::Flags_AXIS_X ? -1.f : 1.f;
   1.203 +		const float ys = mir.flag & MirrorModifierData::Flags_AXIS_Y ? -1.f : 1.f;
   1.204 +		const float zs = mir.flag & MirrorModifierData::Flags_AXIS_Z ? -1.f : 1.f;
   1.205 +
   1.206 +		if (mir.mirror_ob) {
   1.207 +			const aiVector3D center( mir.mirror_ob->obmat[3][0],mir.mirror_ob->obmat[3][1],mir.mirror_ob->obmat[3][2] );
   1.208 +			for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
   1.209 +				aiVector3D& v = mesh->mVertices[i];
   1.210 +		
   1.211 +				v.x = center.x + xs*(center.x - v.x);
   1.212 +				v.y = center.y + ys*(center.y - v.y);
   1.213 +				v.z = center.z + zs*(center.z - v.z);
   1.214 +			}
   1.215 +		}
   1.216 +		else {
   1.217 +			for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
   1.218 +				aiVector3D& v = mesh->mVertices[i];
   1.219 +				v.x *= xs;v.y *= ys;v.z *= zs;
   1.220 +			}
   1.221 +		}
   1.222 +
   1.223 +		if (mesh->mNormals) {
   1.224 +			for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
   1.225 +				aiVector3D& v = mesh->mNormals[i];
   1.226 +				v.x *= xs;v.y *= ys;v.z *= zs;
   1.227 +			}
   1.228 +		}
   1.229 +
   1.230 +		if (mesh->mTangents) {
   1.231 +			for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
   1.232 +				aiVector3D& v = mesh->mTangents[i];
   1.233 +				v.x *= xs;v.y *= ys;v.z *= zs;
   1.234 +			}
   1.235 +		}
   1.236 +
   1.237 +		if (mesh->mBitangents) {
   1.238 +			for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
   1.239 +				aiVector3D& v = mesh->mBitangents[i];
   1.240 +				v.x *= xs;v.y *= ys;v.z *= zs;
   1.241 +			}
   1.242 +		}
   1.243 +
   1.244 +		const float us = mir.flag & MirrorModifierData::Flags_MIRROR_U ? -1.f : 1.f;
   1.245 +		const float vs = mir.flag & MirrorModifierData::Flags_MIRROR_V ? -1.f : 1.f;
   1.246 +
   1.247 +		for (unsigned int n = 0; mesh->HasTextureCoords(n); ++n) {
   1.248 +			for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
   1.249 +				aiVector3D& v = mesh->mTextureCoords[n][i];
   1.250 +				v.x *= us;v.y *= vs;
   1.251 +			}
   1.252 +		}
   1.253 +
   1.254 +		conv_data.meshes->push_back(mesh);
   1.255 +	}
   1.256 +	unsigned int* nind = new unsigned int[out.mNumMeshes*2];
   1.257 +
   1.258 +	std::copy(out.mMeshes,out.mMeshes+out.mNumMeshes,nind);
   1.259 +	std::transform(out.mMeshes,out.mMeshes+out.mNumMeshes,nind+out.mNumMeshes,
   1.260 +		std::bind1st(std::plus< unsigned int >(),out.mNumMeshes));
   1.261 +
   1.262 +	delete[] out.mMeshes;
   1.263 +	out.mMeshes = nind;
   1.264 +	out.mNumMeshes *= 2;
   1.265 +
   1.266 +	ASSIMP_LOG_INFO_F("BlendModifier: Applied the `Mirror` modifier to `",
   1.267 +		orig_object.id.name,"`");
   1.268 +}
   1.269 +
   1.270 +
   1.271 +
   1.272 +
   1.273 +// ------------------------------------------------------------------------------------------------
   1.274 +bool BlenderModifier_Subdivision :: IsActive (const ModifierData& modin)
   1.275 +{
   1.276 +	return modin.type == ModifierData::eModifierType_Subsurf;
   1.277 +}
   1.278 +
   1.279 +// ------------------------------------------------------------------------------------------------
   1.280 +void  BlenderModifier_Subdivision :: DoIt(aiNode& out, ConversionData& conv_data,  const ElemBase& orig_modifier, 
   1.281 +	const Scene& /*in*/,
   1.282 +	const Object& orig_object ) 
   1.283 +{
   1.284 +	// hijacking the ABI, see the big note in BlenderModifierShowcase::ApplyModifiers()
   1.285 +	const SubsurfModifierData& mir = static_cast<const SubsurfModifierData&>(orig_modifier);
   1.286 +	ai_assert(mir.modifier.type == ModifierData::eModifierType_Subsurf);
   1.287 +
   1.288 +	Subdivider::Algorithm algo;
   1.289 +	switch (mir.subdivType) 
   1.290 +	{
   1.291 +	case SubsurfModifierData::TYPE_CatmullClarke:
   1.292 +		algo = Subdivider::CATMULL_CLARKE;
   1.293 +		break;
   1.294 +
   1.295 +	case SubsurfModifierData::TYPE_Simple:
   1.296 +		ASSIMP_LOG_WARN("BlendModifier: The `SIMPLE` subdivision algorithm is not currently implemented, using Catmull-Clarke");
   1.297 +		algo = Subdivider::CATMULL_CLARKE;
   1.298 +		break;
   1.299 +
   1.300 +	default:
   1.301 +		ASSIMP_LOG_WARN_F("BlendModifier: Unrecognized subdivision algorithm: ",mir.subdivType);
   1.302 +		return;
   1.303 +	};
   1.304 +
   1.305 +	boost::scoped_ptr<Subdivider> subd(Subdivider::Create(algo));
   1.306 +	ai_assert(subd);
   1.307 +
   1.308 +	aiMesh** const meshes = &conv_data.meshes[conv_data.meshes->size() - out.mNumMeshes];
   1.309 +	boost::scoped_array<aiMesh*> tempmeshes(new aiMesh*[out.mNumMeshes]());
   1.310 +
   1.311 +	subd->Subdivide(meshes,out.mNumMeshes,tempmeshes.get(),std::max( mir.renderLevels, mir.levels ),true);
   1.312 +	std::copy(tempmeshes.get(),tempmeshes.get()+out.mNumMeshes,meshes);
   1.313 +
   1.314 +	ASSIMP_LOG_INFO_F("BlendModifier: Applied the `Subdivision` modifier to `",
   1.315 +		orig_object.id.name,"`");
   1.316 +}
   1.317 +
   1.318 +#endif