assimp/code/AssetLib/AC/ACLoader.cpp

864 lines
36 KiB
C++
Raw Permalink Normal View History

/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
2022-01-10 20:13:43 +00:00
Copyright (c) 2006-2022, assimp team
2018-01-28 18:42:05 +00:00
All rights reserved.
2015-05-19 03:52:10 +00:00
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the following
conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
2015-05-19 03:52:10 +00:00
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2015-05-19 03:52:10 +00:00
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2015-05-19 03:52:10 +00:00
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2015-05-19 03:52:10 +00:00
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
*/
/** @file Implementation of the AC3D importer class */
#ifndef ASSIMP_BUILD_NO_AC_IMPORTER
// internal headers
#include "ACLoader.h"
#include "Common/Importer.h"
#include <assimp/BaseImporter.h>
2020-02-18 13:41:19 +00:00
#include <assimp/ParsingUtils.h>
#include <assimp/Subdivision.h>
#include <assimp/config.h>
#include <assimp/fast_atof.h>
#include <assimp/importerdesc.h>
2016-06-06 20:04:29 +00:00
#include <assimp/light.h>
#include <assimp/material.h>
#include <assimp/scene.h>
2020-02-18 13:41:19 +00:00
#include <assimp/DefaultLogger.hpp>
2016-06-06 20:04:29 +00:00
#include <assimp/IOSystem.hpp>
2020-02-18 13:41:19 +00:00
#include <assimp/Importer.hpp>
#include <memory>
using namespace Assimp;
static const aiImporterDesc desc = {
2015-05-19 03:57:13 +00:00
"AC3D Importer",
"",
"",
"",
aiImporterFlags_SupportTextFlavour,
0,
0,
0,
0,
"ac acc ac3d"
};
// ------------------------------------------------------------------------------------------------
// skip to the next token
2020-03-13 16:00:14 +00:00
inline const char *AcSkipToNextToken(const char *buffer) {
if (!SkipSpaces(&buffer)) {
2020-03-01 12:15:45 +00:00
ASSIMP_LOG_ERROR("AC3D: Unexpected EOF/EOL");
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
return buffer;
}
// ------------------------------------------------------------------------------------------------
// read a string (may be enclosed in double quotation marks). buffer must point to "
2020-03-13 16:00:14 +00:00
inline const char *AcGetString(const char *buffer, std::string &out) {
2020-03-01 12:15:45 +00:00
if (*buffer == '\0') {
throw DeadlyImportError("AC3D: Unexpected EOF in string");
}
++buffer;
const char *sz = buffer;
while ('\"' != *buffer) {
if (IsLineEnd(*buffer)) {
ASSIMP_LOG_ERROR("AC3D: Unexpected EOF/EOL in string");
out = "ERROR";
break;
}
++buffer;
}
if (IsLineEnd(*buffer)) {
return buffer;
}
out = std::string(sz, (unsigned int)(buffer - sz));
2015-05-19 03:57:13 +00:00
++buffer;
2020-03-01 12:15:45 +00:00
return buffer;
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// read 1 to n floats prefixed with an optional predefined identifier
2020-03-13 16:00:14 +00:00
template <class T>
inline const char *TAcCheckedLoadFloatArray(const char *buffer, const char *name, size_t name_length, size_t num, T *out) {
buffer = AcSkipToNextToken(buffer);
2020-03-01 12:15:45 +00:00
if (0 != name_length) {
if (0 != strncmp(buffer, name, name_length) || !IsSpace(buffer[name_length])) {
2021-05-17 09:29:06 +00:00
ASSIMP_LOG_ERROR("AC3D: Unexpected token. ", name, " was expected.");
2020-03-01 12:15:45 +00:00
return buffer;
}
buffer += name_length + 1;
}
for (unsigned int _i = 0; _i < num; ++_i) {
2020-03-13 16:00:14 +00:00
buffer = AcSkipToNextToken(buffer);
2020-03-01 12:15:45 +00:00
buffer = fast_atoreal_move<float>(buffer, ((float *)out)[_i]);
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
return buffer;
}
// ------------------------------------------------------------------------------------------------
// Constructor to be privately used by Importer
2020-02-18 13:41:19 +00:00
AC3DImporter::AC3DImporter() :
buffer(),
configSplitBFCull(),
configEvalSubdivision(),
mNumMeshes(),
mLights(),
mLightsCounter(0),
mGroupsCounter(0),
mPolysCounter(0),
mWorldsCounter(0) {
2015-05-19 03:57:13 +00:00
// nothing to be done here
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Destructor, private as well
AC3DImporter::~AC3DImporter() = default;
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Returns whether the class can handle the format of the given file.
bool AC3DImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const {
static const uint32_t tokens[] = { AI_MAKE_MAGIC("AC3D") };
return CheckMagicToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens));
}
// ------------------------------------------------------------------------------------------------
// Loader meta information
2020-02-18 13:41:19 +00:00
const aiImporterDesc *AC3DImporter::GetInfo() const {
2015-05-19 03:57:13 +00:00
return &desc;
}
// ------------------------------------------------------------------------------------------------
// Get a pointer to the next line from the file
2020-02-18 13:41:19 +00:00
bool AC3DImporter::GetNextLine() {
2015-05-19 03:57:13 +00:00
SkipLine(&buffer);
return SkipSpaces(&buffer);
}
// ------------------------------------------------------------------------------------------------
// Parse an object section in an AC file
2020-02-18 13:41:19 +00:00
void AC3DImporter::LoadObjectSection(std::vector<Object> &objects) {
if (!TokenMatch(buffer, "OBJECT", 6))
2015-05-19 03:57:13 +00:00
return;
SkipSpaces(&buffer);
++mNumMeshes;
objects.emplace_back();
2020-02-18 13:41:19 +00:00
Object &obj = objects.back();
2015-05-19 03:57:13 +00:00
aiLight *light = nullptr;
2020-02-18 13:41:19 +00:00
if (!ASSIMP_strincmp(buffer, "light", 5)) {
2015-05-19 03:57:13 +00:00
// This is a light source. Add it to the list
mLights->push_back(light = new aiLight());
// Return a point light with no attenuation
light->mType = aiLightSource_POINT;
2020-02-18 13:41:19 +00:00
light->mColorDiffuse = light->mColorSpecular = aiColor3D(1.f, 1.f, 1.f);
2015-05-19 03:57:13 +00:00
light->mAttenuationConstant = 1.f;
// Generate a default name for both the light source and the node
// FIXME - what's the right way to print a size_t? Is 'zu' universally available? stick with the safe version.
2020-02-18 13:41:19 +00:00
light->mName.length = ::ai_snprintf(light->mName.data, MAXLEN, "ACLight_%i", static_cast<unsigned int>(mLights->size()) - 1);
obj.name = std::string(light->mName.data);
2015-05-19 03:57:13 +00:00
2020-05-15 15:59:42 +00:00
ASSIMP_LOG_VERBOSE_DEBUG("AC3D: Light source encountered");
2015-05-19 03:57:13 +00:00
obj.type = Object::Light;
2020-02-18 13:41:19 +00:00
} else if (!ASSIMP_strincmp(buffer, "group", 5)) {
2015-05-19 03:57:13 +00:00
obj.type = Object::Group;
2020-02-18 13:41:19 +00:00
} else if (!ASSIMP_strincmp(buffer, "world", 5)) {
2015-05-19 03:57:13 +00:00
obj.type = Object::World;
2020-02-18 13:41:19 +00:00
} else
obj.type = Object::Poly;
while (GetNextLine()) {
if (TokenMatch(buffer, "kids", 4)) {
2015-05-19 03:57:13 +00:00
SkipSpaces(&buffer);
2020-02-18 13:41:19 +00:00
unsigned int num = strtoul10(buffer, &buffer);
2015-05-19 03:57:13 +00:00
GetNextLine();
2020-02-18 13:41:19 +00:00
if (num) {
2015-05-19 03:57:13 +00:00
// load the children of this object recursively
obj.children.reserve(num);
for (unsigned int i = 0; i < num; ++i)
LoadObjectSection(obj.children);
}
return;
2020-02-18 13:41:19 +00:00
} else if (TokenMatch(buffer, "name", 4)) {
2015-05-19 03:57:13 +00:00
SkipSpaces(&buffer);
2020-03-01 12:15:45 +00:00
buffer = AcGetString(buffer, obj.name);
2015-05-19 03:57:13 +00:00
// If this is a light source, we'll also need to store
// the name of the node in it.
2020-02-18 13:41:19 +00:00
if (light) {
2015-05-19 03:57:13 +00:00
light->mName.Set(obj.name);
}
2020-02-18 13:41:19 +00:00
} else if (TokenMatch(buffer, "texture", 7)) {
2015-05-19 03:57:13 +00:00
SkipSpaces(&buffer);
2020-03-01 12:15:45 +00:00
buffer = AcGetString(buffer, obj.texture);
2020-02-18 13:41:19 +00:00
} else if (TokenMatch(buffer, "texrep", 6)) {
2015-05-19 03:57:13 +00:00
SkipSpaces(&buffer);
2020-03-01 12:15:45 +00:00
buffer = TAcCheckedLoadFloatArray(buffer, "", 0, 2, &obj.texRepeat);
2015-05-19 03:57:13 +00:00
if (!obj.texRepeat.x || !obj.texRepeat.y)
2020-02-18 13:41:19 +00:00
obj.texRepeat = aiVector2D(1.f, 1.f);
} else if (TokenMatch(buffer, "texoff", 6)) {
2015-05-19 03:57:13 +00:00
SkipSpaces(&buffer);
2020-03-01 12:15:45 +00:00
buffer = TAcCheckedLoadFloatArray(buffer, "", 0, 2, &obj.texOffset);
2020-02-18 13:41:19 +00:00
} else if (TokenMatch(buffer, "rot", 3)) {
2015-05-19 03:57:13 +00:00
SkipSpaces(&buffer);
2020-03-01 12:15:45 +00:00
buffer = TAcCheckedLoadFloatArray(buffer, "", 0, 9, &obj.rotation);
2020-02-18 13:41:19 +00:00
} else if (TokenMatch(buffer, "loc", 3)) {
2015-05-19 03:57:13 +00:00
SkipSpaces(&buffer);
2020-03-01 12:15:45 +00:00
buffer = TAcCheckedLoadFloatArray(buffer, "", 0, 3, &obj.translation);
2020-02-18 13:41:19 +00:00
} else if (TokenMatch(buffer, "subdiv", 6)) {
2015-05-19 03:57:13 +00:00
SkipSpaces(&buffer);
2020-02-18 13:41:19 +00:00
obj.subDiv = strtoul10(buffer, &buffer);
} else if (TokenMatch(buffer, "crease", 6)) {
2015-05-19 03:57:13 +00:00
SkipSpaces(&buffer);
obj.crease = fast_atof(buffer);
2020-02-18 13:41:19 +00:00
} else if (TokenMatch(buffer, "numvert", 7)) {
2015-05-19 03:57:13 +00:00
SkipSpaces(&buffer);
2020-02-18 13:41:19 +00:00
unsigned int t = strtoul10(buffer, &buffer);
if (t >= AI_MAX_ALLOC(aiVector3D)) {
2015-05-19 03:57:13 +00:00
throw DeadlyImportError("AC3D: Too many vertices, would run out of memory");
}
obj.vertices.reserve(t);
2020-02-18 13:41:19 +00:00
for (unsigned int i = 0; i < t; ++i) {
if (!GetNextLine()) {
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_ERROR("AC3D: Unexpected EOF: not all vertices have been parsed yet");
2015-05-19 03:57:13 +00:00
break;
2020-02-18 13:41:19 +00:00
} else if (!IsNumeric(*buffer)) {
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_ERROR("AC3D: Unexpected token: not all vertices have been parsed yet");
2015-05-19 03:57:13 +00:00
--buffer; // make sure the line is processed a second time
break;
}
obj.vertices.emplace_back();
2020-02-18 13:41:19 +00:00
aiVector3D &v = obj.vertices.back();
2020-03-01 12:15:45 +00:00
buffer = TAcCheckedLoadFloatArray(buffer, "", 0, 3, &v.x);
2015-05-19 03:57:13 +00:00
}
2020-02-18 13:41:19 +00:00
} else if (TokenMatch(buffer, "numsurf", 7)) {
2015-05-19 03:57:13 +00:00
SkipSpaces(&buffer);
bool Q3DWorkAround = false;
2020-02-18 13:41:19 +00:00
const unsigned int t = strtoul10(buffer, &buffer);
2015-05-19 03:57:13 +00:00
obj.surfaces.reserve(t);
2020-02-18 13:41:19 +00:00
for (unsigned int i = 0; i < t; ++i) {
2015-05-19 03:57:13 +00:00
GetNextLine();
2020-02-18 13:41:19 +00:00
if (!TokenMatch(buffer, "SURF", 4)) {
2015-05-19 03:57:13 +00:00
// FIX: this can occur for some files - Quick 3D for
// example writes no surf chunks
2020-02-18 13:41:19 +00:00
if (!Q3DWorkAround) {
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_WARN("AC3D: SURF token was expected");
2020-05-15 15:59:42 +00:00
ASSIMP_LOG_VERBOSE_DEBUG("Continuing with Quick3D Workaround enabled");
2015-05-19 03:57:13 +00:00
}
--buffer; // make sure the line is processed a second time
// break; --- see fix notes above
Q3DWorkAround = true;
}
SkipSpaces(&buffer);
obj.surfaces.emplace_back();
2020-02-18 13:41:19 +00:00
Surface &surf = obj.surfaces.back();
2015-05-19 03:57:13 +00:00
surf.flags = strtoul_cppstyle(buffer);
while (true) {
2020-02-18 13:41:19 +00:00
if (!GetNextLine()) {
throw DeadlyImportError("AC3D: Unexpected EOF: surface is incomplete");
2015-05-19 03:57:13 +00:00
}
2020-02-18 13:41:19 +00:00
if (TokenMatch(buffer, "mat", 3)) {
2015-05-19 03:57:13 +00:00
SkipSpaces(&buffer);
surf.mat = strtoul10(buffer);
2020-02-18 13:41:19 +00:00
} else if (TokenMatch(buffer, "refs", 4)) {
2015-05-19 03:57:13 +00:00
// --- see fix notes above
2020-02-18 13:41:19 +00:00
if (Q3DWorkAround) {
if (!surf.entries.empty()) {
2015-05-19 03:57:13 +00:00
buffer -= 6;
break;
}
}
SkipSpaces(&buffer);
const unsigned int m = strtoul10(buffer);
surf.entries.reserve(m);
obj.numRefs += m;
2020-02-18 13:41:19 +00:00
for (unsigned int k = 0; k < m; ++k) {
if (!GetNextLine()) {
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_ERROR("AC3D: Unexpected EOF: surface references are incomplete");
2015-05-19 03:57:13 +00:00
break;
}
surf.entries.emplace_back();
2020-02-18 13:41:19 +00:00
Surface::SurfaceEntry &entry = surf.entries.back();
2015-05-19 03:57:13 +00:00
2020-02-18 13:41:19 +00:00
entry.first = strtoul10(buffer, &buffer);
2015-05-19 03:57:13 +00:00
SkipSpaces(&buffer);
2020-03-01 12:15:45 +00:00
buffer = TAcCheckedLoadFloatArray(buffer, "", 0, 2, &entry.second);
2015-05-19 03:57:13 +00:00
}
2020-02-18 13:41:19 +00:00
} else {
2015-05-19 03:57:13 +00:00
--buffer; // make sure the line is processed a second time
break;
}
}
}
}
}
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_ERROR("AC3D: Unexpected EOF: \'kids\' line was expected");
}
// ------------------------------------------------------------------------------------------------
// Convert a material from AC3DImporter::Material to aiMaterial
2020-02-18 13:41:19 +00:00
void AC3DImporter::ConvertMaterial(const Object &object,
const Material &matSrc,
aiMaterial &matDest) {
2015-05-19 03:57:13 +00:00
aiString s;
2020-02-18 13:41:19 +00:00
if (matSrc.name.length()) {
2015-05-19 03:57:13 +00:00
s.Set(matSrc.name);
2020-02-18 13:41:19 +00:00
matDest.AddProperty(&s, AI_MATKEY_NAME);
2015-05-19 03:57:13 +00:00
}
2020-02-18 13:41:19 +00:00
if (object.texture.length()) {
2015-05-19 03:57:13 +00:00
s.Set(object.texture);
2020-02-18 13:41:19 +00:00
matDest.AddProperty(&s, AI_MATKEY_TEXTURE_DIFFUSE(0));
2015-05-19 03:57:13 +00:00
// UV transformation
if (1.f != object.texRepeat.x || 1.f != object.texRepeat.y ||
2020-02-18 13:41:19 +00:00
object.texOffset.x || object.texOffset.y) {
2015-05-19 03:57:13 +00:00
aiUVTransform transform;
transform.mScaling = object.texRepeat;
transform.mTranslation = object.texOffset;
2020-02-18 13:41:19 +00:00
matDest.AddProperty(&transform, 1, AI_MATKEY_UVTRANSFORM_DIFFUSE(0));
2015-05-19 03:57:13 +00:00
}
}
2020-02-18 13:41:19 +00:00
matDest.AddProperty<aiColor3D>(&matSrc.rgb, 1, AI_MATKEY_COLOR_DIFFUSE);
matDest.AddProperty<aiColor3D>(&matSrc.amb, 1, AI_MATKEY_COLOR_AMBIENT);
matDest.AddProperty<aiColor3D>(&matSrc.emis, 1, AI_MATKEY_COLOR_EMISSIVE);
matDest.AddProperty<aiColor3D>(&matSrc.spec, 1, AI_MATKEY_COLOR_SPECULAR);
2015-05-19 03:57:13 +00:00
2020-03-13 16:00:14 +00:00
int n = -1;
2020-02-18 13:41:19 +00:00
if (matSrc.shin) {
2015-05-19 03:57:13 +00:00
n = aiShadingMode_Phong;
2020-02-18 13:41:19 +00:00
matDest.AddProperty<float>(&matSrc.shin, 1, AI_MATKEY_SHININESS);
2020-03-13 16:00:14 +00:00
} else {
2020-02-18 13:41:19 +00:00
n = aiShadingMode_Gouraud;
2020-03-13 16:00:14 +00:00
}
2020-02-18 13:41:19 +00:00
matDest.AddProperty<int>(&n, 1, AI_MATKEY_SHADING_MODEL);
2015-05-19 03:57:13 +00:00
float f = 1.f - matSrc.trans;
2020-02-18 13:41:19 +00:00
matDest.AddProperty<float>(&f, 1, AI_MATKEY_OPACITY);
}
// ------------------------------------------------------------------------------------------------
// Converts the loaded data to the internal verbose representation
2020-02-18 13:41:19 +00:00
aiNode *AC3DImporter::ConvertObjectSection(Object &object,
std::vector<aiMesh *> &meshes,
std::vector<aiMaterial *> &outMaterials,
const std::vector<Material> &materials,
aiNode *parent) {
aiNode *node = new aiNode();
2015-05-19 03:57:13 +00:00
node->mParent = parent;
2020-02-18 13:41:19 +00:00
if (object.vertices.size()) {
if (!object.surfaces.size() || !object.numRefs) {
2015-05-19 03:57:13 +00:00
/* " An object with 7 vertices (no surfaces, no materials defined).
This is a good way of getting point data into AC3D.
The Vertex->create convex-surface/object can be used on these
vertices to 'wrap' a 3d shape around them "
(http://www.opencity.info/html/ac3dfileformat.html)
therefore: if no surfaces are defined return point data only
*/
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_INFO("AC3D: No surfaces defined in object definition, "
2020-02-18 13:41:19 +00:00
"a point list is returned");
2015-05-19 03:57:13 +00:00
meshes.push_back(new aiMesh());
2020-02-18 13:41:19 +00:00
aiMesh *mesh = meshes.back();
2015-05-19 03:57:13 +00:00
mesh->mNumFaces = mesh->mNumVertices = (unsigned int)object.vertices.size();
2020-02-18 13:41:19 +00:00
aiFace *faces = mesh->mFaces = new aiFace[mesh->mNumFaces];
aiVector3D *verts = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
2015-05-19 03:57:13 +00:00
2020-02-18 13:41:19 +00:00
for (unsigned int i = 0; i < mesh->mNumVertices; ++i, ++faces, ++verts) {
2015-05-19 03:57:13 +00:00
*verts = object.vertices[i];
faces->mNumIndices = 1;
faces->mIndices = new unsigned int[1];
faces->mIndices[0] = i;
}
// use the primary material in this case. this should be the
// default material if all objects of the file contain points
// and no faces.
mesh->mMaterialIndex = 0;
outMaterials.push_back(new aiMaterial());
ConvertMaterial(object, materials[0], *outMaterials.back());
2020-02-18 13:41:19 +00:00
} else {
2015-05-19 03:57:13 +00:00
// need to generate one or more meshes for this object.
// find out how many different materials we have
2020-02-18 13:41:19 +00:00
typedef std::pair<unsigned int, unsigned int> IntPair;
typedef std::vector<IntPair> MatTable;
MatTable needMat(materials.size(), IntPair(0, 0));
2015-05-19 03:57:13 +00:00
2020-02-18 13:41:19 +00:00
std::vector<Surface>::iterator it, end = object.surfaces.end();
std::vector<Surface::SurfaceEntry>::iterator it2, end2;
2015-05-19 03:57:13 +00:00
2020-02-18 13:41:19 +00:00
for (it = object.surfaces.begin(); it != end; ++it) {
2015-05-19 03:57:13 +00:00
unsigned int idx = (*it).mat;
2020-02-18 13:41:19 +00:00
if (idx >= needMat.size()) {
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_ERROR("AC3D: material index is out of range");
2015-05-19 03:57:13 +00:00
idx = 0;
}
2020-02-18 13:41:19 +00:00
if ((*it).entries.empty()) {
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_WARN("AC3D: surface her zero vertex references");
2015-05-19 03:57:13 +00:00
}
// validate all vertex indices to make sure we won't crash here
2020-02-18 13:41:19 +00:00
for (it2 = (*it).entries.begin(),
end2 = (*it).entries.end();
it2 != end2; ++it2) {
if ((*it2).first >= object.vertices.size()) {
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_WARN("AC3D: Invalid vertex reference");
2015-05-19 03:57:13 +00:00
(*it2).first = 0;
}
}
2020-03-13 16:00:14 +00:00
if (!needMat[idx].first) {
++node->mNumMeshes;
}
2015-05-19 03:57:13 +00:00
2020-07-17 10:23:50 +00:00
switch ((*it).GetType()) {
// closed line
2020-07-17 10:23:50 +00:00
case Surface::ClosedLine:
needMat[idx].first += (unsigned int)(*it).entries.size();
needMat[idx].second += (unsigned int)(*it).entries.size() << 1u;
break;
2015-05-19 03:57:13 +00:00
// unclosed line
2020-07-17 10:23:50 +00:00
case Surface::OpenLine:
needMat[idx].first += (unsigned int)(*it).entries.size() - 1;
needMat[idx].second += ((unsigned int)(*it).entries.size() - 1) << 1u;
break;
2015-05-19 03:57:13 +00:00
// triangle strip
2020-07-17 10:23:50 +00:00
case Surface::TriangleStrip:
needMat[idx].first += (unsigned int)(*it).entries.size() - 2;
needMat[idx].second += ((unsigned int)(*it).entries.size() - 2) * 3;
break;
default:
2020-07-17 10:23:50 +00:00
// Coerce unknowns to a polygon and warn
ASSIMP_LOG_WARN("AC3D: The type flag of a surface is unknown: ", (*it).flags);
2020-07-17 10:23:50 +00:00
(*it).flags &= ~(Surface::Mask);
// fallthrough
2015-05-19 03:57:13 +00:00
2020-07-17 10:23:50 +00:00
// polygon
case Surface::Polygon:
// the number of faces increments by one, the number
// of vertices by surface.numref.
needMat[idx].first++;
needMat[idx].second += (unsigned int)(*it).entries.size();
2015-05-19 03:57:13 +00:00
};
}
2020-02-18 13:41:19 +00:00
unsigned int *pip = node->mMeshes = new unsigned int[node->mNumMeshes];
2015-05-19 03:57:13 +00:00
unsigned int mat = 0;
const size_t oldm = meshes.size();
for (MatTable::const_iterator cit = needMat.begin(), cend = needMat.end();
2020-02-18 13:41:19 +00:00
cit != cend; ++cit, ++mat) {
2020-03-01 12:15:45 +00:00
if (!(*cit).first) {
continue;
}
2015-05-19 03:57:13 +00:00
// allocate a new aiMesh object
*pip++ = (unsigned int)meshes.size();
2020-02-18 13:41:19 +00:00
aiMesh *mesh = new aiMesh();
2015-05-19 03:57:13 +00:00
meshes.push_back(mesh);
2020-03-13 16:00:14 +00:00
mesh->mMaterialIndex = static_cast<unsigned int>(outMaterials.size());
2015-05-19 03:57:13 +00:00
outMaterials.push_back(new aiMaterial());
ConvertMaterial(object, materials[mat], *outMaterials.back());
// allocate storage for vertices and normals
mesh->mNumFaces = (*cit).first;
if (mesh->mNumFaces == 0) {
throw DeadlyImportError("AC3D: No faces");
} else if (mesh->mNumFaces > AI_MAX_ALLOC(aiFace)) {
throw DeadlyImportError("AC3D: Too many faces, would run out of memory");
}
2020-02-18 13:41:19 +00:00
aiFace *faces = mesh->mFaces = new aiFace[mesh->mNumFaces];
2015-05-19 03:57:13 +00:00
mesh->mNumVertices = (*cit).second;
if (mesh->mNumVertices == 0) {
throw DeadlyImportError("AC3D: No vertices");
} else if (mesh->mNumVertices > AI_MAX_ALLOC(aiVector3D)) {
throw DeadlyImportError("AC3D: Too many vertices, would run out of memory");
}
2020-02-18 13:41:19 +00:00
aiVector3D *vertices = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
2015-05-19 03:57:13 +00:00
unsigned int cur = 0;
// allocate UV coordinates, but only if the texture name for the
// surface is not empty
aiVector3D *uv = nullptr;
2020-02-18 13:41:19 +00:00
if (object.texture.length()) {
2015-05-19 03:57:13 +00:00
uv = mesh->mTextureCoords[0] = new aiVector3D[mesh->mNumVertices];
mesh->mNumUVComponents[0] = 2;
}
2020-02-18 13:41:19 +00:00
for (it = object.surfaces.begin(); it != end; ++it) {
if (mat == (*it).mat) {
const Surface &src = *it;
2015-05-19 03:57:13 +00:00
// closed polygon
2020-07-17 10:23:50 +00:00
uint8_t type = (*it).GetType();
if (type == Surface::Polygon) {
2020-02-18 13:41:19 +00:00
aiFace &face = *faces++;
2020-03-01 12:15:45 +00:00
face.mNumIndices = (unsigned int)src.entries.size();
if (0 != face.mNumIndices) {
2015-05-19 03:57:13 +00:00
face.mIndices = new unsigned int[face.mNumIndices];
2020-02-18 13:41:19 +00:00
for (unsigned int i = 0; i < face.mNumIndices; ++i, ++vertices) {
const Surface::SurfaceEntry &entry = src.entries[i];
2015-05-19 03:57:13 +00:00
face.mIndices[i] = cur++;
// copy vertex positions
if (static_cast<unsigned>(vertices - mesh->mVertices) >= mesh->mNumVertices) {
2015-05-19 03:57:13 +00:00
throw DeadlyImportError("AC3D: Invalid number of vertices");
}
*vertices = object.vertices[entry.first] + object.translation;
// copy texture coordinates
2020-02-18 13:41:19 +00:00
if (uv) {
uv->x = entry.second.x;
uv->y = entry.second.y;
2015-05-19 03:57:13 +00:00
++uv;
}
}
}
2020-07-17 10:23:50 +00:00
} else if (type == Surface::TriangleStrip) {
for (unsigned int i = 0; i < (unsigned int)src.entries.size() - 2; ++i) {
const Surface::SurfaceEntry &entry1 = src.entries[i];
const Surface::SurfaceEntry &entry2 = src.entries[i + 1];
const Surface::SurfaceEntry &entry3 = src.entries[i + 2];
// skip degenerate triangles
if (object.vertices[entry1.first] == object.vertices[entry2.first] ||
2020-07-17 10:23:50 +00:00
object.vertices[entry1.first] == object.vertices[entry3.first] ||
object.vertices[entry2.first] == object.vertices[entry3.first]) {
mesh->mNumFaces--;
mesh->mNumVertices -= 3;
continue;
}
aiFace &face = *faces++;
face.mNumIndices = 3;
face.mIndices = new unsigned int[face.mNumIndices];
face.mIndices[0] = cur++;
face.mIndices[1] = cur++;
face.mIndices[2] = cur++;
if (!(i & 1)) {
*vertices++ = object.vertices[entry1.first] + object.translation;
if (uv) {
uv->x = entry1.second.x;
uv->y = entry1.second.y;
++uv;
}
*vertices++ = object.vertices[entry2.first] + object.translation;
if (uv) {
uv->x = entry2.second.x;
uv->y = entry2.second.y;
++uv;
}
} else {
*vertices++ = object.vertices[entry2.first] + object.translation;
if (uv) {
uv->x = entry2.second.x;
uv->y = entry2.second.y;
++uv;
}
*vertices++ = object.vertices[entry1.first] + object.translation;
if (uv) {
uv->x = entry1.second.x;
uv->y = entry1.second.y;
++uv;
}
}
2020-07-08 03:29:54 +00:00
if (static_cast<unsigned>(vertices - mesh->mVertices) >= mesh->mNumVertices) {
throw DeadlyImportError("AC3D: Invalid number of vertices");
}
*vertices++ = object.vertices[entry3.first] + object.translation;
if (uv) {
uv->x = entry3.second.x;
uv->y = entry3.second.y;
++uv;
}
}
2020-02-18 13:41:19 +00:00
} else {
2015-05-19 03:57:13 +00:00
2020-02-18 13:41:19 +00:00
it2 = (*it).entries.begin();
2015-05-19 03:57:13 +00:00
// either a closed or an unclosed line
unsigned int tmp = (unsigned int)(*it).entries.size();
2020-07-17 10:23:50 +00:00
if (Surface::OpenLine == type) --tmp;
2020-02-18 13:41:19 +00:00
for (unsigned int m = 0; m < tmp; ++m) {
aiFace &face = *faces++;
2015-05-19 03:57:13 +00:00
face.mNumIndices = 2;
face.mIndices = new unsigned int[2];
face.mIndices[0] = cur++;
face.mIndices[1] = cur++;
// copy vertex positions
2020-02-18 13:41:19 +00:00
if (it2 == (*it).entries.end()) {
2015-05-19 03:57:13 +00:00
throw DeadlyImportError("AC3D: Bad line");
}
ai_assert((*it2).first < object.vertices.size());
*vertices++ = object.vertices[(*it2).first];
// copy texture coordinates
2020-02-18 13:41:19 +00:00
if (uv) {
uv->x = (*it2).second.x;
uv->y = (*it2).second.y;
2015-05-19 03:57:13 +00:00
++uv;
}
2020-07-17 10:23:50 +00:00
if (Surface::ClosedLine == type && tmp - 1 == m) {
2015-05-19 03:57:13 +00:00
// if this is a closed line repeat its beginning now
2020-02-18 13:41:19 +00:00
it2 = (*it).entries.begin();
} else
++it2;
2015-05-19 03:57:13 +00:00
// second point
*vertices++ = object.vertices[(*it2).first];
2020-02-18 13:41:19 +00:00
if (uv) {
uv->x = (*it2).second.x;
uv->y = (*it2).second.y;
2015-05-19 03:57:13 +00:00
++uv;
}
}
}
}
}
}
// Now apply catmull clark subdivision if necessary. We split meshes into
// materials which is not done by AC3D during smoothing, so we need to
// collect all meshes using the same material group.
2020-02-18 13:41:19 +00:00
if (object.subDiv) {
2015-05-19 03:57:13 +00:00
if (configEvalSubdivision) {
std::unique_ptr<Subdivider> div(Subdivider::Create(Subdivider::CATMULL_CLARKE));
ASSIMP_LOG_INFO("AC3D: Evaluating subdivision surface: ", object.name);
2015-05-19 03:57:13 +00:00
std::vector<aiMesh *> cpy(meshes.size() - oldm, nullptr);
2020-02-18 13:41:19 +00:00
div->Subdivide(&meshes[oldm], cpy.size(), &cpy.front(), object.subDiv, true);
std::copy(cpy.begin(), cpy.end(), meshes.begin() + oldm);
2015-05-19 03:57:13 +00:00
// previous meshes are deleted vy Subdivide().
2020-02-18 13:41:19 +00:00
} else {
ASSIMP_LOG_INFO("AC3D: Letting the subdivision surface untouched due to my configuration: ", object.name);
2015-05-19 03:57:13 +00:00
}
}
}
}
if (object.name.length())
node->mName.Set(object.name);
2020-02-18 13:41:19 +00:00
else {
2015-05-19 03:57:13 +00:00
// generate a name depending on the type of the node
2020-02-18 13:41:19 +00:00
switch (object.type) {
case Object::Group:
node->mName.length = ::ai_snprintf(node->mName.data, MAXLEN, "ACGroup_%i", mGroupsCounter++);
break;
case Object::Poly:
node->mName.length = ::ai_snprintf(node->mName.data, MAXLEN, "ACPoly_%i", mPolysCounter++);
break;
case Object::Light:
node->mName.length = ::ai_snprintf(node->mName.data, MAXLEN, "ACLight_%i", mLightsCounter++);
break;
// there shouldn't be more than one world, but we don't care
case Object::World:
node->mName.length = ::ai_snprintf(node->mName.data, MAXLEN, "ACWorld_%i", mWorldsCounter++);
break;
2015-05-19 03:57:13 +00:00
}
}
// setup the local transformation matrix of the object
// compute the transformation offset to the parent node
2020-02-18 13:41:19 +00:00
node->mTransformation = aiMatrix4x4(object.rotation);
2015-05-19 03:57:13 +00:00
2020-02-18 13:41:19 +00:00
if (object.type == Object::Group || !object.numRefs) {
2015-05-19 03:57:13 +00:00
node->mTransformation.a4 = object.translation.x;
node->mTransformation.b4 = object.translation.y;
node->mTransformation.c4 = object.translation.z;
}
// add children to the object
2020-02-18 13:41:19 +00:00
if (object.children.size()) {
2015-05-19 03:57:13 +00:00
node->mNumChildren = (unsigned int)object.children.size();
2020-02-18 13:41:19 +00:00
node->mChildren = new aiNode *[node->mNumChildren];
for (unsigned int i = 0; i < node->mNumChildren; ++i) {
node->mChildren[i] = ConvertObjectSection(object.children[i], meshes, outMaterials, materials, node);
2015-05-19 03:57:13 +00:00
}
}
return node;
}
// ------------------------------------------------------------------------------------------------
2020-02-18 13:41:19 +00:00
void AC3DImporter::SetupProperties(const Importer *pImp) {
configSplitBFCull = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_AC_SEPARATE_BFCULL, 1) ? true : false;
configEvalSubdivision = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_AC_EVAL_SUBDIVISION, 1) ? true : false;
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Imports the given file into the given scene structure.
2020-02-18 13:41:19 +00:00
void AC3DImporter::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
if (file == nullptr) {
throw DeadlyImportError("Failed to open AC3D file ", pFile, ".");
2020-03-13 16:00:14 +00:00
}
2015-05-19 03:57:13 +00:00
// allocate storage and copy the contents of the file to a memory buffer
std::vector<char> mBuffer2;
2020-02-18 13:41:19 +00:00
TextFileToBuffer(file.get(), mBuffer2);
2015-05-19 03:57:13 +00:00
buffer = &mBuffer2[0];
mNumMeshes = 0;
2020-02-18 13:41:19 +00:00
mLightsCounter = mPolysCounter = mWorldsCounter = mGroupsCounter = 0;
2015-05-19 03:57:13 +00:00
2020-02-18 13:41:19 +00:00
if (::strncmp(buffer, "AC3D", 4)) {
2015-05-19 03:57:13 +00:00
throw DeadlyImportError("AC3D: No valid AC3D file, magic sequence not found");
}
// print the file format version to the console
2020-02-18 13:41:19 +00:00
unsigned int version = HexDigitToDecimal(buffer[4]);
2015-05-19 03:57:13 +00:00
char msg[3];
2020-02-18 13:41:19 +00:00
ASSIMP_itoa10(msg, 3, version);
ASSIMP_LOG_INFO("AC3D file format version: ", msg);
2015-05-19 03:57:13 +00:00
std::vector<Material> materials;
materials.reserve(5);
std::vector<Object> rootObjects;
rootObjects.reserve(5);
2020-02-18 13:41:19 +00:00
std::vector<aiLight *> lights;
mLights = &lights;
2015-05-19 03:57:13 +00:00
2020-02-18 13:41:19 +00:00
while (GetNextLine()) {
if (TokenMatch(buffer, "MATERIAL", 8)) {
materials.emplace_back();
2020-02-18 13:41:19 +00:00
Material &mat = materials.back();
2015-05-19 03:57:13 +00:00
// manually parse the material ... sscanf would use the buldin atof ...
// Format: (name) rgb %f %f %f amb %f %f %f emis %f %f %f spec %f %f %f shi %d trans %f
2020-03-01 12:15:45 +00:00
buffer = AcSkipToNextToken(buffer);
2020-02-18 13:41:19 +00:00
if ('\"' == *buffer) {
2020-03-01 12:15:45 +00:00
buffer = AcGetString(buffer, mat.name);
buffer = AcSkipToNextToken(buffer);
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
buffer = TAcCheckedLoadFloatArray(buffer, "rgb", 3, 3, &mat.rgb);
buffer = TAcCheckedLoadFloatArray(buffer, "amb", 3, 3, &mat.amb);
buffer = TAcCheckedLoadFloatArray(buffer, "emis", 4, 3, &mat.emis);
buffer = TAcCheckedLoadFloatArray(buffer, "spec", 4, 3, &mat.spec);
buffer = TAcCheckedLoadFloatArray(buffer, "shi", 3, 1, &mat.shin);
buffer = TAcCheckedLoadFloatArray(buffer, "trans", 5, 1, &mat.trans);
2015-05-19 03:57:13 +00:00
}
LoadObjectSection(rootObjects);
}
2020-02-18 13:41:19 +00:00
if (rootObjects.empty() || !mNumMeshes) {
2015-05-19 03:57:13 +00:00
throw DeadlyImportError("AC3D: No meshes have been loaded");
}
2020-02-18 13:41:19 +00:00
if (materials.empty()) {
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_WARN("AC3D: No material has been found");
materials.emplace_back();
2015-05-19 03:57:13 +00:00
}
2020-02-18 13:41:19 +00:00
mNumMeshes += (mNumMeshes >> 2u) + 1;
std::vector<aiMesh *> meshes;
2015-05-19 03:57:13 +00:00
meshes.reserve(mNumMeshes);
2020-02-18 13:41:19 +00:00
std::vector<aiMaterial *> omaterials;
2015-05-19 03:57:13 +00:00
materials.reserve(mNumMeshes);
// generate a dummy root if there are multiple objects on the top layer
2020-02-18 13:41:19 +00:00
Object *root;
2015-05-19 03:57:13 +00:00
if (1 == rootObjects.size())
root = &rootObjects[0];
2020-02-18 13:41:19 +00:00
else {
2015-05-19 03:57:13 +00:00
root = new Object();
}
// now convert the imported stuff to our output data structure
2020-02-18 13:41:19 +00:00
pScene->mRootNode = ConvertObjectSection(*root, meshes, omaterials, materials);
2020-03-13 16:00:14 +00:00
if (1 != rootObjects.size()) {
delete root;
}
2015-05-19 03:57:13 +00:00
2020-03-13 16:00:14 +00:00
if (!::strncmp(pScene->mRootNode->mName.data, "Node", 4)) {
2015-05-19 03:57:13 +00:00
pScene->mRootNode->mName.Set("<AC3DWorld>");
2020-03-13 16:00:14 +00:00
}
2015-05-19 03:57:13 +00:00
// copy meshes
2020-02-18 13:41:19 +00:00
if (meshes.empty()) {
2016-04-03 00:38:00 +00:00
throw DeadlyImportError("An unknown error occurred during converting");
2015-05-19 03:57:13 +00:00
}
pScene->mNumMeshes = (unsigned int)meshes.size();
2020-02-18 13:41:19 +00:00
pScene->mMeshes = new aiMesh *[pScene->mNumMeshes];
::memcpy(pScene->mMeshes, &meshes[0], pScene->mNumMeshes * sizeof(void *));
2015-05-19 03:57:13 +00:00
// copy materials
pScene->mNumMaterials = (unsigned int)omaterials.size();
2020-02-18 13:41:19 +00:00
pScene->mMaterials = new aiMaterial *[pScene->mNumMaterials];
::memcpy(pScene->mMaterials, &omaterials[0], pScene->mNumMaterials * sizeof(void *));
2015-05-19 03:57:13 +00:00
// copy lights
pScene->mNumLights = (unsigned int)lights.size();
2020-02-18 13:41:19 +00:00
if (lights.size()) {
pScene->mLights = new aiLight *[lights.size()];
::memcpy(pScene->mLights, &lights[0], lights.size() * sizeof(void *));
2015-05-19 03:57:13 +00:00
}
}
#endif //!defined ASSIMP_BUILD_NO_AC_IMPORTER