assimp/code/LWO/LWOLoader.cpp

1431 lines
53 KiB
C++
Raw Normal View History

/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
Copyright (c) 2006-2020, assimp team
2018-01-28 18:42:05 +00:00
2017-05-09 17:57:36 +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 LWOLoader.cpp
* @brief Implementation of the LWO importer class
*/
#ifndef ASSIMP_BUILD_NO_LWO_IMPORTER
// internal headers
#include "LWO/LWOLoader.h"
#include "PostProcessing/ConvertToLHProcess.h"
2020-03-01 12:15:45 +00:00
#include "PostProcessing/ProcessHelper.h"
#include <assimp/ByteSwapper.h>
2020-03-01 12:15:45 +00:00
#include <assimp/SGSpatialSort.h>
#include <assimp/StringComparison.h>
2017-02-22 16:20:26 +00:00
#include <assimp/importerdesc.h>
2020-03-01 12:15:45 +00:00
#include <assimp/IOSystem.hpp>
#include <iomanip>
2017-02-22 16:20:26 +00:00
#include <map>
2020-03-01 12:15:45 +00:00
#include <memory>
#include <sstream>
using namespace Assimp;
static const aiImporterDesc desc = {
2015-05-19 03:57:13 +00:00
"LightWave/Modo Object Importer",
"",
"",
"https://www.lightwave3d.com/lightwave_sdk/",
2015-05-19 03:57:13 +00:00
aiImporterFlags_SupportTextFlavour,
0,
0,
0,
0,
"lwo lxo"
};
// ------------------------------------------------------------------------------------------------
// Constructor to be privately used by Importer
2020-03-01 12:15:45 +00:00
LWOImporter::LWOImporter() :
mIsLWO2(),
mIsLXOB(),
mLayers(),
mCurLayer(),
mTags(),
mMapping(),
mSurfaces(),
mFileBuffer(),
fileSize(),
mScene(nullptr),
configSpeedFlag(),
configLayerIndex(),
hasNamedLayer() {
// empty
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Destructor, private as well
2020-03-01 12:15:45 +00:00
LWOImporter::~LWOImporter() {
// empty
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Returns whether the class can handle the format of the given file.
2020-03-01 12:15:45 +00:00
bool LWOImporter::CanRead(const std::string &file, IOSystem *pIOHandler, bool checkSig) const {
const std::string extension = GetExtension(file);
2015-05-19 03:57:13 +00:00
if (extension == "lwo" || extension == "lxo") {
return true;
}
// if check for extension is not enough, check for the magic tokens
if (!extension.length() || checkSig) {
uint32_t tokens[3];
tokens[0] = AI_LWO_FOURCC_LWOB;
tokens[1] = AI_LWO_FOURCC_LWO2;
tokens[2] = AI_LWO_FOURCC_LXOB;
2020-03-01 12:15:45 +00:00
return CheckMagicToken(pIOHandler, file, tokens, 3, 8);
2015-05-19 03:57:13 +00:00
}
return false;
}
// ------------------------------------------------------------------------------------------------
// Setup configuration properties
2020-03-01 12:15:45 +00:00
void LWOImporter::SetupProperties(const Importer *pImp) {
configSpeedFlag = (0 != pImp->GetPropertyInteger(AI_CONFIG_FAVOUR_SPEED, 0) ? true : false);
configLayerIndex = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_LWO_ONE_LAYER_ONLY, UINT_MAX);
configLayerName = pImp->GetPropertyString(AI_CONFIG_IMPORT_LWO_ONE_LAYER_ONLY, "");
}
// ------------------------------------------------------------------------------------------------
// Get list of file extensions
2020-03-01 12:15:45 +00:00
const aiImporterDesc *LWOImporter::GetInfo() const {
2015-05-19 03:57:13 +00:00
return &desc;
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Imports the given file into the given scene structure.
2020-03-01 12:15:45 +00:00
void LWOImporter::InternReadFile(const std::string &pFile,
aiScene *pScene,
IOSystem *pIOHandler) {
std::unique_ptr<IOStream> file(pIOHandler->Open(pFile, "rb"));
2015-05-19 03:57:13 +00:00
// Check whether we can read from the file
2020-03-01 12:15:45 +00:00
if (file.get() == nullptr) {
throw DeadlyImportError("Failed to open LWO file " + pFile + ".");
}
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
if ((this->fileSize = (unsigned int)file->FileSize()) < 12) {
2015-05-19 03:57:13 +00:00
throw DeadlyImportError("LWO: The file is too small to contain the IFF header");
2020-03-01 12:15:45 +00:00
}
2015-05-19 03:57:13 +00:00
// Allocate storage and copy the contents of the file to a memory buffer
2020-03-01 12:15:45 +00:00
std::vector<uint8_t> mBuffer(fileSize);
file->Read(&mBuffer[0], 1, fileSize);
mScene = pScene;
2015-05-19 03:57:13 +00:00
// Determine the type of the file
uint32_t fileType;
2020-03-01 12:15:45 +00:00
const char *sz = IFF::ReadHeader(&mBuffer[0], fileType);
if (sz) {
throw DeadlyImportError(sz);
}
2015-05-19 03:57:13 +00:00
mFileBuffer = &mBuffer[0] + 12;
fileSize -= 12;
// Initialize some members with their default values
2020-03-01 12:15:45 +00:00
hasNamedLayer = false;
2015-05-19 03:57:13 +00:00
// Create temporary storage on the stack but store pointers to it in the class
// instance. Therefore everything will be destructed properly if an exception
// is thrown and we needn't take care of that.
2020-03-01 12:15:45 +00:00
LayerList _mLayers;
SurfaceList _mSurfaces;
TagList _mTags;
2015-05-19 03:57:13 +00:00
TagMappingTable _mMapping;
2020-03-01 12:15:45 +00:00
mLayers = &_mLayers;
mTags = &_mTags;
mMapping = &_mMapping;
mSurfaces = &_mSurfaces;
2015-05-19 03:57:13 +00:00
// Allocate a default layer (layer indices are 1-based from now)
mLayers->push_back(Layer());
mCurLayer = &mLayers->back();
mCurLayer->mName = "<LWODefault>";
2020-03-01 12:15:45 +00:00
mCurLayer->mIndex = (uint16_t) -1;
2015-05-19 03:57:13 +00:00
// old lightwave file format (prior to v6)
if (AI_LWO_FOURCC_LWOB == fileType) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_INFO("LWO file format: LWOB (<= LightWave 5.5)");
2015-05-19 03:57:13 +00:00
mIsLWO2 = false;
mIsLXOB = false;
2015-05-19 03:57:13 +00:00
LoadLWOBFile();
} else if (AI_LWO_FOURCC_LWO2 == fileType) {
// New lightwave format
mIsLXOB = false;
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_INFO("LWO file format: LWO2 (>= LightWave 6)");
} else if (AI_LWO_FOURCC_LXOB == fileType) {
// MODO file format
mIsLXOB = true;
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_INFO("LWO file format: LXOB (Modo)");
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
else {
2015-05-19 03:57:13 +00:00
char szBuff[5];
szBuff[0] = (char)(fileType >> 24u);
szBuff[1] = (char)(fileType >> 16u);
szBuff[2] = (char)(fileType >> 8u);
szBuff[3] = (char)(fileType);
szBuff[4] = '\0';
throw DeadlyImportError(std::string("Unknown LWO sub format: ") + szBuff);
}
if (AI_LWO_FOURCC_LWOB != fileType) {
mIsLWO2 = true;
LoadLWO2File();
// The newer lightwave format allows the user to configure the
// loader that just one layer is used. If this is the case
// we need to check now whether the requested layer has been found.
if (UINT_MAX != configLayerIndex) {
unsigned int layerCount = 0;
2020-03-01 12:15:45 +00:00
for (std::list<LWO::Layer>::iterator itLayers = mLayers->begin(); itLayers != mLayers->end(); ++itLayers)
2015-05-19 03:57:13 +00:00
if (!itLayers->skip)
layerCount++;
2020-03-01 12:15:45 +00:00
if (layerCount != 2)
2015-05-19 03:57:13 +00:00
throw DeadlyImportError("LWO2: The requested layer was not found");
}
if (configLayerName.length() && !hasNamedLayer) {
2020-03-01 12:15:45 +00:00
throw DeadlyImportError("LWO2: Unable to find the requested layer: " + configLayerName);
2015-05-19 03:57:13 +00:00
}
}
// now, as we have loaded all data, we can resolve cross-referenced tags and clips
ResolveTags();
ResolveClips();
// now process all layers and build meshes and nodes
2020-03-01 12:15:45 +00:00
std::vector<aiMesh *> apcMeshes;
std::map<uint16_t, aiNode *> apcNodes;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
apcMeshes.reserve(mLayers->size() * std::min(((unsigned int)mSurfaces->size() / 2u), 1u));
2015-05-19 03:57:13 +00:00
unsigned int iDefaultSurface = UINT_MAX; // index of the default surface
2020-03-01 12:15:45 +00:00
for (LWO::Layer &layer : *mLayers) {
2015-05-19 03:57:13 +00:00
if (layer.skip)
continue;
// I don't know whether there could be dummy layers, but it would be possible
const unsigned int meshStart = (unsigned int)apcMeshes.size();
2020-03-01 12:15:45 +00:00
if (!layer.mFaces.empty() && !layer.mTempPoints.empty()) {
2015-05-19 03:57:13 +00:00
// now sort all faces by the surfaces assigned to them
2020-03-01 12:15:45 +00:00
std::vector<SortedRep> pSorted(mSurfaces->size() + 1);
2015-05-19 03:57:13 +00:00
unsigned int i = 0;
2020-03-01 12:15:45 +00:00
for (FaceList::iterator it = layer.mFaces.begin(), end = layer.mFaces.end(); it != end; ++it, ++i) {
2015-05-19 03:57:13 +00:00
// Check whether we support this face's type
if ((*it).type != AI_LWO_FACE && (*it).type != AI_LWO_PTCH &&
2020-03-01 12:15:45 +00:00
(*it).type != AI_LWO_BONE && (*it).type != AI_LWO_SUBD) {
2015-05-19 03:57:13 +00:00
continue;
}
unsigned int idx = (*it).surfaceIndex;
2020-03-01 12:15:45 +00:00
if (idx >= mTags->size()) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_WARN("LWO: Invalid face surface index");
2015-05-19 03:57:13 +00:00
idx = UINT_MAX;
}
2020-03-01 12:15:45 +00:00
if (UINT_MAX == idx || UINT_MAX == (idx = _mMapping[idx])) {
if (UINT_MAX == iDefaultSurface) {
2015-05-19 03:57:13 +00:00
iDefaultSurface = (unsigned int)mSurfaces->size();
mSurfaces->push_back(LWO::Surface());
2020-03-01 12:15:45 +00:00
LWO::Surface &surf = mSurfaces->back();
2015-05-19 03:57:13 +00:00
surf.mColor.r = surf.mColor.g = surf.mColor.b = 0.6f;
surf.mName = "LWODefaultSurface";
}
idx = iDefaultSurface;
}
pSorted[idx].push_back(i);
}
if (UINT_MAX == iDefaultSurface) {
2020-03-01 12:15:45 +00:00
pSorted.erase(pSorted.end() - 1);
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
for (unsigned int p = 0, j = 0; j < mSurfaces->size(); ++j) {
SortedRep &sorted = pSorted[j];
2015-05-19 03:57:13 +00:00
if (sorted.empty())
continue;
// generate the mesh
2020-03-01 12:15:45 +00:00
aiMesh *mesh = new aiMesh();
2015-05-19 03:57:13 +00:00
apcMeshes.push_back(mesh);
mesh->mNumFaces = (unsigned int)sorted.size();
// count the number of vertices
SortedRep::const_iterator it = sorted.begin(), end = sorted.end();
2020-03-01 12:15:45 +00:00
for (; it != end; ++it) {
2015-05-19 03:57:13 +00:00
mesh->mNumVertices += layer.mFaces[*it].mNumIndices;
}
2020-03-01 12:15:45 +00:00
aiVector3D *nrm = nullptr, *pv = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
aiFace *pf = mesh->mFaces = new aiFace[mesh->mNumFaces];
mesh->mMaterialIndex = j;
2015-05-19 03:57:13 +00:00
// find out which vertex color channels and which texture coordinate
// channels are really required by the material attached to this mesh
unsigned int vUVChannelIndices[AI_MAX_NUMBER_OF_TEXTURECOORDS];
unsigned int vVColorIndices[AI_MAX_NUMBER_OF_COLOR_SETS];
#ifdef ASSIMP_BUILD_DEBUG
2020-03-01 12:15:45 +00:00
for (unsigned int mui = 0; mui < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++mui) {
2015-05-19 03:57:13 +00:00
vUVChannelIndices[mui] = UINT_MAX;
}
2020-03-01 12:15:45 +00:00
for (unsigned int mui = 0; mui < AI_MAX_NUMBER_OF_COLOR_SETS; ++mui) {
2015-05-19 03:57:13 +00:00
vVColorIndices[mui] = UINT_MAX;
}
#endif
2020-03-01 12:15:45 +00:00
FindUVChannels(_mSurfaces[j], sorted, layer, vUVChannelIndices);
FindVCChannels(_mSurfaces[j], sorted, layer, vVColorIndices);
2015-05-19 03:57:13 +00:00
// allocate storage for UV and CV channels
2020-03-01 12:15:45 +00:00
aiVector3D *pvUV[AI_MAX_NUMBER_OF_TEXTURECOORDS];
for (unsigned int mui = 0; mui < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++mui) {
2015-05-19 03:57:13 +00:00
if (UINT_MAX == vUVChannelIndices[mui]) {
break;
}
pvUV[mui] = mesh->mTextureCoords[mui] = new aiVector3D[mesh->mNumVertices];
// LightWave doesn't support more than 2 UV components (?)
mesh->mNumUVComponents[0] = 2;
}
2020-03-01 12:15:45 +00:00
if (layer.mNormals.name.length()) {
2015-05-19 03:57:13 +00:00
nrm = mesh->mNormals = new aiVector3D[mesh->mNumVertices];
2020-03-01 12:15:45 +00:00
}
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
aiColor4D *pvVC[AI_MAX_NUMBER_OF_COLOR_SETS];
for (unsigned int mui = 0; mui < AI_MAX_NUMBER_OF_COLOR_SETS; ++mui) {
2015-05-19 03:57:13 +00:00
if (UINT_MAX == vVColorIndices[mui]) {
break;
}
pvVC[mui] = mesh->mColors[mui] = new aiColor4D[mesh->mNumVertices];
}
// we would not need this extra array, but the code is much cleaner if we use it
2020-03-01 12:15:45 +00:00
std::vector<unsigned int> &smoothingGroups = layer.mPointReferrers;
smoothingGroups.erase(smoothingGroups.begin(), smoothingGroups.end());
smoothingGroups.resize(mesh->mNumFaces, 0);
2015-05-19 03:57:13 +00:00
// now convert all faces
unsigned int vert = 0;
std::vector<unsigned int>::iterator outIt = smoothingGroups.begin();
2020-03-01 12:15:45 +00:00
for (it = sorted.begin(); it != end; ++it, ++outIt) {
const LWO::Face &face = layer.mFaces[*it];
2015-05-19 03:57:13 +00:00
*outIt = face.smoothGroup;
// copy all vertices
2020-03-01 12:15:45 +00:00
for (unsigned int q = 0; q < face.mNumIndices; ++q, ++vert) {
2015-05-19 03:57:13 +00:00
unsigned int idx = face.mIndices[q];
*pv++ = layer.mTempPoints[idx] /*- layer.mPivot*/;
// process UV coordinates
2020-03-01 12:15:45 +00:00
for (unsigned int w = 0; w < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++w) {
2015-05-19 03:57:13 +00:00
if (UINT_MAX == vUVChannelIndices[w]) {
break;
}
2020-03-01 12:15:45 +00:00
aiVector3D *&pp = pvUV[w];
const aiVector2D &src = ((aiVector2D *)&layer.mUVChannels[vUVChannelIndices[w]].rawData[0])[idx];
2015-05-19 03:57:13 +00:00
pp->x = src.x;
pp->y = src.y;
pp++;
}
// process normals (MODO extension)
2020-03-01 12:15:45 +00:00
if (nrm) {
*nrm = ((aiVector3D *)&layer.mNormals.rawData[0])[idx];
2015-05-19 03:57:13 +00:00
nrm->z *= -1.f;
++nrm;
}
// process vertex colors
2020-03-01 12:15:45 +00:00
for (unsigned int w = 0; w < AI_MAX_NUMBER_OF_COLOR_SETS; ++w) {
2015-05-19 03:57:13 +00:00
if (UINT_MAX == vVColorIndices[w]) {
break;
}
2020-03-01 12:15:45 +00:00
*pvVC[w] = ((aiColor4D *)&layer.mVColorChannels[vVColorIndices[w]].rawData[0])[idx];
2015-05-19 03:57:13 +00:00
// If a RGB color map is explicitly requested delete the
// alpha channel - it could theoretically be != 1.
2020-03-01 12:15:45 +00:00
if (_mSurfaces[i].mVCMapType == AI_LWO_RGB)
2015-05-19 03:57:13 +00:00
pvVC[w]->a = 1.f;
pvVC[w]++;
}
#if 0
2015-05-19 03:57:13 +00:00
// process vertex weights. We can't properly reconstruct the whole skeleton for now,
// but we can create dummy bones for all weight channels which we have.
for (unsigned int w = 0; w < layer.mWeightChannels.size();++w)
{
}
#endif
2015-05-19 03:57:13 +00:00
face.mIndices[q] = vert;
}
pf->mIndices = face.mIndices;
pf->mNumIndices = face.mNumIndices;
2020-03-01 12:15:45 +00:00
unsigned int **facePtr = (unsigned int **)&face.mIndices;
*facePtr = nullptr; // HACK: make sure it won't be deleted
2015-05-19 03:57:13 +00:00
pf++;
}
2020-03-01 12:15:45 +00:00
if (!mesh->mNormals) {
2015-05-19 03:57:13 +00:00
// Compute normal vectors for the mesh - we can't use our GenSmoothNormal-
// Step here since it wouldn't handle smoothing groups correctly for LWO.
// So we use a separate implementation.
ComputeNormals(mesh, smoothingGroups, _mSurfaces[j]);
2020-03-01 12:15:45 +00:00
} else {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_DEBUG("LWO2: No need to compute normals, they're already there");
}
2015-05-19 03:57:13 +00:00
++p;
}
}
// Generate nodes to render the mesh. Store the source layer in the mParent member of the nodes
unsigned int num = static_cast<unsigned int>(apcMeshes.size() - meshStart);
2015-05-19 03:57:13 +00:00
if (layer.mName != "<LWODefault>" || num > 0) {
2020-03-01 12:15:45 +00:00
aiNode *pcNode = new aiNode();
2015-05-19 03:57:13 +00:00
pcNode->mName.Set(layer.mName);
2020-03-01 12:15:45 +00:00
pcNode->mParent = (aiNode *)&layer;
2015-05-19 03:57:13 +00:00
pcNode->mNumMeshes = num;
if (pcNode->mNumMeshes) {
pcNode->mMeshes = new unsigned int[pcNode->mNumMeshes];
2020-03-01 12:15:45 +00:00
for (unsigned int p = 0; p < pcNode->mNumMeshes; ++p)
2015-05-19 03:57:13 +00:00
pcNode->mMeshes[p] = p + meshStart;
}
apcNodes[layer.mIndex] = pcNode;
2015-05-19 03:57:13 +00:00
}
}
if (apcNodes.empty() || apcMeshes.empty())
throw DeadlyImportError("LWO: No meshes loaded");
// The RemoveRedundantMaterials step will clean this up later
2020-03-01 12:15:45 +00:00
pScene->mMaterials = new aiMaterial *[pScene->mNumMaterials = (unsigned int)mSurfaces->size()];
for (unsigned int mat = 0; mat < pScene->mNumMaterials; ++mat) {
aiMaterial *pcMat = new aiMaterial();
2015-05-19 03:57:13 +00:00
pScene->mMaterials[mat] = pcMat;
2020-03-01 12:15:45 +00:00
ConvertMaterial((*mSurfaces)[mat], pcMat);
2015-05-19 03:57:13 +00:00
}
// copy the meshes to the output structure
2020-03-01 12:15:45 +00:00
pScene->mMeshes = new aiMesh *[pScene->mNumMeshes = (unsigned int)apcMeshes.size()];
::memcpy(pScene->mMeshes, &apcMeshes[0], pScene->mNumMeshes * sizeof(void *));
2015-05-19 03:57:13 +00:00
// generate the final node graph
GenerateNodeGraph(apcNodes);
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::ComputeNormals(aiMesh *mesh, const std::vector<unsigned int> &smoothingGroups,
const LWO::Surface &surface) {
2015-05-19 03:57:13 +00:00
// Allocate output storage
mesh->mNormals = new aiVector3D[mesh->mNumVertices];
// First generate per-face normals
2020-03-01 12:15:45 +00:00
aiVector3D *out;
2015-05-19 03:57:13 +00:00
std::vector<aiVector3D> faceNormals;
// ... in some cases that's already enough
if (!surface.mMaximumSmoothAngle)
out = mesh->mNormals;
2020-03-01 12:15:45 +00:00
else {
2015-05-19 03:57:13 +00:00
faceNormals.resize(mesh->mNumVertices);
out = &faceNormals[0];
}
2020-03-01 12:15:45 +00:00
aiFace *begin = mesh->mFaces, *const end = mesh->mFaces + mesh->mNumFaces;
for (; begin != end; ++begin) {
aiFace &face = *begin;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
if (face.mNumIndices < 3) {
2015-05-19 03:57:13 +00:00
continue;
}
// LWO doc: "the normal is defined as the cross product of the first and last edges"
2020-03-01 12:15:45 +00:00
aiVector3D *pV1 = mesh->mVertices + face.mIndices[0];
aiVector3D *pV2 = mesh->mVertices + face.mIndices[1];
aiVector3D *pV3 = mesh->mVertices + face.mIndices[face.mNumIndices - 1];
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
aiVector3D vNor = ((*pV2 - *pV1) ^ (*pV3 - *pV1)).Normalize();
for (unsigned int i = 0; i < face.mNumIndices; ++i)
2015-05-19 03:57:13 +00:00
out[face.mIndices[i]] = vNor;
}
2020-03-01 12:15:45 +00:00
if (!surface.mMaximumSmoothAngle) return;
2015-05-19 03:57:13 +00:00
const float posEpsilon = ComputePositionEpsilon(mesh);
// Now generate the spatial sort tree
SGSpatialSort sSort;
std::vector<unsigned int>::const_iterator it = smoothingGroups.begin();
2020-03-01 12:15:45 +00:00
for (begin = mesh->mFaces; begin != end; ++begin, ++it) {
aiFace &face = *begin;
for (unsigned int i = 0; i < face.mNumIndices; ++i) {
2015-05-19 03:57:13 +00:00
unsigned int tt = face.mIndices[i];
2020-03-01 12:15:45 +00:00
sSort.Add(mesh->mVertices[tt], tt, *it);
2015-05-19 03:57:13 +00:00
}
}
// Sort everything - this takes O(nlogn) time
sSort.Prepare();
std::vector<unsigned int> poResult;
poResult.reserve(20);
// Generate vertex normals. We have O(logn) for the binary lookup, which we need
// for n elements, thus the EXPECTED complexity is O(nlogn)
2020-03-01 12:15:45 +00:00
if (surface.mMaximumSmoothAngle < 3.f && !configSpeedFlag) {
2015-05-19 03:57:13 +00:00
const float fLimit = std::cos(surface.mMaximumSmoothAngle);
2020-03-01 12:15:45 +00:00
for (begin = mesh->mFaces, it = smoothingGroups.begin(); begin != end; ++begin, ++it) {
const aiFace &face = *begin;
unsigned int *beginIdx = face.mIndices, *const endIdx = face.mIndices + face.mNumIndices;
for (; beginIdx != endIdx; ++beginIdx) {
2015-05-19 03:57:13 +00:00
unsigned int idx = *beginIdx;
2020-03-01 12:15:45 +00:00
sSort.FindPositions(mesh->mVertices[idx], *it, posEpsilon, poResult, true);
2015-05-19 03:57:13 +00:00
aiVector3D vNormals;
2020-03-01 12:15:45 +00:00
for (std::vector<unsigned int>::const_iterator a = poResult.begin(); a != poResult.end(); ++a) {
const aiVector3D &v = faceNormals[*a];
2015-05-19 03:57:13 +00:00
if (v * faceNormals[idx] < fLimit)
continue;
vNormals += v;
}
mesh->mNormals[idx] = vNormals.Normalize();
}
}
}
2020-03-01 12:15:45 +00:00
// faster code path in case there is no smooth angle
else {
std::vector<bool> vertexDone(mesh->mNumVertices, false);
for (begin = mesh->mFaces, it = smoothingGroups.begin(); begin != end; ++begin, ++it) {
const aiFace &face = *begin;
unsigned int *beginIdx = face.mIndices, *const endIdx = face.mIndices + face.mNumIndices;
for (; beginIdx != endIdx; ++beginIdx) {
2015-05-19 03:57:13 +00:00
unsigned int idx = *beginIdx;
if (vertexDone[idx])
continue;
2020-03-01 12:15:45 +00:00
sSort.FindPositions(mesh->mVertices[idx], *it, posEpsilon, poResult, true);
2015-05-19 03:57:13 +00:00
aiVector3D vNormals;
2020-03-01 12:15:45 +00:00
for (std::vector<unsigned int>::const_iterator a = poResult.begin(); a != poResult.end(); ++a) {
const aiVector3D &v = faceNormals[*a];
2015-05-19 03:57:13 +00:00
vNormals += v;
}
vNormals.Normalize();
2020-03-01 12:15:45 +00:00
for (std::vector<unsigned int>::const_iterator a = poResult.begin(); a != poResult.end(); ++a) {
2015-05-19 03:57:13 +00:00
mesh->mNormals[*a] = vNormals;
vertexDone[*a] = true;
}
}
}
}
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::GenerateNodeGraph(std::map<uint16_t, aiNode *> &apcNodes) {
2015-05-19 03:57:13 +00:00
// now generate the final nodegraph - generate a root node and attach children
2020-03-01 12:15:45 +00:00
aiNode *root = mScene->mRootNode = new aiNode();
2015-05-19 03:57:13 +00:00
root->mName.Set("<LWORoot>");
//Set parent of all children, inserting pivots
2020-03-01 12:15:45 +00:00
std::map<uint16_t, aiNode *> mapPivot;
for (auto itapcNodes = apcNodes.begin(); itapcNodes != apcNodes.end(); ++itapcNodes) {
2015-05-19 03:57:13 +00:00
//Get the parent index
2020-03-01 12:15:45 +00:00
LWO::Layer *nodeLayer = (LWO::Layer *)(itapcNodes->second->mParent);
2015-05-19 03:57:13 +00:00
uint16_t parentIndex = nodeLayer->mParent;
//Create pivot node, store it into the pivot map, and set the parent as the pivot
2020-03-01 12:15:45 +00:00
aiNode *pivotNode = new aiNode();
pivotNode->mName.Set("Pivot-" + std::string(itapcNodes->second->mName.data));
2015-05-19 03:57:13 +00:00
itapcNodes->second->mParent = pivotNode;
//Look for the parent node to attach the pivot to
if (apcNodes.find(parentIndex) != apcNodes.end()) {
pivotNode->mParent = apcNodes[parentIndex];
} else {
//If not, attach to the root node
pivotNode->mParent = root;
}
//Set the node and the pivot node transformation
itapcNodes->second->mTransformation.a4 = -nodeLayer->mPivot.x;
itapcNodes->second->mTransformation.b4 = -nodeLayer->mPivot.y;
itapcNodes->second->mTransformation.c4 = -nodeLayer->mPivot.z;
pivotNode->mTransformation.a4 = nodeLayer->mPivot.x;
pivotNode->mTransformation.b4 = nodeLayer->mPivot.y;
pivotNode->mTransformation.c4 = nodeLayer->mPivot.z;
2020-03-01 12:15:45 +00:00
mapPivot[-(itapcNodes->first + 2)] = pivotNode;
2015-05-19 03:57:13 +00:00
}
//Merge pivot map into node map
for (auto itMapPivot = mapPivot.begin(); itMapPivot != mapPivot.end(); ++itMapPivot) {
2015-05-19 03:57:13 +00:00
apcNodes[itMapPivot->first] = itMapPivot->second;
}
//Set children of all parents
2020-03-01 12:15:45 +00:00
apcNodes[(uint16_t)-1] = root;
for (auto itMapParentNodes = apcNodes.begin(); itMapParentNodes != apcNodes.end(); ++itMapParentNodes) {
for (auto itMapChildNodes = apcNodes.begin(); itMapChildNodes != apcNodes.end(); ++itMapChildNodes) {
2015-05-19 03:57:13 +00:00
if ((itMapParentNodes->first != itMapChildNodes->first) && (itMapParentNodes->second == itMapChildNodes->second->mParent)) {
++(itMapParentNodes->second->mNumChildren);
}
}
if (itMapParentNodes->second->mNumChildren) {
2020-03-01 12:15:45 +00:00
itMapParentNodes->second->mChildren = new aiNode *[itMapParentNodes->second->mNumChildren];
2015-05-19 03:57:13 +00:00
uint16_t p = 0;
for (auto itMapChildNodes = apcNodes.begin(); itMapChildNodes != apcNodes.end(); ++itMapChildNodes) {
2015-05-19 03:57:13 +00:00
if ((itMapParentNodes->first != itMapChildNodes->first) && (itMapParentNodes->second == itMapChildNodes->second->mParent)) {
itMapParentNodes->second->mChildren[p++] = itMapChildNodes->second;
}
}
}
}
2020-03-01 12:15:45 +00:00
if (!mScene->mRootNode->mNumChildren)
2015-05-19 03:57:13 +00:00
throw DeadlyImportError("LWO: Unable to build a valid node graph");
// Remove a single root node with no meshes assigned to it ...
2020-03-01 12:15:45 +00:00
if (1 == mScene->mRootNode->mNumChildren) {
aiNode *pc = mScene->mRootNode->mChildren[0];
pc->mParent = mScene->mRootNode->mChildren[0] = nullptr;
delete mScene->mRootNode;
mScene->mRootNode = pc;
2015-05-19 03:57:13 +00:00
}
// convert the whole stuff to RH with CCW winding
MakeLeftHandedProcess maker;
2020-03-01 12:15:45 +00:00
maker.Execute(mScene);
2015-05-19 03:57:13 +00:00
FlipWindingOrderProcess flipper;
2020-03-01 12:15:45 +00:00
flipper.Execute(mScene);
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::ResolveTags() {
2015-05-19 03:57:13 +00:00
// --- this function is used for both LWO2 and LWOB
mMapping->resize(mTags->size(), UINT_MAX);
2020-03-01 12:15:45 +00:00
for (unsigned int a = 0; a < mTags->size(); ++a) {
2020-03-01 12:15:45 +00:00
const std::string &c = (*mTags)[a];
for (unsigned int i = 0; i < mSurfaces->size(); ++i) {
2020-03-01 12:15:45 +00:00
const std::string &d = (*mSurfaces)[i].mName;
if (!ASSIMP_stricmp(c, d)) {
2015-05-19 03:57:13 +00:00
(*mMapping)[a] = i;
break;
}
}
}
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::ResolveClips() {
for (unsigned int i = 0; i < mClips.size(); ++i) {
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
Clip &clip = mClips[i];
2015-05-19 03:57:13 +00:00
if (Clip::REF == clip.type) {
2020-03-01 12:15:45 +00:00
if (clip.clipRef >= mClips.size()) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWO2: Clip referrer index is out of range");
2015-05-19 03:57:13 +00:00
clip.clipRef = 0;
}
2020-03-01 12:15:45 +00:00
Clip &dest = mClips[clip.clipRef];
2015-05-19 03:57:13 +00:00
if (Clip::REF == dest.type) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWO2: Clip references another clip reference");
2015-05-19 03:57:13 +00:00
clip.type = Clip::UNSUPPORTED;
}
2020-03-01 12:15:45 +00:00
else {
2015-05-19 03:57:13 +00:00
clip.path = dest.path;
clip.type = dest.type;
}
}
}
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::AdjustTexturePath(std::string &out) {
2015-05-19 03:57:13 +00:00
// --- this function is used for both LWO2 and LWOB
2020-03-01 12:15:45 +00:00
if (!mIsLWO2 && ::strstr(out.c_str(), "(sequence)")) {
2015-05-19 03:57:13 +00:00
// remove the (sequence) and append 000
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_INFO("LWOB: Sequence of animated texture found. It will be ignored");
2020-03-01 12:15:45 +00:00
out = out.substr(0, out.length() - 10) + "000";
2015-05-19 03:57:13 +00:00
}
// format: drive:path/file - we just need to insert a slash after the drive
std::string::size_type n = out.find_first_of(':');
if (std::string::npos != n) {
2020-03-01 12:15:45 +00:00
out.insert(n + 1, "/");
2015-05-19 03:57:13 +00:00
}
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::LoadLWOTags(unsigned int size) {
2015-05-19 03:57:13 +00:00
// --- this function is used for both LWO2 and LWOB
2020-03-01 12:15:45 +00:00
const char *szCur = (const char *)mFileBuffer, *szLast = szCur;
const char *const szEnd = szLast + size;
while (szCur < szEnd) {
if (!(*szCur)) {
const size_t len = (size_t)(szCur - szLast);
2015-05-19 03:57:13 +00:00
// FIX: skip empty-sized tags
if (len)
2020-03-01 12:15:45 +00:00
mTags->push_back(std::string(szLast, len));
szCur += (len & 0x1 ? 1 : 2);
2015-05-19 03:57:13 +00:00
szLast = szCur;
}
szCur++;
}
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::LoadLWOPoints(unsigned int length) {
2015-05-19 03:57:13 +00:00
// --- this function is used for both LWO2 and LWOB but for
// LWO2 we need to allocate 25% more storage - it could be we'll
// need to duplicate some points later.
const size_t vertexLen = 12;
2020-03-01 12:15:45 +00:00
if ((length % vertexLen) != 0) {
throw DeadlyImportError("LWO2: Points chunk length is not multiple of vertexLen (12)");
2015-05-19 03:57:13 +00:00
}
unsigned int regularSize = (unsigned int)mCurLayer->mTempPoints.size() + length / 12;
2020-03-01 12:15:45 +00:00
if (mIsLWO2) {
mCurLayer->mTempPoints.reserve(regularSize + (regularSize >> 2u));
mCurLayer->mTempPoints.resize(regularSize);
2015-05-19 03:57:13 +00:00
// initialize all point referrers with the default values
2020-03-01 12:15:45 +00:00
mCurLayer->mPointReferrers.reserve(regularSize + (regularSize >> 2u));
mCurLayer->mPointReferrers.resize(regularSize, UINT_MAX);
} else
mCurLayer->mTempPoints.resize(regularSize);
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
// perform endianness conversions
#ifndef AI_BUILD_BIG_ENDIAN
2020-03-01 12:15:45 +00:00
for (unsigned int i = 0; i<length >> 2; ++i)
ByteSwap::Swap4(mFileBuffer + (i << 2));
#endif
2020-03-01 12:15:45 +00:00
::memcpy(&mCurLayer->mTempPoints[0], mFileBuffer, length);
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::LoadLWO2Polygons(unsigned int length) {
LE_NCONST uint16_t *const end = (LE_NCONST uint16_t *)(mFileBuffer + length);
2015-05-19 03:57:13 +00:00
const uint32_t type = GetU4();
// Determine the type of the polygons
2020-03-01 12:15:45 +00:00
switch (type) {
// read unsupported stuff too (although we won't process it)
case AI_LWO_MBAL:
ASSIMP_LOG_WARN("LWO2: Encountered unsupported primitive chunk (METABALL)");
break;
case AI_LWO_CURV:
ASSIMP_LOG_WARN("LWO2: Encountered unsupported primitive chunk (SPLINE)");
;
break;
// These are ok with no restrictions
case AI_LWO_PTCH:
case AI_LWO_FACE:
case AI_LWO_BONE:
case AI_LWO_SUBD:
break;
default:
// hm!? wtf is this? ok ...
ASSIMP_LOG_ERROR("LWO2: Ignoring unknown polygon type.");
break;
2015-05-19 03:57:13 +00:00
}
// first find out how many faces and vertices we'll finally need
2020-03-01 12:15:45 +00:00
uint16_t *cursor = (uint16_t *)mFileBuffer;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
unsigned int iNumFaces = 0, iNumVertices = 0;
CountVertsAndFacesLWO2(iNumVertices, iNumFaces, cursor, end);
2015-05-19 03:57:13 +00:00
// allocate the output array and copy face indices
2020-03-01 12:15:45 +00:00
if (iNumFaces) {
cursor = (uint16_t *)mFileBuffer;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
mCurLayer->mFaces.resize(iNumFaces, LWO::Face(type));
2015-05-19 03:57:13 +00:00
FaceList::iterator it = mCurLayer->mFaces.begin();
2020-03-01 12:15:45 +00:00
CopyFaceIndicesLWO2(it, cursor, end);
2015-05-19 03:57:13 +00:00
}
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::CountVertsAndFacesLWO2(unsigned int &verts, unsigned int &faces,
uint16_t *&cursor, const uint16_t *const end, unsigned int max) {
while (cursor < end && max--) {
2015-05-19 03:57:13 +00:00
uint16_t numIndices;
::memcpy(&numIndices, cursor++, 2);
AI_LSWAP2(numIndices);
numIndices &= 0x03FF;
verts += numIndices;
++faces;
2020-03-01 12:15:45 +00:00
for (uint16_t i = 0; i < numIndices; i++) {
ReadVSizedIntLWO2((uint8_t *&)cursor);
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::CopyFaceIndicesLWO2(FaceList::iterator &it,
uint16_t *&cursor,
const uint16_t *const end) {
while (cursor < end) {
LWO::Face &face = *it++;
2015-05-19 03:57:13 +00:00
uint16_t numIndices;
::memcpy(&numIndices, cursor++, 2);
AI_LSWAP2(numIndices);
face.mNumIndices = numIndices & 0x03FF;
2020-03-01 12:15:45 +00:00
if (face.mNumIndices) /* byte swapping has already been done */
2015-05-19 03:57:13 +00:00
{
face.mIndices = new unsigned int[face.mNumIndices];
2020-03-01 12:15:45 +00:00
for (unsigned int i = 0; i < face.mNumIndices; i++) {
face.mIndices[i] = ReadVSizedIntLWO2((uint8_t *&)cursor) + mCurLayer->mPointIDXOfs;
if (face.mIndices[i] > mCurLayer->mTempPoints.size()) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_WARN("LWO2: Failure evaluating face record, index is out of range");
2020-03-01 12:15:45 +00:00
face.mIndices[i] = (unsigned int)mCurLayer->mTempPoints.size() - 1;
2015-05-19 03:57:13 +00:00
}
}
2020-03-01 12:15:45 +00:00
} else
throw DeadlyImportError("LWO2: Encountered invalid face record with zero indices");
2015-05-19 03:57:13 +00:00
}
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::LoadLWO2PolygonTags(unsigned int length) {
LE_NCONST uint8_t *const end = mFileBuffer + length;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
AI_LWO_VALIDATE_CHUNK_LENGTH(length, PTAG, 4);
2015-05-19 03:57:13 +00:00
uint32_t type = GetU4();
if (type != AI_LWO_SURF && type != AI_LWO_SMGP)
return;
2020-03-01 12:15:45 +00:00
while (mFileBuffer < end) {
2015-05-19 03:57:13 +00:00
unsigned int i = ReadVSizedIntLWO2(mFileBuffer) + mCurLayer->mFaceIDXOfs;
unsigned int j = GetU2();
2020-03-01 12:15:45 +00:00
if (i >= mCurLayer->mFaces.size()) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_WARN("LWO2: face index in PTAG is out of range");
2015-05-19 03:57:13 +00:00
continue;
}
2020-03-01 12:15:45 +00:00
switch (type) {
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
case AI_LWO_SURF:
mCurLayer->mFaces[i].surfaceIndex = j;
break;
case AI_LWO_SMGP: /* is that really used? */
mCurLayer->mFaces[i].smoothGroup = j;
break;
2015-05-19 03:57:13 +00:00
};
}
}
// ------------------------------------------------------------------------------------------------
template <class T>
2020-03-01 12:15:45 +00:00
VMapEntry *FindEntry(std::vector<T> &list, const std::string &name, bool perPoly) {
for (auto &elem : list) {
2016-05-21 20:28:33 +00:00
if (elem.name == name) {
2020-03-01 12:15:45 +00:00
if (!perPoly) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_WARN("LWO2: Found two VMAP sections with equal names");
2015-05-19 03:57:13 +00:00
}
2016-05-21 20:28:33 +00:00
return &elem;
2015-05-19 03:57:13 +00:00
}
}
2020-03-01 12:15:45 +00:00
list.push_back(T());
VMapEntry *p = &list.back();
2015-05-19 03:57:13 +00:00
p->name = name;
return p;
}
// ------------------------------------------------------------------------------------------------
template <class T>
2020-03-01 12:15:45 +00:00
inline void CreateNewEntry(T &chan, unsigned int srcIdx) {
2015-05-19 03:57:13 +00:00
if (!chan.name.length())
return;
2015-05-19 03:57:13 +00:00
chan.abAssigned[srcIdx] = true;
2020-03-01 12:15:45 +00:00
chan.abAssigned.resize(chan.abAssigned.size() + 1, false);
2020-03-01 12:15:45 +00:00
for (unsigned int a = 0; a < chan.dims; ++a)
chan.rawData.push_back(chan.rawData[srcIdx * chan.dims + a]);
}
// ------------------------------------------------------------------------------------------------
template <class T>
2020-03-01 12:15:45 +00:00
inline void CreateNewEntry(std::vector<T> &list, unsigned int srcIdx) {
for (auto &elem : list) {
CreateNewEntry(elem, srcIdx);
2015-05-19 03:57:13 +00:00
}
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
inline void LWOImporter::DoRecursiveVMAPAssignment(VMapEntry *base, unsigned int numRead,
unsigned int idx, float *data) {
2015-05-19 03:57:13 +00:00
ai_assert(NULL != data);
2020-03-01 12:15:45 +00:00
LWO::ReferrerList &refList = mCurLayer->mPointReferrers;
2015-05-19 03:57:13 +00:00
unsigned int i;
if (idx >= base->abAssigned.size()) {
throw DeadlyImportError("Bad index");
}
2015-05-19 03:57:13 +00:00
base->abAssigned[idx] = true;
2020-03-01 12:15:45 +00:00
for (i = 0; i < numRead; ++i) {
base->rawData[idx * base->dims + i] = data[i];
2015-05-19 03:57:13 +00:00
}
if (UINT_MAX != (i = refList[idx])) {
2020-03-01 12:15:45 +00:00
DoRecursiveVMAPAssignment(base, numRead, i, data);
2015-05-19 03:57:13 +00:00
}
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
inline void AddToSingleLinkedList(ReferrerList &refList, unsigned int srcIdx, unsigned int destIdx) {
if (UINT_MAX == refList[srcIdx]) {
2015-05-19 03:57:13 +00:00
refList[srcIdx] = destIdx;
return;
}
2020-03-01 12:15:45 +00:00
AddToSingleLinkedList(refList, refList[srcIdx], destIdx);
}
// ------------------------------------------------------------------------------------------------
// Load LWO2 vertex map
2020-03-01 12:15:45 +00:00
void LWOImporter::LoadLWO2VertexMap(unsigned int length, bool perPoly) {
LE_NCONST uint8_t *const end = mFileBuffer + length;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
AI_LWO_VALIDATE_CHUNK_LENGTH(length, VMAP, 6);
2015-05-19 03:57:13 +00:00
unsigned int type = GetU4();
unsigned int dims = GetU2();
2020-03-01 12:15:45 +00:00
VMapEntry *base;
2015-05-19 03:57:13 +00:00
// read the name of the vertex map
std::string name;
2020-03-01 12:15:45 +00:00
GetS0(name, length);
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
switch (type) {
case AI_LWO_TXUV:
if (dims != 2) {
ASSIMP_LOG_WARN("LWO2: Skipping UV channel \'" + name + "\' with !2 components");
return;
}
base = FindEntry(mCurLayer->mUVChannels, name, perPoly);
break;
case AI_LWO_WGHT:
case AI_LWO_MNVW:
if (dims != 1) {
ASSIMP_LOG_WARN("LWO2: Skipping Weight Channel \'" + name + "\' with !1 components");
return;
}
base = FindEntry((type == AI_LWO_WGHT ? mCurLayer->mWeightChannels : mCurLayer->mSWeightChannels), name, perPoly);
break;
case AI_LWO_RGB:
case AI_LWO_RGBA:
if (dims != 3 && dims != 4) {
ASSIMP_LOG_WARN("LWO2: Skipping Color Map \'" + name + "\' with a dimension > 4 or < 3");
return;
}
base = FindEntry(mCurLayer->mVColorChannels, name, perPoly);
break;
case AI_LWO_MODO_NORM:
/* This is a non-standard extension chunk used by Luxology's MODO.
2015-05-19 03:57:13 +00:00
* It stores per-vertex normals. This VMAP exists just once, has
* 3 dimensions and is btw extremely beautiful.
*/
2020-03-01 12:15:45 +00:00
if (name != "vert_normals" || dims != 3 || mCurLayer->mNormals.name.length())
return;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
ASSIMP_LOG_INFO("Processing non-standard extension: MODO VMAP.NORM.vert_normals");
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
mCurLayer->mNormals.name = name;
base = &mCurLayer->mNormals;
break;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
case AI_LWO_PICK: /* these VMAPs are just silently dropped */
case AI_LWO_MORF:
case AI_LWO_SPOT:
return;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
default:
if (name == "APS.Level") {
// XXX handle this (seems to be subdivision-related).
}
ASSIMP_LOG_WARN_F("LWO2: Skipping unknown VMAP/VMAD channel \'", name, "\'");
return;
2015-05-19 03:57:13 +00:00
};
base->Allocate((unsigned int)mCurLayer->mTempPoints.size());
// now read all entries in the map
2020-03-01 12:15:45 +00:00
type = std::min(dims, base->dims);
const unsigned int diff = (dims - type) << 2u;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
LWO::FaceList &list = mCurLayer->mFaces;
LWO::PointList &pointList = mCurLayer->mTempPoints;
LWO::ReferrerList &refList = mCurLayer->mPointReferrers;
2015-05-19 03:57:13 +00:00
const unsigned int numPoints = (unsigned int)pointList.size();
2020-03-01 12:15:45 +00:00
const unsigned int numFaces = (unsigned int)list.size();
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
while (mFileBuffer < end) {
2015-05-19 03:57:13 +00:00
unsigned int idx = ReadVSizedIntLWO2(mFileBuffer) + mCurLayer->mPointIDXOfs;
2020-03-01 12:15:45 +00:00
if (idx >= numPoints) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_WARN_F("LWO2: Failure evaluating VMAP/VMAD entry \'", name, "\', vertex index is out of range");
2020-03-01 12:15:45 +00:00
mFileBuffer += base->dims << 2u;
2015-05-19 03:57:13 +00:00
continue;
}
2020-03-01 12:15:45 +00:00
if (perPoly) {
2015-05-19 03:57:13 +00:00
unsigned int polyIdx = ReadVSizedIntLWO2(mFileBuffer) + mCurLayer->mFaceIDXOfs;
2020-03-01 12:15:45 +00:00
if (base->abAssigned[idx]) {
2015-05-19 03:57:13 +00:00
// we have already a VMAP entry for this vertex - thus
// we need to duplicate the corresponding polygon.
2020-03-01 12:15:45 +00:00
if (polyIdx >= numFaces) {
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_WARN_F("LWO2: Failure evaluating VMAD entry \'", name, "\', polygon index is out of range");
2020-03-01 12:15:45 +00:00
mFileBuffer += base->dims << 2u;
2015-05-19 03:57:13 +00:00
continue;
}
2020-03-01 12:15:45 +00:00
LWO::Face &src = list[polyIdx];
2015-05-19 03:57:13 +00:00
// generate a new unique vertex for the corresponding index - but only
// if we can find the index in the face
bool had = false;
2020-03-01 12:15:45 +00:00
for (unsigned int i = 0; i < src.mNumIndices; ++i) {
2015-05-19 03:57:13 +00:00
unsigned int srcIdx = src.mIndices[i], tmp = idx;
do {
if (tmp == srcIdx)
break;
2020-03-01 12:15:45 +00:00
} while ((tmp = refList[tmp]) != UINT_MAX);
2015-05-19 03:57:13 +00:00
if (tmp == UINT_MAX) {
continue;
}
had = true;
2020-03-01 12:15:45 +00:00
refList.resize(refList.size() + 1, UINT_MAX);
2015-05-19 03:57:13 +00:00
idx = (unsigned int)pointList.size();
src.mIndices[i] = (unsigned int)pointList.size();
// store the index of the new vertex in the old vertex
// so we get a single linked list we can traverse in
// only one direction
2020-03-01 12:15:45 +00:00
AddToSingleLinkedList(refList, srcIdx, src.mIndices[i]);
2015-05-19 03:57:13 +00:00
pointList.push_back(pointList[srcIdx]);
2020-03-01 12:15:45 +00:00
CreateNewEntry(mCurLayer->mVColorChannels, srcIdx);
CreateNewEntry(mCurLayer->mUVChannels, srcIdx);
CreateNewEntry(mCurLayer->mWeightChannels, srcIdx);
CreateNewEntry(mCurLayer->mSWeightChannels, srcIdx);
CreateNewEntry(mCurLayer->mNormals, srcIdx);
2015-05-19 03:57:13 +00:00
}
if (!had) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_WARN_F("LWO2: Failure evaluating VMAD entry \'", name, "\', vertex index wasn't found in that polygon");
2015-05-19 03:57:13 +00:00
ai_assert(had);
}
}
}
2016-09-24 18:19:43 +00:00
std::unique_ptr<float[]> temp(new float[type]);
2020-03-01 12:15:45 +00:00
for (unsigned int l = 0; l < type; ++l)
2015-05-19 03:57:13 +00:00
temp[l] = GetF4();
2020-03-01 12:15:45 +00:00
DoRecursiveVMAPAssignment(base, type, idx, temp.get());
2015-05-19 03:57:13 +00:00
mFileBuffer += diff;
}
}
// ------------------------------------------------------------------------------------------------
// Load LWO2 clip
2020-03-01 12:15:45 +00:00
void LWOImporter::LoadLWO2Clip(unsigned int length) {
AI_LWO_VALIDATE_CHUNK_LENGTH(length, CLIP, 10);
2015-05-19 03:57:13 +00:00
mClips.push_back(LWO::Clip());
2020-03-01 12:15:45 +00:00
LWO::Clip &clip = mClips.back();
2015-05-19 03:57:13 +00:00
// first - get the index of the clip
clip.idx = GetU4();
IFF::SubChunkHeader head = IFF::LoadSubChunk(mFileBuffer);
2020-03-01 12:15:45 +00:00
switch (head.type) {
case AI_LWO_STIL:
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, STIL, 1);
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
// "Normal" texture
GetS0(clip.path, head.length);
clip.type = Clip::STILL;
break;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
case AI_LWO_ISEQ:
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, ISEQ, 16);
// Image sequence. We'll later take the first.
{
uint8_t digits = GetU1();
mFileBuffer++;
int16_t offset = GetU2();
mFileBuffer += 4;
int16_t start = GetU2();
mFileBuffer += 4;
std::string s;
std::ostringstream ss;
GetS0(s, head.length);
head.length -= (uint16_t)s.length() + 1;
ss << s;
ss << std::setw(digits) << offset + start;
GetS0(s, head.length);
ss << s;
clip.path = ss.str();
clip.type = Clip::SEQ;
}
break;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
case AI_LWO_STCC:
ASSIMP_LOG_WARN("LWO2: Color shifted images are not supported");
break;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
case AI_LWO_ANIM:
ASSIMP_LOG_WARN("LWO2: Animated textures are not supported");
break;
case AI_LWO_XREF:
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, XREF, 4);
// Just a cross-reference to another CLIp
clip.type = Clip::REF;
clip.clipRef = GetU4();
break;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
case AI_LWO_NEGA:
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, NEGA, 2);
clip.negate = (0 != GetU2());
break;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
default:
ASSIMP_LOG_WARN("LWO2: Encountered unknown CLIP sub-chunk");
2015-05-19 03:57:13 +00:00
}
}
// ------------------------------------------------------------------------------------------------
// Load envelope description
2020-03-01 12:15:45 +00:00
void LWOImporter::LoadLWO2Envelope(unsigned int length) {
LE_NCONST uint8_t *const end = mFileBuffer + length;
AI_LWO_VALIDATE_CHUNK_LENGTH(length, ENVL, 4);
2015-05-19 03:57:13 +00:00
mEnvelopes.push_back(LWO::Envelope());
2020-03-01 12:15:45 +00:00
LWO::Envelope &envelope = mEnvelopes.back();
2015-05-19 03:57:13 +00:00
// Get the index of the envelope
envelope.index = ReadVSizedIntLWO2(mFileBuffer);
2015-05-19 03:57:13 +00:00
// It looks like there might be an extra U4 right after the index,
// at least in modo (LXOB) files: we'll ignore it if it's zero,
// otherwise it represents the start of a subchunk, so we backtrack.
2020-03-01 12:15:45 +00:00
if (mIsLXOB) {
uint32_t extra = GetU4();
2020-03-01 12:15:45 +00:00
if (extra) {
mFileBuffer -= 4;
}
2015-05-19 03:57:13 +00:00
}
// ... and read all subchunks
2020-03-01 12:15:45 +00:00
while (true) {
if (mFileBuffer + 6 >= end) break;
2015-05-19 03:57:13 +00:00
LE_NCONST IFF::SubChunkHeader head = IFF::LoadSubChunk(mFileBuffer);
if (mFileBuffer + head.length > end)
throw DeadlyImportError("LWO2: Invalid envelope chunk length");
2020-03-01 12:15:45 +00:00
uint8_t *const next = mFileBuffer + head.length;
switch (head.type) {
// Type & representation of the envelope
case AI_LWO_TYPE:
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, TYPE, 2);
mFileBuffer++; // skip user format
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
// Determine type of envelope
envelope.type = (LWO::EnvelopeType)*mFileBuffer;
++mFileBuffer;
break;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
// precondition
case AI_LWO_PRE:
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, PRE, 2);
envelope.pre = (LWO::PrePostBehaviour)GetU2();
break;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
// postcondition
case AI_LWO_POST:
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, POST, 2);
envelope.post = (LWO::PrePostBehaviour)GetU2();
break;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
// keyframe
case AI_LWO_KEY: {
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, KEY, 8);
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
envelope.keys.push_back(LWO::Key());
LWO::Key &key = envelope.keys.back();
key.time = GetF4();
key.value = GetF4();
break;
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
// interval interpolation
case AI_LWO_SPAN: {
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, SPAN, 4);
if (envelope.keys.size() < 2)
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_WARN("LWO2: Unexpected SPAN chunk");
2015-05-19 03:57:13 +00:00
else {
2020-03-01 12:15:45 +00:00
LWO::Key &key = envelope.keys.back();
switch (GetU4()) {
2015-05-19 03:57:13 +00:00
case AI_LWO_STEP:
2020-03-01 12:15:45 +00:00
key.inter = LWO::IT_STEP;
break;
2015-05-19 03:57:13 +00:00
case AI_LWO_LINE:
2020-03-01 12:15:45 +00:00
key.inter = LWO::IT_LINE;
break;
2015-05-19 03:57:13 +00:00
case AI_LWO_TCB:
2020-03-01 12:15:45 +00:00
key.inter = LWO::IT_TCB;
break;
2015-05-19 03:57:13 +00:00
case AI_LWO_HERM:
2020-03-01 12:15:45 +00:00
key.inter = LWO::IT_HERM;
break;
2015-05-19 03:57:13 +00:00
case AI_LWO_BEZI:
2020-03-01 12:15:45 +00:00
key.inter = LWO::IT_BEZI;
break;
2015-05-19 03:57:13 +00:00
case AI_LWO_BEZ2:
2020-03-01 12:15:45 +00:00
key.inter = LWO::IT_BEZ2;
break;
2015-05-19 03:57:13 +00:00
default:
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_WARN("LWO2: Unknown interval interpolation mode");
2015-05-19 03:57:13 +00:00
};
// todo ... read params
}
break;
}
2020-03-01 12:15:45 +00:00
default:
ASSIMP_LOG_WARN("LWO2: Encountered unknown ENVL subchunk");
break;
2015-05-19 03:57:13 +00:00
}
// regardless how much we did actually read, go to the next chunk
mFileBuffer = next;
}
}
// ------------------------------------------------------------------------------------------------
// Load file - master function
2020-03-01 12:15:45 +00:00
void LWOImporter::LoadLWO2File() {
2015-05-19 03:57:13 +00:00
bool skip = false;
2020-03-01 12:15:45 +00:00
LE_NCONST uint8_t *const end = mFileBuffer + fileSize;
unsigned int iUnnamed = 0;
2020-03-01 12:15:45 +00:00
while (true) {
if (mFileBuffer + sizeof(IFF::ChunkHeader) > end) break;
2015-05-19 03:57:13 +00:00
const IFF::ChunkHeader head = IFF::LoadChunk(mFileBuffer);
2020-03-01 12:15:45 +00:00
if (mFileBuffer + head.length > end) {
2015-05-19 03:57:13 +00:00
throw DeadlyImportError("LWO2: Chunk length points behind the file");
break;
}
2020-03-01 12:15:45 +00:00
uint8_t *const next = mFileBuffer + head.length;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
if (!head.length) {
2015-05-19 03:57:13 +00:00
mFileBuffer = next;
continue;
}
2020-03-01 12:15:45 +00:00
switch (head.type) {
// new layer
case AI_LWO_LAYR: {
2015-05-19 03:57:13 +00:00
// add a new layer to the list ....
2020-03-01 12:15:45 +00:00
mLayers->push_back(LWO::Layer());
LWO::Layer &layer = mLayers->back();
2015-05-19 03:57:13 +00:00
mCurLayer = &layer;
2020-03-01 12:15:45 +00:00
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, LAYR, 16);
2015-05-19 03:57:13 +00:00
// layer index.
layer.mIndex = GetU2();
// Continue loading this layer or ignore it? Check the layer index property
2020-03-01 12:15:45 +00:00
if (UINT_MAX != configLayerIndex && (configLayerIndex - 1) != layer.mIndex) {
2015-05-19 03:57:13 +00:00
skip = true;
2020-03-01 12:15:45 +00:00
} else
skip = false;
2015-05-19 03:57:13 +00:00
// pivot point
mFileBuffer += 2; /* unknown */
mCurLayer->mPivot.x = GetF4();
mCurLayer->mPivot.y = GetF4();
mCurLayer->mPivot.z = GetF4();
2020-03-01 12:15:45 +00:00
GetS0(layer.mName, head.length - 16);
2015-05-19 03:57:13 +00:00
// if the name is empty, generate a default name
2020-03-01 12:15:45 +00:00
if (layer.mName.empty()) {
2015-05-19 03:57:13 +00:00
char buffer[128]; // should be sufficiently large
::ai_snprintf(buffer, 128, "Layer_%i", iUnnamed++);
2015-05-19 03:57:13 +00:00
layer.mName = buffer;
}
// load this layer or ignore it? Check the layer name property
if (configLayerName.length() && configLayerName != layer.mName) {
skip = true;
2020-03-01 12:15:45 +00:00
} else
hasNamedLayer = true;
2015-05-19 03:57:13 +00:00
// optional: parent of this layer
if (mFileBuffer + 2 <= next)
layer.mParent = GetU2();
2020-03-01 12:15:45 +00:00
else
layer.mParent = (uint16_t) -1;
2015-05-19 03:57:13 +00:00
// Set layer skip parameter
layer.skip = skip;
break;
}
2020-03-01 12:15:45 +00:00
// vertex list
case AI_LWO_PNTS: {
2015-05-19 03:57:13 +00:00
if (skip)
break;
unsigned int old = (unsigned int)mCurLayer->mTempPoints.size();
LoadLWOPoints(head.length);
mCurLayer->mPointIDXOfs = old;
break;
}
2020-03-01 12:15:45 +00:00
// vertex tags
case AI_LWO_VMAD:
if (mCurLayer->mFaces.empty()) {
ASSIMP_LOG_WARN("LWO2: Unexpected VMAD chunk");
break;
}
// --- intentionally no break here
case AI_LWO_VMAP: {
2015-05-19 03:57:13 +00:00
if (skip)
break;
if (mCurLayer->mTempPoints.empty())
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_WARN("LWO2: Unexpected VMAP chunk");
2020-03-01 12:15:45 +00:00
else
LoadLWO2VertexMap(head.length, head.type == AI_LWO_VMAD);
2015-05-19 03:57:13 +00:00
break;
}
2020-03-01 12:15:45 +00:00
// face list
case AI_LWO_POLS: {
2015-05-19 03:57:13 +00:00
if (skip)
break;
unsigned int old = (unsigned int)mCurLayer->mFaces.size();
LoadLWO2Polygons(head.length);
mCurLayer->mFaceIDXOfs = old;
break;
}
2020-03-01 12:15:45 +00:00
// polygon tags
case AI_LWO_PTAG: {
2015-05-19 03:57:13 +00:00
if (skip)
break;
2018-04-26 12:10:18 +00:00
if (mCurLayer->mFaces.empty()) {
ASSIMP_LOG_WARN("LWO2: Unexpected PTAG");
} else {
LoadLWO2PolygonTags(head.length);
}
2015-05-19 03:57:13 +00:00
break;
}
2020-03-01 12:15:45 +00:00
// list of tags
case AI_LWO_TAGS: {
2018-04-26 12:10:18 +00:00
if (!mTags->empty()) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_WARN("LWO2: SRFS chunk encountered twice");
2020-03-01 12:15:45 +00:00
} else {
2018-04-26 12:10:18 +00:00
LoadLWOTags(head.length);
}
2015-05-19 03:57:13 +00:00
break;
}
2020-03-01 12:15:45 +00:00
// surface chunk
case AI_LWO_SURF: {
2015-05-19 03:57:13 +00:00
LoadLWO2Surface(head.length);
break;
}
2020-03-01 12:15:45 +00:00
// clip chunk
case AI_LWO_CLIP: {
2015-05-19 03:57:13 +00:00
LoadLWO2Clip(head.length);
break;
}
2020-03-01 12:15:45 +00:00
// envelope chunk
case AI_LWO_ENVL: {
2015-05-19 03:57:13 +00:00
LoadLWO2Envelope(head.length);
break;
}
}
mFileBuffer = next;
}
}
#endif // !! ASSIMP_BUILD_NO_LWO_IMPORTER