assimp/code/AssetLib/LWS/LWSLoader.cpp

919 lines
34 KiB
C++
Raw Normal View History

/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
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 LWSLoader.cpp
2015-05-19 03:52:10 +00:00
* @brief Implementation of the LWS importer class
*/
#ifndef ASSIMP_BUILD_NO_LWS_IMPORTER
#include "AssetLib/LWS/LWSLoader.h"
#include "Common/Importer.h"
2020-03-01 12:15:45 +00:00
#include "PostProcessing/ConvertToLHProcess.h"
2020-03-01 12:15:45 +00:00
#include <assimp/GenericProperty.h>
#include <assimp/ParsingUtils.h>
2017-06-21 12:31:37 +00:00
#include <assimp/SceneCombiner.h>
#include <assimp/SkeletonMeshBuilder.h>
2020-03-01 12:15:45 +00:00
#include <assimp/fast_atof.h>
#include <assimp/importerdesc.h>
2016-06-06 20:04:29 +00:00
#include <assimp/scene.h>
2020-03-01 12:15:45 +00:00
#include <assimp/DefaultLogger.hpp>
2016-06-06 20:04:29 +00:00
#include <assimp/IOSystem.hpp>
#include <memory>
using namespace Assimp;
2023-11-11 08:59:42 +00:00
static constexpr aiImporterDesc desc = {
2015-05-19 03:57:13 +00:00
"LightWave Scene Importer",
"",
"",
"http://www.newtek.com/lightwave.html=",
aiImporterFlags_SupportTextFlavour,
0,
0,
0,
0,
"lws mot"
};
// ------------------------------------------------------------------------------------------------
// Recursive parsing of LWS files
void LWS::Element::Parse(const char *&buffer, const char *end) {
for (; SkipSpacesAndLineEnd(&buffer, end); SkipLine(&buffer, end)) {
2015-05-19 03:57:13 +00:00
// begin of a new element with children
bool sub = false;
if (*buffer == '{') {
++buffer;
SkipSpaces(&buffer, end);
2015-05-19 03:57:13 +00:00
sub = true;
2020-03-01 12:15:45 +00:00
} else if (*buffer == '}')
2015-05-19 03:57:13 +00:00
return;
children.emplace_back();
2015-05-19 03:57:13 +00:00
// copy data line - read token per token
2020-03-01 12:15:45 +00:00
const char *cur = buffer;
while (!IsSpaceOrNewLine(*buffer))
++buffer;
children.back().tokens[0] = std::string(cur, (size_t)(buffer - cur));
SkipSpaces(&buffer, end);
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
if (children.back().tokens[0] == "Plugin") {
2020-05-15 15:59:42 +00:00
ASSIMP_LOG_VERBOSE_DEBUG("LWS: Skipping over plugin-specific data");
2015-05-19 03:57:13 +00:00
// strange stuff inside Plugin/Endplugin blocks. Needn't
// follow LWS syntax, so we skip over it
for (; SkipSpacesAndLineEnd(&buffer, end); SkipLine(&buffer, end)) {
2020-03-01 12:15:45 +00:00
if (!::strncmp(buffer, "EndPlugin", 9)) {
2015-05-19 03:57:13 +00:00
break;
}
}
continue;
}
cur = buffer;
while (!IsLineEnd(*buffer)) {
2020-03-01 12:15:45 +00:00
++buffer;
}
2020-03-01 12:15:45 +00:00
children.back().tokens[1] = std::string(cur, (size_t)(buffer - cur));
2015-05-19 03:57:13 +00:00
// parse more elements recursively
if (sub) {
children.back().Parse(buffer, end);
}
2015-05-19 03:57:13 +00:00
}
}
// ------------------------------------------------------------------------------------------------
// Constructor to be privately used by Importer
2020-03-01 12:15:45 +00:00
LWSImporter::LWSImporter() :
configSpeedFlag(),
io(),
first(),
last(),
fps(),
noSkeletonMesh() {
2015-05-19 03:57:13 +00:00
// nothing to do here
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Returns whether the class can handle the format of the given file.
bool LWSImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const {
static const uint32_t tokens[] = {
AI_MAKE_MAGIC("LWSC"),
AI_MAKE_MAGIC("LWMO")
};
return CheckMagicToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens));
}
// ------------------------------------------------------------------------------------------------
// Get list of file extensions
2020-03-01 12:15:45 +00:00
const aiImporterDesc *LWSImporter::GetInfo() const {
2015-05-19 03:57:13 +00:00
return &desc;
}
static constexpr int MagicHackNo = 150392;
// ------------------------------------------------------------------------------------------------
// Setup configuration properties
2020-03-01 12:15:45 +00:00
void LWSImporter::SetupProperties(const Importer *pImp) {
2015-05-19 03:57:13 +00:00
// AI_CONFIG_FAVOUR_SPEED
2020-03-01 12:15:45 +00:00
configSpeedFlag = (0 != pImp->GetPropertyInteger(AI_CONFIG_FAVOUR_SPEED, 0));
2015-05-19 03:57:13 +00:00
// AI_CONFIG_IMPORT_LWS_ANIM_START
first = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_LWS_ANIM_START,
MagicHackNo /* magic hack */);
2015-05-19 03:57:13 +00:00
// AI_CONFIG_IMPORT_LWS_ANIM_END
last = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_LWS_ANIM_END,
MagicHackNo /* magic hack */);
2015-05-19 03:57:13 +00:00
if (last < first) {
2020-03-01 12:15:45 +00:00
std::swap(last, first);
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
noSkeletonMesh = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_NO_SKELETON_MESHES, 0) != 0;
}
// ------------------------------------------------------------------------------------------------
// Read an envelope description
2020-03-01 12:15:45 +00:00
void LWSImporter::ReadEnvelope(const LWS::Element &dad, LWO::Envelope &fill) {
2015-05-19 03:57:13 +00:00
if (dad.children.empty()) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWS: Envelope descriptions must not be empty");
2015-05-19 03:57:13 +00:00
return;
}
// reserve enough storage
2020-03-01 12:15:45 +00:00
std::list<LWS::Element>::const_iterator it = dad.children.begin();
2015-05-19 03:57:13 +00:00
fill.keys.reserve(strtoul10(it->tokens[1].c_str()));
for (++it; it != dad.children.end(); ++it) {
2020-03-01 12:15:45 +00:00
const char *c = (*it).tokens[1].c_str();
const char *end = c + (*it).tokens[1].size();
2015-05-19 03:57:13 +00:00
if ((*it).tokens[0] == "Key") {
fill.keys.emplace_back();
2020-03-01 12:15:45 +00:00
LWO::Key &key = fill.keys.back();
2015-05-19 03:57:13 +00:00
float f;
SkipSpaces(&c, end);
2020-03-01 12:15:45 +00:00
c = fast_atoreal_move<float>(c, key.value);
SkipSpaces(&c, end);
2020-03-01 12:15:45 +00:00
c = fast_atoreal_move<float>(c, f);
2015-05-19 03:57:13 +00:00
key.time = f;
2020-03-01 12:15:45 +00:00
unsigned int span = strtoul10(c, &c), num = 0;
2015-05-19 03:57:13 +00:00
switch (span) {
case 0:
key.inter = LWO::IT_TCB;
num = 5;
break;
case 1:
case 2:
key.inter = LWO::IT_HERM;
num = 5;
break;
case 3:
key.inter = LWO::IT_LINE;
num = 0;
break;
case 4:
key.inter = LWO::IT_STEP;
num = 0;
break;
case 5:
key.inter = LWO::IT_BEZ2;
num = 4;
break;
default:
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWS: Unknown span type");
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
for (unsigned int i = 0; i < num; ++i) {
SkipSpaces(&c, end);
2020-03-01 12:15:45 +00:00
c = fast_atoreal_move<float>(c, key.params[i]);
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
} else if ((*it).tokens[0] == "Behaviors") {
SkipSpaces(&c, end);
2020-03-01 12:15:45 +00:00
fill.pre = (LWO::PrePostBehaviour)strtoul10(c, &c);
SkipSpaces(&c, end);
2020-03-01 12:15:45 +00:00
fill.post = (LWO::PrePostBehaviour)strtoul10(c, &c);
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Read animation channels in the old LightWave animation format
void LWSImporter::ReadEnvelope_Old(std::list<LWS::Element>::const_iterator &it,const std::list<LWS::Element>::const_iterator &endIt,
LWS::NodeDesc &nodes, unsigned int) {
if (++it == endIt) {
ASSIMP_LOG_ERROR("LWS: Encountered unexpected end of file while parsing object motion");
return;
}
const unsigned int num = strtoul10((*it).tokens[0].c_str());
2015-05-19 03:57:13 +00:00
for (unsigned int i = 0; i < num; ++i) {
nodes.channels.emplace_back();
2020-03-01 12:15:45 +00:00
LWO::Envelope &envl = nodes.channels.back();
2015-05-19 03:57:13 +00:00
envl.index = i;
2020-03-01 12:15:45 +00:00
envl.type = (LWO::EnvelopeType)(i + 1);
2015-05-19 03:52:10 +00:00
if (++it == endIt) {
ASSIMP_LOG_ERROR("LWS: Encountered unexpected end of file while parsing object motion");
return;
}
const unsigned int sub_num = strtoul10((*it).tokens[0].c_str());
2020-03-01 12:15:45 +00:00
for (unsigned int n = 0; n < sub_num; ++n) {
if (++it == endIt) {
ASSIMP_LOG_ERROR("LWS: Encountered unexpected end of file while parsing object motion");
return;
}
2015-05-19 03:57:13 +00:00
// parse value and time, skip the rest for the moment.
LWO::Key key;
2020-03-01 12:15:45 +00:00
const char *c = fast_atoreal_move<float>((*it).tokens[0].c_str(), key.value);
const char *end = c + (*it).tokens[0].size();
SkipSpaces(&c, end);
2015-05-19 03:57:13 +00:00
float f;
2020-03-01 12:15:45 +00:00
fast_atoreal_move<float>((*it).tokens[0].c_str(), f);
2015-05-19 03:57:13 +00:00
key.time = f;
envl.keys.emplace_back(key);
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Setup a nice name for a node
2020-03-01 12:15:45 +00:00
void LWSImporter::SetupNodeName(aiNode *nd, LWS::NodeDesc &src) {
2015-05-19 03:57:13 +00:00
const unsigned int combined = src.number | ((unsigned int)src.type) << 28u;
2015-05-19 03:57:13 +00:00
// the name depends on the type. We break LWS's strange naming convention
// and return human-readable, but still machine-parsable and unique, strings.
2020-03-01 12:15:45 +00:00
if (src.type == LWS::NodeDesc::OBJECT) {
2015-05-19 03:57:13 +00:00
if (src.path.length()) {
std::string::size_type s = src.path.find_last_of("\\/");
if (s == std::string::npos) {
2015-05-19 03:57:13 +00:00
s = 0;
} else {
2020-03-01 12:15:45 +00:00
++s;
}
std::string::size_type t = src.path.substr(s).find_last_of('.');
2015-05-19 03:52:10 +00:00
nd->mName.length = ::ai_snprintf(nd->mName.data, AI_MAXLEN, "%s_(%08X)", src.path.substr(s).substr(0, t).c_str(), combined);
if (nd->mName.length > AI_MAXLEN) {
nd->mName.length = AI_MAXLEN;
}
2015-05-19 03:57:13 +00:00
return;
}
}
nd->mName.length = ::ai_snprintf(nd->mName.data, AI_MAXLEN, "%s_(%08X)", src.name, combined);
}
// ------------------------------------------------------------------------------------------------
// Recursively build the scene-graph
2020-03-01 12:15:45 +00:00
void LWSImporter::BuildGraph(aiNode *nd, LWS::NodeDesc &src, std::vector<AttachmentInfo> &attach,
BatchLoader &batch,
aiCamera **&camOut,
aiLight **&lightOut,
std::vector<aiNodeAnim *> &animOut) {
2015-05-19 03:57:13 +00:00
// Setup a very cryptic name for the node, we want the user to be happy
2020-03-01 12:15:45 +00:00
SetupNodeName(nd, src);
aiNode *ndAnim = nd;
2015-05-19 03:57:13 +00:00
// If the node is an object
if (src.type == LWS::NodeDesc::OBJECT) {
2015-05-19 03:57:13 +00:00
// If the object is from an external file, get it
aiScene *obj = nullptr;
2020-03-01 12:15:45 +00:00
if (src.path.length()) {
obj = batch.GetImport(src.id);
if (!obj) {
2021-05-13 11:05:31 +00:00
ASSIMP_LOG_ERROR("LWS: Failed to read external file ", src.path);
2020-03-01 12:15:45 +00:00
} else {
if (obj->mRootNode->mNumChildren == 1) {
2015-05-19 03:52:10 +00:00
//If the pivot is not set for this layer, get it from the external object
if (!src.isPivotSet) {
src.pivotPos.x = +obj->mRootNode->mTransformation.a4;
src.pivotPos.y = +obj->mRootNode->mTransformation.b4;
src.pivotPos.z = -obj->mRootNode->mTransformation.c4; //The sign is the RH to LH back conversion
}
2015-05-19 03:52:10 +00:00
//Remove first node from obj (the old pivot), reset transform of second node (the mesh node)
2020-03-01 12:15:45 +00:00
aiNode *newRootNode = obj->mRootNode->mChildren[0];
obj->mRootNode->mChildren[0] = nullptr;
2015-05-19 03:57:13 +00:00
delete obj->mRootNode;
obj->mRootNode = newRootNode;
obj->mRootNode->mTransformation.a4 = 0.0;
obj->mRootNode->mTransformation.b4 = 0.0;
obj->mRootNode->mTransformation.c4 = 0.0;
}
}
}
2015-05-19 03:52:10 +00:00
2015-05-19 03:57:13 +00:00
//Setup the pivot node (also the animation node), the one we received
nd->mName = std::string("Pivot:") + nd->mName.data;
2015-05-19 03:57:13 +00:00
ndAnim = nd;
2015-05-19 03:52:10 +00:00
//Add the attachment node to it
nd->mNumChildren = 1;
2020-03-01 12:15:45 +00:00
nd->mChildren = new aiNode *[1];
nd->mChildren[0] = new aiNode();
nd->mChildren[0]->mParent = nd;
nd->mChildren[0]->mTransformation.a4 = -src.pivotPos.x;
nd->mChildren[0]->mTransformation.b4 = -src.pivotPos.y;
nd->mChildren[0]->mTransformation.c4 = -src.pivotPos.z;
2015-05-19 03:57:13 +00:00
SetupNodeName(nd->mChildren[0], src);
2015-05-19 03:52:10 +00:00
2015-05-19 03:57:13 +00:00
//Update the attachment node
nd = nd->mChildren[0];
2015-05-19 03:52:10 +00:00
//Push attachment, if the object came from an external file
if (obj) {
attach.emplace_back(obj, nd);
}
}
2015-05-19 03:57:13 +00:00
// If object is a light source - setup a corresponding ai structure
else if (src.type == LWS::NodeDesc::LIGHT) {
2020-03-01 12:15:45 +00:00
aiLight *lit = *lightOut++ = new aiLight();
2015-05-19 03:57:13 +00:00
// compute final light color
2020-03-01 12:15:45 +00:00
lit->mColorDiffuse = lit->mColorSpecular = src.lightColor * src.lightIntensity;
2015-05-19 03:57:13 +00:00
// name to attach light to node -> unique due to LWs indexing system
lit->mName = nd->mName;
2018-05-13 14:35:03 +00:00
// determine light type and setup additional members
2015-05-19 03:57:13 +00:00
if (src.lightType == 2) { /* spot light */
lit->mType = aiLightSource_SPOT;
2020-03-01 12:15:45 +00:00
lit->mAngleInnerCone = (float)AI_DEG_TO_RAD(src.lightConeAngle);
lit->mAngleOuterCone = lit->mAngleInnerCone + (float)AI_DEG_TO_RAD(src.lightEdgeAngle);
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
} else if (src.lightType == 1) { /* directional light source */
2015-05-19 03:57:13 +00:00
lit->mType = aiLightSource_DIRECTIONAL;
} else {
2020-03-01 12:15:45 +00:00
lit->mType = aiLightSource_POINT;
}
2015-05-19 03:57:13 +00:00
// fixme: no proper handling of light falloffs yet
if (src.lightFalloffType == 1) {
2015-05-19 03:57:13 +00:00
lit->mAttenuationConstant = 1.f;
} else if (src.lightFalloffType == 2) {
2015-05-19 03:57:13 +00:00
lit->mAttenuationLinear = 1.f;
} else {
2015-05-19 03:57:13 +00:00
lit->mAttenuationQuadratic = 1.f;
}
} else if (src.type == LWS::NodeDesc::CAMERA) { // If object is a camera - setup a corresponding ai structure
2020-03-01 12:15:45 +00:00
aiCamera *cam = *camOut++ = new aiCamera();
2015-05-19 03:57:13 +00:00
// name to attach cam to node -> unique due to LWs indexing system
cam->mName = nd->mName;
}
// Get the node transformation from the LWO key
2020-03-01 12:15:45 +00:00
LWO::AnimResolver resolver(src.channels, fps);
2015-05-19 03:57:13 +00:00
resolver.ExtractBindPose(ndAnim->mTransformation);
// .. and construct animation channels
aiNodeAnim *anim = nullptr;
2015-05-19 03:57:13 +00:00
if (first != last) {
2020-03-01 12:15:45 +00:00
resolver.SetAnimationRange(first, last);
resolver.ExtractAnimChannel(&anim, AI_LWO_ANIM_FLAG_SAMPLE_ANIMS | AI_LWO_ANIM_FLAG_START_AT_ZERO);
2015-05-19 03:57:13 +00:00
if (anim) {
anim->mNodeName = ndAnim->mName;
animOut.push_back(anim);
}
}
// Add children
if (!src.children.empty()) {
2020-03-01 12:15:45 +00:00
nd->mChildren = new aiNode *[src.children.size()];
for (std::list<LWS::NodeDesc *>::iterator it = src.children.begin(); it != src.children.end(); ++it) {
aiNode *ndd = nd->mChildren[nd->mNumChildren++] = new aiNode();
2015-05-19 03:57:13 +00:00
ndd->mParent = nd;
2020-03-01 12:15:45 +00:00
BuildGraph(ndd, **it, attach, batch, camOut, lightOut, animOut);
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Determine the exact location of a LWO file
2020-03-01 12:15:45 +00:00
std::string LWSImporter::FindLWOFile(const std::string &in) {
// insert missing directory separator if necessary
std::string tmp(in);
2020-03-01 12:15:45 +00:00
if (in.length() > 3 && in[1] == ':' && in[2] != '\\' && in[2] != '/') {
2015-05-19 03:57:13 +00:00
tmp = in[0] + (std::string(":\\") + in.substr(2));
}
2015-05-19 03:57:13 +00:00
if (io->Exists(tmp)) {
return in;
}
// file is not accessible for us ... maybe it's packed by
// LightWave's 'Package Scene' command?
// Relevant for us are the following two directories:
// <folder>\Objects\<hh>\<*>.lwo
// <folder>\Scenes\<hh>\<*>.lws
// where <hh> is optional.
std::string test = std::string("..") + (io->getOsSeparator() + tmp);
if (io->Exists(test)) {
return test;
}
test = std::string("..") + (io->getOsSeparator() + test);
if (io->Exists(test)) {
return test;
}
// return original path, maybe the IOsystem knows better
return tmp;
}
// ------------------------------------------------------------------------------------------------
// Read file into given scene data structure
void LWSImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
2015-05-19 03:57:13 +00:00
io = pIOHandler;
2020-03-01 12:15:45 +00:00
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
if (file == nullptr) {
throw DeadlyImportError("Failed to open LWS file ", pFile, ".");
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<char> mBuffer;
TextFileToBuffer(file.get(), mBuffer);
2015-05-19 03:57:13 +00:00
// Parse the file structure
2020-03-01 12:15:45 +00:00
LWS::Element root;
const char *dummy = &mBuffer[0];
const char *dummyEnd = dummy + mBuffer.size();
root.Parse(dummy, dummyEnd);
2015-05-19 03:57:13 +00:00
// Construct a Batch-importer to read more files recursively
2015-05-19 03:57:13 +00:00
BatchLoader batch(pIOHandler);
// Construct an array to receive the flat output graph
std::list<LWS::NodeDesc> nodes;
unsigned int cur_light = 0, cur_camera = 0, cur_object = 0;
2022-08-09 07:32:01 +00:00
unsigned int num_light = 0, num_camera = 0;
2015-05-19 03:57:13 +00:00
// check magic identifier, 'LWSC'
bool motion_file = false;
2020-03-01 12:15:45 +00:00
std::list<LWS::Element>::const_iterator it = root.children.begin();
2015-05-19 03:57:13 +00:00
if ((*it).tokens[0] == "LWMO") {
2015-05-19 03:57:13 +00:00
motion_file = true;
}
2015-05-19 03:57:13 +00:00
if ((*it).tokens[0] != "LWSC" && !motion_file) {
2015-05-19 03:57:13 +00:00
throw DeadlyImportError("LWS: Not a LightWave scene, magic tag LWSC not found");
}
2015-05-19 03:57:13 +00:00
// get file format version and print to log
++it;
if (it == root.children.end() || (*it).tokens[0].empty()) {
ASSIMP_LOG_ERROR("Invalid LWS file detectedm abort import.");
return;
}
2015-05-19 03:57:13 +00:00
unsigned int version = strtoul10((*it).tokens[0].c_str());
2021-05-13 11:05:31 +00:00
ASSIMP_LOG_INFO("LWS file format version is ", (*it).tokens[0]);
2015-05-19 03:57:13 +00:00
first = 0.;
2020-03-01 12:15:45 +00:00
last = 60.;
fps = 25.; // seems to be a good default frame rate
2015-05-19 03:57:13 +00:00
// Now read all elements in a very straightforward manner
2015-05-19 03:57:13 +00:00
for (; it != root.children.end(); ++it) {
2020-03-01 12:15:45 +00:00
const char *c = (*it).tokens[1].c_str();
const char *end = c + (*it).tokens[1].size();
2015-05-19 03:57:13 +00:00
// 'FirstFrame': begin of animation slice
if ((*it).tokens[0] == "FirstFrame") {
// see SetupProperties()
if (150392. != first ) {
first = strtoul10(c, &c) - 1.; // we're zero-based
}
} else if ((*it).tokens[0] == "LastFrame") { // 'LastFrame': end of animation slice
// see SetupProperties()
if (150392. != last ) {
last = strtoul10(c, &c) - 1.; // we're zero-based
}
} else if ((*it).tokens[0] == "FramesPerSecond") { // 'FramesPerSecond': frames per second
2020-03-01 12:15:45 +00:00
fps = strtoul10(c, &c);
} else if ((*it).tokens[0] == "LoadObjectLayer") { // 'LoadObjectLayer': load a layer of a specific LWO file
2015-05-19 03:57:13 +00:00
// get layer index
2020-03-01 12:15:45 +00:00
const int layer = strtoul10(c, &c);
2015-05-19 03:57:13 +00:00
// setup the layer to be loaded
BatchLoader::PropertyMap props;
2020-03-01 12:15:45 +00:00
SetGenericProperty(props.ints, AI_CONFIG_IMPORT_LWO_ONE_LAYER_ONLY, layer);
2015-05-19 03:57:13 +00:00
// add node to list
LWS::NodeDesc d;
d.type = LWS::NodeDesc::OBJECT;
if (version >= 4) { // handle LWSC 4 explicit ID
SkipSpaces(&c, end);
2020-03-01 12:15:45 +00:00
d.number = strtoul16(c, &c) & AI_LWS_MASK;
} else {
2020-03-01 12:15:45 +00:00
d.number = cur_object++;
}
2015-05-19 03:57:13 +00:00
// and add the file to the import list
SkipSpaces(&c, end);
2020-03-01 12:15:45 +00:00
std::string path = FindLWOFile(c);
2015-05-19 03:57:13 +00:00
d.path = path;
2020-03-01 12:15:45 +00:00
d.id = batch.AddLoadRequest(path, 0, &props);
2015-05-19 03:57:13 +00:00
nodes.push_back(d);
} else if ((*it).tokens[0] == "LoadObject") { // 'LoadObject': load a LWO file into the scene-graph
2015-05-19 03:57:13 +00:00
// add node to list
LWS::NodeDesc d;
d.type = LWS::NodeDesc::OBJECT;
if (version >= 4) { // handle LWSC 4 explicit ID
2020-03-01 12:15:45 +00:00
d.number = strtoul16(c, &c) & AI_LWS_MASK;
SkipSpaces(&c, end);
} else {
2020-03-01 12:15:45 +00:00
d.number = cur_object++;
}
2020-03-01 12:15:45 +00:00
std::string path = FindLWOFile(c);
d.id = batch.AddLoadRequest(path, 0, nullptr);
2015-05-19 03:57:13 +00:00
d.path = path;
nodes.push_back(d);
} else if ((*it).tokens[0] == "AddNullObject") { // 'AddNullObject': add a dummy node to the hierarchy
2015-05-19 03:57:13 +00:00
// add node to list
LWS::NodeDesc d;
d.type = LWS::NodeDesc::OBJECT;
if (version >= 4) { // handle LWSC 4 explicit ID
2020-03-01 12:15:45 +00:00
d.number = strtoul16(c, &c) & AI_LWS_MASK;
SkipSpaces(&c, end);
} else {
2020-03-01 12:15:45 +00:00
d.number = cur_object++;
}
d.name = c;
2015-05-19 03:57:13 +00:00
nodes.push_back(d);
}
// 'NumChannels': Number of envelope channels assigned to last layer
else if ((*it).tokens[0] == "NumChannels") {
// ignore for now
}
// 'Channel': preceedes any envelope description
else if ((*it).tokens[0] == "Channel") {
if (nodes.empty()) {
if (motion_file) {
// LightWave motion file. Add dummy node
LWS::NodeDesc d;
d.type = LWS::NodeDesc::OBJECT;
d.name = c;
d.number = cur_object++;
nodes.push_back(d);
}
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWS: Unexpected keyword: \'Channel\'");
} else {
// important: index of channel
nodes.back().channels.emplace_back();
LWO::Envelope &env = nodes.back().channels.back();
2015-05-19 03:57:13 +00:00
env.index = strtoul10(c);
2015-05-19 03:57:13 +00:00
// currently we can just interpret the standard channels 0...9
// (hack) assume that index-i yields the binary channel type from LWO
env.type = (LWO::EnvelopeType)(env.index + 1);
}
2015-05-19 03:57:13 +00:00
}
// 'Envelope': a single animation channel
else if ((*it).tokens[0] == "Envelope") {
if (nodes.empty() || nodes.back().channels.empty())
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWS: Unexpected keyword: \'Envelope\'");
2015-05-19 03:57:13 +00:00
else {
2020-03-01 12:15:45 +00:00
ReadEnvelope((*it), nodes.back().channels.back());
2015-05-19 03:57:13 +00:00
}
}
// 'ObjectMotion': animation information for older lightwave formats
2020-03-01 12:15:45 +00:00
else if (version < 3 && ((*it).tokens[0] == "ObjectMotion" ||
(*it).tokens[0] == "CameraMotion" ||
(*it).tokens[0] == "LightMotion")) {
2015-05-19 03:57:13 +00:00
if (nodes.empty())
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_ERROR("LWS: Unexpected keyword: \'<Light|Object|Camera>Motion\'");
2015-05-19 03:57:13 +00:00
else {
2020-03-01 12:15:45 +00:00
ReadEnvelope_Old(it, root.children.end(), nodes.back(), version);
2015-05-19 03:57:13 +00:00
}
}
// 'Pre/PostBehavior': pre/post animation behaviour for LWSC 2
else if (version == 2 && (*it).tokens[0] == "Pre/PostBehavior") {
if (nodes.empty())
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWS: Unexpected keyword: \'Pre/PostBehavior'");
2015-05-19 03:57:13 +00:00
else {
2020-03-01 12:15:45 +00:00
for (std::list<LWO::Envelope>::iterator envelopeIt = nodes.back().channels.begin(); envelopeIt != nodes.back().channels.end(); ++envelopeIt) {
2015-05-19 03:57:13 +00:00
// two ints per envelope
2020-03-01 12:15:45 +00:00
LWO::Envelope &env = *envelopeIt;
env.pre = (LWO::PrePostBehaviour)strtoul10(c, &c);
SkipSpaces(&c, end);
2020-03-01 12:15:45 +00:00
env.post = (LWO::PrePostBehaviour)strtoul10(c, &c);
SkipSpaces(&c, end);
2015-05-19 03:57:13 +00:00
}
}
}
// 'ParentItem': specifies the parent of the current element
else if ((*it).tokens[0] == "ParentItem") {
if (nodes.empty()) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWS: Unexpected keyword: \'ParentItem\'");
} else {
2020-03-01 12:15:45 +00:00
nodes.back().parent = strtoul16(c, &c);
}
2015-05-19 03:57:13 +00:00
}
// 'ParentObject': deprecated one for older formats
else if (version < 3 && (*it).tokens[0] == "ParentObject") {
if (nodes.empty()) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWS: Unexpected keyword: \'ParentObject\'");
} else {
2020-03-01 12:15:45 +00:00
nodes.back().parent = strtoul10(c, &c) | (1u << 28u);
2015-05-19 03:57:13 +00:00
}
}
// 'AddCamera': add a camera to the scenegraph
else if ((*it).tokens[0] == "AddCamera") {
// add node to list
LWS::NodeDesc d;
d.type = LWS::NodeDesc::CAMERA;
if (version >= 4) { // handle LWSC 4 explicit ID
2020-03-01 12:15:45 +00:00
d.number = strtoul16(c, &c) & AI_LWS_MASK;
} else {
2020-03-01 12:15:45 +00:00
d.number = cur_camera++;
}
2015-05-19 03:57:13 +00:00
nodes.push_back(d);
num_camera++;
}
// 'CameraName': set name of currently active camera
else if ((*it).tokens[0] == "CameraName") {
if (nodes.empty() || nodes.back().type != LWS::NodeDesc::CAMERA) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWS: Unexpected keyword: \'CameraName\'");
} else {
2020-03-01 12:15:45 +00:00
nodes.back().name = c;
}
2015-05-19 03:57:13 +00:00
}
// 'AddLight': add a light to the scenegraph
else if ((*it).tokens[0] == "AddLight") {
// add node to list
LWS::NodeDesc d;
d.type = LWS::NodeDesc::LIGHT;
if (version >= 4) { // handle LWSC 4 explicit ID
2020-03-01 12:15:45 +00:00
d.number = strtoul16(c, &c) & AI_LWS_MASK;
} else {
2020-03-01 12:15:45 +00:00
d.number = cur_light++;
}
2015-05-19 03:57:13 +00:00
nodes.push_back(d);
num_light++;
}
// 'LightName': set name of currently active light
else if ((*it).tokens[0] == "LightName") {
if (nodes.empty() || nodes.back().type != LWS::NodeDesc::LIGHT) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWS: Unexpected keyword: \'LightName\'");
} else {
2020-03-01 12:15:45 +00:00
nodes.back().name = c;
}
2015-05-19 03:57:13 +00:00
}
// 'LightIntensity': set intensity of currently active light
2020-03-01 12:15:45 +00:00
else if ((*it).tokens[0] == "LightIntensity" || (*it).tokens[0] == "LgtIntensity") {
if (nodes.empty() || nodes.back().type != LWS::NodeDesc::LIGHT) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWS: Unexpected keyword: \'LightIntensity\'");
} else {
const std::string env = "(envelope)";
if (0 == strncmp(c, env.c_str(), env.size())) {
ASSIMP_LOG_ERROR("LWS: envelopes for LightIntensity not supported, set to 1.0");
nodes.back().lightIntensity = (ai_real)1.0;
} else {
fast_atoreal_move<float>(c, nodes.back().lightIntensity);
}
}
2015-05-19 03:57:13 +00:00
}
// 'LightType': set type of currently active light
else if ((*it).tokens[0] == "LightType") {
if (nodes.empty() || nodes.back().type != LWS::NodeDesc::LIGHT) {
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_ERROR("LWS: Unexpected keyword: \'LightType\'");
} else {
2020-03-01 12:15:45 +00:00
nodes.back().lightType = strtoul10(c);
}
2015-05-19 03:57:13 +00:00
}
// 'LightFalloffType': set falloff type of currently active light
else if ((*it).tokens[0] == "LightFalloffType") {
if (nodes.empty() || nodes.back().type != LWS::NodeDesc::LIGHT) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWS: Unexpected keyword: \'LightFalloffType\'");
} else {
2020-03-01 12:15:45 +00:00
nodes.back().lightFalloffType = strtoul10(c);
}
2015-05-19 03:57:13 +00:00
}
// 'LightConeAngle': set cone angle of currently active light
else if ((*it).tokens[0] == "LightConeAngle") {
if (nodes.empty() || nodes.back().type != LWS::NodeDesc::LIGHT) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWS: Unexpected keyword: \'LightConeAngle\'");
} else {
2020-03-01 12:15:45 +00:00
nodes.back().lightConeAngle = fast_atof(c);
}
2015-05-19 03:57:13 +00:00
}
// 'LightEdgeAngle': set area where we're smoothing from min to max intensity
else if ((*it).tokens[0] == "LightEdgeAngle") {
if (nodes.empty() || nodes.back().type != LWS::NodeDesc::LIGHT) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWS: Unexpected keyword: \'LightEdgeAngle\'");
} else {
2020-03-01 12:15:45 +00:00
nodes.back().lightEdgeAngle = fast_atof(c);
}
2015-05-19 03:57:13 +00:00
}
// 'LightColor': set color of currently active light
else if ((*it).tokens[0] == "LightColor") {
if (nodes.empty() || nodes.back().type != LWS::NodeDesc::LIGHT) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWS: Unexpected keyword: \'LightColor\'");
} else {
2020-03-01 12:15:45 +00:00
c = fast_atoreal_move<float>(c, (float &)nodes.back().lightColor.r);
SkipSpaces(&c, end);
2020-03-01 12:15:45 +00:00
c = fast_atoreal_move<float>(c, (float &)nodes.back().lightColor.g);
SkipSpaces(&c, end);
2020-03-01 12:15:45 +00:00
c = fast_atoreal_move<float>(c, (float &)nodes.back().lightColor.b);
2015-05-19 03:57:13 +00:00
}
}
// 'PivotPosition': position of local transformation origin
else if ((*it).tokens[0] == "PivotPosition" || (*it).tokens[0] == "PivotPoint") {
if (nodes.empty()) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWS: Unexpected keyword: \'PivotPosition\'");
} else {
2020-03-01 12:15:45 +00:00
c = fast_atoreal_move<float>(c, (float &)nodes.back().pivotPos.x);
SkipSpaces(&c, end);
2020-03-01 12:15:45 +00:00
c = fast_atoreal_move<float>(c, (float &)nodes.back().pivotPos.y);
SkipSpaces(&c, end);
2020-03-01 12:15:45 +00:00
c = fast_atoreal_move<float>(c, (float &)nodes.back().pivotPos.z);
// Mark pivotPos as set
nodes.back().isPivotSet = true;
2015-05-19 03:57:13 +00:00
}
}
}
// resolve parenting
2020-03-01 12:15:45 +00:00
for (std::list<LWS::NodeDesc>::iterator ndIt = nodes.begin(); ndIt != nodes.end(); ++ndIt) {
2015-05-19 03:57:13 +00:00
// check whether there is another node which calls us a parent
for (std::list<LWS::NodeDesc>::iterator dit = nodes.begin(); dit != nodes.end(); ++dit) {
2020-03-01 12:15:45 +00:00
if (dit != ndIt && *ndIt == (*dit).parent) {
2015-05-19 03:57:13 +00:00
if ((*dit).parent_resolved) {
// fixme: it's still possible to produce an overflow due to cross references ..
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWS: Found cross reference in scene-graph");
2015-05-19 03:57:13 +00:00
continue;
}
2020-03-01 12:15:45 +00:00
ndIt->children.push_back(&*dit);
(*dit).parent_resolved = &*ndIt;
2015-05-19 03:57:13 +00:00
}
}
}
// find out how many nodes have no parent yet
unsigned int no_parent = 0;
2020-03-01 12:15:45 +00:00
for (std::list<LWS::NodeDesc>::iterator ndIt = nodes.begin(); ndIt != nodes.end(); ++ndIt) {
if (!ndIt->parent_resolved) {
++no_parent;
}
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
if (!no_parent) {
2015-05-19 03:57:13 +00:00
throw DeadlyImportError("LWS: Unable to find scene root node");
2020-03-01 12:15:45 +00:00
}
2015-05-19 03:57:13 +00:00
// Load all subsequent files
batch.LoadAll();
// and build the final output graph by attaching the loaded external
// files to ourselves. first build a master graph
2020-03-01 12:15:45 +00:00
aiScene *master = new aiScene();
aiNode *nd = master->mRootNode = new aiNode();
2015-05-19 03:57:13 +00:00
// allocate storage for cameras&lights
if (num_camera > 0u) {
2020-03-01 12:15:45 +00:00
master->mCameras = new aiCamera *[master->mNumCameras = num_camera];
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
aiCamera **cams = master->mCameras;
2015-05-19 03:57:13 +00:00
if (num_light) {
2020-03-01 12:15:45 +00:00
master->mLights = new aiLight *[master->mNumLights = num_light];
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
aiLight **lights = master->mLights;
2015-05-19 03:57:13 +00:00
std::vector<AttachmentInfo> attach;
2020-03-01 12:15:45 +00:00
std::vector<aiNodeAnim *> anims;
2015-05-19 03:57:13 +00:00
nd->mName.Set("<LWSRoot>");
2020-03-01 12:15:45 +00:00
nd->mChildren = new aiNode *[no_parent];
for (std::list<LWS::NodeDesc>::iterator ndIt = nodes.begin(); ndIt != nodes.end(); ++ndIt) {
if (!ndIt->parent_resolved) {
aiNode *ro = nd->mChildren[nd->mNumChildren++] = new aiNode();
2015-05-19 03:57:13 +00:00
ro->mParent = nd;
// ... and build the scene graph. If we encounter object nodes,
// add then to our attachment table.
2020-03-01 12:15:45 +00:00
BuildGraph(ro, *ndIt, attach, batch, cams, lights, anims);
2015-05-19 03:57:13 +00:00
}
}
// create a master animation channel for us
if (anims.size()) {
2020-03-01 12:15:45 +00:00
master->mAnimations = new aiAnimation *[master->mNumAnimations = 1];
aiAnimation *anim = master->mAnimations[0] = new aiAnimation();
2015-05-19 03:57:13 +00:00
anim->mName.Set("LWSMasterAnim");
// LWS uses seconds as time units, but we convert to frames
anim->mTicksPerSecond = fps;
2020-03-01 12:15:45 +00:00
anim->mDuration = last - (first - 1); /* fixme ... zero or one-based?*/
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
anim->mChannels = new aiNodeAnim *[anim->mNumChannels = static_cast<unsigned int>(anims.size())];
std::copy(anims.begin(), anims.end(), anim->mChannels);
2015-05-19 03:57:13 +00:00
}
// convert the master scene to RH
MakeLeftHandedProcess monster_cheat;
monster_cheat.Execute(master);
// .. ccw
FlipWindingOrderProcess flipper;
flipper.Execute(master);
// OK ... finally build the output graph
2020-03-01 12:15:45 +00:00
SceneCombiner::MergeScenes(&pScene, master, attach,
AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES | (!configSpeedFlag ? (
AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY | AI_INT_MERGE_SCENE_GEN_UNIQUE_MATNAMES) :
0));
2015-05-19 03:57:13 +00:00
// Check flags
if (!pScene->mNumMeshes || !pScene->mNumMaterials) {
pScene->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;
if (pScene->mNumAnimations && !noSkeletonMesh) {
// construct skeleton mesh
SkeletonMeshBuilder builder(pScene);
}
}
}
#endif // !! ASSIMP_BUILD_NO_LWS_IMPORTER