assimp/code/AssetLib/NFF/NFFLoader.cpp

1171 lines
48 KiB
C++
Raw Permalink Normal View History

/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
Copyright (c) 2006-2020, assimp team
2018-01-28 18:42:05 +00:00
2017-05-09 17:57:36 +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 STL importer class */
#ifndef ASSIMP_BUILD_NO_NFF_IMPORTER
// internal headers
#include "NFFLoader.h"
#include <assimp/ParsingUtils.h>
2020-03-01 12:15:45 +00:00
#include <assimp/RemoveComments.h>
#include <assimp/StandardShapes.h>
#include <assimp/fast_atof.h>
2017-02-22 16:20:26 +00:00
#include <assimp/importerdesc.h>
2020-03-01 12:15:45 +00:00
#include <assimp/qnan.h>
#include <assimp/scene.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/IOSystem.hpp>
#include <memory>
using namespace Assimp;
static const aiImporterDesc desc = {
2015-05-19 03:57:13 +00:00
"Neutral File Format Importer",
"",
"",
"",
aiImporterFlags_SupportBinaryFlavour,
0,
0,
0,
0,
"enff nff"
};
// ------------------------------------------------------------------------------------------------
// Constructor to be privately used by Importer
2020-03-01 12:15:45 +00:00
NFFImporter::NFFImporter() {}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Destructor, private as well
2020-03-01 12:15:45 +00:00
NFFImporter::~NFFImporter() {}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Returns whether the class can handle the format of the given file.
2020-03-01 12:15:45 +00:00
bool NFFImporter::CanRead(const std::string &pFile, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const {
return SimpleExtensionCheck(pFile, "nff", "enff");
}
// ------------------------------------------------------------------------------------------------
// Get the list of all supported file extensions
2020-03-01 12:15:45 +00:00
const aiImporterDesc *NFFImporter::GetInfo() const {
2015-05-19 03:57:13 +00:00
return &desc;
}
// ------------------------------------------------------------------------------------------------
#define AI_NFF_PARSE_FLOAT(f) \
2020-03-01 12:15:45 +00:00
SkipSpaces(&sz); \
if (!::IsLineEnd(*sz)) sz = fast_atoreal_move<float>(sz, (float &)f);
// ------------------------------------------------------------------------------------------------
#define AI_NFF_PARSE_TRIPLE(v) \
2020-03-01 12:15:45 +00:00
AI_NFF_PARSE_FLOAT(v[0]) \
AI_NFF_PARSE_FLOAT(v[1]) \
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_FLOAT(v[2])
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
#define AI_NFF_PARSE_SHAPE_INFORMATION() \
aiVector3D center, radius(1.0f, get_qnan(), get_qnan()); \
AI_NFF_PARSE_TRIPLE(center); \
AI_NFF_PARSE_TRIPLE(radius); \
if (is_qnan(radius.z)) radius.z = radius.x; \
if (is_qnan(radius.y)) radius.y = radius.x; \
curMesh.radius = radius; \
curMesh.center = center;
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
#define AI_NFF2_GET_NEXT_TOKEN() \
do { \
if (!GetNextLine(buffer, line)) { \
ASSIMP_LOG_WARN("NFF2: Unexpected EOF, can't read next token"); \
break; \
} \
SkipSpaces(line, &sz); \
} while (IsLineEnd(*sz))
// ------------------------------------------------------------------------------------------------
2018-04-26 12:10:18 +00:00
// Loads the material table for the NFF2 file format from an external file
2020-03-01 12:15:45 +00:00
void NFFImporter::LoadNFF2MaterialTable(std::vector<ShadingInfo> &output,
const std::string &path, IOSystem *pIOHandler) {
std::unique_ptr<IOStream> file(pIOHandler->Open(path, "rb"));
2015-05-19 03:57:13 +00:00
// Check whether we can read from the file
2020-03-01 12:15:45 +00:00
if (!file.get()) {
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_ERROR("NFF2: Unable to open material library " + path + ".");
2015-05-19 03:57:13 +00:00
return;
}
// get the size of the file
const unsigned int m = (unsigned int)file->FileSize();
// allocate storage and copy the contents of the file to a memory buffer
// (terminate it with zero)
2020-03-01 12:15:45 +00:00
std::vector<char> mBuffer2(m + 1);
TextFileToBuffer(file.get(), mBuffer2);
const char *buffer = &mBuffer2[0];
2015-05-19 03:57:13 +00:00
// First of all: remove all comments from the file
2020-03-01 12:15:45 +00:00
CommentRemover::RemoveLineComments("//", &mBuffer2[0]);
2015-05-19 03:57:13 +00:00
// The file should start with the magic sequence "mat"
2020-03-01 12:15:45 +00:00
if (!TokenMatch(buffer, "mat", 3)) {
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_ERROR_F("NFF2: Not a valid material library ", path, ".");
2015-05-19 03:57:13 +00:00
return;
}
ShadingInfo *curShader = nullptr;
2015-05-19 03:57:13 +00:00
// No read the file line per line
char line[4096];
2020-03-01 12:15:45 +00:00
const char *sz;
while (GetNextLine(buffer, line)) {
SkipSpaces(line, &sz);
2015-05-19 03:57:13 +00:00
// 'version' defines the version of the file format
2020-03-01 12:15:45 +00:00
if (TokenMatch(sz, "version", 7)) {
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_INFO_F("NFF (Sense8) material library file format: ", std::string(sz));
2015-05-19 03:57:13 +00:00
}
// 'matdef' starts a new material in the file
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "matdef", 6)) {
2015-05-19 03:57:13 +00:00
// add a new material to the list
2020-03-01 12:15:45 +00:00
output.push_back(ShadingInfo());
curShader = &output.back();
2015-05-19 03:57:13 +00:00
// parse the name of the material
2020-03-01 12:15:45 +00:00
} else if (!TokenMatch(sz, "valid", 5)) {
2015-05-19 03:57:13 +00:00
// check whether we have an active material at the moment
2020-03-01 12:15:45 +00:00
if (!IsLineEnd(*sz)) {
if (!curShader) {
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_ERROR_F("NFF2 material library: Found element ", sz, "but there is no active material");
2015-05-19 03:57:13 +00:00
continue;
}
2020-03-01 12:15:45 +00:00
} else
continue;
2015-05-19 03:57:13 +00:00
// now read the material property and determine its type
aiColor3D c;
2020-03-01 12:15:45 +00:00
if (TokenMatch(sz, "ambient", 7)) {
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_TRIPLE(c);
curShader->ambient = c;
2020-03-01 12:15:45 +00:00
} else if (TokenMatch(sz, "diffuse", 7) || TokenMatch(sz, "ambientdiffuse", 14) /* correct? */) {
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_TRIPLE(c);
curShader->diffuse = curShader->ambient = c;
2020-03-01 12:15:45 +00:00
} else if (TokenMatch(sz, "specular", 8)) {
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_TRIPLE(c);
curShader->specular = c;
2020-03-01 12:15:45 +00:00
} else if (TokenMatch(sz, "emission", 8)) {
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_TRIPLE(c);
curShader->emissive = c;
2020-03-01 12:15:45 +00:00
} else if (TokenMatch(sz, "shininess", 9)) {
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_FLOAT(curShader->shininess);
2020-03-01 12:15:45 +00:00
} else if (TokenMatch(sz, "opacity", 7)) {
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_FLOAT(curShader->opacity);
}
}
}
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Imports the given file into the given scene structure.
2020-03-01 12:15:45 +00:00
void NFFImporter::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-03-01 12:15:45 +00:00
if (!file.get())
throw DeadlyImportError("Failed to open NFF 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-03-01 12:15:45 +00:00
TextFileToBuffer(file.get(), mBuffer2);
const char *buffer = &mBuffer2[0];
2015-05-19 03:57:13 +00:00
// mesh arrays - separate here to make the handling of the pointers below easier.
std::vector<MeshInfo> meshes;
std::vector<MeshInfo> meshesWithNormals;
std::vector<MeshInfo> meshesWithUVCoords;
std::vector<MeshInfo> meshesLocked;
char line[4096];
2020-03-01 12:15:45 +00:00
const char *sz;
2015-05-19 03:57:13 +00:00
// camera parameters
2020-03-01 12:15:45 +00:00
aiVector3D camPos, camUp(0.f, 1.f, 0.f), camLookAt(0.f, 0.f, 1.f);
2015-05-19 03:57:13 +00:00
float angle = 45.f;
aiVector2D resolution;
bool hasCam = false;
MeshInfo *currentMeshWithNormals = nullptr;
MeshInfo *currentMesh = nullptr;
MeshInfo *currentMeshWithUVCoords = nullptr;
2015-05-19 03:57:13 +00:00
ShadingInfo s; // current material info
2018-05-13 14:35:03 +00:00
// degree of tessellation
2015-05-19 03:57:13 +00:00
unsigned int iTesselation = 4;
// some temporary variables we need to parse the file
2020-03-01 12:15:45 +00:00
unsigned int sphere = 0,
cylinder = 0,
cone = 0,
numNamed = 0,
dodecahedron = 0,
octahedron = 0,
tetrahedron = 0,
hexahedron = 0;
2015-05-19 03:57:13 +00:00
// lights imported from the file
std::vector<Light> lights;
// check whether this is the NFF2 file format
2020-03-01 12:15:45 +00:00
if (TokenMatch(buffer, "nff", 3)) {
2015-05-19 03:57:13 +00:00
const float qnan = get_qnan();
2020-03-01 12:15:45 +00:00
const aiColor4D cQNAN = aiColor4D(qnan, 0.f, 0.f, 1.f);
const aiVector3D vQNAN = aiVector3D(qnan, 0.f, 0.f);
2015-05-19 03:57:13 +00:00
// another NFF file format ... just a raw parser has been implemented
// no support for further details, I don't think it is worth the effort
// http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/nff/nff2.html
// http://www.netghost.narod.ru/gff/graphics/summary/sense8.htm
// First of all: remove all comments from the file
2020-03-01 12:15:45 +00:00
CommentRemover::RemoveLineComments("//", &mBuffer2[0]);
while (GetNextLine(buffer, line)) {
SkipSpaces(line, &sz);
if (TokenMatch(sz, "version", 7)) {
ASSIMP_LOG_INFO_F("NFF (Sense8) file format: ", sz);
} else if (TokenMatch(sz, "viewpos", 7)) {
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_TRIPLE(camPos);
hasCam = true;
2020-03-01 12:15:45 +00:00
} else if (TokenMatch(sz, "viewdir", 7)) {
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_TRIPLE(camLookAt);
hasCam = true;
}
// This starts a new object section
2020-03-01 12:15:45 +00:00
else if (!IsSpaceOrNewLine(*sz)) {
2015-05-19 03:57:13 +00:00
unsigned int subMeshIdx = 0;
// read the name of the object, skip all spaces
// at the end of it.
2020-03-01 12:15:45 +00:00
const char *sz3 = sz;
while (!IsSpaceOrNewLine(*sz))
++sz;
std::string objectName = std::string(sz3, (unsigned int)(sz - sz3));
2015-05-19 03:57:13 +00:00
const unsigned int objStart = (unsigned int)meshes.size();
// There could be a material table in a separate file
std::vector<ShadingInfo> materialTable;
2020-03-01 12:15:45 +00:00
while (true) {
2015-05-19 03:57:13 +00:00
AI_NFF2_GET_NEXT_TOKEN();
// material table - an external file
2020-03-01 12:15:45 +00:00
if (TokenMatch(sz, "mtable", 6)) {
2015-05-19 03:57:13 +00:00
SkipSpaces(&sz);
sz3 = sz;
2020-03-01 12:15:45 +00:00
while (!IsSpaceOrNewLine(*sz))
++sz;
const unsigned int diff = (unsigned int)(sz - sz3);
if (!diff)
ASSIMP_LOG_WARN("NFF2: Found empty mtable token");
else {
2015-05-19 03:57:13 +00:00
// The material table has the file extension .mat.
// If it is not there, we need to append it
2020-03-01 12:15:45 +00:00
std::string path = std::string(sz3, diff);
if (std::string::npos == path.find_last_of(".mat")) {
2015-05-19 03:57:13 +00:00
path.append(".mat");
}
// Now extract the working directory from the path to
// this file and append the material library filename
// to it.
2020-03-01 12:15:45 +00:00
std::string::size_type sepPos;
if ((std::string::npos == (sepPos = path.find_last_of('\\')) || !sepPos) &&
(std::string::npos == (sepPos = path.find_last_of('/')) || !sepPos)) {
sepPos = pFile.find_last_of('\\');
if (std::string::npos == sepPos) {
sepPos = pFile.find_last_of('/');
}
if (std::string::npos != sepPos) {
path = pFile.substr(0, sepPos + 1) + path;
2015-05-19 03:57:13 +00:00
}
}
2020-03-01 12:15:45 +00:00
LoadNFF2MaterialTable(materialTable, path, pIOHandler);
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
} else
break;
2015-05-19 03:57:13 +00:00
}
// read the numbr of vertices
2020-03-01 12:15:45 +00:00
unsigned int num = ::strtoul10(sz, &sz);
2015-05-19 03:57:13 +00:00
// temporary storage
2020-03-01 12:15:45 +00:00
std::vector<aiColor4D> tempColors;
std::vector<aiVector3D> tempPositions, tempTextureCoords, tempNormals;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
bool hasNormals = false, hasUVs = false, hasColor = false;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
tempPositions.reserve(num);
tempColors.reserve(num);
tempNormals.reserve(num);
tempTextureCoords.reserve(num);
for (unsigned int i = 0; i < num; ++i) {
2015-05-19 03:57:13 +00:00
AI_NFF2_GET_NEXT_TOKEN();
aiVector3D v;
AI_NFF_PARSE_TRIPLE(v);
tempPositions.push_back(v);
// parse all other attributes in the line
2020-03-01 12:15:45 +00:00
while (true) {
2015-05-19 03:57:13 +00:00
SkipSpaces(&sz);
2020-03-01 12:15:45 +00:00
if (IsLineEnd(*sz)) break;
2015-05-19 03:57:13 +00:00
// color definition
2020-03-01 12:15:45 +00:00
if (TokenMatch(sz, "0x", 2)) {
2015-05-19 03:57:13 +00:00
hasColor = true;
2020-03-01 12:15:45 +00:00
unsigned int numIdx = ::strtoul16(sz, &sz);
2015-05-19 03:57:13 +00:00
aiColor4D clr;
clr.a = 1.f;
// 0xRRGGBB
clr.r = ((numIdx >> 16u) & 0xff) / 255.f;
2020-03-01 12:15:45 +00:00
clr.g = ((numIdx >> 8u) & 0xff) / 255.f;
clr.b = ((numIdx)&0xff) / 255.f;
2015-05-19 03:57:13 +00:00
tempColors.push_back(clr);
}
// normal vector
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "norm", 4)) {
2015-05-19 03:57:13 +00:00
hasNormals = true;
AI_NFF_PARSE_TRIPLE(v);
tempNormals.push_back(v);
}
// UV coordinate
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "uv", 2)) {
2015-05-19 03:57:13 +00:00
hasUVs = true;
AI_NFF_PARSE_FLOAT(v.x);
AI_NFF_PARSE_FLOAT(v.y);
v.z = 0.f;
tempTextureCoords.push_back(v);
}
}
// fill in dummies for all attributes that have not been set
if (tempNormals.size() != tempPositions.size())
tempNormals.push_back(vQNAN);
if (tempTextureCoords.size() != tempPositions.size())
tempTextureCoords.push_back(vQNAN);
if (tempColors.size() != tempPositions.size())
tempColors.push_back(cQNAN);
}
AI_NFF2_GET_NEXT_TOKEN();
2020-03-01 12:15:45 +00:00
if (!num) throw DeadlyImportError("NFF2: There are zero vertices");
num = ::strtoul10(sz, &sz);
2015-05-19 03:57:13 +00:00
std::vector<unsigned int> tempIdx;
tempIdx.reserve(10);
2020-03-01 12:15:45 +00:00
for (unsigned int i = 0; i < num; ++i) {
2015-05-19 03:57:13 +00:00
AI_NFF2_GET_NEXT_TOKEN();
2020-03-01 12:15:45 +00:00
SkipSpaces(line, &sz);
unsigned int numIdx = ::strtoul10(sz, &sz);
2015-05-19 03:57:13 +00:00
// read all faces indices
2020-03-01 12:15:45 +00:00
if (numIdx) {
2015-05-19 03:57:13 +00:00
// mesh.faces.push_back(numIdx);
// tempIdx.erase(tempIdx.begin(),tempIdx.end());
tempIdx.resize(numIdx);
2020-03-01 12:15:45 +00:00
for (unsigned int a = 0; a < numIdx; ++a) {
SkipSpaces(sz, &sz);
unsigned int m = ::strtoul10(sz, &sz);
if (m >= (unsigned int)tempPositions.size()) {
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_ERROR("NFF2: Vertex index overflow");
2020-03-01 12:15:45 +00:00
m = 0;
2015-05-19 03:57:13 +00:00
}
// mesh.vertices.push_back (tempPositions[idx]);
tempIdx[a] = m;
}
}
// build a temporary shader object for the face.
ShadingInfo shader;
unsigned int matIdx = 0;
// white material color - we have vertex colors
2020-03-01 12:15:45 +00:00
shader.color = aiColor3D(1.f, 1.f, 1.f);
aiColor4D c = aiColor4D(1.f, 1.f, 1.f, 1.f);
while (true) {
SkipSpaces(sz, &sz);
if (IsLineEnd(*sz)) break;
2015-05-19 03:57:13 +00:00
// per-polygon colors
2020-03-01 12:15:45 +00:00
if (TokenMatch(sz, "0x", 2)) {
2015-05-19 03:57:13 +00:00
hasColor = true;
2020-03-01 12:15:45 +00:00
const char *sz2 = sz;
numIdx = ::strtoul16(sz, &sz);
const unsigned int diff = (unsigned int)(sz - sz2);
2015-05-19 03:57:13 +00:00
// 0xRRGGBB
2020-03-01 12:15:45 +00:00
if (diff > 3) {
2015-05-19 03:57:13 +00:00
c.r = ((numIdx >> 16u) & 0xff) / 255.f;
2020-03-01 12:15:45 +00:00
c.g = ((numIdx >> 8u) & 0xff) / 255.f;
c.b = ((numIdx)&0xff) / 255.f;
2015-05-19 03:57:13 +00:00
}
// 0xRGB
2020-03-01 12:15:45 +00:00
else {
2015-05-19 03:57:13 +00:00
c.r = ((numIdx >> 8u) & 0xf) / 16.f;
c.g = ((numIdx >> 4u) & 0xf) / 16.f;
2020-03-01 12:15:45 +00:00
c.b = ((numIdx)&0xf) / 16.f;
2015-05-19 03:57:13 +00:00
}
}
// TODO - implement texture mapping here
#if 0
2015-05-19 03:57:13 +00:00
// mirror vertex texture coordinate?
else if (TokenMatch(sz,"mirror",6))
{
}
// texture coordinate scaling
else if (TokenMatch(sz,"scale",5))
{
}
// texture coordinate translation
else if (TokenMatch(sz,"trans",5))
{
}
// texture coordinate rotation angle
else if (TokenMatch(sz,"rot",3))
{
}
#endif
2015-05-19 03:57:13 +00:00
// texture file name for this polygon + mapping information
2020-03-01 12:15:45 +00:00
else if ('_' == sz[0]) {
2015-05-19 03:57:13 +00:00
// get mapping information
2020-03-01 12:15:45 +00:00
switch (sz[1]) {
case 'v':
case 'V':
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
shader.shaded = false;
break;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
case 't':
case 'T':
case 'u':
case 'U':
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
ASSIMP_LOG_WARN("Unsupported NFF2 texture attribute: trans");
2015-05-19 03:57:13 +00:00
};
2020-03-01 12:15:45 +00:00
if (!sz[1] || '_' != sz[2]) {
2018-04-19 15:21:21 +00:00
ASSIMP_LOG_WARN("NFF2: Expected underscore after texture attributes");
2015-05-19 03:57:13 +00:00
continue;
}
2020-03-01 12:15:45 +00:00
const char *sz2 = sz + 3;
while (!IsSpaceOrNewLine(*sz))
++sz;
const unsigned int diff = (unsigned int)(sz - sz2);
if (diff) shader.texFile = std::string(sz2, diff);
2015-05-19 03:57:13 +00:00
}
// Two-sided material?
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "both", 4)) {
2015-05-19 03:57:13 +00:00
shader.twoSided = true;
}
// Material ID?
2020-03-01 12:15:45 +00:00
else if (!materialTable.empty() && TokenMatch(sz, "matid", 5)) {
2015-05-19 03:57:13 +00:00
SkipSpaces(&sz);
2020-03-01 12:15:45 +00:00
matIdx = ::strtoul10(sz, &sz);
if (matIdx >= materialTable.size()) {
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_ERROR("NFF2: Material index overflow.");
2015-05-19 03:57:13 +00:00
matIdx = 0;
}
// now combine our current shader with the shader we
// read from the material table.
2020-03-01 12:15:45 +00:00
ShadingInfo &mat = materialTable[matIdx];
shader.ambient = mat.ambient;
shader.diffuse = mat.diffuse;
shader.emissive = mat.emissive;
shader.opacity = mat.opacity;
shader.specular = mat.specular;
2015-05-19 03:57:13 +00:00
shader.shininess = mat.shininess;
2020-03-01 12:15:45 +00:00
} else
SkipToken(sz);
2015-05-19 03:57:13 +00:00
}
// search the list of all shaders we have for this object whether
// there is an identical one. In this case, we append our mesh
// data to it.
MeshInfo *mesh = nullptr;
2015-05-19 03:57:13 +00:00
for (std::vector<MeshInfo>::iterator it = meshes.begin() + objStart, end = meshes.end();
2020-03-01 12:15:45 +00:00
it != end; ++it) {
if ((*it).shader == shader && (*it).matIndex == matIdx) {
2015-05-19 03:57:13 +00:00
// we have one, we can append our data to it
mesh = &(*it);
}
}
2020-03-01 12:15:45 +00:00
if (!mesh) {
meshes.push_back(MeshInfo(PatchType_Simple, false));
2015-05-19 03:57:13 +00:00
mesh = &meshes.back();
mesh->matIndex = matIdx;
// We need to add a new mesh to the list. We assign
// an unique name to it to make sure the scene will
// pass the validation step for the moment.
// TODO: fix naming of objects in the scenegraph later
2020-03-01 12:15:45 +00:00
if (objectName.length()) {
::strcpy(mesh->name, objectName.c_str());
ASSIMP_itoa10(&mesh->name[objectName.length()], 30, subMeshIdx++);
2015-05-19 03:57:13 +00:00
}
// copy the shader to the mesh.
mesh->shader = shader;
}
// fill the mesh with data
2020-03-01 12:15:45 +00:00
if (!tempIdx.empty()) {
2015-05-19 03:57:13 +00:00
mesh->faces.push_back((unsigned int)tempIdx.size());
for (std::vector<unsigned int>::const_iterator it = tempIdx.begin(), end = tempIdx.end();
2020-03-01 12:15:45 +00:00
it != end; ++it) {
2017-11-07 17:12:44 +00:00
unsigned int m = *it;
2015-05-19 03:57:13 +00:00
// copy colors -vertex color specifications override polygon color specifications
2020-03-01 12:15:45 +00:00
if (hasColor) {
const aiColor4D &clr = tempColors[m];
mesh->colors.push_back((is_qnan(clr.r) ? c : clr));
2015-05-19 03:57:13 +00:00
}
// positions should always be there
2020-03-01 12:15:45 +00:00
mesh->vertices.push_back(tempPositions[m]);
2015-05-19 03:57:13 +00:00
// copy normal vectors
if (hasNormals)
2020-03-01 12:15:45 +00:00
mesh->normals.push_back(tempNormals[m]);
2015-05-19 03:57:13 +00:00
// copy texture coordinates
if (hasUVs)
2020-03-01 12:15:45 +00:00
mesh->uvs.push_back(tempTextureCoords[m]);
2015-05-19 03:57:13 +00:00
}
}
}
2020-03-01 12:15:45 +00:00
if (!num) throw DeadlyImportError("NFF2: There are zero faces");
2015-05-19 03:57:13 +00:00
}
}
camLookAt = camLookAt + camPos;
2020-03-01 12:15:45 +00:00
} else // "Normal" Neutral file format that is quite more common
2015-05-19 03:57:13 +00:00
{
2020-03-01 12:15:45 +00:00
while (GetNextLine(buffer, line)) {
2015-05-19 03:57:13 +00:00
sz = line;
2020-03-01 12:15:45 +00:00
if ('p' == line[0] || TokenMatch(sz, "tpp", 3)) {
MeshInfo *out = nullptr;
2015-05-19 03:57:13 +00:00
// 'tpp' - texture polygon patch primitive
2020-03-01 12:15:45 +00:00
if ('t' == line[0]) {
currentMeshWithUVCoords = nullptr;
2020-03-01 12:15:45 +00:00
for (auto &mesh : meshesWithUVCoords) {
if (mesh.shader == s) {
2016-05-21 21:37:10 +00:00
currentMeshWithUVCoords = &mesh;
2015-05-19 03:57:13 +00:00
break;
}
}
2020-03-01 12:15:45 +00:00
if (!currentMeshWithUVCoords) {
2015-05-19 03:57:13 +00:00
meshesWithUVCoords.push_back(MeshInfo(PatchType_UVAndNormals));
currentMeshWithUVCoords = &meshesWithUVCoords.back();
currentMeshWithUVCoords->shader = s;
}
out = currentMeshWithUVCoords;
}
// 'pp' - polygon patch primitive
2020-03-01 12:15:45 +00:00
else if ('p' == line[1]) {
currentMeshWithNormals = nullptr;
2020-03-01 12:15:45 +00:00
for (auto &mesh : meshesWithNormals) {
if (mesh.shader == s) {
2016-05-21 21:37:10 +00:00
currentMeshWithNormals = &mesh;
2015-05-19 03:57:13 +00:00
break;
}
}
2020-03-01 12:15:45 +00:00
if (!currentMeshWithNormals) {
2015-05-19 03:57:13 +00:00
meshesWithNormals.push_back(MeshInfo(PatchType_Normals));
currentMeshWithNormals = &meshesWithNormals.back();
currentMeshWithNormals->shader = s;
}
2020-03-01 12:15:45 +00:00
sz = &line[2];
out = currentMeshWithNormals;
2015-05-19 03:57:13 +00:00
}
// 'p' - polygon primitive
2020-03-01 12:15:45 +00:00
else {
currentMesh = nullptr;
2020-03-01 12:15:45 +00:00
for (auto &mesh : meshes) {
if (mesh.shader == s) {
2016-05-21 21:37:10 +00:00
currentMesh = &mesh;
2015-05-19 03:57:13 +00:00
break;
}
}
2020-03-01 12:15:45 +00:00
if (!currentMesh) {
2015-05-19 03:57:13 +00:00
meshes.push_back(MeshInfo(PatchType_Simple));
currentMesh = &meshes.back();
currentMesh->shader = s;
}
2020-03-01 12:15:45 +00:00
sz = &line[1];
out = currentMesh;
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
SkipSpaces(sz, &sz);
2017-11-07 17:12:44 +00:00
unsigned int m = strtoul10(sz);
2015-05-19 03:57:13 +00:00
// ---- flip the face order
2020-03-01 12:15:45 +00:00
out->vertices.resize(out->vertices.size() + m);
if (out != currentMesh) {
2015-05-19 03:57:13 +00:00
out->normals.resize(out->vertices.size());
}
2020-03-01 12:15:45 +00:00
if (out == currentMeshWithUVCoords) {
2015-05-19 03:57:13 +00:00
out->uvs.resize(out->vertices.size());
}
2020-03-01 12:15:45 +00:00
for (unsigned int n = 0; n < m; ++n) {
if (!GetNextLine(buffer, line)) {
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_ERROR("NFF: Unexpected EOF was encountered. Patch definition incomplete");
2015-05-19 03:57:13 +00:00
continue;
}
2020-03-01 12:15:45 +00:00
aiVector3D v;
sz = &line[0];
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_TRIPLE(v);
2020-03-01 12:15:45 +00:00
out->vertices[out->vertices.size() - n - 1] = v;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
if (out != currentMesh) {
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_TRIPLE(v);
2020-03-01 12:15:45 +00:00
out->normals[out->vertices.size() - n - 1] = v;
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
if (out == currentMeshWithUVCoords) {
2015-05-19 03:57:13 +00:00
// FIX: in one test file this wraps over multiple lines
SkipSpaces(&sz);
2020-03-01 12:15:45 +00:00
if (IsLineEnd(*sz)) {
GetNextLine(buffer, line);
2015-05-19 03:57:13 +00:00
sz = line;
}
AI_NFF_PARSE_FLOAT(v.x);
SkipSpaces(&sz);
2020-03-01 12:15:45 +00:00
if (IsLineEnd(*sz)) {
GetNextLine(buffer, line);
2015-05-19 03:57:13 +00:00
sz = line;
}
AI_NFF_PARSE_FLOAT(v.y);
v.y = 1.f - v.y;
2020-03-01 12:15:45 +00:00
out->uvs[out->vertices.size() - n - 1] = v;
2015-05-19 03:57:13 +00:00
}
}
out->faces.push_back(m);
}
// 'f' - shading information block
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "f", 1)) {
2015-05-19 03:57:13 +00:00
float d;
// read the RGB colors
AI_NFF_PARSE_TRIPLE(s.color);
// read the other properties
AI_NFF_PARSE_FLOAT(s.diffuse.r);
AI_NFF_PARSE_FLOAT(s.specular.r);
AI_NFF_PARSE_FLOAT(d); // skip shininess and transmittance
AI_NFF_PARSE_FLOAT(d);
AI_NFF_PARSE_FLOAT(s.refracti);
// NFF2 uses full colors here so we need to use them too
// although NFF uses simple scaling factors
2020-03-01 12:15:45 +00:00
s.diffuse.g = s.diffuse.b = s.diffuse.r;
2015-05-19 03:57:13 +00:00
s.specular.g = s.specular.b = s.specular.r;
// if the next one is NOT a number we assume it is a texture file name
// this feature is used by some NFF files on the internet and it has
// been implemented as it can be really useful
SkipSpaces(&sz);
2020-03-01 12:15:45 +00:00
if (!IsNumeric(*sz)) {
2015-05-19 03:57:13 +00:00
// TODO: Support full file names with spaces and quotation marks ...
2020-03-01 12:15:45 +00:00
const char *p = sz;
while (!IsSpaceOrNewLine(*sz))
++sz;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
unsigned int diff = (unsigned int)(sz - p);
if (diff) {
s.texFile = std::string(p, diff);
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
} else {
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_FLOAT(s.ambient); // optional
}
}
// 'shader' - other way to specify a texture
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "shader", 6)) {
2015-05-19 03:57:13 +00:00
SkipSpaces(&sz);
2020-03-01 12:15:45 +00:00
const char *old = sz;
while (!IsSpaceOrNewLine(*sz))
++sz;
2015-05-19 03:57:13 +00:00
s.texFile = std::string(old, (uintptr_t)sz - (uintptr_t)old);
}
// 'l' - light source
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "l", 1)) {
2015-05-19 03:57:13 +00:00
lights.push_back(Light());
2020-03-01 12:15:45 +00:00
Light &light = lights.back();
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_TRIPLE(light.position);
2020-03-01 12:15:45 +00:00
AI_NFF_PARSE_FLOAT(light.intensity);
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_TRIPLE(light.color);
}
// 's' - sphere
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "s", 1)) {
meshesLocked.push_back(MeshInfo(PatchType_Simple, true));
MeshInfo &curMesh = meshesLocked.back();
curMesh.shader = s;
curMesh.shader.mapping = aiTextureMapping_SPHERE;
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_SHAPE_INFORMATION();
// we don't need scaling or translation here - we do it in the node's transform
2020-03-01 12:15:45 +00:00
StandardShapes::MakeSphere(iTesselation, curMesh.vertices);
curMesh.faces.resize(curMesh.vertices.size() / 3, 3);
2015-05-19 03:57:13 +00:00
// generate a name for the mesh
2020-03-01 12:15:45 +00:00
::ai_snprintf(curMesh.name, MeshInfo::MaxNameLen, "sphere_%i", sphere++);
2015-05-19 03:57:13 +00:00
}
// 'dod' - dodecahedron
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "dod", 3)) {
meshesLocked.push_back(MeshInfo(PatchType_Simple, true));
MeshInfo &curMesh = meshesLocked.back();
curMesh.shader = s;
curMesh.shader.mapping = aiTextureMapping_SPHERE;
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_SHAPE_INFORMATION();
// we don't need scaling or translation here - we do it in the node's transform
2020-03-01 12:15:45 +00:00
StandardShapes::MakeDodecahedron(curMesh.vertices);
curMesh.faces.resize(curMesh.vertices.size() / 3, 3);
2015-05-19 03:57:13 +00:00
// generate a name for the mesh
2020-03-01 12:15:45 +00:00
::ai_snprintf(curMesh.name, 128, "dodecahedron_%i", dodecahedron++);
2015-05-19 03:57:13 +00:00
}
// 'oct' - octahedron
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "oct", 3)) {
meshesLocked.push_back(MeshInfo(PatchType_Simple, true));
MeshInfo &curMesh = meshesLocked.back();
curMesh.shader = s;
curMesh.shader.mapping = aiTextureMapping_SPHERE;
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_SHAPE_INFORMATION();
// we don't need scaling or translation here - we do it in the node's transform
2020-03-01 12:15:45 +00:00
StandardShapes::MakeOctahedron(curMesh.vertices);
curMesh.faces.resize(curMesh.vertices.size() / 3, 3);
2015-05-19 03:57:13 +00:00
// generate a name for the mesh
2020-03-01 12:15:45 +00:00
::ai_snprintf(curMesh.name, MeshInfo::MaxNameLen, "octahedron_%i", octahedron++);
2015-05-19 03:57:13 +00:00
}
// 'tet' - tetrahedron
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "tet", 3)) {
meshesLocked.push_back(MeshInfo(PatchType_Simple, true));
MeshInfo &curMesh = meshesLocked.back();
curMesh.shader = s;
curMesh.shader.mapping = aiTextureMapping_SPHERE;
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_SHAPE_INFORMATION();
// we don't need scaling or translation here - we do it in the node's transform
2020-03-01 12:15:45 +00:00
StandardShapes::MakeTetrahedron(curMesh.vertices);
curMesh.faces.resize(curMesh.vertices.size() / 3, 3);
2015-05-19 03:57:13 +00:00
// generate a name for the mesh
2020-03-01 12:15:45 +00:00
::ai_snprintf(curMesh.name, MeshInfo::MaxNameLen, "tetrahedron_%i", tetrahedron++);
2015-05-19 03:57:13 +00:00
}
// 'hex' - hexahedron
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "hex", 3)) {
meshesLocked.push_back(MeshInfo(PatchType_Simple, true));
MeshInfo &curMesh = meshesLocked.back();
curMesh.shader = s;
curMesh.shader.mapping = aiTextureMapping_BOX;
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_SHAPE_INFORMATION();
// we don't need scaling or translation here - we do it in the node's transform
2020-03-01 12:15:45 +00:00
StandardShapes::MakeHexahedron(curMesh.vertices);
curMesh.faces.resize(curMesh.vertices.size() / 3, 3);
2015-05-19 03:57:13 +00:00
// generate a name for the mesh
2020-03-01 12:15:45 +00:00
::ai_snprintf(curMesh.name, MeshInfo::MaxNameLen, "hexahedron_%i", hexahedron++);
2015-05-19 03:57:13 +00:00
}
// 'c' - cone
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "c", 1)) {
meshesLocked.push_back(MeshInfo(PatchType_Simple, true));
MeshInfo &curMesh = meshesLocked.back();
curMesh.shader = s;
curMesh.shader.mapping = aiTextureMapping_CYLINDER;
if (!GetNextLine(buffer, line)) {
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_ERROR("NFF: Unexpected end of file (cone definition not complete)");
2015-05-19 03:57:13 +00:00
break;
}
sz = line;
// read the two center points and the respective radii
2020-03-01 12:15:45 +00:00
aiVector3D center1, center2;
float radius1 = 0.f, radius2 = 0.f;
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_TRIPLE(center1);
AI_NFF_PARSE_FLOAT(radius1);
2020-03-01 12:15:45 +00:00
if (!GetNextLine(buffer, line)) {
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_ERROR("NFF: Unexpected end of file (cone definition not complete)");
2015-05-19 03:57:13 +00:00
break;
}
sz = line;
AI_NFF_PARSE_TRIPLE(center2);
AI_NFF_PARSE_FLOAT(radius2);
// compute the center point of the cone/cylinder -
// it is its local transformation origin
2020-03-01 12:15:45 +00:00
curMesh.dir = center2 - center1;
curMesh.center = center1 + curMesh.dir / (ai_real)2.0;
2015-05-19 03:57:13 +00:00
float f;
2020-03-01 12:15:45 +00:00
if ((f = curMesh.dir.Length()) < 10e-3f) {
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_ERROR("NFF: Cone height is close to zero");
2015-05-19 03:57:13 +00:00
continue;
}
2020-03-01 12:15:45 +00:00
curMesh.dir /= f; // normalize
2015-05-19 03:57:13 +00:00
// generate the cone - it consists of simple triangles
StandardShapes::MakeCone(f, radius1, radius2,
2020-03-01 12:15:45 +00:00
integer_pow(4, iTesselation), curMesh.vertices);
2015-05-19 03:57:13 +00:00
// MakeCone() returns tris
2020-03-01 12:15:45 +00:00
curMesh.faces.resize(curMesh.vertices.size() / 3, 3);
2015-05-19 03:57:13 +00:00
// generate a name for the mesh. 'cone' if it a cone,
// 'cylinder' if it is a cylinder. Funny, isn't it?
2020-03-01 12:15:45 +00:00
if (radius1 != radius2) {
::ai_snprintf(curMesh.name, MeshInfo::MaxNameLen, "cone_%i", cone++);
} else {
::ai_snprintf(curMesh.name, MeshInfo::MaxNameLen, "cylinder_%i", cylinder++);
}
2015-05-19 03:57:13 +00:00
}
2018-05-13 14:35:03 +00:00
// 'tess' - tessellation
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "tess", 4)) {
2015-05-19 03:57:13 +00:00
SkipSpaces(&sz);
iTesselation = strtoul10(sz);
}
// 'from' - camera position
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "from", 4)) {
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_TRIPLE(camPos);
hasCam = true;
}
// 'at' - camera look-at vector
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "at", 2)) {
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_TRIPLE(camLookAt);
hasCam = true;
}
// 'up' - camera up vector
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "up", 2)) {
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_TRIPLE(camUp);
hasCam = true;
}
// 'angle' - (half?) camera field of view
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "angle", 5)) {
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_FLOAT(angle);
hasCam = true;
}
// 'resolution' - used to compute the screen aspect
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "resolution", 10)) {
2015-05-19 03:57:13 +00:00
AI_NFF_PARSE_FLOAT(resolution.x);
AI_NFF_PARSE_FLOAT(resolution.y);
hasCam = true;
}
// 'pb' - bezier patch. Not supported yet
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "pb", 2)) {
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_ERROR("NFF: Encountered unsupported ID: bezier patch");
2015-05-19 03:57:13 +00:00
}
// 'pn' - NURBS. Not supported yet
2020-03-01 12:15:45 +00:00
else if (TokenMatch(sz, "pn", 2) || TokenMatch(sz, "pnn", 3)) {
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_ERROR("NFF: Encountered unsupported ID: NURBS");
2015-05-19 03:57:13 +00:00
}
// '' - comment
2020-03-01 12:15:45 +00:00
else if ('#' == line[0]) {
const char *space;
SkipSpaces(&line[1], &space);
if (!IsLineEnd(*space)) {
ASSIMP_LOG_INFO(space);
2018-04-26 12:10:18 +00:00
}
2015-05-19 03:57:13 +00:00
}
}
}
// copy all arrays into one large
2020-03-01 12:15:45 +00:00
meshes.reserve(meshes.size() + meshesLocked.size() + meshesWithNormals.size() + meshesWithUVCoords.size());
meshes.insert(meshes.end(), meshesLocked.begin(), meshesLocked.end());
meshes.insert(meshes.end(), meshesWithNormals.begin(), meshesWithNormals.end());
meshes.insert(meshes.end(), meshesWithUVCoords.begin(), meshesWithUVCoords.end());
2015-05-19 03:57:13 +00:00
// now generate output meshes. first find out how many meshes we'll need
std::vector<MeshInfo>::const_iterator it = meshes.begin(), end = meshes.end();
2020-03-01 12:15:45 +00:00
for (; it != end; ++it) {
if (!(*it).faces.empty()) {
2015-05-19 03:57:13 +00:00
++pScene->mNumMeshes;
2020-03-01 12:15:45 +00:00
if ((*it).name[0]) ++numNamed;
2015-05-19 03:57:13 +00:00
}
}
// generate a dummy root node - assign all unnamed elements such
// as polygons and polygon patches to the root node and generate
// sub nodes for named objects such as spheres and cones.
2020-03-01 12:15:45 +00:00
aiNode *const root = new aiNode();
2015-05-19 03:57:13 +00:00
root->mName.Set("<NFF_Root>");
2020-03-01 12:15:45 +00:00
root->mNumChildren = numNamed + (hasCam ? 1 : 0) + (unsigned int)lights.size();
root->mNumMeshes = pScene->mNumMeshes - numNamed;
2015-05-19 03:57:13 +00:00
aiNode **ppcChildren = nullptr;
unsigned int *pMeshes = nullptr;
2015-05-19 03:57:13 +00:00
if (root->mNumMeshes)
pMeshes = root->mMeshes = new unsigned int[root->mNumMeshes];
if (root->mNumChildren)
2020-03-01 12:15:45 +00:00
ppcChildren = root->mChildren = new aiNode *[root->mNumChildren];
2015-05-19 03:57:13 +00:00
// generate the camera
2020-03-01 12:15:45 +00:00
if (hasCam) {
ai_assert(ppcChildren);
2020-03-01 12:15:45 +00:00
aiNode *nd = new aiNode();
*ppcChildren = nd;
2015-05-19 03:57:13 +00:00
nd->mName.Set("<NFF_Camera>");
nd->mParent = root;
// allocate the camera in the scene
pScene->mNumCameras = 1;
2020-03-01 12:15:45 +00:00
pScene->mCameras = new aiCamera *[1];
aiCamera *c = pScene->mCameras[0] = new aiCamera;
2015-05-19 03:57:13 +00:00
c->mName = nd->mName; // make sure the names are identical
2020-03-01 12:15:45 +00:00
c->mHorizontalFOV = AI_DEG_TO_RAD(angle);
c->mLookAt = camLookAt - camPos;
c->mPosition = camPos;
c->mUp = camUp;
2015-05-19 03:57:13 +00:00
// If the resolution is not specified in the file, we
// need to set 1.0 as aspect.
2020-03-01 12:15:45 +00:00
c->mAspect = (!resolution.y ? 0.f : resolution.x / resolution.y);
2015-05-19 03:57:13 +00:00
++ppcChildren;
}
// generate light sources
2020-03-01 12:15:45 +00:00
if (!lights.empty()) {
ai_assert(ppcChildren);
2015-05-19 03:57:13 +00:00
pScene->mNumLights = (unsigned int)lights.size();
2020-03-01 12:15:45 +00:00
pScene->mLights = new aiLight *[pScene->mNumLights];
for (unsigned int i = 0; i < pScene->mNumLights; ++i, ++ppcChildren) {
const Light &l = lights[i];
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
aiNode *nd = new aiNode();
*ppcChildren = nd;
2015-05-19 03:57:13 +00:00
nd->mParent = root;
2020-03-01 12:15:45 +00:00
nd->mName.length = ::ai_snprintf(nd->mName.data, 1024, "<NFF_Light%u>", i);
2015-05-19 03:57:13 +00:00
// allocate the light in the scene data structure
2020-03-01 12:15:45 +00:00
aiLight *out = pScene->mLights[i] = new aiLight();
2015-05-19 03:57:13 +00:00
out->mName = nd->mName; // make sure the names are identical
out->mType = aiLightSource_POINT;
out->mColorDiffuse = out->mColorSpecular = l.color * l.intensity;
out->mPosition = l.position;
}
}
2020-03-01 12:15:45 +00:00
if (!pScene->mNumMeshes) throw DeadlyImportError("NFF: No meshes loaded");
pScene->mMeshes = new aiMesh *[pScene->mNumMeshes];
pScene->mMaterials = new aiMaterial *[pScene->mNumMaterials = pScene->mNumMeshes];
2017-11-07 17:12:44 +00:00
unsigned int m = 0;
2020-03-01 12:15:45 +00:00
for (it = meshes.begin(); it != end; ++it) {
if ((*it).faces.empty()) continue;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
const MeshInfo &src = *it;
aiMesh *const mesh = pScene->mMeshes[m] = new aiMesh();
2015-05-19 03:57:13 +00:00
mesh->mNumVertices = (unsigned int)src.vertices.size();
mesh->mNumFaces = (unsigned int)src.faces.size();
// Generate sub nodes for named meshes
if (src.name[0] && nullptr != ppcChildren) {
2020-03-01 12:15:45 +00:00
aiNode *const node = *ppcChildren = new aiNode();
2015-05-19 03:57:13 +00:00
node->mParent = root;
node->mNumMeshes = 1;
node->mMeshes = new unsigned int[1];
node->mMeshes[0] = m;
node->mName.Set(src.name);
// setup the transformation matrix of the node
2020-03-01 12:15:45 +00:00
aiMatrix4x4::FromToMatrix(aiVector3D(0.f, 1.f, 0.f),
src.dir, node->mTransformation);
aiMatrix4x4 &mat = node->mTransformation;
mat.a1 *= src.radius.x;
mat.b1 *= src.radius.x;
mat.c1 *= src.radius.x;
mat.a2 *= src.radius.y;
mat.b2 *= src.radius.y;
mat.c2 *= src.radius.y;
mat.a3 *= src.radius.z;
mat.b3 *= src.radius.z;
mat.c3 *= src.radius.z;
2015-05-19 03:57:13 +00:00
mat.a4 = src.center.x;
mat.b4 = src.center.y;
mat.c4 = src.center.z;
++ppcChildren;
} else {
*pMeshes++ = m;
}
2015-05-19 03:57:13 +00:00
// copy vertex positions
mesh->mVertices = new aiVector3D[mesh->mNumVertices];
2020-03-01 12:15:45 +00:00
::memcpy(mesh->mVertices, &src.vertices[0],
sizeof(aiVector3D) * mesh->mNumVertices);
2015-05-19 03:57:13 +00:00
// NFF2: there could be vertex colors
2020-03-01 12:15:45 +00:00
if (!src.colors.empty()) {
2015-05-19 03:57:13 +00:00
ai_assert(src.colors.size() == src.vertices.size());
// copy vertex colors
mesh->mColors[0] = new aiColor4D[mesh->mNumVertices];
2020-03-01 12:15:45 +00:00
::memcpy(mesh->mColors[0], &src.colors[0],
sizeof(aiColor4D) * mesh->mNumVertices);
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
if (!src.normals.empty()) {
2015-05-19 03:57:13 +00:00
ai_assert(src.normals.size() == src.vertices.size());
// copy normal vectors
mesh->mNormals = new aiVector3D[mesh->mNumVertices];
2020-03-01 12:15:45 +00:00
::memcpy(mesh->mNormals, &src.normals[0],
sizeof(aiVector3D) * mesh->mNumVertices);
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
if (!src.uvs.empty()) {
2015-05-19 03:57:13 +00:00
ai_assert(src.uvs.size() == src.vertices.size());
// copy texture coordinates
mesh->mTextureCoords[0] = new aiVector3D[mesh->mNumVertices];
2020-03-01 12:15:45 +00:00
::memcpy(mesh->mTextureCoords[0], &src.uvs[0],
sizeof(aiVector3D) * mesh->mNumVertices);
2015-05-19 03:57:13 +00:00
}
// generate faces
unsigned int p = 0;
2020-03-01 12:15:45 +00:00
aiFace *pFace = mesh->mFaces = new aiFace[mesh->mNumFaces];
2015-05-19 03:57:13 +00:00
for (std::vector<unsigned int>::const_iterator it2 = src.faces.begin(),
2020-03-01 12:15:45 +00:00
end2 = src.faces.end();
it2 != end2; ++it2, ++pFace) {
pFace->mIndices = new unsigned int[pFace->mNumIndices = *it2];
for (unsigned int o = 0; o < pFace->mNumIndices; ++o)
2015-05-19 03:57:13 +00:00
pFace->mIndices[o] = p++;
}
// generate a material for the mesh
2020-03-01 12:15:45 +00:00
aiMaterial *pcMat = (aiMaterial *)(pScene->mMaterials[m] = new aiMaterial());
2015-05-19 03:57:13 +00:00
mesh->mMaterialIndex = m++;
2020-03-01 12:15:45 +00:00
aiString matName;
matName.Set(AI_DEFAULT_MATERIAL_NAME);
pcMat->AddProperty(&matName, AI_MATKEY_NAME);
2015-05-19 03:57:13 +00:00
// FIX: Ignore diffuse == 0
2020-03-01 12:15:45 +00:00
aiColor3D c = src.shader.color * (src.shader.diffuse.r ? src.shader.diffuse : aiColor3D(1.f, 1.f, 1.f));
pcMat->AddProperty(&c, 1, AI_MATKEY_COLOR_DIFFUSE);
2015-05-19 03:57:13 +00:00
c = src.shader.color * src.shader.specular;
2020-03-01 12:15:45 +00:00
pcMat->AddProperty(&c, 1, AI_MATKEY_COLOR_SPECULAR);
2015-05-19 03:57:13 +00:00
// NFF2 - default values for NFF
2020-03-01 12:15:45 +00:00
pcMat->AddProperty(&src.shader.ambient, 1, AI_MATKEY_COLOR_AMBIENT);
pcMat->AddProperty(&src.shader.emissive, 1, AI_MATKEY_COLOR_EMISSIVE);
pcMat->AddProperty(&src.shader.opacity, 1, AI_MATKEY_OPACITY);
2015-05-19 03:57:13 +00:00
// setup the first texture layer, if existing
2020-03-01 12:15:45 +00:00
if (src.shader.texFile.length()) {
matName.Set(src.shader.texFile);
pcMat->AddProperty(&matName, AI_MATKEY_TEXTURE_DIFFUSE(0));
2015-05-19 03:57:13 +00:00
if (aiTextureMapping_UV != src.shader.mapping) {
2020-03-01 12:15:45 +00:00
aiVector3D v(0.f, -1.f, 0.f);
pcMat->AddProperty(&v, 1, AI_MATKEY_TEXMAP_AXIS_DIFFUSE(0));
pcMat->AddProperty((int *)&src.shader.mapping, 1, AI_MATKEY_MAPPING_DIFFUSE(0));
2015-05-19 03:57:13 +00:00
}
}
// setup the name of the material
2020-03-01 12:15:45 +00:00
if (src.shader.name.length()) {
matName.Set(src.shader.texFile);
pcMat->AddProperty(&matName, AI_MATKEY_NAME);
2015-05-19 03:57:13 +00:00
}
// setup some more material properties that are specific to NFF2
int i;
2020-03-01 12:15:45 +00:00
if (src.shader.twoSided) {
2015-05-19 03:57:13 +00:00
i = 1;
2020-03-01 12:15:45 +00:00
pcMat->AddProperty(&i, 1, AI_MATKEY_TWOSIDED);
2015-05-19 03:57:13 +00:00
}
i = (src.shader.shaded ? aiShadingMode_Gouraud : aiShadingMode_NoShading);
2020-03-01 12:15:45 +00:00
if (src.shader.shininess) {
2015-05-19 03:57:13 +00:00
i = aiShadingMode_Phong;
2020-03-01 12:15:45 +00:00
pcMat->AddProperty(&src.shader.shininess, 1, AI_MATKEY_SHININESS);
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
pcMat->AddProperty(&i, 1, AI_MATKEY_SHADING_MODEL);
2015-05-19 03:57:13 +00:00
}
pScene->mRootNode = root;
}
#endif // !! ASSIMP_BUILD_NO_NFF_IMPORTER