rev |
line source |
nuclear@0
|
1 /*
|
nuclear@0
|
2 Open Asset Import Library (assimp)
|
nuclear@0
|
3 ----------------------------------------------------------------------
|
nuclear@0
|
4
|
nuclear@0
|
5 Copyright (c) 2006-2012, assimp team
|
nuclear@0
|
6 All rights reserved.
|
nuclear@0
|
7
|
nuclear@0
|
8 Redistribution and use of this software in source and binary forms,
|
nuclear@0
|
9 with or without modification, are permitted provided that the
|
nuclear@0
|
10 following conditions are met:
|
nuclear@0
|
11
|
nuclear@0
|
12 * Redistributions of source code must retain the above
|
nuclear@0
|
13 copyright notice, this list of conditions and the
|
nuclear@0
|
14 following disclaimer.
|
nuclear@0
|
15
|
nuclear@0
|
16 * Redistributions in binary form must reproduce the above
|
nuclear@0
|
17 copyright notice, this list of conditions and the
|
nuclear@0
|
18 following disclaimer in the documentation and/or other
|
nuclear@0
|
19 materials provided with the distribution.
|
nuclear@0
|
20
|
nuclear@0
|
21 * Neither the name of the assimp team, nor the names of its
|
nuclear@0
|
22 contributors may be used to endorse or promote products
|
nuclear@0
|
23 derived from this software without specific prior
|
nuclear@0
|
24 written permission of the assimp team.
|
nuclear@0
|
25
|
nuclear@0
|
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
nuclear@0
|
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
nuclear@0
|
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
nuclear@0
|
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
nuclear@0
|
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
nuclear@0
|
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
nuclear@0
|
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
nuclear@0
|
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
nuclear@0
|
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
nuclear@0
|
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
nuclear@0
|
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
nuclear@0
|
37
|
nuclear@0
|
38 ----------------------------------------------------------------------
|
nuclear@0
|
39 */
|
nuclear@0
|
40
|
nuclear@0
|
41 /** @file BlenderModifier.cpp
|
nuclear@0
|
42 * @brief Implementation of some blender modifiers (i.e subdivision, mirror).
|
nuclear@0
|
43 */
|
nuclear@0
|
44 #include "AssimpPCH.h"
|
nuclear@0
|
45
|
nuclear@0
|
46 #ifndef ASSIMP_BUILD_NO_BLEND_IMPORTER
|
nuclear@0
|
47 #include "BlenderModifier.h"
|
nuclear@0
|
48 #include "SceneCombiner.h"
|
nuclear@0
|
49 #include "Subdivision.h"
|
nuclear@0
|
50
|
nuclear@0
|
51 #include <functional>
|
nuclear@0
|
52
|
nuclear@0
|
53 using namespace Assimp;
|
nuclear@0
|
54 using namespace Assimp::Blender;
|
nuclear@0
|
55
|
nuclear@0
|
56 template <typename T> BlenderModifier* god() {
|
nuclear@0
|
57 return new T();
|
nuclear@0
|
58 }
|
nuclear@0
|
59
|
nuclear@0
|
60 // add all available modifiers here
|
nuclear@0
|
61 typedef BlenderModifier* (*fpCreateModifier)();
|
nuclear@0
|
62 static const fpCreateModifier creators[] = {
|
nuclear@0
|
63 &god<BlenderModifier_Mirror>,
|
nuclear@0
|
64 &god<BlenderModifier_Subdivision>,
|
nuclear@0
|
65
|
nuclear@0
|
66 NULL // sentinel
|
nuclear@0
|
67 };
|
nuclear@0
|
68
|
nuclear@0
|
69 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
70 // just testing out some new macros to simplify logging
|
nuclear@0
|
71 #define ASSIMP_LOG_WARN_F(string,...)\
|
nuclear@0
|
72 DefaultLogger::get()->warn((Formatter::format(string),__VA_ARGS__))
|
nuclear@0
|
73
|
nuclear@0
|
74 #define ASSIMP_LOG_ERROR_F(string,...)\
|
nuclear@0
|
75 DefaultLogger::get()->error((Formatter::format(string),__VA_ARGS__))
|
nuclear@0
|
76
|
nuclear@0
|
77 #define ASSIMP_LOG_DEBUG_F(string,...)\
|
nuclear@0
|
78 DefaultLogger::get()->debug((Formatter::format(string),__VA_ARGS__))
|
nuclear@0
|
79
|
nuclear@0
|
80 #define ASSIMP_LOG_INFO_F(string,...)\
|
nuclear@0
|
81 DefaultLogger::get()->info((Formatter::format(string),__VA_ARGS__))
|
nuclear@0
|
82
|
nuclear@0
|
83
|
nuclear@0
|
84 #define ASSIMP_LOG_WARN(string)\
|
nuclear@0
|
85 DefaultLogger::get()->warn(string)
|
nuclear@0
|
86
|
nuclear@0
|
87 #define ASSIMP_LOG_ERROR(string)\
|
nuclear@0
|
88 DefaultLogger::get()->error(string)
|
nuclear@0
|
89
|
nuclear@0
|
90 #define ASSIMP_LOG_DEBUG(string)\
|
nuclear@0
|
91 DefaultLogger::get()->debug(string)
|
nuclear@0
|
92
|
nuclear@0
|
93 #define ASSIMP_LOG_INFO(string)\
|
nuclear@0
|
94 DefaultLogger::get()->info(string)
|
nuclear@0
|
95
|
nuclear@0
|
96
|
nuclear@0
|
97 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
98 struct SharedModifierData : ElemBase
|
nuclear@0
|
99 {
|
nuclear@0
|
100 ModifierData modifier;
|
nuclear@0
|
101 };
|
nuclear@0
|
102
|
nuclear@0
|
103 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
104 void BlenderModifierShowcase::ApplyModifiers(aiNode& out, ConversionData& conv_data, const Scene& in, const Object& orig_object )
|
nuclear@0
|
105 {
|
nuclear@0
|
106 size_t cnt = 0u, ful = 0u;
|
nuclear@0
|
107
|
nuclear@0
|
108 // NOTE: this cast is potentially unsafe by design, so we need to perform type checks before
|
nuclear@0
|
109 // we're allowed to dereference the pointers without risking to crash. We might still be
|
nuclear@0
|
110 // invoking UB btw - we're assuming that the ModifierData member of the respective modifier
|
nuclear@0
|
111 // structures is at offset sizeof(vftable) with no padding.
|
nuclear@0
|
112 const SharedModifierData* cur = boost::static_pointer_cast<const SharedModifierData> ( orig_object.modifiers.first.get() );
|
nuclear@0
|
113 for (; cur; cur = boost::static_pointer_cast<const SharedModifierData> ( cur->modifier.next.get() ), ++ful) {
|
nuclear@0
|
114 ai_assert(cur->dna_type);
|
nuclear@0
|
115
|
nuclear@0
|
116 const Structure* s = conv_data.db.dna.Get( cur->dna_type );
|
nuclear@0
|
117 if (!s) {
|
nuclear@0
|
118 ASSIMP_LOG_WARN_F("BlendModifier: could not resolve DNA name: ",cur->dna_type);
|
nuclear@0
|
119 continue;
|
nuclear@0
|
120 }
|
nuclear@0
|
121
|
nuclear@0
|
122 // this is a common trait of all XXXMirrorData structures in BlenderDNA
|
nuclear@0
|
123 const Field* f = s->Get("modifier");
|
nuclear@0
|
124 if (!f || f->offset != 0) {
|
nuclear@0
|
125 ASSIMP_LOG_WARN("BlendModifier: expected a `modifier` member at offset 0");
|
nuclear@0
|
126 continue;
|
nuclear@0
|
127 }
|
nuclear@0
|
128
|
nuclear@0
|
129 s = conv_data.db.dna.Get( f->type );
|
nuclear@0
|
130 if (!s || s->name != "ModifierData") {
|
nuclear@0
|
131 ASSIMP_LOG_WARN("BlendModifier: expected a ModifierData structure as first member");
|
nuclear@0
|
132 continue;
|
nuclear@0
|
133 }
|
nuclear@0
|
134
|
nuclear@0
|
135 // now, we can be sure that we should be fine to dereference *cur* as
|
nuclear@0
|
136 // ModifierData (with the above note).
|
nuclear@0
|
137 const ModifierData& dat = cur->modifier;
|
nuclear@0
|
138
|
nuclear@0
|
139 const fpCreateModifier* curgod = creators;
|
nuclear@0
|
140 std::vector< BlenderModifier* >::iterator curmod = cached_modifiers->begin(), endmod = cached_modifiers->end();
|
nuclear@0
|
141
|
nuclear@0
|
142 for (;*curgod;++curgod,++curmod) { // allocate modifiers on the fly
|
nuclear@0
|
143 if (curmod == endmod) {
|
nuclear@0
|
144 cached_modifiers->push_back((*curgod)());
|
nuclear@0
|
145
|
nuclear@0
|
146 endmod = cached_modifiers->end();
|
nuclear@0
|
147 curmod = endmod-1;
|
nuclear@0
|
148 }
|
nuclear@0
|
149
|
nuclear@0
|
150 BlenderModifier* const modifier = *curmod;
|
nuclear@0
|
151 if(modifier->IsActive(dat)) {
|
nuclear@0
|
152 modifier->DoIt(out,conv_data,*boost::static_pointer_cast<const ElemBase>(cur),in,orig_object);
|
nuclear@0
|
153 cnt++;
|
nuclear@0
|
154
|
nuclear@0
|
155 curgod = NULL;
|
nuclear@0
|
156 break;
|
nuclear@0
|
157 }
|
nuclear@0
|
158 }
|
nuclear@0
|
159 if (curgod) {
|
nuclear@0
|
160 ASSIMP_LOG_WARN_F("Couldn't find a handler for modifier: ",dat.name);
|
nuclear@0
|
161 }
|
nuclear@0
|
162 }
|
nuclear@0
|
163
|
nuclear@0
|
164 // Even though we managed to resolve some or all of the modifiers on this
|
nuclear@0
|
165 // object, we still can't say whether our modifier implementations were
|
nuclear@0
|
166 // able to fully do their job.
|
nuclear@0
|
167 if (ful) {
|
nuclear@0
|
168 ASSIMP_LOG_DEBUG_F("BlendModifier: found handlers for ",cnt," of ",ful," modifiers on `",orig_object.id.name,
|
nuclear@0
|
169 "`, check log messages above for errors");
|
nuclear@0
|
170 }
|
nuclear@0
|
171 }
|
nuclear@0
|
172
|
nuclear@0
|
173
|
nuclear@0
|
174
|
nuclear@0
|
175 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
176 bool BlenderModifier_Mirror :: IsActive (const ModifierData& modin)
|
nuclear@0
|
177 {
|
nuclear@0
|
178 return modin.type == ModifierData::eModifierType_Mirror;
|
nuclear@0
|
179 }
|
nuclear@0
|
180
|
nuclear@0
|
181 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
182 void BlenderModifier_Mirror :: DoIt(aiNode& out, ConversionData& conv_data, const ElemBase& orig_modifier,
|
nuclear@0
|
183 const Scene& /*in*/,
|
nuclear@0
|
184 const Object& orig_object )
|
nuclear@0
|
185 {
|
nuclear@0
|
186 // hijacking the ABI, see the big note in BlenderModifierShowcase::ApplyModifiers()
|
nuclear@0
|
187 const MirrorModifierData& mir = static_cast<const MirrorModifierData&>(orig_modifier);
|
nuclear@0
|
188 ai_assert(mir.modifier.type == ModifierData::eModifierType_Mirror);
|
nuclear@0
|
189
|
nuclear@0
|
190 conv_data.meshes->reserve(conv_data.meshes->size() + out.mNumMeshes);
|
nuclear@0
|
191
|
nuclear@0
|
192 // XXX not entirely correct, mirroring on two axes results in 4 distinct objects in blender ...
|
nuclear@0
|
193
|
nuclear@0
|
194 // take all input meshes and clone them
|
nuclear@0
|
195 for (unsigned int i = 0; i < out.mNumMeshes; ++i) {
|
nuclear@0
|
196 aiMesh* mesh;
|
nuclear@0
|
197 SceneCombiner::Copy(&mesh,conv_data.meshes[out.mMeshes[i]]);
|
nuclear@0
|
198
|
nuclear@0
|
199 const float xs = mir.flag & MirrorModifierData::Flags_AXIS_X ? -1.f : 1.f;
|
nuclear@0
|
200 const float ys = mir.flag & MirrorModifierData::Flags_AXIS_Y ? -1.f : 1.f;
|
nuclear@0
|
201 const float zs = mir.flag & MirrorModifierData::Flags_AXIS_Z ? -1.f : 1.f;
|
nuclear@0
|
202
|
nuclear@0
|
203 if (mir.mirror_ob) {
|
nuclear@0
|
204 const aiVector3D center( mir.mirror_ob->obmat[3][0],mir.mirror_ob->obmat[3][1],mir.mirror_ob->obmat[3][2] );
|
nuclear@0
|
205 for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
|
nuclear@0
|
206 aiVector3D& v = mesh->mVertices[i];
|
nuclear@0
|
207
|
nuclear@0
|
208 v.x = center.x + xs*(center.x - v.x);
|
nuclear@0
|
209 v.y = center.y + ys*(center.y - v.y);
|
nuclear@0
|
210 v.z = center.z + zs*(center.z - v.z);
|
nuclear@0
|
211 }
|
nuclear@0
|
212 }
|
nuclear@0
|
213 else {
|
nuclear@0
|
214 for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
|
nuclear@0
|
215 aiVector3D& v = mesh->mVertices[i];
|
nuclear@0
|
216 v.x *= xs;v.y *= ys;v.z *= zs;
|
nuclear@0
|
217 }
|
nuclear@0
|
218 }
|
nuclear@0
|
219
|
nuclear@0
|
220 if (mesh->mNormals) {
|
nuclear@0
|
221 for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
|
nuclear@0
|
222 aiVector3D& v = mesh->mNormals[i];
|
nuclear@0
|
223 v.x *= xs;v.y *= ys;v.z *= zs;
|
nuclear@0
|
224 }
|
nuclear@0
|
225 }
|
nuclear@0
|
226
|
nuclear@0
|
227 if (mesh->mTangents) {
|
nuclear@0
|
228 for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
|
nuclear@0
|
229 aiVector3D& v = mesh->mTangents[i];
|
nuclear@0
|
230 v.x *= xs;v.y *= ys;v.z *= zs;
|
nuclear@0
|
231 }
|
nuclear@0
|
232 }
|
nuclear@0
|
233
|
nuclear@0
|
234 if (mesh->mBitangents) {
|
nuclear@0
|
235 for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
|
nuclear@0
|
236 aiVector3D& v = mesh->mBitangents[i];
|
nuclear@0
|
237 v.x *= xs;v.y *= ys;v.z *= zs;
|
nuclear@0
|
238 }
|
nuclear@0
|
239 }
|
nuclear@0
|
240
|
nuclear@0
|
241 const float us = mir.flag & MirrorModifierData::Flags_MIRROR_U ? -1.f : 1.f;
|
nuclear@0
|
242 const float vs = mir.flag & MirrorModifierData::Flags_MIRROR_V ? -1.f : 1.f;
|
nuclear@0
|
243
|
nuclear@0
|
244 for (unsigned int n = 0; mesh->HasTextureCoords(n); ++n) {
|
nuclear@0
|
245 for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
|
nuclear@0
|
246 aiVector3D& v = mesh->mTextureCoords[n][i];
|
nuclear@0
|
247 v.x *= us;v.y *= vs;
|
nuclear@0
|
248 }
|
nuclear@0
|
249 }
|
nuclear@0
|
250
|
nuclear@0
|
251 conv_data.meshes->push_back(mesh);
|
nuclear@0
|
252 }
|
nuclear@0
|
253 unsigned int* nind = new unsigned int[out.mNumMeshes*2];
|
nuclear@0
|
254
|
nuclear@0
|
255 std::copy(out.mMeshes,out.mMeshes+out.mNumMeshes,nind);
|
nuclear@0
|
256 std::transform(out.mMeshes,out.mMeshes+out.mNumMeshes,nind+out.mNumMeshes,
|
nuclear@0
|
257 std::bind1st(std::plus< unsigned int >(),out.mNumMeshes));
|
nuclear@0
|
258
|
nuclear@0
|
259 delete[] out.mMeshes;
|
nuclear@0
|
260 out.mMeshes = nind;
|
nuclear@0
|
261 out.mNumMeshes *= 2;
|
nuclear@0
|
262
|
nuclear@0
|
263 ASSIMP_LOG_INFO_F("BlendModifier: Applied the `Mirror` modifier to `",
|
nuclear@0
|
264 orig_object.id.name,"`");
|
nuclear@0
|
265 }
|
nuclear@0
|
266
|
nuclear@0
|
267
|
nuclear@0
|
268
|
nuclear@0
|
269
|
nuclear@0
|
270 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
271 bool BlenderModifier_Subdivision :: IsActive (const ModifierData& modin)
|
nuclear@0
|
272 {
|
nuclear@0
|
273 return modin.type == ModifierData::eModifierType_Subsurf;
|
nuclear@0
|
274 }
|
nuclear@0
|
275
|
nuclear@0
|
276 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
277 void BlenderModifier_Subdivision :: DoIt(aiNode& out, ConversionData& conv_data, const ElemBase& orig_modifier,
|
nuclear@0
|
278 const Scene& /*in*/,
|
nuclear@0
|
279 const Object& orig_object )
|
nuclear@0
|
280 {
|
nuclear@0
|
281 // hijacking the ABI, see the big note in BlenderModifierShowcase::ApplyModifiers()
|
nuclear@0
|
282 const SubsurfModifierData& mir = static_cast<const SubsurfModifierData&>(orig_modifier);
|
nuclear@0
|
283 ai_assert(mir.modifier.type == ModifierData::eModifierType_Subsurf);
|
nuclear@0
|
284
|
nuclear@0
|
285 Subdivider::Algorithm algo;
|
nuclear@0
|
286 switch (mir.subdivType)
|
nuclear@0
|
287 {
|
nuclear@0
|
288 case SubsurfModifierData::TYPE_CatmullClarke:
|
nuclear@0
|
289 algo = Subdivider::CATMULL_CLARKE;
|
nuclear@0
|
290 break;
|
nuclear@0
|
291
|
nuclear@0
|
292 case SubsurfModifierData::TYPE_Simple:
|
nuclear@0
|
293 ASSIMP_LOG_WARN("BlendModifier: The `SIMPLE` subdivision algorithm is not currently implemented, using Catmull-Clarke");
|
nuclear@0
|
294 algo = Subdivider::CATMULL_CLARKE;
|
nuclear@0
|
295 break;
|
nuclear@0
|
296
|
nuclear@0
|
297 default:
|
nuclear@0
|
298 ASSIMP_LOG_WARN_F("BlendModifier: Unrecognized subdivision algorithm: ",mir.subdivType);
|
nuclear@0
|
299 return;
|
nuclear@0
|
300 };
|
nuclear@0
|
301
|
nuclear@0
|
302 boost::scoped_ptr<Subdivider> subd(Subdivider::Create(algo));
|
nuclear@0
|
303 ai_assert(subd);
|
nuclear@0
|
304
|
nuclear@0
|
305 aiMesh** const meshes = &conv_data.meshes[conv_data.meshes->size() - out.mNumMeshes];
|
nuclear@0
|
306 boost::scoped_array<aiMesh*> tempmeshes(new aiMesh*[out.mNumMeshes]());
|
nuclear@0
|
307
|
nuclear@0
|
308 subd->Subdivide(meshes,out.mNumMeshes,tempmeshes.get(),std::max( mir.renderLevels, mir.levels ),true);
|
nuclear@0
|
309 std::copy(tempmeshes.get(),tempmeshes.get()+out.mNumMeshes,meshes);
|
nuclear@0
|
310
|
nuclear@0
|
311 ASSIMP_LOG_INFO_F("BlendModifier: Applied the `Subdivision` modifier to `",
|
nuclear@0
|
312 orig_object.id.name,"`");
|
nuclear@0
|
313 }
|
nuclear@0
|
314
|
nuclear@0
|
315 #endif
|