assimp/code/AssetLib/LWS/LWSLoader.cpp

933 lines
34 KiB
C++
Raw Normal View History

/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
2021-02-28 11:17:54 +00:00
Copyright (c) 2006-2021, 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;
static const 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
2020-03-01 12:15:45 +00:00
void LWS::Element::Parse(const char *&buffer) {
for (; SkipSpacesAndLineEnd(&buffer); SkipLine(&buffer)) {
2015-05-19 03:57:13 +00:00
// begin of a new element with children
bool sub = false;
if (*buffer == '{') {
++buffer;
SkipSpaces(&buffer);
sub = true;
2020-03-01 12:15:45 +00:00
} else if (*buffer == '}')
2015-05-19 03:57:13 +00:00
return;
children.push_back(Element());
// 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));
2015-05-19 03:57:13 +00:00
SkipSpaces(&buffer);
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
2020-03-01 12:15:45 +00:00
for (; SkipSpacesAndLineEnd(&buffer); SkipLine(&buffer)) {
if (!::strncmp(buffer, "EndPlugin", 9)) {
2015-05-19 03:57:13 +00:00
//SkipLine(&buffer);
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) {
2015-05-19 03:57:13 +00:00
children.back().Parse(buffer);
}
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
// Destructor, private as well
2020-03-01 12:15:45 +00:00
LWSImporter::~LWSImporter() {
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.
2020-03-01 12:15:45 +00:00
bool LWSImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const {
2015-05-19 03:57:13 +00:00
const std::string extension = GetExtension(pFile);
if (extension == "lws" || extension == "mot") {
2015-05-19 03:57:13 +00:00
return true;
}
2015-05-19 03:57:13 +00:00
// if check for extension is not enough, check for the magic tokens LWSC and LWMO
if (!extension.length() || checkSig) {
uint32_t tokens[2];
tokens[0] = AI_MAKE_MAGIC("LWSC");
tokens[1] = AI_MAKE_MAGIC("LWMO");
2020-03-01 12:15:45 +00:00
return CheckMagicToken(pIOHandler, pFile, tokens, 2);
2015-05-19 03:57:13 +00:00
}
return false;
}
// ------------------------------------------------------------------------------------------------
// 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;
}
// ------------------------------------------------------------------------------------------------
// 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,
2020-03-01 12:15:45 +00:00
150392 /* 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,
2020-03-01 12:15:45 +00:00
150392 /* 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();
2015-05-19 03:57:13 +00:00
if ((*it).tokens[0] == "Key") {
fill.keys.push_back(LWO::Key());
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);
2020-03-01 12:15:45 +00:00
c = fast_atoreal_move<float>(c, key.value);
2015-05-19 03:57:13 +00:00
SkipSpaces(&c);
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) {
2015-05-19 03:57:13 +00:00
SkipSpaces(&c);
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") {
2015-05-19 03:57:13 +00:00
SkipSpaces(&c);
2020-03-01 12:15:45 +00:00
fill.pre = (LWO::PrePostBehaviour)strtoul10(c, &c);
2015-05-19 03:57:13 +00:00
SkipSpaces(&c);
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(
2020-03-01 12:15:45 +00:00
std::list<LWS::Element>::const_iterator &it,
const std::list<LWS::Element>::const_iterator &end,
LWS::NodeDesc &nodes,
unsigned int /*version*/) {
unsigned int num, sub_num;
if (++it == end) goto unexpected_end;
2015-05-19 03:57:13 +00:00
num = strtoul10((*it).tokens[0].c_str());
for (unsigned int i = 0; i < num; ++i) {
2015-05-19 03:52:10 +00:00
2015-05-19 03:57:13 +00:00
nodes.channels.push_back(LWO::Envelope());
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 == end) {
goto unexpected_end;
}
2015-05-19 03:57:13 +00:00
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) {
2020-03-01 12:15:45 +00:00
if (++it == end) goto unexpected_end;
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);
2015-05-19 03:57:13 +00:00
SkipSpaces(&c);
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;
2015-05-19 03:57:13 +00:00
envl.keys.push_back(key);
}
}
return;
unexpected_end:
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWS: Encountered unexpected end of file while parsing object motion");
}
// ------------------------------------------------------------------------------------------------
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
2020-03-01 12:15:45 +00:00
nd->mName.length = ::ai_snprintf(nd->mName.data, MAXLEN, "%s_(%08X)", src.path.substr(s).substr(0, t).c_str(), combined);
2015-05-19 03:57:13 +00:00
return;
}
}
2020-03-01 12:15:45 +00:00
nd->mName.length = ::ai_snprintf(nd->mName.data, 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) {
2018-04-20 14:23:24 +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) {
2020-03-01 12:15:45 +00:00
attach.push_back(AttachmentInfo(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
2020-06-21 10:03:38 +00:00
if (file.get() == 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];
2015-05-19 03:57:13 +00:00
root.Parse(dummy);
// 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;
unsigned int num_light = 0, num_camera = 0, num_object = 0;
// 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;
unsigned int version = strtoul10((*it).tokens[0].c_str());
2018-04-20 14:23:24 +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();
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);
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);
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);
++num_object;
} 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;
2015-05-19 03:57:13 +00:00
SkipSpaces(&c);
} 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);
++num_object;
} 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;
2015-05-19 03:57:13 +00:00
SkipSpaces(&c);
} 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);
num_object++;
}
// '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\'");
2015-05-19 03:57:13 +00:00
}
// important: index of channel
nodes.back().channels.push_back(LWO::Envelope());
2020-03-01 12:15:45 +00:00
LWO::Envelope &env = nodes.back().channels.back();
2015-05-19 03:57:13 +00:00
env.index = strtoul10(c);
// currently we can just interpret the standard channels 0...9
// (hack) assume that index-i yields the binary channel type from LWO
2020-03-01 12:15:45 +00:00
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);
env.post = (LWO::PrePostBehaviour)strtoul10(c, &c);
SkipSpaces(&c);
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\'");
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
else
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\'");
2015-05-19 03:57:13 +00:00
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
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\'");
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
else
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
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\'");
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
else
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\'");
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
else
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\'");
2020-03-01 12:15:45 +00:00
else
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\'");
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
else
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\'");
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
else
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\'");
2015-05-19 03:57:13 +00:00
else {
2020-03-01 12:15:45 +00:00
c = fast_atoreal_move<float>(c, (float &)nodes.back().lightColor.r);
2015-05-19 03:57:13 +00:00
SkipSpaces(&c);
2020-03-01 12:15:45 +00:00
c = fast_atoreal_move<float>(c, (float &)nodes.back().lightColor.g);
2015-05-19 03:57:13 +00:00
SkipSpaces(&c);
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\'");
2015-05-19 03:57:13 +00:00
else {
2020-03-01 12:15:45 +00:00
c = fast_atoreal_move<float>(c, (float &)nodes.back().pivotPos.x);
2015-05-19 03:57:13 +00:00
SkipSpaces(&c);
2020-03-01 12:15:45 +00:00
c = fast_atoreal_move<float>(c, (float &)nodes.back().pivotPos.y);
2015-05-19 03:57:13 +00:00
SkipSpaces(&c);
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) {
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