assimp/code/AssetLib/3DS/3DSConverter.cpp

811 lines
32 KiB
C++
Raw Normal View History

/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
2024-02-23 21:30:05 +00:00
Copyright (c) 2006-2024, assimp team
2018-01-28 18:42:05 +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 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
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
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
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
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
*/
/** @file Implementation of the 3ds importer class */
#ifndef ASSIMP_BUILD_NO_3DS_IMPORTER
// internal headers
#include "3DSLoader.h"
#include "Common/TargetAnimation.h"
#include <assimp/StringComparison.h>
2016-06-06 20:04:29 +00:00
#include <assimp/scene.h>
#include <assimp/DefaultLogger.hpp>
#include <cctype>
#include <memory>
2023-11-11 20:13:47 +00:00
namespace Assimp {
2023-11-11 20:13:47 +00:00
static constexpr unsigned int NotSet = 0xcdcdcdcd;
// ------------------------------------------------------------------------------------------------
// Setup final material indices, generae a default material if necessary
void Discreet3DSImporter::ReplaceDefaultMaterial() {
2015-05-19 03:57:13 +00:00
// Try to find an existing material that matches the
// typical default material setting:
// - no textures
// - diffuse color (in grey!)
// NOTE: This is here to workaround the fact that some
// exporters are writing a default material, too.
unsigned int idx(NotSet);
for (unsigned int i = 0; i < mScene->mMaterials.size(); ++i) {
std::string s = mScene->mMaterials[i].mName;
2023-11-11 20:13:47 +00:00
for (char &it : s) {
it = static_cast<char>(::tolower(static_cast<unsigned char>(it)));
}
2015-05-19 03:57:13 +00:00
if (std::string::npos == s.find("default")) continue;
2015-05-19 03:57:13 +00:00
if (mScene->mMaterials[i].mDiffuse.r !=
mScene->mMaterials[i].mDiffuse.g ||
mScene->mMaterials[i].mDiffuse.r !=
mScene->mMaterials[i].mDiffuse.b) continue;
if (ContainsTextures(i)) {
2015-05-19 03:57:13 +00:00
continue;
}
idx = i;
}
if (NotSet == idx) {
idx = (unsigned int)mScene->mMaterials.size();
}
2015-05-19 03:57:13 +00:00
// now iterate through all meshes and through all faces and
// find all faces that are using the default material
unsigned int cnt = 0;
for (std::vector<D3DS::Mesh>::iterator
i = mScene->mMeshes.begin();
i != mScene->mMeshes.end(); ++i) {
2015-05-19 03:57:13 +00:00
for (std::vector<unsigned int>::iterator
a = (*i).mFaceMaterials.begin();
a != (*i).mFaceMaterials.end(); ++a) {
2015-05-19 03:57:13 +00:00
// NOTE: The additional check seems to be necessary,
// some exporters seem to generate invalid data here
if (0xcdcdcdcd == (*a)) {
2015-05-19 03:57:13 +00:00
(*a) = idx;
++cnt;
} else if ((*a) >= mScene->mMaterials.size()) {
2015-05-19 03:57:13 +00:00
(*a) = idx;
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_WARN("Material index overflow in 3DS file. Using default material");
2015-05-19 03:57:13 +00:00
++cnt;
}
}
}
if (cnt && idx == mScene->mMaterials.size()) {
2015-05-19 03:57:13 +00:00
// We need to create our own default material
D3DS::Material sMat("%%%DEFAULT");
sMat.mDiffuse = aiColor3D(0.3f, 0.3f, 0.3f);
2015-05-19 03:57:13 +00:00
mScene->mMaterials.push_back(sMat);
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_INFO("3DS: Generating default material");
2015-05-19 03:57:13 +00:00
}
}
// ------------------------------------------------------------------------------------------------
// Check whether all indices are valid. Otherwise we'd crash before the validation step is reached
void Discreet3DSImporter::CheckIndices(D3DS::Mesh &sMesh) {
for (std::vector<D3DS::Face>::iterator i = sMesh.mFaces.begin(); i != sMesh.mFaces.end(); ++i) {
2015-05-19 03:57:13 +00:00
// check whether all indices are in range
for (unsigned int a = 0; a < 3; ++a) {
if ((*i).mIndices[a] >= sMesh.mPositions.size()) {
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_WARN("3DS: Vertex index overflow)");
(*i).mIndices[a] = (uint32_t)sMesh.mPositions.size() - 1;
2015-05-19 03:57:13 +00:00
}
if (!sMesh.mTexCoords.empty() && (*i).mIndices[a] >= sMesh.mTexCoords.size()) {
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_WARN("3DS: Texture coordinate index overflow)");
(*i).mIndices[a] = (uint32_t)sMesh.mTexCoords.size() - 1;
2015-05-19 03:57:13 +00:00
}
}
}
}
// ------------------------------------------------------------------------------------------------
// Generate out unique verbose format representation
void Discreet3DSImporter::MakeUnique(D3DS::Mesh &sMesh) {
2015-05-19 03:57:13 +00:00
// TODO: really necessary? I don't think. Just a waste of memory and time
// to do it now in a separate buffer.
// Allocate output storage
std::vector<aiVector3D> vNew(sMesh.mFaces.size() * 3);
2015-05-19 03:57:13 +00:00
std::vector<aiVector3D> vNew2;
if (sMesh.mTexCoords.size())
vNew2.resize(sMesh.mFaces.size() * 3);
for (unsigned int i = 0, base = 0; i < sMesh.mFaces.size(); ++i) {
D3DS::Face &face = sMesh.mFaces[i];
2015-05-19 03:57:13 +00:00
// Positions
for (unsigned int a = 0; a < 3; ++a, ++base) {
2015-05-19 03:57:13 +00:00
vNew[base] = sMesh.mPositions[face.mIndices[a]];
if (sMesh.mTexCoords.size())
vNew2[base] = sMesh.mTexCoords[face.mIndices[a]];
face.mIndices[a] = base;
}
}
sMesh.mPositions = vNew;
sMesh.mTexCoords = vNew2;
}
// ------------------------------------------------------------------------------------------------
// Convert a 3DS texture to texture keys in an aiMaterial
void CopyTexture(aiMaterial &mat, D3DS::Texture &texture, aiTextureType type) {
2015-05-19 03:57:13 +00:00
// Setup the texture name
aiString tex;
tex.Set(texture.mMapName);
mat.AddProperty(&tex, AI_MATKEY_TEXTURE(type, 0));
2015-05-19 03:57:13 +00:00
// Setup the texture blend factor
if (is_not_qnan(texture.mTextureBlend))
mat.AddProperty<ai_real>(&texture.mTextureBlend, 1, AI_MATKEY_TEXBLEND(type, 0));
2015-05-19 03:57:13 +00:00
// Setup the texture mapping mode
2018-06-01 09:28:33 +00:00
int mapMode = static_cast<int>(texture.mMapMode);
mat.AddProperty<int>(&mapMode, 1, AI_MATKEY_MAPPINGMODE_U(type, 0));
mat.AddProperty<int>(&mapMode, 1, AI_MATKEY_MAPPINGMODE_V(type, 0));
2015-05-19 03:57:13 +00:00
// Mirroring - double the scaling values
// FIXME: this is not really correct ...
if (texture.mMapMode == aiTextureMapMode_Mirror) {
texture.mScaleU *= 2.0;
texture.mScaleV *= 2.0;
texture.mOffsetU /= 2.0;
texture.mOffsetV /= 2.0;
2015-05-19 03:57:13 +00:00
}
// Setup texture UV transformations
mat.AddProperty<ai_real>(&texture.mOffsetU, 5, AI_MATKEY_UVTRANSFORM(type, 0));
}
// ------------------------------------------------------------------------------------------------
// Convert a 3DS material to an aiMaterial
void Discreet3DSImporter::ConvertMaterial(D3DS::Material &oldMat,
aiMaterial &mat) {
2015-05-19 03:57:13 +00:00
// NOTE: Pass the background image to the viewer by bypassing the
// material system. This is an evil hack, never do it again!
if (0 != mBackgroundImage.length() && bHasBG) {
2015-05-19 03:57:13 +00:00
aiString tex;
tex.Set(mBackgroundImage);
mat.AddProperty(&tex, AI_MATKEY_GLOBAL_BACKGROUND_IMAGE);
2015-05-19 03:57:13 +00:00
// Be sure this is only done for the first material
mBackgroundImage = std::string();
2015-05-19 03:57:13 +00:00
}
// At first add the base ambient color of the scene to the material
oldMat.mAmbient.r += mClrAmbient.r;
oldMat.mAmbient.g += mClrAmbient.g;
oldMat.mAmbient.b += mClrAmbient.b;
aiString name;
name.Set(oldMat.mName);
mat.AddProperty(&name, AI_MATKEY_NAME);
2015-05-19 03:57:13 +00:00
// Material colors
mat.AddProperty(&oldMat.mAmbient, 1, AI_MATKEY_COLOR_AMBIENT);
mat.AddProperty(&oldMat.mDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
mat.AddProperty(&oldMat.mSpecular, 1, AI_MATKEY_COLOR_SPECULAR);
mat.AddProperty(&oldMat.mEmissive, 1, AI_MATKEY_COLOR_EMISSIVE);
2015-05-19 03:57:13 +00:00
// Phong shininess and shininess strength
if (D3DS::Discreet3DS::Phong == oldMat.mShading ||
D3DS::Discreet3DS::Metal == oldMat.mShading) {
if (!oldMat.mSpecularExponent || !oldMat.mShininessStrength) {
2015-05-19 03:57:13 +00:00
oldMat.mShading = D3DS::Discreet3DS::Gouraud;
} else {
mat.AddProperty(&oldMat.mSpecularExponent, 1, AI_MATKEY_SHININESS);
mat.AddProperty(&oldMat.mShininessStrength, 1, AI_MATKEY_SHININESS_STRENGTH);
2015-05-19 03:57:13 +00:00
}
}
// Opacity
mat.AddProperty<ai_real>(&oldMat.mTransparency, 1, AI_MATKEY_OPACITY);
2015-05-19 03:57:13 +00:00
// Bump height scaling
mat.AddProperty<ai_real>(&oldMat.mBumpHeight, 1, AI_MATKEY_BUMPSCALING);
2015-05-19 03:57:13 +00:00
// Two sided rendering?
if (oldMat.mTwoSided) {
2015-05-19 03:57:13 +00:00
int i = 1;
mat.AddProperty<int>(&i, 1, AI_MATKEY_TWOSIDED);
2015-05-19 03:57:13 +00:00
}
// Shading mode
aiShadingMode eShading = aiShadingMode_NoShading;
switch (oldMat.mShading) {
case D3DS::Discreet3DS::Flat:
eShading = aiShadingMode_Flat;
break;
// I don't know what "Wire" shading should be,
// assume it is simple lambertian diffuse shading
case D3DS::Discreet3DS::Wire: {
// Set the wireframe flag
unsigned int iWire = 1;
mat.AddProperty<int>((int *)&iWire, 1, AI_MATKEY_ENABLE_WIREFRAME);
}
2023-11-11 20:13:47 +00:00
[[fallthrough]];
case D3DS::Discreet3DS::Gouraud:
eShading = aiShadingMode_Gouraud;
break;
// assume cook-torrance shading for metals.
case D3DS::Discreet3DS::Phong:
eShading = aiShadingMode_Phong;
break;
case D3DS::Discreet3DS::Metal:
eShading = aiShadingMode_CookTorrance;
break;
// FIX to workaround a warning with GCC 4 who complained
// about a missing case Blinn: here - Blinn isn't a valid
// value in the 3DS Loader, it is just needed for ASE
case D3DS::Discreet3DS::Blinn:
eShading = aiShadingMode_Blinn;
break;
2015-05-19 03:57:13 +00:00
}
2018-06-01 09:28:33 +00:00
int eShading_ = static_cast<int>(eShading);
mat.AddProperty<int>(&eShading_, 1, AI_MATKEY_SHADING_MODEL);
2015-05-19 03:57:13 +00:00
// DIFFUSE texture
if (oldMat.sTexDiffuse.mMapName.length() > 0)
CopyTexture(mat, oldMat.sTexDiffuse, aiTextureType_DIFFUSE);
2015-05-19 03:57:13 +00:00
// SPECULAR texture
if (oldMat.sTexSpecular.mMapName.length() > 0)
CopyTexture(mat, oldMat.sTexSpecular, aiTextureType_SPECULAR);
2015-05-19 03:57:13 +00:00
// OPACITY texture
if (oldMat.sTexOpacity.mMapName.length() > 0)
CopyTexture(mat, oldMat.sTexOpacity, aiTextureType_OPACITY);
2015-05-19 03:57:13 +00:00
// EMISSIVE texture
if (oldMat.sTexEmissive.mMapName.length() > 0)
CopyTexture(mat, oldMat.sTexEmissive, aiTextureType_EMISSIVE);
2015-05-19 03:57:13 +00:00
// BUMP texture
if (oldMat.sTexBump.mMapName.length() > 0)
CopyTexture(mat, oldMat.sTexBump, aiTextureType_HEIGHT);
2015-05-19 03:57:13 +00:00
// SHININESS texture
if (oldMat.sTexShininess.mMapName.length() > 0)
CopyTexture(mat, oldMat.sTexShininess, aiTextureType_SHININESS);
2015-05-19 03:57:13 +00:00
// REFLECTION texture
if (oldMat.sTexReflective.mMapName.length() > 0)
CopyTexture(mat, oldMat.sTexReflective, aiTextureType_REFLECTION);
2015-05-19 03:57:13 +00:00
// Store the name of the material itself, too
if (oldMat.mName.length()) {
2015-05-19 03:57:13 +00:00
aiString tex;
tex.Set(oldMat.mName);
mat.AddProperty(&tex, AI_MATKEY_NAME);
2015-05-19 03:57:13 +00:00
}
}
// ------------------------------------------------------------------------------------------------
// Split meshes by their materials and generate output aiMesh'es
void Discreet3DSImporter::ConvertMeshes(aiScene *pcOut) {
std::vector<aiMesh *> avOutMeshes;
2015-05-19 03:57:13 +00:00
avOutMeshes.reserve(mScene->mMeshes.size() * 2);
unsigned int iFaceCnt = 0, num = 0;
2015-05-19 03:57:13 +00:00
aiString name;
// we need to split all meshes by their materials
for (std::vector<D3DS::Mesh>::iterator i = mScene->mMeshes.begin(); i != mScene->mMeshes.end(); ++i) {
std::unique_ptr<std::vector<unsigned int>[]> aiSplit(new std::vector<unsigned int>[mScene->mMaterials.size()]);
2015-05-19 03:57:13 +00:00
name.length = ASSIMP_itoa10(name.data, num++);
2015-05-19 03:57:13 +00:00
unsigned int iNum = 0;
for (std::vector<unsigned int>::const_iterator a = (*i).mFaceMaterials.begin();
a != (*i).mFaceMaterials.end(); ++a, ++iNum) {
2015-05-19 03:57:13 +00:00
aiSplit[*a].push_back(iNum);
}
// now generate submeshes
for (unsigned int p = 0; p < mScene->mMaterials.size(); ++p) {
2015-05-19 03:57:13 +00:00
if (aiSplit[p].empty()) {
continue;
}
aiMesh *meshOut = new aiMesh();
2015-05-19 03:57:13 +00:00
meshOut->mName = name;
meshOut->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
// be sure to setup the correct material index
meshOut->mMaterialIndex = p;
// use the color data as temporary storage
meshOut->mColors[0] = (aiColor4D *)(&*i);
2015-05-19 03:57:13 +00:00
avOutMeshes.push_back(meshOut);
// convert vertices
meshOut->mNumFaces = (unsigned int)aiSplit[p].size();
meshOut->mNumVertices = meshOut->mNumFaces * 3;
2015-05-19 03:57:13 +00:00
// allocate enough storage for faces
meshOut->mFaces = new aiFace[meshOut->mNumFaces];
iFaceCnt += meshOut->mNumFaces;
meshOut->mVertices = new aiVector3D[meshOut->mNumVertices];
meshOut->mNormals = new aiVector3D[meshOut->mNumVertices];
if ((*i).mTexCoords.size()) {
2015-05-19 03:57:13 +00:00
meshOut->mTextureCoords[0] = new aiVector3D[meshOut->mNumVertices];
}
for (unsigned int q = 0, base = 0; q < aiSplit[p].size(); ++q) {
2015-05-19 03:57:13 +00:00
unsigned int index = aiSplit[p][q];
aiFace &face = meshOut->mFaces[q];
2015-05-19 03:57:13 +00:00
face.mIndices = new unsigned int[3];
face.mNumIndices = 3;
for (unsigned int a = 0; a < 3; ++a, ++base) {
2015-05-19 03:57:13 +00:00
unsigned int idx = (*i).mFaces[index].mIndices[a];
meshOut->mVertices[base] = (*i).mPositions[idx];
meshOut->mNormals[base] = (*i).mNormals[idx];
2015-05-19 03:57:13 +00:00
if ((*i).mTexCoords.size())
meshOut->mTextureCoords[0][base] = (*i).mTexCoords[idx];
face.mIndices[a] = base;
}
}
}
}
// Copy them to the output array
pcOut->mNumMeshes = (unsigned int)avOutMeshes.size();
pcOut->mMeshes = new aiMesh *[pcOut->mNumMeshes]();
for (unsigned int a = 0; a < pcOut->mNumMeshes; ++a) {
2015-05-19 03:57:13 +00:00
pcOut->mMeshes[a] = avOutMeshes[a];
}
// We should have at least one face here
if (!iFaceCnt) {
throw DeadlyImportError("No faces loaded. The mesh is empty");
}
}
// ------------------------------------------------------------------------------------------------
// Add a node to the scenegraph and setup its final transformation
void Discreet3DSImporter::AddNodeToGraph(aiScene *pcSOut, aiNode *pcOut,
D3DS::Node *pcIn, aiMatrix4x4 & /*absTrafo*/) {
2015-05-19 03:57:13 +00:00
std::vector<unsigned int> iArray;
iArray.reserve(3);
aiMatrix4x4 abs;
// Find all meshes with the same name as the node
for (unsigned int a = 0; a < pcSOut->mNumMeshes; ++a) {
const D3DS::Mesh *pcMesh = (const D3DS::Mesh *)pcSOut->mMeshes[a]->mColors[0];
ai_assert(nullptr != pcMesh);
2015-05-19 03:57:13 +00:00
if (pcIn->mName == pcMesh->mName)
iArray.push_back(a);
}
if (!iArray.empty()) {
2015-05-19 03:57:13 +00:00
// The matrix should be identical for all meshes with the
// same name. It HAS to be identical for all meshes .....
D3DS::Mesh *imesh = ((D3DS::Mesh *)pcSOut->mMeshes[iArray[0]]->mColors[0]);
2015-05-19 03:57:13 +00:00
// Compute the inverse of the transformation matrix to move the
// vertices back to their relative and local space
aiMatrix4x4 mInv = imesh->mMat, mInvTransposed = imesh->mMat;
mInv.Inverse();
mInvTransposed.Transpose();
2015-05-19 03:57:13 +00:00
aiVector3D pivot = pcIn->vPivot;
pcOut->mNumMeshes = (unsigned int)iArray.size();
pcOut->mMeshes = new unsigned int[iArray.size()];
for (unsigned int i = 0; i < iArray.size(); ++i) {
2015-05-19 03:57:13 +00:00
const unsigned int iIndex = iArray[i];
aiMesh *const mesh = pcSOut->mMeshes[iIndex];
2015-05-19 03:57:13 +00:00
if (mesh->mColors[1] == nullptr) {
2015-05-19 03:57:13 +00:00
// Transform the vertices back into their local space
// fixme: consider computing normals after this, so we don't need to transform them
const aiVector3D *const pvEnd = mesh->mVertices + mesh->mNumVertices;
aiVector3D *pvCurrent = mesh->mVertices, *t2 = mesh->mNormals;
2015-05-19 03:57:13 +00:00
for (; pvCurrent != pvEnd; ++pvCurrent, ++t2) {
*pvCurrent = mInv * (*pvCurrent);
*t2 = mInvTransposed * (*t2);
}
// Handle negative transformation matrix determinant -> invert vertex x
if (imesh->mMat.Determinant() < 0.0f) {
2015-05-19 03:57:13 +00:00
/* we *must* have normals */
for (pvCurrent = mesh->mVertices, t2 = mesh->mNormals; pvCurrent != pvEnd; ++pvCurrent, ++t2) {
pvCurrent->x *= -1.f;
t2->x *= -1.f;
}
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_INFO("3DS: Flipping mesh X-Axis");
2015-05-19 03:57:13 +00:00
}
// Handle pivot point
if (pivot.x || pivot.y || pivot.z) {
for (pvCurrent = mesh->mVertices; pvCurrent != pvEnd; ++pvCurrent) {
2015-05-19 03:57:13 +00:00
*pvCurrent -= pivot;
}
}
mesh->mColors[1] = (aiColor4D *)1;
} else
mesh->mColors[1] = (aiColor4D *)1;
2015-05-19 03:57:13 +00:00
// Setup the mesh index
pcOut->mMeshes[i] = iIndex;
}
}
// Setup the name of the node
// First instance keeps its name otherwise something might break, all others will be postfixed with their instance number
if (pcIn->mInstanceNumber > 1) {
2015-05-19 03:57:13 +00:00
char tmp[12];
ASSIMP_itoa10(tmp, pcIn->mInstanceNumber);
std::string tempStr = pcIn->mName + "_inst_";
tempStr += tmp;
pcOut->mName.Set(tempStr);
} else
2015-05-19 03:57:13 +00:00
pcOut->mName.Set(pcIn->mName);
// Now build the transformation matrix of the node
// ROTATION
if (pcIn->aRotationKeys.size()) {
2015-05-19 03:57:13 +00:00
// FIX to get to Assimp's quaternion conventions
for (std::vector<aiQuatKey>::iterator it = pcIn->aRotationKeys.begin(); it != pcIn->aRotationKeys.end(); ++it) {
(*it).mValue.w *= -1.f;
}
pcOut->mTransformation = aiMatrix4x4(pcIn->aRotationKeys[0].mValue.GetMatrix());
} else if (pcIn->aCameraRollKeys.size()) {
aiMatrix4x4::RotationZ(AI_DEG_TO_RAD(-pcIn->aCameraRollKeys[0].mValue),
pcOut->mTransformation);
2015-05-19 03:57:13 +00:00
}
// SCALING
aiMatrix4x4 &m = pcOut->mTransformation;
if (pcIn->aScalingKeys.size()) {
const aiVector3D &v = pcIn->aScalingKeys[0].mValue;
m.a1 *= v.x;
m.b1 *= v.x;
m.c1 *= v.x;
m.a2 *= v.y;
m.b2 *= v.y;
m.c2 *= v.y;
m.a3 *= v.z;
m.b3 *= v.z;
m.c3 *= v.z;
2015-05-19 03:57:13 +00:00
}
// TRANSLATION
if (pcIn->aPositionKeys.size()) {
const aiVector3D &v = pcIn->aPositionKeys[0].mValue;
2015-05-19 03:57:13 +00:00
m.a4 += v.x;
m.b4 += v.y;
m.c4 += v.z;
}
// Generate animation channels for the node
if (pcIn->aPositionKeys.size() > 1 || pcIn->aRotationKeys.size() > 1 ||
pcIn->aScalingKeys.size() > 1 || pcIn->aCameraRollKeys.size() > 1 ||
pcIn->aTargetPositionKeys.size() > 1) {
aiAnimation *anim = pcSOut->mAnimations[0];
2018-04-19 14:48:43 +00:00
ai_assert(nullptr != anim);
2015-05-19 03:57:13 +00:00
if (pcIn->aCameraRollKeys.size() > 1) {
2020-05-15 15:59:42 +00:00
ASSIMP_LOG_VERBOSE_DEBUG("3DS: Converting camera roll track ...");
2015-05-19 03:57:13 +00:00
// Camera roll keys - in fact they're just rotations
// around the camera's z axis. The angles are given
// in degrees (and they're clockwise).
pcIn->aRotationKeys.resize(pcIn->aCameraRollKeys.size());
for (unsigned int i = 0; i < pcIn->aCameraRollKeys.size(); ++i) {
aiQuatKey &q = pcIn->aRotationKeys[i];
aiFloatKey &f = pcIn->aCameraRollKeys[i];
2015-05-19 03:57:13 +00:00
q.mTime = f.mTime;
2015-05-19 03:57:13 +00:00
// FIX to get to Assimp quaternion conventions
q.mValue = aiQuaternion(0.f, 0.f, AI_DEG_TO_RAD(/*-*/ f.mValue));
2015-05-19 03:57:13 +00:00
}
}
#if 0
2015-05-19 03:57:13 +00:00
if (pcIn->aTargetPositionKeys.size() > 1)
{
2020-05-15 15:59:42 +00:00
ASSIMP_LOG_VERBOSE_DEBUG("3DS: Converting target track ...");
2015-05-19 03:57:13 +00:00
// Camera or spot light - need to convert the separate
// target position channel to our representation
TargetAnimationHelper helper;
if (pcIn->aPositionKeys.empty())
{
// We can just pass zero here ...
helper.SetFixedMainAnimationChannel(aiVector3D());
}
else helper.SetMainAnimationChannel(&pcIn->aPositionKeys);
helper.SetTargetAnimationChannel(&pcIn->aTargetPositionKeys);
// Do the conversion
std::vector<aiVectorKey> distanceTrack;
helper.Process(&distanceTrack);
// Now add a new node as child, name it <ourName>.Target
// and assign the distance track to it. This is that the
// information where the target is and how it moves is
// not lost
D3DS::Node* nd = new D3DS::Node();
pcIn->push_back(nd);
nd->mName = pcIn->mName + ".Target";
aiNodeAnim* nda = anim->mChannels[anim->mNumChannels++] = new aiNodeAnim();
nda->mNodeName.Set(nd->mName);
nda->mNumPositionKeys = (unsigned int)distanceTrack.size();
nda->mPositionKeys = new aiVectorKey[nda->mNumPositionKeys];
::memcpy(nda->mPositionKeys,&distanceTrack[0],
sizeof(aiVectorKey)*nda->mNumPositionKeys);
}
#endif
2015-05-19 03:57:13 +00:00
// Cameras or lights define their transformation in their parent node and in the
// corresponding light or camera chunks. However, we read and process the latter
// to be able to return valid cameras/lights even if no scenegraph is given.
for (unsigned int n = 0; n < pcSOut->mNumCameras; ++n) {
2015-05-19 03:57:13 +00:00
if (pcSOut->mCameras[n]->mName == pcOut->mName) {
pcSOut->mCameras[n]->mLookAt = aiVector3D(0.f, 0.f, 1.f);
2015-05-19 03:57:13 +00:00
}
}
for (unsigned int n = 0; n < pcSOut->mNumLights; ++n) {
2015-05-19 03:57:13 +00:00
if (pcSOut->mLights[n]->mName == pcOut->mName) {
pcSOut->mLights[n]->mDirection = aiVector3D(0.f, 0.f, 1.f);
2015-05-19 03:57:13 +00:00
}
}
// Allocate a new node anim and setup its name
aiNodeAnim *nda = anim->mChannels[anim->mNumChannels++] = new aiNodeAnim();
2015-05-19 03:57:13 +00:00
nda->mNodeName.Set(pcIn->mName);
// POSITION keys
if (pcIn->aPositionKeys.size() > 0) {
2015-05-19 03:57:13 +00:00
nda->mNumPositionKeys = (unsigned int)pcIn->aPositionKeys.size();
nda->mPositionKeys = new aiVectorKey[nda->mNumPositionKeys];
::memcpy(nda->mPositionKeys, &pcIn->aPositionKeys[0],
sizeof(aiVectorKey) * nda->mNumPositionKeys);
2015-05-19 03:57:13 +00:00
}
// ROTATION keys
if (pcIn->aRotationKeys.size() > 0) {
2015-05-19 03:57:13 +00:00
nda->mNumRotationKeys = (unsigned int)pcIn->aRotationKeys.size();
nda->mRotationKeys = new aiQuatKey[nda->mNumRotationKeys];
// Rotations are quaternion offsets
aiQuaternion abs1;
for (unsigned int n = 0; n < nda->mNumRotationKeys; ++n) {
const aiQuatKey &q = pcIn->aRotationKeys[n];
2015-05-19 03:57:13 +00:00
abs1 = (n ? abs1 * q.mValue : q.mValue);
nda->mRotationKeys[n].mTime = q.mTime;
nda->mRotationKeys[n].mValue = abs1.Normalize();
2015-05-19 03:57:13 +00:00
}
}
// SCALING keys
if (pcIn->aScalingKeys.size() > 0) {
2015-05-19 03:57:13 +00:00
nda->mNumScalingKeys = (unsigned int)pcIn->aScalingKeys.size();
nda->mScalingKeys = new aiVectorKey[nda->mNumScalingKeys];
::memcpy(nda->mScalingKeys, &pcIn->aScalingKeys[0],
sizeof(aiVectorKey) * nda->mNumScalingKeys);
2015-05-19 03:57:13 +00:00
}
}
// Allocate storage for children
pcOut->mNumChildren = (unsigned int)pcIn->mChildren.size();
pcOut->mChildren = new aiNode *[pcIn->mChildren.size()];
2015-05-19 03:57:13 +00:00
// Recursively process all children
const unsigned int size = static_cast<unsigned int>(pcIn->mChildren.size());
for (unsigned int i = 0; i < size; ++i) {
2015-05-19 03:57:13 +00:00
pcOut->mChildren[i] = new aiNode();
pcOut->mChildren[i]->mParent = pcOut;
AddNodeToGraph(pcSOut, pcOut->mChildren[i], pcIn->mChildren[i], abs);
2015-05-19 03:57:13 +00:00
}
}
// ------------------------------------------------------------------------------------------------
// Find out how many node animation channels we'll have finally
void CountTracks(D3DS::Node *node, unsigned int &cnt) {
2015-05-19 03:57:13 +00:00
//////////////////////////////////////////////////////////////////////////////
// We will never generate more than one channel for a node, so
// this is rather easy here.
if (node->aPositionKeys.size() > 1 || node->aRotationKeys.size() > 1 ||
node->aScalingKeys.size() > 1 || node->aCameraRollKeys.size() > 1 ||
node->aTargetPositionKeys.size() > 1) {
2015-05-19 03:57:13 +00:00
++cnt;
// account for the additional channel for the camera/spotlight target position
if (node->aTargetPositionKeys.size() > 1) ++cnt;
2015-05-19 03:57:13 +00:00
}
// Recursively process all children
for (unsigned int i = 0; i < node->mChildren.size(); ++i)
CountTracks(node->mChildren[i], cnt);
}
// ------------------------------------------------------------------------------------------------
// Generate the output node graph
void Discreet3DSImporter::GenerateNodeGraph(aiScene *pcOut) {
2015-05-19 03:57:13 +00:00
pcOut->mRootNode = new aiNode();
if (0 == mRootNode->mChildren.size()) {
2015-05-19 03:57:13 +00:00
//////////////////////////////////////////////////////////////////////////////
// It seems the file is so messed up that it has not even a hierarchy.
// generate a flat hiearachy which looks like this:
//
// ROOT_NODE
// |
// ----------------------------------------
// | | | | |
// MESH_0 MESH_1 MESH_2 ... MESH_N CAMERA_0 ....
//
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_WARN("No hierarchy information has been found in the file. ");
2015-05-19 03:57:13 +00:00
pcOut->mRootNode->mNumChildren = pcOut->mNumMeshes +
static_cast<unsigned int>(mScene->mCameras.size() + mScene->mLights.size());
2015-05-19 03:57:13 +00:00
pcOut->mRootNode->mChildren = new aiNode *[pcOut->mRootNode->mNumChildren];
2015-05-19 03:57:13 +00:00
pcOut->mRootNode->mName.Set("<3DSDummyRoot>");
// Build dummy nodes for all meshes
unsigned int a = 0;
for (unsigned int i = 0; i < pcOut->mNumMeshes; ++i, ++a) {
aiNode *pcNode = pcOut->mRootNode->mChildren[a] = new aiNode();
2015-05-19 03:57:13 +00:00
pcNode->mParent = pcOut->mRootNode;
pcNode->mMeshes = new unsigned int[1];
pcNode->mMeshes[0] = i;
pcNode->mNumMeshes = 1;
// Build a name for the node
pcNode->mName.length = ai_snprintf(pcNode->mName.data, MAXLEN, "3DSMesh_%u", i);
2015-05-19 03:57:13 +00:00
}
// Build dummy nodes for all cameras
for (unsigned int i = 0; i < (unsigned int)mScene->mCameras.size(); ++i, ++a) {
aiNode *pcNode = pcOut->mRootNode->mChildren[a] = new aiNode();
2015-05-19 03:57:13 +00:00
pcNode->mParent = pcOut->mRootNode;
// Build a name for the node
pcNode->mName = mScene->mCameras[i]->mName;
}
// Build dummy nodes for all lights
for (unsigned int i = 0; i < (unsigned int)mScene->mLights.size(); ++i, ++a) {
aiNode *pcNode = pcOut->mRootNode->mChildren[a] = new aiNode();
2015-05-19 03:57:13 +00:00
pcNode->mParent = pcOut->mRootNode;
// Build a name for the node
pcNode->mName = mScene->mLights[i]->mName;
}
} else {
2015-05-19 03:57:13 +00:00
// First of all: find out how many scaling, rotation and translation
// animation tracks we'll have afterwards
unsigned int numChannel = 0;
CountTracks(mRootNode, numChannel);
2015-05-19 03:57:13 +00:00
if (numChannel) {
2015-05-19 03:57:13 +00:00
// Allocate a primary animation channel
pcOut->mNumAnimations = 1;
pcOut->mAnimations = new aiAnimation *[1];
aiAnimation *anim = pcOut->mAnimations[0] = new aiAnimation();
2015-05-19 03:57:13 +00:00
anim->mName.Set("3DSMasterAnim");
// Allocate enough storage for all node animation channels,
// but don't set the mNumChannels member - we'll use it to
// index into the array
anim->mChannels = new aiNodeAnim *[numChannel];
2015-05-19 03:57:13 +00:00
}
aiMatrix4x4 m;
AddNodeToGraph(pcOut, pcOut->mRootNode, mRootNode, m);
2015-05-19 03:57:13 +00:00
}
// We used the first and second vertex color set to store some temporary values so we need to cleanup here
for (unsigned int a = 0; a < pcOut->mNumMeshes; ++a) {
pcOut->mMeshes[a]->mColors[0] = nullptr;
pcOut->mMeshes[a]->mColors[1] = nullptr;
2015-05-19 03:57:13 +00:00
}
pcOut->mRootNode->mTransformation = aiMatrix4x4(
1.f, 0.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, -1.f, 0.f, 0.f,
0.f, 0.f, 0.f, 1.f) *
pcOut->mRootNode->mTransformation;
2015-05-19 03:57:13 +00:00
// If the root node is unnamed name it "<3DSRoot>"
if (::strstr(pcOut->mRootNode->mName.data, "UNNAMED") ||
(pcOut->mRootNode->mName.data[0] == '$' && pcOut->mRootNode->mName.data[1] == '$')) {
2015-05-19 03:57:13 +00:00
pcOut->mRootNode->mName.Set("<3DSRoot>");
}
}
// ------------------------------------------------------------------------------------------------
// Convert all meshes in the scene and generate the final output scene.
void Discreet3DSImporter::ConvertScene(aiScene *pcOut) {
2015-05-19 03:57:13 +00:00
// Allocate enough storage for all output materials
pcOut->mNumMaterials = (unsigned int)mScene->mMaterials.size();
pcOut->mMaterials = new aiMaterial *[pcOut->mNumMaterials];
2015-05-19 03:57:13 +00:00
// ... and convert the 3DS materials to aiMaterial's
for (unsigned int i = 0; i < pcOut->mNumMaterials; ++i) {
aiMaterial *pcNew = new aiMaterial();
ConvertMaterial(mScene->mMaterials[i], *pcNew);
2015-05-19 03:57:13 +00:00
pcOut->mMaterials[i] = pcNew;
}
// Generate the output mesh list
ConvertMeshes(pcOut);
// Now copy all light sources to the output scene
pcOut->mNumLights = (unsigned int)mScene->mLights.size();
if (pcOut->mNumLights) {
pcOut->mLights = new aiLight *[pcOut->mNumLights];
::memcpy(pcOut->mLights, &mScene->mLights[0], sizeof(void *) * pcOut->mNumLights);
2015-05-19 03:57:13 +00:00
}
// Now copy all cameras to the output scene
pcOut->mNumCameras = (unsigned int)mScene->mCameras.size();
if (pcOut->mNumCameras) {
pcOut->mCameras = new aiCamera *[pcOut->mNumCameras];
::memcpy(pcOut->mCameras, &mScene->mCameras[0], sizeof(void *) * pcOut->mNumCameras);
2015-05-19 03:57:13 +00:00
}
}
2023-11-11 20:13:47 +00:00
} // namespace Assimp
#endif // !! ASSIMP_BUILD_NO_3DS_IMPORTER