2015-05-19 03:48:29 +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-05-19 03:48:29 +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
|
2015-05-19 03:48:29 +00:00
|
|
|
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
|
2015-05-19 03:48:29 +00:00
|
|
|
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
|
2015-05-19 03:48:29 +00:00
|
|
|
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
|
2015-05-19 03:48:29 +00:00
|
|
|
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
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file RawLoader.cpp
|
2015-05-19 03:52:10 +00:00
|
|
|
* @brief Implementation of the RAW importer class
|
2015-05-19 03:48:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_RAW_IMPORTER
|
|
|
|
|
|
|
|
// internal headers
|
|
|
|
#include "RawLoader.h"
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/ParsingUtils.h>
|
|
|
|
#include <assimp/fast_atof.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>
|
|
|
|
#include <memory>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
|
|
|
static const aiImporterDesc desc = {
|
2015-05-19 03:57:13 +00:00
|
|
|
"Raw Importer",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
aiImporterFlags_SupportTextFlavour,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"raw"
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
2020-06-23 19:05:42 +00:00
|
|
|
RAWImporter::RAWImporter() {
|
|
|
|
// empty
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Destructor, private as well
|
2020-06-23 19:05:42 +00:00
|
|
|
RAWImporter::~RAWImporter() {
|
|
|
|
// empty
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Returns whether the class can handle the format of the given file.
|
2022-01-18 21:26:16 +00:00
|
|
|
bool RAWImporter::CanRead(const std::string &filename, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const {
|
|
|
|
return SimpleExtensionCheck(filename, "raw");
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
const aiImporterDesc *RAWImporter::GetInfo() const {
|
2015-05-19 03:57:13 +00:00
|
|
|
return &desc;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Imports the given file into the given scene structure.
|
2020-06-23 19:05:42 +00:00
|
|
|
void RAWImporter::InternReadFile(const std::string &pFile,
|
|
|
|
aiScene *pScene, IOSystem *pIOHandler) {
|
|
|
|
std::unique_ptr<IOStream> file(pIOHandler->Open(pFile, "rb"));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Check whether we can read from the file
|
2020-06-23 19:05:42 +00:00
|
|
|
if (file.get() == nullptr) {
|
2020-08-18 16:35:08 +00:00
|
|
|
throw DeadlyImportError("Failed to open RAW file ", pFile, ".");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// allocate storage and copy the contents of the file to a memory buffer
|
|
|
|
// (terminate it with zero)
|
|
|
|
std::vector<char> mBuffer2;
|
2020-06-23 19:05:42 +00:00
|
|
|
TextFileToBuffer(file.get(), mBuffer2);
|
|
|
|
const char *buffer = &mBuffer2[0];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// list of groups loaded from the file
|
2020-06-23 19:05:42 +00:00
|
|
|
std::vector<GroupInformation> outGroups(1, GroupInformation("<default>"));
|
|
|
|
std::vector<GroupInformation>::iterator curGroup = outGroups.begin();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// now read all lines
|
|
|
|
char line[4096];
|
2020-06-23 19:05:42 +00:00
|
|
|
while (GetNextLine(buffer, line)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// if the line starts with a non-numeric identifier, it marks
|
|
|
|
// the beginning of a new group
|
2020-06-23 19:05:42 +00:00
|
|
|
const char *sz = line;
|
|
|
|
SkipSpaces(&sz);
|
|
|
|
if (IsLineEnd(*sz)) continue;
|
|
|
|
if (!IsNumeric(*sz)) {
|
|
|
|
const char *sz2 = sz;
|
|
|
|
while (!IsSpaceOrNewLine(*sz2))
|
|
|
|
++sz2;
|
|
|
|
const unsigned int length = (unsigned int)(sz2 - sz);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// find an existing group with this name
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::vector<GroupInformation>::iterator it = outGroups.begin(), end = outGroups.end();
|
|
|
|
it != end; ++it) {
|
|
|
|
if (length == (*it).name.length() && !::strcmp(sz, (*it).name.c_str())) {
|
|
|
|
curGroup = it;
|
|
|
|
sz2 = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
if (sz2) {
|
|
|
|
outGroups.push_back(GroupInformation(std::string(sz, length)));
|
|
|
|
curGroup = outGroups.end() - 1;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// there can be maximally 12 floats plus an extra texture file name
|
|
|
|
float data[12];
|
|
|
|
unsigned int num;
|
2020-06-23 19:05:42 +00:00
|
|
|
for (num = 0; num < 12; ++num) {
|
|
|
|
if (!SkipSpaces(&sz) || !IsNumeric(*sz)) break;
|
|
|
|
sz = fast_atoreal_move<float>(sz, data[num]);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
if (num != 12 && num != 9) {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_ERROR("A line may have either 9 or 12 floats and an optional texture");
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
MeshInformation *output = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
const char *sz2 = sz;
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int length;
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!IsLineEnd(*sz)) {
|
|
|
|
while (!IsSpaceOrNewLine(*sz2))
|
|
|
|
++sz2;
|
|
|
|
length = (unsigned int)(sz2 - sz);
|
|
|
|
} else if (9 == num) {
|
2015-05-19 03:57:13 +00:00
|
|
|
sz = "%default%";
|
|
|
|
length = 9;
|
2020-06-23 19:05:42 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
sz = "";
|
|
|
|
length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// search in the list of meshes whether we have one with this texture
|
2020-06-23 19:05:42 +00:00
|
|
|
for (auto &mesh : (*curGroup).meshes) {
|
|
|
|
if (length == mesh.name.length() && (length ? !::strcmp(sz, mesh.name.c_str()) : true)) {
|
2016-05-22 10:45:50 +00:00
|
|
|
output = &mesh;
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if we don't have the mesh, create it
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!output) {
|
|
|
|
(*curGroup).meshes.push_back(MeshInformation(std::string(sz, length)));
|
2015-05-19 03:57:13 +00:00
|
|
|
output = &((*curGroup).meshes.back());
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
if (12 == num) {
|
|
|
|
aiColor4D v(data[0], data[1], data[2], 1.0f);
|
2015-05-19 03:57:13 +00:00
|
|
|
output->colors.push_back(v);
|
|
|
|
output->colors.push_back(v);
|
|
|
|
output->colors.push_back(v);
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
output->vertices.push_back(aiVector3D(data[3], data[4], data[5]));
|
|
|
|
output->vertices.push_back(aiVector3D(data[6], data[7], data[8]));
|
|
|
|
output->vertices.push_back(aiVector3D(data[9], data[10], data[11]));
|
|
|
|
} else {
|
|
|
|
output->vertices.push_back(aiVector3D(data[0], data[1], data[2]));
|
|
|
|
output->vertices.push_back(aiVector3D(data[3], data[4], data[5]));
|
|
|
|
output->vertices.push_back(aiVector3D(data[6], data[7], data[8]));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pScene->mRootNode = new aiNode();
|
|
|
|
pScene->mRootNode->mName.Set("<RawRoot>");
|
|
|
|
|
|
|
|
// count the number of valid groups
|
|
|
|
// (meshes can't be empty)
|
2020-06-23 19:05:42 +00:00
|
|
|
for (auto &outGroup : outGroups) {
|
|
|
|
if (!outGroup.meshes.empty()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
++pScene->mRootNode->mNumChildren;
|
2020-06-23 19:05:42 +00:00
|
|
|
pScene->mNumMeshes += (unsigned int)outGroup.meshes.size();
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!pScene->mNumMeshes) {
|
2015-05-19 03:57:13 +00:00
|
|
|
throw DeadlyImportError("RAW: No meshes loaded. The file seems to be corrupt or empty.");
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
pScene->mMeshes = new aiMesh *[pScene->mNumMeshes];
|
|
|
|
aiNode **cc;
|
|
|
|
if (1 == pScene->mRootNode->mNumChildren) {
|
2015-05-19 03:57:13 +00:00
|
|
|
cc = &pScene->mRootNode;
|
|
|
|
pScene->mRootNode->mNumChildren = 0;
|
2017-12-24 12:26:53 +00:00
|
|
|
} else {
|
2020-06-23 19:05:42 +00:00
|
|
|
cc = new aiNode *[pScene->mRootNode->mNumChildren];
|
|
|
|
memset(cc, 0, sizeof(aiNode *) * pScene->mRootNode->mNumChildren);
|
2017-12-24 12:26:53 +00:00
|
|
|
pScene->mRootNode->mChildren = cc;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pScene->mNumMaterials = pScene->mNumMeshes;
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMaterial **mats = pScene->mMaterials = new aiMaterial *[pScene->mNumMaterials];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
unsigned int meshIdx = 0;
|
2020-06-23 19:05:42 +00:00
|
|
|
for (auto &outGroup : outGroups) {
|
|
|
|
if (outGroup.meshes.empty()) continue;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiNode *node;
|
|
|
|
if (pScene->mRootNode->mNumChildren) {
|
2015-05-19 03:57:13 +00:00
|
|
|
node = *cc = new aiNode();
|
|
|
|
node->mParent = pScene->mRootNode;
|
2020-06-23 19:05:42 +00:00
|
|
|
} else
|
|
|
|
node = *cc;
|
2016-05-22 10:45:50 +00:00
|
|
|
node->mName.Set(outGroup.name);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// add all meshes
|
2020-06-23 19:05:42 +00:00
|
|
|
node->mNumMeshes = (unsigned int)outGroup.meshes.size();
|
|
|
|
unsigned int *pi = node->mMeshes = new unsigned int[node->mNumMeshes];
|
|
|
|
for (std::vector<MeshInformation>::iterator it2 = outGroup.meshes.begin(),
|
|
|
|
end2 = outGroup.meshes.end();
|
|
|
|
it2 != end2; ++it2) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ai_assert(!(*it2).vertices.empty());
|
|
|
|
|
|
|
|
// allocate the mesh
|
|
|
|
*pi++ = meshIdx;
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMesh *mesh = pScene->mMeshes[meshIdx] = new aiMesh();
|
2015-05-19 03:57:13 +00:00
|
|
|
mesh->mMaterialIndex = meshIdx++;
|
|
|
|
|
|
|
|
mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
|
|
|
|
|
|
|
|
// allocate storage for the vertex components and copy them
|
|
|
|
mesh->mNumVertices = (unsigned int)(*it2).vertices.size();
|
2020-06-23 19:05:42 +00:00
|
|
|
mesh->mVertices = new aiVector3D[mesh->mNumVertices];
|
|
|
|
::memcpy(mesh->mVertices, &(*it2).vertices[0], sizeof(aiVector3D) * mesh->mNumVertices);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if ((*it2).colors.size()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ai_assert((*it2).colors.size() == mesh->mNumVertices);
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
mesh->mColors[0] = new aiColor4D[mesh->mNumVertices];
|
|
|
|
::memcpy(mesh->mColors[0], &(*it2).colors[0], sizeof(aiColor4D) * mesh->mNumVertices);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// generate triangles
|
|
|
|
ai_assert(0 == mesh->mNumVertices % 3);
|
2020-06-23 19:05:42 +00:00
|
|
|
aiFace *fc = mesh->mFaces = new aiFace[mesh->mNumFaces = mesh->mNumVertices / 3];
|
|
|
|
aiFace *const fcEnd = fc + mesh->mNumFaces;
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int n = 0;
|
2020-06-23 19:05:42 +00:00
|
|
|
while (fc != fcEnd) {
|
|
|
|
aiFace &f = *fc++;
|
2015-05-19 03:57:13 +00:00
|
|
|
f.mIndices = new unsigned int[f.mNumIndices = 3];
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int m = 0; m < 3; ++m)
|
2015-05-19 03:57:13 +00:00
|
|
|
f.mIndices[m] = n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate a material for the mesh
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMaterial *mat = new aiMaterial();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiColor4D clr(1.0f, 1.0f, 1.0f, 1.0f);
|
2015-05-19 03:57:13 +00:00
|
|
|
if ("%default%" == (*it2).name) // a gray default material
|
|
|
|
{
|
|
|
|
clr.r = clr.g = clr.b = 0.6f;
|
2020-06-23 19:05:42 +00:00
|
|
|
} else if ((*it2).name.length() > 0) // a texture
|
2015-05-19 03:57:13 +00:00
|
|
|
{
|
|
|
|
aiString s;
|
|
|
|
s.Set((*it2).name);
|
2020-06-23 19:05:42 +00:00
|
|
|
mat->AddProperty(&s, AI_MATKEY_TEXTURE_DIFFUSE(0));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
mat->AddProperty<aiColor4D>(&clr, 1, AI_MATKEY_COLOR_DIFFUSE);
|
2015-05-19 03:57:13 +00:00
|
|
|
*mats++ = mat;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !! ASSIMP_BUILD_NO_RAW_IMPORTER
|