2015-12-14 06:14:19 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
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
|
|
|
|
2015-12-14 06:14:19 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the following
|
|
|
|
conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file SIBImporter.cpp
|
2016-02-07 21:21:58 +00:00
|
|
|
* @brief Implementation of the SIB importer class.
|
2015-12-14 06:14:19 +00:00
|
|
|
*
|
|
|
|
* The Nevercenter Silo SIB format is undocumented.
|
|
|
|
* All details here have been reverse engineered from
|
|
|
|
* studying the binary files output by Silo.
|
|
|
|
*
|
|
|
|
* Nevertheless, this implementation is reasonably complete.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_SIB_IMPORTER
|
|
|
|
|
|
|
|
// internal headers
|
|
|
|
#include "SIBImporter.h"
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/ByteSwapper.h>
|
|
|
|
#include <assimp/StreamReader.h>
|
|
|
|
#include <assimp/TinyFormatter.h>
|
2023-11-06 10:09:22 +00:00
|
|
|
#include "utf8.h"
|
2017-02-22 16:20:26 +00:00
|
|
|
#include <assimp/importerdesc.h>
|
2020-06-23 19:05:42 +00:00
|
|
|
#include <assimp/scene.h>
|
|
|
|
#include <assimp/DefaultLogger.hpp>
|
|
|
|
#include <assimp/IOSystem.hpp>
|
2021-05-05 01:24:44 +00:00
|
|
|
#include <assimp/StringUtils.h>
|
2017-02-22 16:20:26 +00:00
|
|
|
|
|
|
|
#include <map>
|
2015-12-14 06:14:19 +00:00
|
|
|
|
2023-11-11 08:59:42 +00:00
|
|
|
namespace Assimp {
|
2015-12-14 06:14:19 +00:00
|
|
|
|
2023-11-11 08:59:42 +00:00
|
|
|
static constexpr aiImporterDesc desc = {
|
2015-12-14 06:14:19 +00:00
|
|
|
"Silo SIB Importer",
|
|
|
|
"Richard Mitton (http://www.codersnotes.com/about)",
|
|
|
|
"",
|
|
|
|
"Does not apply subdivision.",
|
|
|
|
aiImporterFlags_SupportBinaryFlavour,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
"sib"
|
|
|
|
};
|
|
|
|
|
2017-05-26 07:18:23 +00:00
|
|
|
struct SIBChunk {
|
2020-06-23 19:05:42 +00:00
|
|
|
uint32_t Tag;
|
|
|
|
uint32_t Size;
|
2023-04-19 15:26:03 +00:00
|
|
|
};
|
2015-12-14 06:14:19 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
enum {
|
|
|
|
POS,
|
|
|
|
NRM,
|
|
|
|
UV,
|
2017-05-26 07:18:23 +00:00
|
|
|
N
|
|
|
|
};
|
2015-12-14 06:14:19 +00:00
|
|
|
|
2023-11-11 08:59:42 +00:00
|
|
|
using SIBPair = std::pair<uint32_t, uint32_t>;
|
2015-12-14 06:14:19 +00:00
|
|
|
|
2017-05-26 07:18:23 +00:00
|
|
|
struct SIBEdge {
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t faceA, faceB;
|
|
|
|
bool creased;
|
|
|
|
};
|
|
|
|
|
2017-05-26 07:18:23 +00:00
|
|
|
struct SIBMesh {
|
2015-12-14 06:14:19 +00:00
|
|
|
aiMatrix4x4 axis;
|
|
|
|
uint32_t numPts;
|
|
|
|
std::vector<aiVector3D> pos, nrm, uv;
|
|
|
|
std::vector<uint32_t> idx;
|
|
|
|
std::vector<uint32_t> faceStart;
|
|
|
|
std::vector<uint32_t> mtls;
|
|
|
|
std::vector<SIBEdge> edges;
|
|
|
|
std::map<SIBPair, uint32_t> edgeMap;
|
|
|
|
};
|
|
|
|
|
2017-05-26 07:18:23 +00:00
|
|
|
struct SIBObject {
|
2015-12-14 06:14:19 +00:00
|
|
|
aiString name;
|
|
|
|
aiMatrix4x4 axis;
|
|
|
|
size_t meshIdx, meshCount;
|
|
|
|
};
|
|
|
|
|
2017-05-26 07:18:23 +00:00
|
|
|
struct SIB {
|
2020-06-23 19:05:42 +00:00
|
|
|
std::vector<aiMaterial *> mtls;
|
|
|
|
std::vector<aiMesh *> meshes;
|
|
|
|
std::vector<aiLight *> lights;
|
2015-12-14 06:14:19 +00:00
|
|
|
std::vector<SIBObject> objs, insts;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
static SIBEdge &GetEdge(SIBMesh *mesh, uint32_t posA, uint32_t posB) {
|
2015-12-14 06:14:19 +00:00
|
|
|
SIBPair pair = (posA < posB) ? SIBPair(posA, posB) : SIBPair(posB, posA);
|
|
|
|
std::map<SIBPair, uint32_t>::iterator it = mesh->edgeMap.find(pair);
|
|
|
|
if (it != mesh->edgeMap.end())
|
|
|
|
return mesh->edges[it->second];
|
|
|
|
|
|
|
|
SIBEdge edge;
|
|
|
|
edge.creased = false;
|
|
|
|
edge.faceA = edge.faceB = 0xffffffff;
|
2016-11-20 03:06:56 +00:00
|
|
|
mesh->edgeMap[pair] = static_cast<uint32_t>(mesh->edges.size());
|
2015-12-14 06:14:19 +00:00
|
|
|
mesh->edges.push_back(edge);
|
|
|
|
return mesh->edges.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Helpers for reading chunked data.
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
#define TAG(A, B, C, D) ((A << 24) | (B << 16) | (C << 8) | D)
|
2015-12-14 06:14:19 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
static SIBChunk ReadChunk(StreamReaderLE *stream) {
|
2015-12-14 06:14:19 +00:00
|
|
|
SIBChunk chunk;
|
|
|
|
chunk.Tag = stream->GetU4();
|
|
|
|
chunk.Size = stream->GetU4();
|
|
|
|
if (chunk.Size > stream->GetRemainingSizeToLimit())
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_ERROR("SIB: Chunk overflow");
|
2015-12-14 06:14:19 +00:00
|
|
|
ByteSwap::Swap4(&chunk.Tag);
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
static aiColor3D ReadColor(StreamReaderLE *stream) {
|
2015-12-14 06:14:19 +00:00
|
|
|
float r = stream->GetF4();
|
|
|
|
float g = stream->GetF4();
|
|
|
|
float b = stream->GetF4();
|
|
|
|
stream->GetU4(); // Colors have an unused(?) 4th component.
|
|
|
|
return aiColor3D(r, g, b);
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
static void UnknownChunk(StreamReaderLE * /*stream*/, const SIBChunk &chunk) {
|
2021-05-05 01:24:44 +00:00
|
|
|
char temp[4] = {
|
2020-06-23 19:05:42 +00:00
|
|
|
static_cast<char>((chunk.Tag >> 24) & 0xff),
|
|
|
|
static_cast<char>((chunk.Tag >> 16) & 0xff),
|
|
|
|
static_cast<char>((chunk.Tag >> 8) & 0xff),
|
2021-05-05 01:24:44 +00:00
|
|
|
static_cast<char>(chunk.Tag & 0xff)
|
2016-01-30 23:41:14 +00:00
|
|
|
};
|
2016-07-15 05:38:29 +00:00
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_WARN("SIB: Skipping unknown '", ai_str_toprintable(temp, 4), "' chunk.");
|
2015-12-14 06:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reads a UTF-16LE string and returns it at UTF-8.
|
2017-09-29 19:58:58 +00:00
|
|
|
static aiString ReadString(StreamReaderLE *stream, uint32_t numWChars) {
|
2020-06-23 19:05:42 +00:00
|
|
|
if (nullptr == stream || 0 == numWChars) {
|
2021-05-03 15:01:59 +00:00
|
|
|
return aiString();
|
2017-06-16 14:43:39 +00:00
|
|
|
}
|
2017-09-29 19:58:58 +00:00
|
|
|
|
2015-12-14 06:14:19 +00:00
|
|
|
// Allocate buffers (max expansion is 1 byte -> 4 bytes for UTF-8)
|
2017-06-16 14:43:39 +00:00
|
|
|
std::vector<unsigned char> str;
|
2020-06-23 19:05:42 +00:00
|
|
|
str.reserve(numWChars * 4 + 1);
|
|
|
|
uint16_t *temp = new uint16_t[numWChars];
|
|
|
|
for (uint32_t n = 0; n < numWChars; ++n) {
|
|
|
|
temp[n] = stream->GetU2();
|
2017-09-29 19:58:58 +00:00
|
|
|
}
|
2015-12-14 06:14:19 +00:00
|
|
|
|
|
|
|
// Convert it and NUL-terminate.
|
2020-06-23 19:05:42 +00:00
|
|
|
const uint16_t *start(temp), *end(temp + numWChars);
|
|
|
|
utf8::utf16to8(start, end, back_inserter(str));
|
|
|
|
str[str.size() - 1] = '\0';
|
2015-12-14 06:14:19 +00:00
|
|
|
|
|
|
|
// Return the final string.
|
2017-06-16 14:43:39 +00:00
|
|
|
aiString result = aiString((const char *)&str[0]);
|
2015-12-14 06:14:19 +00:00
|
|
|
delete[] temp;
|
2017-09-29 19:58:58 +00:00
|
|
|
|
2015-12-14 06:14:19 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns whether the class can handle the format of the given file.
|
2022-01-18 21:27:19 +00:00
|
|
|
bool SIBImporter::CanRead(const std::string &filename, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const {
|
|
|
|
return SimpleExtensionCheck(filename, "sib");
|
2015-12-14 06:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
const aiImporterDesc *SIBImporter::GetInfo() const {
|
2015-12-14 06:14:19 +00:00
|
|
|
return &desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
static void ReadVerts(SIBMesh *mesh, StreamReaderLE *stream, uint32_t count) {
|
|
|
|
if (nullptr == mesh || nullptr == stream) {
|
2017-09-29 19:58:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-12-14 06:14:19 +00:00
|
|
|
|
2017-09-29 19:58:58 +00:00
|
|
|
mesh->pos.resize(count);
|
2020-06-23 19:05:42 +00:00
|
|
|
for (uint32_t n = 0; n < count; ++n) {
|
|
|
|
mesh->pos[n].x = stream->GetF4();
|
|
|
|
mesh->pos[n].y = stream->GetF4();
|
|
|
|
mesh->pos[n].z = stream->GetF4();
|
2015-12-14 06:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
static void ReadFaces(SIBMesh *mesh, StreamReaderLE *stream) {
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t ptIdx = 0;
|
2020-06-23 19:05:42 +00:00
|
|
|
while (stream->GetRemainingSizeToLimit() > 0) {
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t numPoints = stream->GetU4();
|
|
|
|
|
|
|
|
// Store room for the N index channels, plus the point count.
|
|
|
|
size_t pos = mesh->idx.size() + 1;
|
2020-06-23 19:05:42 +00:00
|
|
|
mesh->idx.resize(pos + numPoints * N);
|
|
|
|
mesh->idx[pos - 1] = numPoints;
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t *idx = &mesh->idx[pos];
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
mesh->faceStart.push_back(static_cast<uint32_t>(pos - 1));
|
2015-12-14 06:14:19 +00:00
|
|
|
mesh->mtls.push_back(0);
|
|
|
|
|
|
|
|
// Read all the position data.
|
|
|
|
// UV/normals will be supplied later.
|
|
|
|
// Positions are supplied indexed already, so we preserve that
|
|
|
|
// mapping. UVs are supplied uniquely, so we allocate unique indices.
|
2020-06-23 19:05:42 +00:00
|
|
|
for (uint32_t n = 0; n < numPoints; n++, idx += N, ptIdx++) {
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t p = stream->GetU4();
|
|
|
|
if (p >= mesh->pos.size())
|
|
|
|
throw DeadlyImportError("Vertex index is out of range.");
|
|
|
|
idx[POS] = p;
|
|
|
|
idx[NRM] = ptIdx;
|
|
|
|
idx[UV] = ptIdx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate data channels for normals/UVs.
|
2020-06-23 19:05:42 +00:00
|
|
|
mesh->nrm.resize(ptIdx, aiVector3D(0, 0, 0));
|
|
|
|
mesh->uv.resize(ptIdx, aiVector3D(0, 0, 0));
|
2015-12-14 06:14:19 +00:00
|
|
|
|
|
|
|
mesh->numPts = ptIdx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
static void ReadUVs(SIBMesh *mesh, StreamReaderLE *stream) {
|
|
|
|
while (stream->GetRemainingSizeToLimit() > 0) {
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t faceIdx = stream->GetU4();
|
|
|
|
uint32_t numPoints = stream->GetU4();
|
|
|
|
|
|
|
|
if (faceIdx >= mesh->faceStart.size())
|
|
|
|
throw DeadlyImportError("Invalid face index.");
|
|
|
|
|
|
|
|
uint32_t pos = mesh->faceStart[faceIdx];
|
|
|
|
uint32_t *idx = &mesh->idx[pos + 1];
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (uint32_t n = 0; n < numPoints; n++, idx += N) {
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t id = idx[UV];
|
|
|
|
mesh->uv[id].x = stream->GetF4();
|
|
|
|
mesh->uv[id].y = stream->GetF4();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
static void ReadMtls(SIBMesh *mesh, StreamReaderLE *stream) {
|
2015-12-14 06:14:19 +00:00
|
|
|
// Material assignments are stored run-length encoded.
|
|
|
|
// Also, we add 1 to each material so that we can use mtl #0
|
|
|
|
// as the default material.
|
|
|
|
uint32_t prevFace = stream->GetU4();
|
|
|
|
uint32_t prevMtl = stream->GetU4() + 1;
|
2020-06-23 19:05:42 +00:00
|
|
|
while (stream->GetRemainingSizeToLimit() > 0) {
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t face = stream->GetU4();
|
|
|
|
uint32_t mtl = stream->GetU4() + 1;
|
2020-06-23 19:05:42 +00:00
|
|
|
while (prevFace < face) {
|
2015-12-14 06:14:19 +00:00
|
|
|
if (prevFace >= mesh->mtls.size())
|
|
|
|
throw DeadlyImportError("Invalid face index.");
|
|
|
|
mesh->mtls[prevFace++] = prevMtl;
|
|
|
|
}
|
|
|
|
|
|
|
|
prevFace = face;
|
|
|
|
prevMtl = mtl;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (prevFace < mesh->mtls.size())
|
|
|
|
mesh->mtls[prevFace++] = prevMtl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
static void ReadAxis(aiMatrix4x4 &axis, StreamReaderLE *stream) {
|
2015-12-14 06:14:19 +00:00
|
|
|
axis.a4 = stream->GetF4();
|
|
|
|
axis.b4 = stream->GetF4();
|
|
|
|
axis.c4 = stream->GetF4();
|
|
|
|
axis.d4 = 1;
|
|
|
|
axis.a1 = stream->GetF4();
|
|
|
|
axis.b1 = stream->GetF4();
|
|
|
|
axis.c1 = stream->GetF4();
|
|
|
|
axis.d1 = 0;
|
|
|
|
axis.a2 = stream->GetF4();
|
|
|
|
axis.b2 = stream->GetF4();
|
|
|
|
axis.c2 = stream->GetF4();
|
|
|
|
axis.d2 = 0;
|
|
|
|
axis.a3 = stream->GetF4();
|
|
|
|
axis.b3 = stream->GetF4();
|
|
|
|
axis.c3 = stream->GetF4();
|
|
|
|
axis.d3 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
static void ReadEdges(SIBMesh *mesh, StreamReaderLE *stream) {
|
|
|
|
while (stream->GetRemainingSizeToLimit() > 0) {
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t posA = stream->GetU4();
|
|
|
|
uint32_t posB = stream->GetU4();
|
|
|
|
GetEdge(mesh, posA, posB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
static void ReadCreases(SIBMesh *mesh, StreamReaderLE *stream) {
|
|
|
|
while (stream->GetRemainingSizeToLimit() > 0) {
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t edge = stream->GetU4();
|
|
|
|
if (edge >= mesh->edges.size())
|
|
|
|
throw DeadlyImportError("SIB: Invalid edge index.");
|
|
|
|
mesh->edges[edge].creased = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
static void ConnectFaces(SIBMesh *mesh) {
|
2015-12-14 06:14:19 +00:00
|
|
|
// Find faces connected to each edge.
|
|
|
|
size_t numFaces = mesh->faceStart.size();
|
2020-06-23 19:05:42 +00:00
|
|
|
for (size_t faceIdx = 0; faceIdx < numFaces; faceIdx++) {
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t *idx = &mesh->idx[mesh->faceStart[faceIdx]];
|
|
|
|
uint32_t numPoints = *idx++;
|
2020-06-23 19:05:42 +00:00
|
|
|
uint32_t prev = idx[(numPoints - 1) * N + POS];
|
2016-07-15 05:38:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (uint32_t i = 0; i < numPoints; i++, idx += N) {
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t next = idx[POS];
|
|
|
|
|
|
|
|
// Find this edge.
|
2020-06-23 19:05:42 +00:00
|
|
|
SIBEdge &edge = GetEdge(mesh, prev, next);
|
2015-12-14 06:14:19 +00:00
|
|
|
|
|
|
|
// Link this face onto it.
|
|
|
|
// This gives potentially undesirable normals when used
|
|
|
|
// with non-2-manifold surfaces, but then so does Silo to begin with.
|
|
|
|
if (edge.faceA == 0xffffffff)
|
2016-11-20 03:06:56 +00:00
|
|
|
edge.faceA = static_cast<uint32_t>(faceIdx);
|
2017-03-19 20:46:23 +00:00
|
|
|
else if (edge.faceB == 0xffffffff)
|
2016-11-20 03:06:56 +00:00
|
|
|
edge.faceB = static_cast<uint32_t>(faceIdx);
|
2015-12-14 06:14:19 +00:00
|
|
|
|
|
|
|
prev = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
static aiVector3D CalculateVertexNormal(SIBMesh *mesh, uint32_t faceIdx, uint32_t pos,
|
|
|
|
const std::vector<aiVector3D> &faceNormals) {
|
2016-07-15 05:38:29 +00:00
|
|
|
// Creased edges complicate this. We need to find the start/end range of the
|
2015-12-14 06:14:19 +00:00
|
|
|
// ring of faces that touch this position.
|
|
|
|
// We do this in two passes. The first pass is to find the end of the range,
|
|
|
|
// the second is to work backwards to the start and calculate the final normal.
|
|
|
|
aiVector3D vtxNormal;
|
2020-06-23 19:05:42 +00:00
|
|
|
for (int pass = 0; pass < 2; pass++) {
|
2015-12-14 06:14:19 +00:00
|
|
|
vtxNormal = aiVector3D(0, 0, 0);
|
|
|
|
uint32_t startFaceIdx = faceIdx;
|
|
|
|
uint32_t prevFaceIdx = faceIdx;
|
|
|
|
|
|
|
|
// Process each connected face.
|
2020-06-23 19:05:42 +00:00
|
|
|
while (true) {
|
2015-12-14 06:14:19 +00:00
|
|
|
// Accumulate the face normal.
|
|
|
|
vtxNormal += faceNormals[faceIdx];
|
|
|
|
|
|
|
|
uint32_t nextFaceIdx = 0xffffffff;
|
|
|
|
|
|
|
|
// Move to the next edge sharing this position.
|
2020-06-23 19:05:42 +00:00
|
|
|
uint32_t *idx = &mesh->idx[mesh->faceStart[faceIdx]];
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t numPoints = *idx++;
|
2020-06-23 19:05:42 +00:00
|
|
|
uint32_t posA = idx[(numPoints - 1) * N + POS];
|
|
|
|
for (uint32_t n = 0; n < numPoints; n++, idx += N) {
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t posB = idx[POS];
|
|
|
|
|
|
|
|
// Test if this edge shares our target position.
|
2020-06-23 19:05:42 +00:00
|
|
|
if (posA == pos || posB == pos) {
|
|
|
|
SIBEdge &edge = GetEdge(mesh, posA, posB);
|
2015-12-14 06:14:19 +00:00
|
|
|
|
2017-03-19 20:46:23 +00:00
|
|
|
// Non-manifold meshes can produce faces which share
|
|
|
|
// positions but have no edge entry, so check it.
|
2020-06-23 19:05:42 +00:00
|
|
|
if (edge.faceA == faceIdx || edge.faceB == faceIdx) {
|
2017-03-19 20:46:23 +00:00
|
|
|
// Move to whichever side we didn't just come from.
|
|
|
|
if (!edge.creased) {
|
|
|
|
if (edge.faceA != prevFaceIdx && edge.faceA != faceIdx && edge.faceA != 0xffffffff)
|
|
|
|
nextFaceIdx = edge.faceA;
|
|
|
|
else if (edge.faceB != prevFaceIdx && edge.faceB != faceIdx && edge.faceB != 0xffffffff)
|
|
|
|
nextFaceIdx = edge.faceB;
|
|
|
|
}
|
2015-12-14 06:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
posA = posB;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop once we hit either an creased/unconnected edge, or we
|
|
|
|
// wrapped around and hit our start point.
|
|
|
|
if (nextFaceIdx == 0xffffffff || nextFaceIdx == startFaceIdx)
|
|
|
|
break;
|
|
|
|
|
|
|
|
prevFaceIdx = faceIdx;
|
|
|
|
faceIdx = nextFaceIdx;
|
2016-07-15 05:38:29 +00:00
|
|
|
}
|
2015-12-14 06:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize it.
|
|
|
|
float len = vtxNormal.Length();
|
|
|
|
if (len > 0.000000001f)
|
|
|
|
vtxNormal /= len;
|
|
|
|
return vtxNormal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
static void CalculateNormals(SIBMesh *mesh) {
|
2015-12-14 06:14:19 +00:00
|
|
|
size_t numFaces = mesh->faceStart.size();
|
|
|
|
|
|
|
|
// Calculate face normals.
|
|
|
|
std::vector<aiVector3D> faceNormals(numFaces);
|
2020-06-23 19:05:42 +00:00
|
|
|
for (size_t faceIdx = 0; faceIdx < numFaces; faceIdx++) {
|
|
|
|
uint32_t *idx = &mesh->idx[mesh->faceStart[faceIdx]];
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t numPoints = *idx++;
|
|
|
|
|
|
|
|
aiVector3D faceNormal(0, 0, 0);
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
uint32_t *prev = &idx[(numPoints - 1) * N];
|
2015-12-14 06:14:19 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (uint32_t i = 0; i < numPoints; i++) {
|
|
|
|
uint32_t *next = &idx[i * N];
|
2015-12-14 06:14:19 +00:00
|
|
|
|
|
|
|
faceNormal += mesh->pos[prev[POS]] ^ mesh->pos[next[POS]];
|
|
|
|
prev = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
faceNormals[faceIdx] = faceNormal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate vertex normals.
|
2020-06-23 19:05:42 +00:00
|
|
|
for (size_t faceIdx = 0; faceIdx < numFaces; faceIdx++) {
|
|
|
|
uint32_t *idx = &mesh->idx[mesh->faceStart[faceIdx]];
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t numPoints = *idx++;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (uint32_t i = 0; i < numPoints; i++) {
|
|
|
|
uint32_t pos = idx[i * N + POS];
|
|
|
|
uint32_t nrm = idx[i * N + NRM];
|
2016-11-20 03:06:56 +00:00
|
|
|
aiVector3D vtxNorm = CalculateVertexNormal(mesh, static_cast<uint32_t>(faceIdx), pos, faceNormals);
|
2015-12-14 06:14:19 +00:00
|
|
|
mesh->nrm[nrm] = vtxNorm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
struct TempMesh {
|
2015-12-14 06:14:19 +00:00
|
|
|
std::vector<aiVector3D> vtx;
|
|
|
|
std::vector<aiVector3D> nrm;
|
|
|
|
std::vector<aiVector3D> uv;
|
2020-06-23 19:05:42 +00:00
|
|
|
std::vector<aiFace> faces;
|
2015-12-14 06:14:19 +00:00
|
|
|
};
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
static void ReadShape(SIB *sib, StreamReaderLE *stream) {
|
2015-12-14 06:14:19 +00:00
|
|
|
SIBMesh smesh;
|
|
|
|
aiString name;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
while (stream->GetRemainingSizeToLimit() >= sizeof(SIBChunk)) {
|
2015-12-14 06:14:19 +00:00
|
|
|
SIBChunk chunk = ReadChunk(stream);
|
|
|
|
unsigned oldLimit = stream->SetReadLimit(stream->GetCurrentPos() + chunk.Size);
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
switch (chunk.Tag) {
|
|
|
|
case TAG('M', 'I', 'R', 'P'): break; // mirror plane maybe?
|
|
|
|
case TAG('I', 'M', 'R', 'P'): break; // instance mirror? (not supported here yet)
|
|
|
|
case TAG('D', 'I', 'N', 'F'): break; // display info, not needed
|
|
|
|
case TAG('P', 'I', 'N', 'F'): break; // ?
|
|
|
|
case TAG('V', 'M', 'I', 'R'): break; // ?
|
|
|
|
case TAG('F', 'M', 'I', 'R'): break; // ?
|
|
|
|
case TAG('T', 'X', 'S', 'M'): break; // ?
|
|
|
|
case TAG('F', 'A', 'H', 'S'): break; // ?
|
|
|
|
case TAG('V', 'R', 'T', 'S'): ReadVerts(&smesh, stream, chunk.Size / 12); break;
|
|
|
|
case TAG('F', 'A', 'C', 'S'): ReadFaces(&smesh, stream); break;
|
|
|
|
case TAG('F', 'T', 'V', 'S'): ReadUVs(&smesh, stream); break;
|
|
|
|
case TAG('S', 'N', 'A', 'M'): name = ReadString(stream, chunk.Size / 2); break;
|
|
|
|
case TAG('F', 'A', 'M', 'A'): ReadMtls(&smesh, stream); break;
|
|
|
|
case TAG('A', 'X', 'I', 'S'): ReadAxis(smesh.axis, stream); break;
|
|
|
|
case TAG('E', 'D', 'G', 'S'): ReadEdges(&smesh, stream); break;
|
|
|
|
case TAG('E', 'C', 'R', 'S'): ReadCreases(&smesh, stream); break;
|
|
|
|
default: UnknownChunk(stream, chunk); break;
|
2015-12-14 06:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stream->SetCurrentPos(stream->GetReadLimit());
|
|
|
|
stream->SetReadLimit(oldLimit);
|
|
|
|
}
|
|
|
|
|
2016-11-24 20:15:07 +00:00
|
|
|
ai_assert(smesh.faceStart.size() == smesh.mtls.size()); // sanity check
|
2015-12-14 06:14:19 +00:00
|
|
|
|
|
|
|
// Silo doesn't store any normals in the file - we need to compute
|
|
|
|
// them ourselves. We can't let AssImp handle it as AssImp doesn't
|
|
|
|
// know about our creased edges.
|
|
|
|
ConnectFaces(&smesh);
|
|
|
|
CalculateNormals(&smesh);
|
|
|
|
|
|
|
|
// Construct the transforms.
|
|
|
|
aiMatrix4x4 worldToLocal = smesh.axis;
|
|
|
|
worldToLocal.Inverse();
|
|
|
|
aiMatrix4x4 worldToLocalN = worldToLocal;
|
|
|
|
worldToLocalN.a4 = worldToLocalN.b4 = worldToLocalN.c4 = 0.0f;
|
|
|
|
worldToLocalN.Inverse().Transpose();
|
|
|
|
|
|
|
|
// Allocate final mesh data.
|
|
|
|
// We'll allocate one mesh for each material. (we'll strip unused ones after)
|
|
|
|
std::vector<TempMesh> meshes(sib->mtls.size());
|
|
|
|
|
|
|
|
// Un-index the source data and apply to each vertex.
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned fi = 0; fi < smesh.faceStart.size(); fi++) {
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t start = smesh.faceStart[fi];
|
|
|
|
uint32_t mtl = smesh.mtls[fi];
|
|
|
|
uint32_t *idx = &smesh.idx[start];
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (mtl >= meshes.size()) {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_ERROR("SIB: Face material index is invalid.");
|
2015-12-14 06:14:19 +00:00
|
|
|
mtl = 0;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
TempMesh &dest = meshes[mtl];
|
2015-12-14 06:14:19 +00:00
|
|
|
|
|
|
|
aiFace face;
|
|
|
|
face.mNumIndices = *idx++;
|
|
|
|
face.mIndices = new unsigned[face.mNumIndices];
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned pt = 0; pt < face.mNumIndices; pt++, idx += N) {
|
2015-12-14 06:14:19 +00:00
|
|
|
size_t vtxIdx = dest.vtx.size();
|
2016-11-20 03:06:56 +00:00
|
|
|
face.mIndices[pt] = static_cast<unsigned int>(vtxIdx);
|
2015-12-14 06:14:19 +00:00
|
|
|
|
|
|
|
// De-index it. We don't need to validate here as
|
|
|
|
// we did it when creating the data.
|
|
|
|
aiVector3D pos = smesh.pos[idx[POS]];
|
|
|
|
aiVector3D nrm = smesh.nrm[idx[NRM]];
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVector3D uv = smesh.uv[idx[UV]];
|
2015-12-14 06:14:19 +00:00
|
|
|
|
|
|
|
// The verts are supplied in world-space, so let's
|
|
|
|
// transform them back into the local space of this mesh:
|
|
|
|
pos = worldToLocal * pos;
|
|
|
|
nrm = worldToLocalN * nrm;
|
|
|
|
|
|
|
|
dest.vtx.push_back(pos);
|
|
|
|
dest.nrm.push_back(nrm);
|
|
|
|
dest.uv.push_back(uv);
|
|
|
|
}
|
|
|
|
dest.faces.push_back(face);
|
|
|
|
}
|
|
|
|
|
|
|
|
SIBObject obj;
|
|
|
|
obj.name = name;
|
|
|
|
obj.axis = smesh.axis;
|
|
|
|
obj.meshIdx = sib->meshes.size();
|
2016-07-15 05:38:29 +00:00
|
|
|
|
2015-12-14 06:14:19 +00:00
|
|
|
// Now that we know the size of everything,
|
|
|
|
// we can build the final one-material-per-mesh data.
|
2020-06-23 19:05:42 +00:00
|
|
|
for (size_t n = 0; n < meshes.size(); n++) {
|
|
|
|
TempMesh &src = meshes[n];
|
2015-12-14 06:14:19 +00:00
|
|
|
if (src.faces.empty())
|
|
|
|
continue;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMesh *mesh = new aiMesh;
|
2015-12-14 06:14:19 +00:00
|
|
|
mesh->mName = name;
|
2016-11-20 03:06:56 +00:00
|
|
|
mesh->mNumFaces = static_cast<unsigned int>(src.faces.size());
|
2015-12-14 06:14:19 +00:00
|
|
|
mesh->mFaces = new aiFace[mesh->mNumFaces];
|
2016-11-20 03:06:56 +00:00
|
|
|
mesh->mNumVertices = static_cast<unsigned int>(src.vtx.size());
|
2015-12-14 06:14:19 +00:00
|
|
|
mesh->mVertices = new aiVector3D[mesh->mNumVertices];
|
|
|
|
mesh->mNormals = new aiVector3D[mesh->mNumVertices];
|
|
|
|
mesh->mTextureCoords[0] = new aiVector3D[mesh->mNumVertices];
|
|
|
|
mesh->mNumUVComponents[0] = 2;
|
2016-11-20 03:06:56 +00:00
|
|
|
mesh->mMaterialIndex = static_cast<unsigned int>(n);
|
2015-12-14 06:14:19 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned i = 0; i < mesh->mNumVertices; i++) {
|
2015-12-14 06:14:19 +00:00
|
|
|
mesh->mVertices[i] = src.vtx[i];
|
|
|
|
mesh->mNormals[i] = src.nrm[i];
|
|
|
|
mesh->mTextureCoords[0][i] = src.uv[i];
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned i = 0; i < mesh->mNumFaces; i++) {
|
2015-12-14 06:14:19 +00:00
|
|
|
mesh->mFaces[i] = src.faces[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
sib->meshes.push_back(mesh);
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.meshCount = sib->meshes.size() - obj.meshIdx;
|
|
|
|
sib->objs.push_back(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
static void ReadMaterial(SIB *sib, StreamReaderLE *stream) {
|
2015-12-14 06:14:19 +00:00
|
|
|
aiColor3D diff = ReadColor(stream);
|
|
|
|
aiColor3D ambi = ReadColor(stream);
|
|
|
|
aiColor3D spec = ReadColor(stream);
|
|
|
|
aiColor3D emis = ReadColor(stream);
|
|
|
|
float shiny = (float)stream->GetU4();
|
|
|
|
|
|
|
|
uint32_t nameLen = stream->GetU4();
|
2020-06-23 19:05:42 +00:00
|
|
|
aiString name = ReadString(stream, nameLen / 2);
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t texLen = stream->GetU4();
|
2020-06-23 19:05:42 +00:00
|
|
|
aiString tex = ReadString(stream, texLen / 2);
|
2015-12-14 06:14:19 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMaterial *mtl = new aiMaterial();
|
2015-12-14 06:14:19 +00:00
|
|
|
mtl->AddProperty(&diff, 1, AI_MATKEY_COLOR_DIFFUSE);
|
|
|
|
mtl->AddProperty(&ambi, 1, AI_MATKEY_COLOR_AMBIENT);
|
|
|
|
mtl->AddProperty(&spec, 1, AI_MATKEY_COLOR_SPECULAR);
|
|
|
|
mtl->AddProperty(&emis, 1, AI_MATKEY_COLOR_EMISSIVE);
|
|
|
|
mtl->AddProperty(&shiny, 1, AI_MATKEY_SHININESS);
|
|
|
|
mtl->AddProperty(&name, AI_MATKEY_NAME);
|
|
|
|
if (tex.length > 0) {
|
|
|
|
mtl->AddProperty(&tex, AI_MATKEY_TEXTURE_DIFFUSE(0));
|
|
|
|
mtl->AddProperty(&tex, AI_MATKEY_TEXTURE_AMBIENT(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
sib->mtls.push_back(mtl);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
static void ReadLightInfo(aiLight *light, StreamReaderLE *stream) {
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t type = stream->GetU4();
|
|
|
|
switch (type) {
|
|
|
|
case 0: light->mType = aiLightSource_POINT; break;
|
|
|
|
case 1: light->mType = aiLightSource_SPOT; break;
|
|
|
|
case 2: light->mType = aiLightSource_DIRECTIONAL; break;
|
|
|
|
default: light->mType = aiLightSource_UNDEFINED; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
light->mPosition.x = stream->GetF4();
|
|
|
|
light->mPosition.y = stream->GetF4();
|
|
|
|
light->mPosition.z = stream->GetF4();
|
|
|
|
light->mDirection.x = stream->GetF4();
|
|
|
|
light->mDirection.y = stream->GetF4();
|
|
|
|
light->mDirection.z = stream->GetF4();
|
|
|
|
light->mColorDiffuse = ReadColor(stream);
|
|
|
|
light->mColorAmbient = ReadColor(stream);
|
|
|
|
light->mColorSpecular = ReadColor(stream);
|
2016-07-15 05:38:29 +00:00
|
|
|
ai_real spotExponent = stream->GetF4();
|
|
|
|
ai_real spotCutoff = stream->GetF4();
|
2015-12-14 06:14:19 +00:00
|
|
|
light->mAttenuationConstant = stream->GetF4();
|
|
|
|
light->mAttenuationLinear = stream->GetF4();
|
|
|
|
light->mAttenuationQuadratic = stream->GetF4();
|
|
|
|
|
|
|
|
// Silo uses the OpenGL default lighting model for it's
|
|
|
|
// spot cutoff/exponent. AssImp unfortunately, does not.
|
|
|
|
// Let's try and approximate it by solving for the
|
|
|
|
// 99% and 1% percentiles.
|
|
|
|
// OpenGL: I = cos(angle)^E
|
|
|
|
// Solving: angle = acos(I^(1/E))
|
2020-06-23 19:05:42 +00:00
|
|
|
ai_real E = ai_real(1.0) / std::max(spotExponent, (ai_real)0.00001);
|
2016-10-15 00:29:45 +00:00
|
|
|
ai_real inner = std::acos(std::pow((ai_real)0.99, E));
|
|
|
|
ai_real outer = std::acos(std::pow((ai_real)0.01, E));
|
2015-12-14 06:14:19 +00:00
|
|
|
|
|
|
|
// Apply the cutoff.
|
|
|
|
outer = std::min(outer, AI_DEG_TO_RAD(spotCutoff));
|
|
|
|
|
|
|
|
light->mAngleInnerCone = std::min(inner, outer);
|
|
|
|
light->mAngleOuterCone = outer;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
static void ReadLight(SIB *sib, StreamReaderLE *stream) {
|
|
|
|
aiLight *light = new aiLight();
|
2015-12-14 06:14:19 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
while (stream->GetRemainingSizeToLimit() >= sizeof(SIBChunk)) {
|
2015-12-14 06:14:19 +00:00
|
|
|
SIBChunk chunk = ReadChunk(stream);
|
|
|
|
unsigned oldLimit = stream->SetReadLimit(stream->GetCurrentPos() + chunk.Size);
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
switch (chunk.Tag) {
|
|
|
|
case TAG('L', 'N', 'F', 'O'): ReadLightInfo(light, stream); break;
|
|
|
|
case TAG('S', 'N', 'A', 'M'): light->mName = ReadString(stream, chunk.Size / 2); break;
|
|
|
|
default: UnknownChunk(stream, chunk); break;
|
2015-12-14 06:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stream->SetCurrentPos(stream->GetReadLimit());
|
|
|
|
stream->SetReadLimit(oldLimit);
|
|
|
|
}
|
|
|
|
|
|
|
|
sib->lights.push_back(light);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
static void ReadScale(aiMatrix4x4 &axis, StreamReaderLE *stream) {
|
2015-12-14 06:14:19 +00:00
|
|
|
aiMatrix4x4 scale;
|
|
|
|
scale.a1 = stream->GetF4();
|
|
|
|
scale.b1 = stream->GetF4();
|
|
|
|
scale.c1 = stream->GetF4();
|
|
|
|
scale.d1 = stream->GetF4();
|
|
|
|
scale.a2 = stream->GetF4();
|
|
|
|
scale.b2 = stream->GetF4();
|
|
|
|
scale.c2 = stream->GetF4();
|
|
|
|
scale.d2 = stream->GetF4();
|
|
|
|
scale.a3 = stream->GetF4();
|
|
|
|
scale.b3 = stream->GetF4();
|
|
|
|
scale.c3 = stream->GetF4();
|
|
|
|
scale.d3 = stream->GetF4();
|
|
|
|
scale.a4 = stream->GetF4();
|
|
|
|
scale.b4 = stream->GetF4();
|
|
|
|
scale.c4 = stream->GetF4();
|
|
|
|
scale.d4 = stream->GetF4();
|
|
|
|
|
|
|
|
axis = axis * scale;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
static void ReadInstance(SIB *sib, StreamReaderLE *stream) {
|
2015-12-14 06:14:19 +00:00
|
|
|
SIBObject inst;
|
|
|
|
uint32_t shapeIndex = 0;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
while (stream->GetRemainingSizeToLimit() >= sizeof(SIBChunk)) {
|
2015-12-14 06:14:19 +00:00
|
|
|
SIBChunk chunk = ReadChunk(stream);
|
|
|
|
unsigned oldLimit = stream->SetReadLimit(stream->GetCurrentPos() + chunk.Size);
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
switch (chunk.Tag) {
|
|
|
|
case TAG('D', 'I', 'N', 'F'): break; // display info, not needed
|
|
|
|
case TAG('P', 'I', 'N', 'F'): break; // ?
|
|
|
|
case TAG('A', 'X', 'I', 'S'): ReadAxis(inst.axis, stream); break;
|
|
|
|
case TAG('I', 'N', 'S', 'I'): shapeIndex = stream->GetU4(); break;
|
|
|
|
case TAG('S', 'M', 'T', 'X'): ReadScale(inst.axis, stream); break;
|
|
|
|
case TAG('S', 'N', 'A', 'M'): inst.name = ReadString(stream, chunk.Size / 2); break;
|
|
|
|
default: UnknownChunk(stream, chunk); break;
|
2015-12-14 06:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stream->SetCurrentPos(stream->GetReadLimit());
|
|
|
|
stream->SetReadLimit(oldLimit);
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (shapeIndex >= sib->objs.size()) {
|
|
|
|
throw DeadlyImportError("SIB: Invalid shape index.");
|
2016-11-24 20:15:07 +00:00
|
|
|
}
|
2015-12-14 06:14:19 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
const SIBObject &src = sib->objs[shapeIndex];
|
2015-12-14 06:14:19 +00:00
|
|
|
inst.meshIdx = src.meshIdx;
|
|
|
|
inst.meshCount = src.meshCount;
|
|
|
|
sib->insts.push_back(inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
static void CheckVersion(StreamReaderLE *stream) {
|
2015-12-14 06:14:19 +00:00
|
|
|
uint32_t version = stream->GetU4();
|
2020-06-23 19:05:42 +00:00
|
|
|
if (version < 1 || version > 2) {
|
|
|
|
throw DeadlyImportError("SIB: Unsupported file version.");
|
2016-11-24 20:15:07 +00:00
|
|
|
}
|
2015-12-14 06:14:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
static void ReadScene(SIB *sib, StreamReaderLE *stream) {
|
2015-12-14 06:14:19 +00:00
|
|
|
// Parse each chunk in turn.
|
2020-06-23 19:05:42 +00:00
|
|
|
while (stream->GetRemainingSizeToLimit() >= sizeof(SIBChunk)) {
|
2015-12-14 06:14:19 +00:00
|
|
|
SIBChunk chunk = ReadChunk(stream);
|
|
|
|
unsigned oldLimit = stream->SetReadLimit(stream->GetCurrentPos() + chunk.Size);
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
switch (chunk.Tag) {
|
|
|
|
case TAG('H', 'E', 'A', 'D'): CheckVersion(stream); break;
|
|
|
|
case TAG('S', 'H', 'A', 'P'): ReadShape(sib, stream); break;
|
|
|
|
case TAG('G', 'R', 'P', 'S'): break; // group assignment, we don't import this
|
|
|
|
case TAG('T', 'E', 'X', 'P'): break; // ?
|
|
|
|
case TAG('I', 'N', 'S', 'T'): ReadInstance(sib, stream); break;
|
|
|
|
case TAG('M', 'A', 'T', 'R'): ReadMaterial(sib, stream); break;
|
|
|
|
case TAG('L', 'G', 'H', 'T'): ReadLight(sib, stream); break;
|
|
|
|
default: UnknownChunk(stream, chunk); break;
|
2015-12-14 06:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stream->SetCurrentPos(stream->GetReadLimit());
|
|
|
|
stream->SetReadLimit(oldLimit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Imports the given file into the given scene structure.
|
2020-06-23 19:05:42 +00:00
|
|
|
void SIBImporter::InternReadFile(const std::string &pFile,
|
|
|
|
aiScene *pScene, IOSystem *pIOHandler) {
|
2021-05-05 21:32:10 +00:00
|
|
|
|
|
|
|
auto file = pIOHandler->Open(pFile, "rb");
|
|
|
|
if (!file)
|
|
|
|
throw DeadlyImportError("SIB: Could not open ", pFile);
|
|
|
|
|
|
|
|
StreamReaderLE stream(file);
|
2015-12-14 06:14:19 +00:00
|
|
|
|
|
|
|
// We should have at least one chunk
|
|
|
|
if (stream.GetRemainingSize() < 16)
|
2020-08-18 16:35:08 +00:00
|
|
|
throw DeadlyImportError("SIB file is either empty or corrupt: ", pFile);
|
2015-12-14 06:14:19 +00:00
|
|
|
|
|
|
|
SIB sib;
|
|
|
|
|
|
|
|
// Default material.
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMaterial *defmtl = new aiMaterial;
|
2015-12-14 06:14:19 +00:00
|
|
|
aiString defname = aiString(AI_DEFAULT_MATERIAL_NAME);
|
|
|
|
defmtl->AddProperty(&defname, AI_MATKEY_NAME);
|
|
|
|
sib.mtls.push_back(defmtl);
|
|
|
|
|
|
|
|
// Read it all.
|
|
|
|
ReadScene(&sib, &stream);
|
|
|
|
|
|
|
|
// Join the instances and objects together.
|
|
|
|
size_t firstInst = sib.objs.size();
|
|
|
|
sib.objs.insert(sib.objs.end(), sib.insts.begin(), sib.insts.end());
|
|
|
|
sib.insts.clear();
|
|
|
|
|
|
|
|
// Transfer to the aiScene.
|
2016-11-20 03:06:56 +00:00
|
|
|
pScene->mNumMaterials = static_cast<unsigned int>(sib.mtls.size());
|
|
|
|
pScene->mNumMeshes = static_cast<unsigned int>(sib.meshes.size());
|
|
|
|
pScene->mNumLights = static_cast<unsigned int>(sib.lights.size());
|
2020-06-23 19:05:42 +00:00
|
|
|
pScene->mMaterials = pScene->mNumMaterials ? new aiMaterial *[pScene->mNumMaterials] : nullptr;
|
|
|
|
pScene->mMeshes = pScene->mNumMeshes ? new aiMesh *[pScene->mNumMeshes] : nullptr;
|
|
|
|
pScene->mLights = pScene->mNumLights ? new aiLight *[pScene->mNumLights] : nullptr;
|
2015-12-14 06:14:19 +00:00
|
|
|
if (pScene->mNumMaterials)
|
2020-06-23 19:05:42 +00:00
|
|
|
memcpy(pScene->mMaterials, &sib.mtls[0], sizeof(aiMaterial *) * pScene->mNumMaterials);
|
2015-12-14 06:14:19 +00:00
|
|
|
if (pScene->mNumMeshes)
|
2020-06-23 19:05:42 +00:00
|
|
|
memcpy(pScene->mMeshes, &sib.meshes[0], sizeof(aiMesh *) * pScene->mNumMeshes);
|
2015-12-14 06:14:19 +00:00
|
|
|
if (pScene->mNumLights)
|
2020-06-23 19:05:42 +00:00
|
|
|
memcpy(pScene->mLights, &sib.lights[0], sizeof(aiLight *) * pScene->mNumLights);
|
2015-12-14 06:14:19 +00:00
|
|
|
|
|
|
|
// Construct the root node.
|
|
|
|
size_t childIdx = 0;
|
|
|
|
aiNode *root = new aiNode();
|
|
|
|
root->mName.Set("<SIBRoot>");
|
2016-11-20 03:06:56 +00:00
|
|
|
root->mNumChildren = static_cast<unsigned int>(sib.objs.size() + sib.lights.size());
|
2020-06-23 19:05:42 +00:00
|
|
|
root->mChildren = root->mNumChildren ? new aiNode *[root->mNumChildren] : nullptr;
|
2015-12-14 06:14:19 +00:00
|
|
|
pScene->mRootNode = root;
|
|
|
|
|
|
|
|
// Add nodes for each object.
|
2020-06-23 19:05:42 +00:00
|
|
|
for (size_t n = 0; n < sib.objs.size(); n++) {
|
2017-11-14 17:08:41 +00:00
|
|
|
ai_assert(root->mChildren);
|
2020-06-23 19:05:42 +00:00
|
|
|
SIBObject &obj = sib.objs[n];
|
|
|
|
aiNode *node = new aiNode;
|
2015-12-14 06:14:19 +00:00
|
|
|
root->mChildren[childIdx++] = node;
|
|
|
|
node->mName = obj.name;
|
|
|
|
node->mParent = root;
|
|
|
|
node->mTransformation = obj.axis;
|
|
|
|
|
2016-11-20 03:06:56 +00:00
|
|
|
node->mNumMeshes = static_cast<unsigned int>(obj.meshCount);
|
2020-06-23 19:05:42 +00:00
|
|
|
node->mMeshes = node->mNumMeshes ? new unsigned[node->mNumMeshes] : nullptr;
|
|
|
|
for (unsigned i = 0; i < node->mNumMeshes; i++)
|
2016-11-20 03:06:56 +00:00
|
|
|
node->mMeshes[i] = static_cast<unsigned int>(obj.meshIdx + i);
|
2015-12-14 06:14:19 +00:00
|
|
|
|
|
|
|
// Mark instanced objects as being so.
|
2020-06-23 19:05:42 +00:00
|
|
|
if (n >= firstInst) {
|
|
|
|
node->mMetaData = aiMetadata::Alloc(1);
|
|
|
|
node->mMetaData->Set(0, "IsInstance", true);
|
2015-12-14 06:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add nodes for each light.
|
|
|
|
// (no transformation as the light is already in world space)
|
2020-06-23 19:05:42 +00:00
|
|
|
for (size_t n = 0; n < sib.lights.size(); n++) {
|
2017-11-14 17:08:41 +00:00
|
|
|
ai_assert(root->mChildren);
|
2020-06-23 19:05:42 +00:00
|
|
|
aiLight *light = sib.lights[n];
|
|
|
|
if (nullptr != light) {
|
|
|
|
aiNode *node = new aiNode;
|
|
|
|
root->mChildren[childIdx++] = node;
|
2016-05-15 17:56:29 +00:00
|
|
|
node->mName = light->mName;
|
|
|
|
node->mParent = root;
|
|
|
|
}
|
2015-12-14 06:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-11 08:59:42 +00:00
|
|
|
} // namespace Assimp
|
|
|
|
|
2015-12-14 06:14:19 +00:00
|
|
|
#endif // !! ASSIMP_BUILD_NO_SIB_IMPORTER
|