Torque3D/Engine/lib/assimp/code/AssetLib/3DS/3DSConverter.cpp

817 lines
32 KiB
C++
Raw Permalink Normal View History

2019-02-08 22:25:43 +00:00
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
2024-12-09 20:22:47 +00:00
Copyright (c) 2006-2024, assimp team
2019-02-08 22:25:43 +00:00
All rights reserved.
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.
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
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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"
2019-11-10 14:40:50 +00:00
#include "Common/TargetAnimation.h"
2022-04-26 16:56:24 +00:00
#include <assimp/StringComparison.h>
2019-02-08 22:25:43 +00:00
#include <assimp/scene.h>
#include <assimp/DefaultLogger.hpp>
#include <cctype>
2022-04-26 16:56:24 +00:00
#include <memory>
2019-02-08 22:25:43 +00:00
2024-12-09 20:22:47 +00:00
namespace Assimp {
2019-02-08 22:25:43 +00:00
2024-12-09 20:22:47 +00:00
static constexpr unsigned int NotSet = 0xcdcdcdcd;
2019-02-08 22:25:43 +00:00
// ------------------------------------------------------------------------------------------------
// Setup final material indices, generae a default material if necessary
2022-04-26 16:56:24 +00:00
void Discreet3DSImporter::ReplaceDefaultMaterial() {
2019-02-08 22:25:43 +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.
2022-04-26 16:56:24 +00:00
unsigned int idx(NotSet);
for (unsigned int i = 0; i < mScene->mMaterials.size(); ++i) {
std::string s = mScene->mMaterials[i].mName;
2024-12-09 20:22:47 +00:00
for (char &it : s) {
2022-04-26 16:56:24 +00:00
it = static_cast<char>(::tolower(static_cast<unsigned char>(it)));
2019-02-08 22:25:43 +00:00
}
2022-04-26 16:56:24 +00:00
if (std::string::npos == s.find("default")) continue;
2019-02-08 22:25:43 +00:00
if (mScene->mMaterials[i].mDiffuse.r !=
2022-04-26 16:56:24 +00:00
mScene->mMaterials[i].mDiffuse.g ||
mScene->mMaterials[i].mDiffuse.r !=
mScene->mMaterials[i].mDiffuse.b) continue;
if (ContainsTextures(i)) {
2019-02-08 22:25:43 +00:00
continue;
}
idx = i;
}
2022-04-26 16:56:24 +00:00
if (NotSet == idx) {
idx = (unsigned int)mScene->mMaterials.size();
2019-02-08 22:25:43 +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
2022-04-26 16:56:24 +00:00
i = mScene->mMeshes.begin();
i != mScene->mMeshes.end(); ++i) {
2019-02-08 22:25:43 +00:00
for (std::vector<unsigned int>::iterator
2022-04-26 16:56:24 +00:00
a = (*i).mFaceMaterials.begin();
a != (*i).mFaceMaterials.end(); ++a) {
2019-02-08 22:25:43 +00:00
// NOTE: The additional check seems to be necessary,
// some exporters seem to generate invalid data here
2022-04-26 16:56:24 +00:00
if (0xcdcdcdcd == (*a)) {
2019-02-08 22:25:43 +00:00
(*a) = idx;
++cnt;
2022-04-26 16:56:24 +00:00
} else if ((*a) >= mScene->mMaterials.size()) {
2019-02-08 22:25:43 +00:00
(*a) = idx;
2019-03-05 20:39:38 +00:00
ASSIMP_LOG_WARN("Material index overflow in 3DS file. Using default material");
2019-02-08 22:25:43 +00:00
++cnt;
}
}
}
2022-04-26 16:56:24 +00:00
if (cnt && idx == mScene->mMaterials.size()) {
2019-02-08 22:25:43 +00:00
// We need to create our own default material
2019-03-05 20:39:38 +00:00
D3DS::Material sMat("%%%DEFAULT");
2022-04-26 16:56:24 +00:00
sMat.mDiffuse = aiColor3D(0.3f, 0.3f, 0.3f);
2019-02-08 22:25:43 +00:00
mScene->mMaterials.push_back(sMat);
2019-03-05 20:39:38 +00:00
ASSIMP_LOG_INFO("3DS: Generating default material");
2019-02-08 22:25:43 +00:00
}
}
// ------------------------------------------------------------------------------------------------
// Check whether all indices are valid. Otherwise we'd crash before the validation step is reached
2022-04-26 16:56:24 +00:00
void Discreet3DSImporter::CheckIndices(D3DS::Mesh &sMesh) {
for (std::vector<D3DS::Face>::iterator i = sMesh.mFaces.begin(); i != sMesh.mFaces.end(); ++i) {
2019-02-08 22:25:43 +00:00
// check whether all indices are in range
2022-04-26 16:56:24 +00:00
for (unsigned int a = 0; a < 3; ++a) {
if ((*i).mIndices[a] >= sMesh.mPositions.size()) {
2019-03-05 20:39:38 +00:00
ASSIMP_LOG_WARN("3DS: Vertex index overflow)");
2022-04-26 16:56:24 +00:00
(*i).mIndices[a] = (uint32_t)sMesh.mPositions.size() - 1;
2019-02-08 22:25:43 +00:00
}
2022-04-26 16:56:24 +00:00
if (!sMesh.mTexCoords.empty() && (*i).mIndices[a] >= sMesh.mTexCoords.size()) {
2019-03-05 20:39:38 +00:00
ASSIMP_LOG_WARN("3DS: Texture coordinate index overflow)");
2022-04-26 16:56:24 +00:00
(*i).mIndices[a] = (uint32_t)sMesh.mTexCoords.size() - 1;
2019-02-08 22:25:43 +00:00
}
}
}
}
// ------------------------------------------------------------------------------------------------
// Generate out unique verbose format representation
2022-04-26 16:56:24 +00:00
void Discreet3DSImporter::MakeUnique(D3DS::Mesh &sMesh) {
2019-02-08 22:25:43 +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
2022-04-26 16:56:24 +00:00
std::vector<aiVector3D> vNew(sMesh.mFaces.size() * 3);
2019-02-08 22:25:43 +00:00
std::vector<aiVector3D> vNew2;
if (sMesh.mTexCoords.size())
vNew2.resize(sMesh.mFaces.size() * 3);
2022-04-26 16:56:24 +00:00
for (unsigned int i = 0, base = 0; i < sMesh.mFaces.size(); ++i) {
D3DS::Face &face = sMesh.mFaces[i];
2019-02-08 22:25:43 +00:00
// Positions
2022-04-26 16:56:24 +00:00
for (unsigned int a = 0; a < 3; ++a, ++base) {
2019-02-08 22:25:43 +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
2022-04-26 16:56:24 +00:00
void CopyTexture(aiMaterial &mat, D3DS::Texture &texture, aiTextureType type) {
2019-02-08 22:25:43 +00:00
// Setup the texture name
aiString tex;
2022-04-26 16:56:24 +00:00
tex.Set(texture.mMapName);
mat.AddProperty(&tex, AI_MATKEY_TEXTURE(type, 0));
2019-02-08 22:25:43 +00:00
// Setup the texture blend factor
if (is_not_qnan(texture.mTextureBlend))
2022-04-26 16:56:24 +00:00
mat.AddProperty<ai_real>(&texture.mTextureBlend, 1, AI_MATKEY_TEXBLEND(type, 0));
2019-02-08 22:25:43 +00:00
// Setup the texture mapping mode
2019-03-05 20:39:38 +00:00
int mapMode = static_cast<int>(texture.mMapMode);
2022-04-26 16:56:24 +00:00
mat.AddProperty<int>(&mapMode, 1, AI_MATKEY_MAPPINGMODE_U(type, 0));
mat.AddProperty<int>(&mapMode, 1, AI_MATKEY_MAPPINGMODE_V(type, 0));
2019-02-08 22:25:43 +00:00
// Mirroring - double the scaling values
// FIXME: this is not really correct ...
2022-04-26 16:56:24 +00:00
if (texture.mMapMode == aiTextureMapMode_Mirror) {
2019-02-08 22:25:43 +00:00
texture.mScaleU *= 2.0;
texture.mScaleV *= 2.0;
texture.mOffsetU /= 2.0;
texture.mOffsetV /= 2.0;
}
// Setup texture UV transformations
2022-04-26 16:56:24 +00:00
mat.AddProperty<ai_real>(&texture.mOffsetU, 5, AI_MATKEY_UVTRANSFORM(type, 0));
2019-02-08 22:25:43 +00:00
}
// ------------------------------------------------------------------------------------------------
// Convert a 3DS material to an aiMaterial
2022-04-26 16:56:24 +00:00
void Discreet3DSImporter::ConvertMaterial(D3DS::Material &oldMat,
aiMaterial &mat) {
2019-02-08 22:25:43 +00:00
// NOTE: Pass the background image to the viewer by bypassing the
// material system. This is an evil hack, never do it again!
2022-04-26 16:56:24 +00:00
if (0 != mBackgroundImage.length() && bHasBG) {
2019-02-08 22:25:43 +00:00
aiString tex;
2022-04-26 16:56:24 +00:00
tex.Set(mBackgroundImage);
mat.AddProperty(&tex, AI_MATKEY_GLOBAL_BACKGROUND_IMAGE);
2019-02-08 22:25:43 +00:00
// Be sure this is only done for the first material
2022-04-26 16:56:24 +00:00
mBackgroundImage = std::string();
2019-02-08 22:25:43 +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;
2022-04-26 16:56:24 +00:00
name.Set(oldMat.mName);
mat.AddProperty(&name, AI_MATKEY_NAME);
2019-02-08 22:25:43 +00:00
// Material colors
2022-04-26 16:56:24 +00:00
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);
2019-02-08 22:25:43 +00:00
// Phong shininess and shininess strength
if (D3DS::Discreet3DS::Phong == oldMat.mShading ||
2022-04-26 16:56:24 +00:00
D3DS::Discreet3DS::Metal == oldMat.mShading) {
if (!oldMat.mSpecularExponent || !oldMat.mShininessStrength) {
2019-02-08 22:25:43 +00:00
oldMat.mShading = D3DS::Discreet3DS::Gouraud;
2022-04-26 16:56:24 +00:00
} else {
mat.AddProperty(&oldMat.mSpecularExponent, 1, AI_MATKEY_SHININESS);
mat.AddProperty(&oldMat.mShininessStrength, 1, AI_MATKEY_SHININESS_STRENGTH);
2019-02-08 22:25:43 +00:00
}
}
// Opacity
2022-04-26 16:56:24 +00:00
mat.AddProperty<ai_real>(&oldMat.mTransparency, 1, AI_MATKEY_OPACITY);
2019-02-08 22:25:43 +00:00
// Bump height scaling
2022-04-26 16:56:24 +00:00
mat.AddProperty<ai_real>(&oldMat.mBumpHeight, 1, AI_MATKEY_BUMPSCALING);
2019-02-08 22:25:43 +00:00
// Two sided rendering?
2022-04-26 16:56:24 +00:00
if (oldMat.mTwoSided) {
2019-02-08 22:25:43 +00:00
int i = 1;
2022-04-26 16:56:24 +00:00
mat.AddProperty<int>(&i, 1, AI_MATKEY_TWOSIDED);
2019-02-08 22:25:43 +00:00
}
// Shading mode
aiShadingMode eShading = aiShadingMode_NoShading;
2022-04-26 16:56:24 +00:00
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);
}
2024-12-09 20:22:47 +00:00
[[fallthrough]];
2022-04-26 16:56:24 +00:00
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;
2019-02-08 22:25:43 +00:00
}
2019-03-05 20:39:38 +00:00
int eShading_ = static_cast<int>(eShading);
mat.AddProperty<int>(&eShading_, 1, AI_MATKEY_SHADING_MODEL);
2019-02-08 22:25:43 +00:00
// DIFFUSE texture
2022-04-26 16:56:24 +00:00
if (oldMat.sTexDiffuse.mMapName.length() > 0)
CopyTexture(mat, oldMat.sTexDiffuse, aiTextureType_DIFFUSE);
2019-02-08 22:25:43 +00:00
// SPECULAR texture
2022-04-26 16:56:24 +00:00
if (oldMat.sTexSpecular.mMapName.length() > 0)
CopyTexture(mat, oldMat.sTexSpecular, aiTextureType_SPECULAR);
2019-02-08 22:25:43 +00:00
// OPACITY texture
2022-04-26 16:56:24 +00:00
if (oldMat.sTexOpacity.mMapName.length() > 0)
CopyTexture(mat, oldMat.sTexOpacity, aiTextureType_OPACITY);
2019-02-08 22:25:43 +00:00
// EMISSIVE texture
2022-04-26 16:56:24 +00:00
if (oldMat.sTexEmissive.mMapName.length() > 0)
CopyTexture(mat, oldMat.sTexEmissive, aiTextureType_EMISSIVE);
2019-02-08 22:25:43 +00:00
// BUMP texture
2022-04-26 16:56:24 +00:00
if (oldMat.sTexBump.mMapName.length() > 0)
CopyTexture(mat, oldMat.sTexBump, aiTextureType_HEIGHT);
2019-02-08 22:25:43 +00:00
// SHININESS texture
2022-04-26 16:56:24 +00:00
if (oldMat.sTexShininess.mMapName.length() > 0)
CopyTexture(mat, oldMat.sTexShininess, aiTextureType_SHININESS);
2019-02-08 22:25:43 +00:00
// REFLECTION texture
2022-04-26 16:56:24 +00:00
if (oldMat.sTexReflective.mMapName.length() > 0)
CopyTexture(mat, oldMat.sTexReflective, aiTextureType_REFLECTION);
2019-02-08 22:25:43 +00:00
// Store the name of the material itself, too
2022-04-26 16:56:24 +00:00
if (oldMat.mName.length()) {
2019-02-08 22:25:43 +00:00
aiString tex;
2022-04-26 16:56:24 +00:00
tex.Set(oldMat.mName);
mat.AddProperty(&tex, AI_MATKEY_NAME);
2019-02-08 22:25:43 +00:00
}
}
// ------------------------------------------------------------------------------------------------
// Split meshes by their materials and generate output aiMesh'es
2022-04-26 16:56:24 +00:00
void Discreet3DSImporter::ConvertMeshes(aiScene *pcOut) {
std::vector<aiMesh *> avOutMeshes;
2019-02-08 22:25:43 +00:00
avOutMeshes.reserve(mScene->mMeshes.size() * 2);
2022-04-26 16:56:24 +00:00
unsigned int iFaceCnt = 0, num = 0;
2019-02-08 22:25:43 +00:00
aiString name;
// we need to split all meshes by their materials
2022-04-26 16:56:24 +00:00
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()]);
2019-02-08 22:25:43 +00:00
2022-04-26 16:56:24 +00:00
name.length = ASSIMP_itoa10(name.data, num++);
2019-02-08 22:25:43 +00:00
unsigned int iNum = 0;
2022-04-26 16:56:24 +00:00
for (std::vector<unsigned int>::const_iterator a = (*i).mFaceMaterials.begin();
a != (*i).mFaceMaterials.end(); ++a, ++iNum) {
2019-02-08 22:25:43 +00:00
aiSplit[*a].push_back(iNum);
}
// now generate submeshes
2022-04-26 16:56:24 +00:00
for (unsigned int p = 0; p < mScene->mMaterials.size(); ++p) {
2019-02-08 22:25:43 +00:00
if (aiSplit[p].empty()) {
continue;
}
2022-04-26 16:56:24 +00:00
aiMesh *meshOut = new aiMesh();
2019-02-08 22:25:43 +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
2022-04-26 16:56:24 +00:00
meshOut->mColors[0] = (aiColor4D *)(&*i);
2019-02-08 22:25:43 +00:00
avOutMeshes.push_back(meshOut);
// convert vertices
meshOut->mNumFaces = (unsigned int)aiSplit[p].size();
2022-04-26 16:56:24 +00:00
meshOut->mNumVertices = meshOut->mNumFaces * 3;
2019-02-08 22:25:43 +00:00
// allocate enough storage for faces
meshOut->mFaces = new aiFace[meshOut->mNumFaces];
iFaceCnt += meshOut->mNumFaces;
meshOut->mVertices = new aiVector3D[meshOut->mNumVertices];
2022-04-26 16:56:24 +00:00
meshOut->mNormals = new aiVector3D[meshOut->mNumVertices];
if ((*i).mTexCoords.size()) {
2019-02-08 22:25:43 +00:00
meshOut->mTextureCoords[0] = new aiVector3D[meshOut->mNumVertices];
}
2022-04-26 16:56:24 +00:00
for (unsigned int q = 0, base = 0; q < aiSplit[p].size(); ++q) {
2019-02-08 22:25:43 +00:00
unsigned int index = aiSplit[p][q];
2022-04-26 16:56:24 +00:00
aiFace &face = meshOut->mFaces[q];
2019-02-08 22:25:43 +00:00
face.mIndices = new unsigned int[3];
face.mNumIndices = 3;
2022-04-26 16:56:24 +00:00
for (unsigned int a = 0; a < 3; ++a, ++base) {
2019-02-08 22:25:43 +00:00
unsigned int idx = (*i).mFaces[index].mIndices[a];
2022-04-26 16:56:24 +00:00
meshOut->mVertices[base] = (*i).mPositions[idx];
meshOut->mNormals[base] = (*i).mNormals[idx];
2019-02-08 22:25:43 +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();
2022-04-26 16:56:24 +00:00
pcOut->mMeshes = new aiMesh *[pcOut->mNumMeshes]();
for (unsigned int a = 0; a < pcOut->mNumMeshes; ++a) {
2019-02-08 22:25:43 +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
2022-04-26 16:56:24 +00:00
void Discreet3DSImporter::AddNodeToGraph(aiScene *pcSOut, aiNode *pcOut,
D3DS::Node *pcIn, aiMatrix4x4 & /*absTrafo*/) {
2019-02-08 22:25:43 +00:00
std::vector<unsigned int> iArray;
iArray.reserve(3);
aiMatrix4x4 abs;
// Find all meshes with the same name as the node
2022-04-26 16:56:24 +00:00
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);
2019-02-08 22:25:43 +00:00
if (pcIn->mName == pcMesh->mName)
iArray.push_back(a);
}
2022-04-26 16:56:24 +00:00
if (!iArray.empty()) {
2019-02-08 22:25:43 +00:00
// The matrix should be identical for all meshes with the
// same name. It HAS to be identical for all meshes .....
2022-04-26 16:56:24 +00:00
D3DS::Mesh *imesh = ((D3DS::Mesh *)pcSOut->mMeshes[iArray[0]]->mColors[0]);
2019-02-08 22:25:43 +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;
2022-04-26 16:56:24 +00:00
mInv.Inverse();
mInvTransposed.Transpose();
2019-02-08 22:25:43 +00:00
aiVector3D pivot = pcIn->vPivot;
pcOut->mNumMeshes = (unsigned int)iArray.size();
pcOut->mMeshes = new unsigned int[iArray.size()];
2022-04-26 16:56:24 +00:00
for (unsigned int i = 0; i < iArray.size(); ++i) {
2019-02-08 22:25:43 +00:00
const unsigned int iIndex = iArray[i];
2022-04-26 16:56:24 +00:00
aiMesh *const mesh = pcSOut->mMeshes[iIndex];
2019-02-08 22:25:43 +00:00
2022-04-26 16:56:24 +00:00
if (mesh->mColors[1] == nullptr) {
2019-02-08 22:25:43 +00:00
// Transform the vertices back into their local space
// fixme: consider computing normals after this, so we don't need to transform them
2022-04-26 16:56:24 +00:00
const aiVector3D *const pvEnd = mesh->mVertices + mesh->mNumVertices;
aiVector3D *pvCurrent = mesh->mVertices, *t2 = mesh->mNormals;
2019-02-08 22:25:43 +00:00
for (; pvCurrent != pvEnd; ++pvCurrent, ++t2) {
*pvCurrent = mInv * (*pvCurrent);
*t2 = mInvTransposed * (*t2);
}
// Handle negative transformation matrix determinant -> invert vertex x
2022-04-26 16:56:24 +00:00
if (imesh->mMat.Determinant() < 0.0f) {
2019-02-08 22:25:43 +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;
}
2019-03-05 20:39:38 +00:00
ASSIMP_LOG_INFO("3DS: Flipping mesh X-Axis");
2019-02-08 22:25:43 +00:00
}
// Handle pivot point
2022-04-26 16:56:24 +00:00
if (pivot.x || pivot.y || pivot.z) {
for (pvCurrent = mesh->mVertices; pvCurrent != pvEnd; ++pvCurrent) {
2019-02-08 22:25:43 +00:00
*pvCurrent -= pivot;
}
}
2022-04-26 16:56:24 +00:00
mesh->mColors[1] = (aiColor4D *)1;
} else
mesh->mColors[1] = (aiColor4D *)1;
2019-02-08 22:25:43 +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
2022-04-26 16:56:24 +00:00
if (pcIn->mInstanceNumber > 1) {
2019-02-08 22:25:43 +00:00
char tmp[12];
ASSIMP_itoa10(tmp, pcIn->mInstanceNumber);
std::string tempStr = pcIn->mName + "_inst_";
tempStr += tmp;
pcOut->mName.Set(tempStr);
2022-04-26 16:56:24 +00:00
} else
2019-02-08 22:25:43 +00:00
pcOut->mName.Set(pcIn->mName);
// Now build the transformation matrix of the node
// ROTATION
2022-04-26 16:56:24 +00:00
if (pcIn->aRotationKeys.size()) {
2019-02-08 22:25:43 +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;
}
2022-04-26 16:56:24 +00:00
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);
2019-02-08 22:25:43 +00:00
}
// SCALING
2022-04-26 16:56:24 +00:00
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;
2019-02-08 22:25:43 +00:00
}
// TRANSLATION
2022-04-26 16:56:24 +00:00
if (pcIn->aPositionKeys.size()) {
const aiVector3D &v = pcIn->aPositionKeys[0].mValue;
2019-02-08 22:25:43 +00:00
m.a4 += v.x;
m.b4 += v.y;
m.c4 += v.z;
}
// Generate animation channels for the node
2022-04-26 16:56:24 +00:00
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];
2019-03-05 20:39:38 +00:00
ai_assert(nullptr != anim);
2019-02-08 22:25:43 +00:00
2022-04-26 16:56:24 +00:00
if (pcIn->aCameraRollKeys.size() > 1) {
ASSIMP_LOG_VERBOSE_DEBUG("3DS: Converting camera roll track ...");
2019-02-08 22:25:43 +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());
2022-04-26 16:56:24 +00:00
for (unsigned int i = 0; i < pcIn->aCameraRollKeys.size(); ++i) {
aiQuatKey &q = pcIn->aRotationKeys[i];
aiFloatKey &f = pcIn->aCameraRollKeys[i];
2019-02-08 22:25:43 +00:00
2022-04-26 16:56:24 +00:00
q.mTime = f.mTime;
2019-02-08 22:25:43 +00:00
// FIX to get to Assimp quaternion conventions
2022-04-26 16:56:24 +00:00
q.mValue = aiQuaternion(0.f, 0.f, AI_DEG_TO_RAD(/*-*/ f.mValue));
2019-02-08 22:25:43 +00:00
}
}
#if 0
if (pcIn->aTargetPositionKeys.size() > 1)
{
2022-04-26 16:56:24 +00:00
ASSIMP_LOG_VERBOSE_DEBUG("3DS: Converting target track ...");
2019-02-08 22:25:43 +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
// 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
2024-12-09 20:22:47 +00:00
// to be able to return valid cameras/lights even if no scenegraph is given.
2022-04-26 16:56:24 +00:00
for (unsigned int n = 0; n < pcSOut->mNumCameras; ++n) {
2019-02-08 22:25:43 +00:00
if (pcSOut->mCameras[n]->mName == pcOut->mName) {
2022-04-26 16:56:24 +00:00
pcSOut->mCameras[n]->mLookAt = aiVector3D(0.f, 0.f, 1.f);
2019-02-08 22:25:43 +00:00
}
}
2022-04-26 16:56:24 +00:00
for (unsigned int n = 0; n < pcSOut->mNumLights; ++n) {
2019-02-08 22:25:43 +00:00
if (pcSOut->mLights[n]->mName == pcOut->mName) {
2022-04-26 16:56:24 +00:00
pcSOut->mLights[n]->mDirection = aiVector3D(0.f, 0.f, 1.f);
2019-02-08 22:25:43 +00:00
}
}
// Allocate a new node anim and setup its name
2022-04-26 16:56:24 +00:00
aiNodeAnim *nda = anim->mChannels[anim->mNumChannels++] = new aiNodeAnim();
2019-02-08 22:25:43 +00:00
nda->mNodeName.Set(pcIn->mName);
// POSITION keys
2022-04-26 16:56:24 +00:00
if (pcIn->aPositionKeys.size() > 0) {
2019-02-08 22:25:43 +00:00
nda->mNumPositionKeys = (unsigned int)pcIn->aPositionKeys.size();
nda->mPositionKeys = new aiVectorKey[nda->mNumPositionKeys];
2022-04-26 16:56:24 +00:00
::memcpy(nda->mPositionKeys, &pcIn->aPositionKeys[0],
sizeof(aiVectorKey) * nda->mNumPositionKeys);
2019-02-08 22:25:43 +00:00
}
// ROTATION keys
2022-04-26 16:56:24 +00:00
if (pcIn->aRotationKeys.size() > 0) {
2019-02-08 22:25:43 +00:00
nda->mNumRotationKeys = (unsigned int)pcIn->aRotationKeys.size();
nda->mRotationKeys = new aiQuatKey[nda->mNumRotationKeys];
// Rotations are quaternion offsets
aiQuaternion abs1;
2022-04-26 16:56:24 +00:00
for (unsigned int n = 0; n < nda->mNumRotationKeys; ++n) {
const aiQuatKey &q = pcIn->aRotationKeys[n];
2019-02-08 22:25:43 +00:00
abs1 = (n ? abs1 * q.mValue : q.mValue);
2022-04-26 16:56:24 +00:00
nda->mRotationKeys[n].mTime = q.mTime;
2019-02-08 22:25:43 +00:00
nda->mRotationKeys[n].mValue = abs1.Normalize();
}
}
// SCALING keys
2022-04-26 16:56:24 +00:00
if (pcIn->aScalingKeys.size() > 0) {
2019-02-08 22:25:43 +00:00
nda->mNumScalingKeys = (unsigned int)pcIn->aScalingKeys.size();
nda->mScalingKeys = new aiVectorKey[nda->mNumScalingKeys];
2022-04-26 16:56:24 +00:00
::memcpy(nda->mScalingKeys, &pcIn->aScalingKeys[0],
sizeof(aiVectorKey) * nda->mNumScalingKeys);
2019-02-08 22:25:43 +00:00
}
}
// Allocate storage for children
2024-12-09 20:22:47 +00:00
const unsigned int size = static_cast<unsigned int>(pcIn->mChildren.size());
pcOut->mNumChildren = size;
if (size == 0) {
return;
}
2022-04-26 16:56:24 +00:00
pcOut->mChildren = new aiNode *[pcIn->mChildren.size()];
2019-02-08 22:25:43 +00:00
// Recursively process all children
2024-12-09 20:22:47 +00:00
2022-04-26 16:56:24 +00:00
for (unsigned int i = 0; i < size; ++i) {
2019-02-08 22:25:43 +00:00
pcOut->mChildren[i] = new aiNode();
pcOut->mChildren[i]->mParent = pcOut;
2022-04-26 16:56:24 +00:00
AddNodeToGraph(pcSOut, pcOut->mChildren[i], pcIn->mChildren[i], abs);
2019-02-08 22:25:43 +00:00
}
}
// ------------------------------------------------------------------------------------------------
// Find out how many node animation channels we'll have finally
2022-04-26 16:56:24 +00:00
void CountTracks(D3DS::Node *node, unsigned int &cnt) {
2019-02-08 22:25:43 +00:00
//////////////////////////////////////////////////////////////////////////////
// We will never generate more than one channel for a node, so
// this is rather easy here.
2022-04-26 16:56:24 +00:00
if (node->aPositionKeys.size() > 1 || node->aRotationKeys.size() > 1 ||
node->aScalingKeys.size() > 1 || node->aCameraRollKeys.size() > 1 ||
node->aTargetPositionKeys.size() > 1) {
2019-02-08 22:25:43 +00:00
++cnt;
// account for the additional channel for the camera/spotlight target position
2022-04-26 16:56:24 +00:00
if (node->aTargetPositionKeys.size() > 1) ++cnt;
2019-02-08 22:25:43 +00:00
}
// Recursively process all children
2022-04-26 16:56:24 +00:00
for (unsigned int i = 0; i < node->mChildren.size(); ++i)
CountTracks(node->mChildren[i], cnt);
2019-02-08 22:25:43 +00:00
}
// ------------------------------------------------------------------------------------------------
// Generate the output node graph
2022-04-26 16:56:24 +00:00
void Discreet3DSImporter::GenerateNodeGraph(aiScene *pcOut) {
2019-02-08 22:25:43 +00:00
pcOut->mRootNode = new aiNode();
2022-04-26 16:56:24 +00:00
if (0 == mRootNode->mChildren.size()) {
2019-02-08 22:25:43 +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 ....
//
2019-03-05 20:39:38 +00:00
ASSIMP_LOG_WARN("No hierarchy information has been found in the file. ");
2019-02-08 22:25:43 +00:00
pcOut->mRootNode->mNumChildren = pcOut->mNumMeshes +
2022-04-26 16:56:24 +00:00
static_cast<unsigned int>(mScene->mCameras.size() + mScene->mLights.size());
2019-02-08 22:25:43 +00:00
2022-04-26 16:56:24 +00:00
pcOut->mRootNode->mChildren = new aiNode *[pcOut->mRootNode->mNumChildren];
2019-02-08 22:25:43 +00:00
pcOut->mRootNode->mName.Set("<3DSDummyRoot>");
// Build dummy nodes for all meshes
unsigned int a = 0;
2022-04-26 16:56:24 +00:00
for (unsigned int i = 0; i < pcOut->mNumMeshes; ++i, ++a) {
aiNode *pcNode = pcOut->mRootNode->mChildren[a] = new aiNode();
2019-02-08 22:25:43 +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
2024-12-09 20:22:47 +00:00
pcNode->mName.length = ai_snprintf(pcNode->mName.data, AI_MAXLEN, "3DSMesh_%u", i);
2019-02-08 22:25:43 +00:00
}
// Build dummy nodes for all cameras
2022-04-26 16:56:24 +00:00
for (unsigned int i = 0; i < (unsigned int)mScene->mCameras.size(); ++i, ++a) {
aiNode *pcNode = pcOut->mRootNode->mChildren[a] = new aiNode();
2019-02-08 22:25:43 +00:00
pcNode->mParent = pcOut->mRootNode;
// Build a name for the node
pcNode->mName = mScene->mCameras[i]->mName;
}
// Build dummy nodes for all lights
2022-04-26 16:56:24 +00:00
for (unsigned int i = 0; i < (unsigned int)mScene->mLights.size(); ++i, ++a) {
aiNode *pcNode = pcOut->mRootNode->mChildren[a] = new aiNode();
2019-02-08 22:25:43 +00:00
pcNode->mParent = pcOut->mRootNode;
// Build a name for the node
pcNode->mName = mScene->mLights[i]->mName;
}
2022-04-26 16:56:24 +00:00
} else {
2019-02-08 22:25:43 +00:00
// First of all: find out how many scaling, rotation and translation
// animation tracks we'll have afterwards
unsigned int numChannel = 0;
2022-04-26 16:56:24 +00:00
CountTracks(mRootNode, numChannel);
2019-02-08 22:25:43 +00:00
2022-04-26 16:56:24 +00:00
if (numChannel) {
2019-02-08 22:25:43 +00:00
// Allocate a primary animation channel
pcOut->mNumAnimations = 1;
2022-04-26 16:56:24 +00:00
pcOut->mAnimations = new aiAnimation *[1];
aiAnimation *anim = pcOut->mAnimations[0] = new aiAnimation();
2019-02-08 22:25:43 +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
2022-04-26 16:56:24 +00:00
anim->mChannels = new aiNodeAnim *[numChannel];
2019-02-08 22:25:43 +00:00
}
aiMatrix4x4 m;
2022-04-26 16:56:24 +00:00
AddNodeToGraph(pcOut, pcOut->mRootNode, mRootNode, m);
2019-02-08 22:25:43 +00:00
}
// We used the first and second vertex color set to store some temporary values so we need to cleanup here
2022-04-26 16:56:24 +00:00
for (unsigned int a = 0; a < pcOut->mNumMeshes; ++a) {
pcOut->mMeshes[a]->mColors[0] = nullptr;
pcOut->mMeshes[a]->mColors[1] = nullptr;
2019-02-08 22:25:43 +00:00
}
pcOut->mRootNode->mTransformation = aiMatrix4x4(
2022-04-26 16:56:24 +00:00
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;
2019-02-08 22:25:43 +00:00
// If the root node is unnamed name it "<3DSRoot>"
2022-04-26 16:56:24 +00:00
if (::strstr(pcOut->mRootNode->mName.data, "UNNAMED") ||
(pcOut->mRootNode->mName.data[0] == '$' && pcOut->mRootNode->mName.data[1] == '$')) {
2019-02-08 22:25:43 +00:00
pcOut->mRootNode->mName.Set("<3DSRoot>");
}
}
// ------------------------------------------------------------------------------------------------
// Convert all meshes in the scene and generate the final output scene.
2022-04-26 16:56:24 +00:00
void Discreet3DSImporter::ConvertScene(aiScene *pcOut) {
2019-02-08 22:25:43 +00:00
// Allocate enough storage for all output materials
pcOut->mNumMaterials = (unsigned int)mScene->mMaterials.size();
2022-04-26 16:56:24 +00:00
pcOut->mMaterials = new aiMaterial *[pcOut->mNumMaterials];
2019-02-08 22:25:43 +00:00
// ... and convert the 3DS materials to aiMaterial's
2022-04-26 16:56:24 +00:00
for (unsigned int i = 0; i < pcOut->mNumMaterials; ++i) {
aiMaterial *pcNew = new aiMaterial();
ConvertMaterial(mScene->mMaterials[i], *pcNew);
2019-02-08 22:25:43 +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();
2022-04-26 16:56:24 +00:00
if (pcOut->mNumLights) {
pcOut->mLights = new aiLight *[pcOut->mNumLights];
::memcpy(pcOut->mLights, &mScene->mLights[0], sizeof(void *) * pcOut->mNumLights);
2019-02-08 22:25:43 +00:00
}
// Now copy all cameras to the output scene
pcOut->mNumCameras = (unsigned int)mScene->mCameras.size();
2022-04-26 16:56:24 +00:00
if (pcOut->mNumCameras) {
pcOut->mCameras = new aiCamera *[pcOut->mNumCameras];
::memcpy(pcOut->mCameras, &mScene->mCameras[0], sizeof(void *) * pcOut->mNumCameras);
2019-02-08 22:25:43 +00:00
}
}
2024-12-09 20:22:47 +00:00
} // namespace Assimp
2019-02-08 22:25:43 +00:00
#endif // !! ASSIMP_BUILD_NO_3DS_IMPORTER