XMl-Migration: Migration of IrrMesh.

pull/2966/head
Kim Kulling 2020-02-03 21:19:03 +01:00
parent 00ad892a49
commit c1fcee9c5a
4 changed files with 484 additions and 483 deletions

View File

@ -92,6 +92,7 @@ void AMFImporter::Clear() {
AMFImporter::~AMFImporter() { AMFImporter::~AMFImporter() {
if (mReader != nullptr) { if (mReader != nullptr) {
delete mReader; delete mReader;
mReader = nullptr;
} }
// Clear() is accounting if data already is deleted. So, just check again if all data is deleted. // Clear() is accounting if data already is deleted. So, just check again if all data is deleted.
@ -157,11 +158,11 @@ void AMFImporter::Throw_IncorrectAttr(const std::string &nodeName, const std::st
} }
void AMFImporter::Throw_IncorrectAttrValue(const std::string &nodeName, const std::string &pAttrName) { void AMFImporter::Throw_IncorrectAttrValue(const std::string &nodeName, const std::string &pAttrName) {
throw DeadlyImportError("Attribute \"" + pAttrName + "\" in node <" + std::string(mReader->getNodeName()) + "> has incorrect value."); throw DeadlyImportError("Attribute \"" + pAttrName + "\" in node <" + nodeName + "> has incorrect value.");
} }
void AMFImporter::Throw_MoreThanOnceDefined(const std::string &pNodeType, const std::string &pDescription) { void AMFImporter::Throw_MoreThanOnceDefined(const std::string &nodeType, const std::string &nodeName, const std::string &pDescription) {
throw DeadlyImportError("\"" + pNodeType + "\" node can be used only once in " + mReader->getNodeName() + ". Description: " + pDescription); throw DeadlyImportError("\"" + nodeType + "\" node can be used only once in " + nodeName + ". Description: " + pDescription);
} }
void AMFImporter::Throw_ID_NotFound(const std::string &pID) const { void AMFImporter::Throw_ID_NotFound(const std::string &pID) const {
@ -172,11 +173,14 @@ void AMFImporter::Throw_ID_NotFound(const std::string &pID) const {
/************************************************************* Functions: XML set ************************************************************/ /************************************************************* Functions: XML set ************************************************************/
/*********************************************************************************************************************************************/ /*********************************************************************************************************************************************/
void AMFImporter::XML_CheckNode_MustHaveChildren() { void AMFImporter::XML_CheckNode_MustHaveChildren( XmlNode *node ) {
if (mReader->isEmptyElement()) throw DeadlyImportError(std::string("Node <") + mReader->getNodeName() + "> must have children."); //if (mReader->isEmptyElement()) throw DeadlyImportError(std::string("Node <") + mReader->getNodeName() + "> must have children.");
if (node->getNode()->children().begin() == node->getNode()->children().end()) {
throw DeadlyImportError(std::string("Node <") + std::string(node->getNode()->name()) + "> must have children.");
}
} }
void AMFImporter::XML_CheckNode_SkipUnsupported(const std::string &pParentNodeName) { /*void AMFImporter::XML_CheckNode_SkipUnsupported(XmlNode *node, const std::string &pParentNodeName) {
static const size_t Uns_Skip_Len = 3; static const size_t Uns_Skip_Len = 3;
const char *Uns_Skip[Uns_Skip_Len] = { "composite", "edge", "normal" }; const char *Uns_Skip[Uns_Skip_Len] = { "composite", "edge", "normal" };
@ -216,9 +220,10 @@ casu_cres:
ASSIMP_LOG_WARN_F("Skipping node \"", nn, "\" in ", pParentNodeName, "."); ASSIMP_LOG_WARN_F("Skipping node \"", nn, "\" in ", pParentNodeName, ".");
} }
} }
*/
bool AMFImporter::XML_SearchNode(const std::string &pNodeName) { bool AMFImporter::XML_SearchNode(const std::string &pNodeName) {
mReader->while (mReader->read()) {
mReader->while (mReader->read()) {
//if((mReader->getNodeType() == irr::io::EXN_ELEMENT) && XML_CheckNode_NameEqual(pNodeName)) return true; //if((mReader->getNodeType() == irr::io::EXN_ELEMENT) && XML_CheckNode_NameEqual(pNodeName)) return true;
if ((mReader->getNodeType() == pugi::node_element) && XML_CheckNode_NameEqual(pNodeName)) { if ((mReader->getNodeType() == pugi::node_element) && XML_CheckNode_NameEqual(pNodeName)) {
return true; return true;
@ -594,7 +599,7 @@ void AMFImporter::ParseNode_Object(XmlNode *nodeInst) {
for (pugi::xml_attribute_iterator ait = node->attributes_begin(); ait != node->attributes_end(); ++ait) { for (pugi::xml_attribute_iterator ait = node->attributes_begin(); ait != node->attributes_end(); ++ait) {
if (ait->name() == "id") { if (ait->name() == "id") {
id = ait->as_string(); id = ait->as_string();
} }
} }
// Read attributes for node <object>. // Read attributes for node <object>.
/*MACRO_ATTRREAD_LOOPBEG; /*MACRO_ATTRREAD_LOOPBEG;
@ -612,11 +617,13 @@ void AMFImporter::ParseNode_Object(XmlNode *nodeInst) {
// Check for child nodes // Check for child nodes
for (pugi::xml_node_iterator it = node->children().begin(); it != node->children->end(); ++it) { for (pugi::xml_node_iterator it = node->children().begin(); it != node->children->end(); ++it) {
bool col_read = false; bool col_read = false;
if (it->name() == "mesh") { if (it->name() == "mesh") {
ParseNode_Mesh( it ); ParseNode_Mesh(*it);
} } else if (it->name() == "metadata") {
ParseNode_Metadata(*it);
}
} }
if (!mReader->isEmptyElement()) { if (!mReader->isEmptyElement()) {
bool col_read = false; bool col_read = false;

View File

@ -272,15 +272,15 @@ private:
/// \throw DeadlyImportError. /// \throw DeadlyImportError.
/// \param [in] pNodeType - type of node which defined one more time. /// \param [in] pNodeType - type of node which defined one more time.
/// \param [in] pDescription - message about error. E.g. what the node defined while exception raised. /// \param [in] pDescription - message about error. E.g. what the node defined while exception raised.
void Throw_MoreThanOnceDefined(const std::string& pNodeType, const std::string& pDescription); void Throw_MoreThanOnceDefined(const std::string &nodeType, const std::string &nodeName, const std::string &pDescription);
/// Call that function when referenced element ID are not found in graph and exception must be raised. /// Call that function when referenced element ID are not found in graph and exception must be raised.
/// \param [in] pID - ID of of element which not found. /// \param [in] pID - ID of of element which not found.
/// \throw DeadlyImportError. /// \throw DeadlyImportError.
void Throw_ID_NotFound(const std::string& pID) const; void Throw_ID_NotFound(const std::string& pID) const;
/// Check if current node have children: <node>...</node>. If not then exception will throwed. /// Check if current node have children: <node>...</node>. If not then exception will thrown.
void XML_CheckNode_MustHaveChildren(); void XML_CheckNode_MustHaveChildren(XmlNode *node);
/// Check if current node name is equal to pNodeName. /// Check if current node name is equal to pNodeName.
/// \param [in] pNodeName - name for checking. /// \param [in] pNodeName - name for checking.
@ -292,7 +292,7 @@ private:
/// Skip unsupported node and report about that. Depend on node name can be skipped begin tag of node all whole node. /// Skip unsupported node and report about that. Depend on node name can be skipped begin tag of node all whole node.
/// \param [in] pParentNodeName - parent node name. Used for reporting. /// \param [in] pParentNodeName - parent node name. Used for reporting.
void XML_CheckNode_SkipUnsupported(const std::string& pParentNodeName); //void XML_CheckNode_SkipUnsupported(XmlNode *node, const std::string &pParentNodeName);
/// Search for specified node in file. XML file read pointer(mReader) will point to found node or file end after search is end. /// Search for specified node in file. XML file read pointer(mReader) will point to found node or file end after search is end.
/// \param [in] pNodeName - requested node name. /// \param [in] pNodeName - requested node name.

View File

@ -43,493 +43,470 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/** @file Implementation of the IrrMesh importer class */ /** @file Implementation of the IrrMesh importer class */
#ifndef ASSIMP_BUILD_NO_IRRMESH_IMPORTER #ifndef ASSIMP_BUILD_NO_IRRMESH_IMPORTER
#include "IRRMeshLoader.h" #include "IRRMeshLoader.h"
#include <assimp/ParsingUtils.h> #include <assimp/ParsingUtils.h>
#include <assimp/fast_atof.h> #include <assimp/fast_atof.h>
#include <memory>
#include <assimp/IOSystem.hpp>
#include <assimp/mesh.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/material.h>
#include <assimp/scene.h>
#include <assimp/importerdesc.h> #include <assimp/importerdesc.h>
#include <assimp/material.h>
#include <assimp/mesh.h>
#include <assimp/scene.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/IOSystem.hpp>
#include <memory>
using namespace Assimp; using namespace Assimp;
using namespace irr;
using namespace irr::io;
static const aiImporterDesc desc = { static const aiImporterDesc desc = {
"Irrlicht Mesh Reader", "Irrlicht Mesh Reader",
"", "",
"", "",
"http://irrlicht.sourceforge.net/", "http://irrlicht.sourceforge.net/",
aiImporterFlags_SupportTextFlavour, aiImporterFlags_SupportTextFlavour,
0, 0,
0, 0,
0, 0,
0, 0,
"xml irrmesh" "xml irrmesh"
}; };
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Constructor to be privately used by Importer // Constructor to be privately used by Importer
IRRMeshImporter::IRRMeshImporter() IRRMeshImporter::IRRMeshImporter() {}
{}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Destructor, private as well // Destructor, private as well
IRRMeshImporter::~IRRMeshImporter() IRRMeshImporter::~IRRMeshImporter() {}
{}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Returns whether the class can handle the format of the given file. // Returns whether the class can handle the format of the given file.
bool IRRMeshImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const bool IRRMeshImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const {
{ /* NOTE: A simple check for the file extension is not enough
/* NOTE: A simple check for the file extension is not enough
* here. Irrmesh and irr are easy, but xml is too generic * here. Irrmesh and irr are easy, but xml is too generic
* and could be collada, too. So we need to open the file and * and could be collada, too. So we need to open the file and
* search for typical tokens. * search for typical tokens.
*/ */
const std::string extension = GetExtension(pFile); const std::string extension = GetExtension(pFile);
if (extension == "irrmesh")return true; if (extension == "irrmesh")
else if (extension == "xml" || checkSig) return true;
{ else if (extension == "xml" || checkSig) {
/* If CanRead() is called to check whether the loader /* If CanRead() is called to check whether the loader
* supports a specific file extension in general we * supports a specific file extension in general we
* must return true here. * must return true here.
*/ */
if (!pIOHandler)return true; if (!pIOHandler) return true;
const char* tokens[] = {"irrmesh"}; const char *tokens[] = { "irrmesh" };
return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1); return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 1);
} }
return false; return false;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Get a list of all file extensions which are handled by this class // Get a list of all file extensions which are handled by this class
const aiImporterDesc* IRRMeshImporter::GetInfo () const const aiImporterDesc *IRRMeshImporter::GetInfo() const {
{ return &desc;
return &desc;
} }
static void releaseMaterial( aiMaterial **mat ) { static void releaseMaterial(aiMaterial **mat) {
if(*mat!= nullptr) { if (*mat != nullptr) {
delete *mat; delete *mat;
*mat = nullptr; *mat = nullptr;
} }
} }
static void releaseMesh( aiMesh **mesh ) { static void releaseMesh(aiMesh **mesh) {
if (*mesh != nullptr){ if (*mesh != nullptr) {
delete *mesh; delete *mesh;
*mesh = nullptr; *mesh = nullptr;
} }
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Imports the given file into the given scene structure. // Imports the given file into the given scene structure.
void IRRMeshImporter::InternReadFile( const std::string& pFile, void IRRMeshImporter::InternReadFile(const std::string &pFile,
aiScene* pScene, IOSystem* pIOHandler) aiScene *pScene, IOSystem *pIOHandler) {
{ std::unique_ptr<IOStream> file(pIOHandler->Open(pFile));
std::unique_ptr<IOStream> file( pIOHandler->Open( pFile));
// Check whether we can read from the file
// Check whether we can read from the file if (file.get() == NULL)
if( file.get() == NULL) throw DeadlyImportError("Failed to open IRRMESH file " + pFile + "");
throw DeadlyImportError( "Failed to open IRRMESH file " + pFile + "");
// Construct the irrXML parser
// Construct the irrXML parser XmlParser parser;
CIrrXML_IOStreamReader st(file.get()); pugi::xml_node *root = parser.parse(file.get());
reader = createIrrXMLReader((IFileReadCallBack*) &st); /*CIrrXML_IOStreamReader st(file.get());
reader = createIrrXMLReader((IFileReadCallBack*) &st);*/
// final data
std::vector<aiMaterial*> materials; // final data
std::vector<aiMesh*> meshes; std::vector<aiMaterial *> materials;
materials.reserve (5); std::vector<aiMesh *> meshes;
meshes.reserve(5); materials.reserve(5);
meshes.reserve(5);
// temporary data - current mesh buffer
aiMaterial* curMat = nullptr; // temporary data - current mesh buffer
aiMesh* curMesh = nullptr; aiMaterial *curMat = nullptr;
unsigned int curMatFlags = 0; aiMesh *curMesh = nullptr;
unsigned int curMatFlags = 0;
std::vector<aiVector3D> curVertices,curNormals,curTangents,curBitangents;
std::vector<aiColor4D> curColors; std::vector<aiVector3D> curVertices, curNormals, curTangents, curBitangents;
std::vector<aiVector3D> curUVs,curUV2s; std::vector<aiColor4D> curColors;
std::vector<aiVector3D> curUVs, curUV2s;
// some temporary variables
int textMeaning = 0; // some temporary variables
int vertexFormat = 0; // 0 = normal; 1 = 2 tcoords, 2 = tangents int textMeaning = 0;
bool useColors = false; int vertexFormat = 0; // 0 = normal; 1 = 2 tcoords, 2 = tangents
bool useColors = false;
// Parse the XML file
while (reader->read()) { // Parse the XML file
switch (reader->getNodeType()) { for (pugi::xml_node child : root->children()) {
case EXN_ELEMENT: if (child.type() == pugi::node_element) {
if (!ASSIMP_stricmp(child.name(), "buffer") && (curMat || curMesh)) {
if (!ASSIMP_stricmp(reader->getNodeName(),"buffer") && (curMat || curMesh)) { // end of previous buffer. A material and a mesh should be there
// end of previous buffer. A material and a mesh should be there if (!curMat || !curMesh) {
if ( !curMat || !curMesh) { ASSIMP_LOG_ERROR("IRRMESH: A buffer must contain a mesh and a material");
ASSIMP_LOG_ERROR("IRRMESH: A buffer must contain a mesh and a material"); releaseMaterial(&curMat);
releaseMaterial( &curMat ); releaseMesh(&curMesh);
releaseMesh( &curMesh ); } else {
} else { materials.push_back(curMat);
materials.push_back(curMat); meshes.push_back(curMesh);
meshes.push_back(curMesh); }
} curMat = nullptr;
curMat = nullptr; curMesh = nullptr;
curMesh = nullptr;
curVertices.clear();
curVertices.clear(); curColors.clear();
curColors.clear(); curNormals.clear();
curNormals.clear(); curUV2s.clear();
curUV2s.clear(); curUVs.clear();
curUVs.clear(); curTangents.clear();
curTangents.clear(); curBitangents.clear();
curBitangents.clear(); }
}
if (!ASSIMP_stricmp(child.name(), "material")) {
if (curMat) {
if (!ASSIMP_stricmp(reader->getNodeName(),"material")) { ASSIMP_LOG_WARN("IRRMESH: Only one material description per buffer, please");
if (curMat) { releaseMaterial(&curMat);
ASSIMP_LOG_WARN("IRRMESH: Only one material description per buffer, please"); }
releaseMaterial( &curMat ); curMat = ParseMaterial(curMatFlags);
} }
curMat = ParseMaterial(curMatFlags); /* no else here! */ if (!ASSIMP_stricmp(child.name(), "vertices")) {
} pugi::xml_attribute attr = child.attribute("vertexCount");
/* no else here! */ if (!ASSIMP_stricmp(reader->getNodeName(),"vertices")) int num = attr.as_int();
{ //int num = reader->getAttributeValueAsInt("vertexCount");
int num = reader->getAttributeValueAsInt("vertexCount");
if (!num) {
if (!num) { // This is possible ... remove the mesh from the list and skip further reading
// This is possible ... remove the mesh from the list and skip further reading ASSIMP_LOG_WARN("IRRMESH: Found mesh with zero vertices");
ASSIMP_LOG_WARN("IRRMESH: Found mesh with zero vertices");
releaseMaterial(&curMat);
releaseMaterial( &curMat ); releaseMesh(&curMesh);
releaseMesh( &curMesh ); textMeaning = 0;
textMeaning = 0; continue;
continue; }
}
curVertices.reserve(num);
curVertices.reserve(num); curNormals.reserve(num);
curNormals.reserve(num); curColors.reserve(num);
curColors.reserve(num); curUVs.reserve(num);
curUVs.reserve(num);
// Determine the file format
// Determine the file format //const char *t = reader->getAttributeValueSafe("type");
const char* t = reader->getAttributeValueSafe("type"); pugi::xml_attribute t = child.attribute("type");
if (!ASSIMP_stricmp("2tcoords", t)) { if (!ASSIMP_stricmp("2tcoords", t.name())) {
curUV2s.reserve (num); curUV2s.reserve(num);
vertexFormat = 1; vertexFormat = 1;
if (curMatFlags & AI_IRRMESH_EXTRA_2ND_TEXTURE) { if (curMatFlags & AI_IRRMESH_EXTRA_2ND_TEXTURE) {
// ********************************************************* // *********************************************************
// We have a second texture! So use this UV channel // We have a second texture! So use this UV channel
// for it. The 2nd texture can be either a normal // for it. The 2nd texture can be either a normal
// texture (solid_2layer or lightmap_xxx) or a normal // texture (solid_2layer or lightmap_xxx) or a normal
// map (normal_..., parallax_...) // map (normal_..., parallax_...)
// ********************************************************* // *********************************************************
int idx = 1; int idx = 1;
aiMaterial* mat = ( aiMaterial* ) curMat; aiMaterial *mat = (aiMaterial *)curMat;
if (curMatFlags & AI_IRRMESH_MAT_lightmap){ if (curMatFlags & AI_IRRMESH_MAT_lightmap) {
mat->AddProperty(&idx,1,AI_MATKEY_UVWSRC_LIGHTMAP(0)); mat->AddProperty(&idx, 1, AI_MATKEY_UVWSRC_LIGHTMAP(0));
} } else if (curMatFlags & AI_IRRMESH_MAT_normalmap_solid) {
else if (curMatFlags & AI_IRRMESH_MAT_normalmap_solid){ mat->AddProperty(&idx, 1, AI_MATKEY_UVWSRC_NORMALS(0));
mat->AddProperty(&idx,1,AI_MATKEY_UVWSRC_NORMALS(0)); } else if (curMatFlags & AI_IRRMESH_MAT_solid_2layer) {
} mat->AddProperty(&idx, 1, AI_MATKEY_UVWSRC_DIFFUSE(1));
else if (curMatFlags & AI_IRRMESH_MAT_solid_2layer) { }
mat->AddProperty(&idx,1,AI_MATKEY_UVWSRC_DIFFUSE(1)); }
} } else if (!ASSIMP_stricmp("tangents", t.name())) {
} curTangents.reserve(num);
} curBitangents.reserve(num);
else if (!ASSIMP_stricmp("tangents", t)) { vertexFormat = 2;
curTangents.reserve (num); } else if (ASSIMP_stricmp("standard", t.name())) {
curBitangents.reserve (num); releaseMaterial(&curMat);
vertexFormat = 2; ASSIMP_LOG_WARN("IRRMESH: Unknown vertex format");
} } else
else if (ASSIMP_stricmp("standard", t)) { vertexFormat = 0;
releaseMaterial( &curMat ); textMeaning = 1;
ASSIMP_LOG_WARN("IRRMESH: Unknown vertex format"); } else if (!ASSIMP_stricmp(child.name(), "indices")) {
} if (curVertices.empty() && curMat) {
else vertexFormat = 0; releaseMaterial(&curMat);
textMeaning = 1; throw DeadlyImportError("IRRMESH: indices must come after vertices");
} }
else if (!ASSIMP_stricmp(reader->getNodeName(),"indices")) {
if (curVertices.empty() && curMat) { textMeaning = 2;
releaseMaterial( &curMat );
throw DeadlyImportError("IRRMESH: indices must come after vertices"); // start a new mesh
} curMesh = new aiMesh();
textMeaning = 2; // allocate storage for all faces
pugi::xml_attribute attr = child.attribute("indexCount");
// start a new mesh curMesh->mNumVertices = attr.as_int();
curMesh = new aiMesh(); if (!curMesh->mNumVertices) {
// This is possible ... remove the mesh from the list and skip further reading
// allocate storage for all faces ASSIMP_LOG_WARN("IRRMESH: Found mesh with zero indices");
curMesh->mNumVertices = reader->getAttributeValueAsInt("indexCount");
if (!curMesh->mNumVertices) { // mesh - away
// This is possible ... remove the mesh from the list and skip further reading releaseMesh(&curMesh);
ASSIMP_LOG_WARN("IRRMESH: Found mesh with zero indices");
// material - away
// mesh - away releaseMaterial(&curMat);
releaseMesh( &curMesh );
textMeaning = 0;
// material - away continue;
releaseMaterial( &curMat ); }
textMeaning = 0; if (curMesh->mNumVertices % 3) {
continue; ASSIMP_LOG_WARN("IRRMESH: Number if indices isn't divisible by 3");
} }
if (curMesh->mNumVertices % 3) { curMesh->mNumFaces = curMesh->mNumVertices / 3;
ASSIMP_LOG_WARN("IRRMESH: Number if indices isn't divisible by 3"); curMesh->mFaces = new aiFace[curMesh->mNumFaces];
}
// setup some members
curMesh->mNumFaces = curMesh->mNumVertices / 3; curMesh->mMaterialIndex = (unsigned int)materials.size();
curMesh->mFaces = new aiFace[curMesh->mNumFaces]; curMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
// setup some members // allocate storage for all vertices
curMesh->mMaterialIndex = (unsigned int)materials.size(); curMesh->mVertices = new aiVector3D[curMesh->mNumVertices];
curMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
if (curNormals.size() == curVertices.size()) {
// allocate storage for all vertices curMesh->mNormals = new aiVector3D[curMesh->mNumVertices];
curMesh->mVertices = new aiVector3D[curMesh->mNumVertices]; }
if (curTangents.size() == curVertices.size()) {
if (curNormals.size() == curVertices.size()) { curMesh->mTangents = new aiVector3D[curMesh->mNumVertices];
curMesh->mNormals = new aiVector3D[curMesh->mNumVertices]; }
} if (curBitangents.size() == curVertices.size()) {
if (curTangents.size() == curVertices.size()) { curMesh->mBitangents = new aiVector3D[curMesh->mNumVertices];
curMesh->mTangents = new aiVector3D[curMesh->mNumVertices]; }
} if (curColors.size() == curVertices.size() && useColors) {
if (curBitangents.size() == curVertices.size()) { curMesh->mColors[0] = new aiColor4D[curMesh->mNumVertices];
curMesh->mBitangents = new aiVector3D[curMesh->mNumVertices]; }
} if (curUVs.size() == curVertices.size()) {
if (curColors.size() == curVertices.size() && useColors) { curMesh->mTextureCoords[0] = new aiVector3D[curMesh->mNumVertices];
curMesh->mColors[0] = new aiColor4D[curMesh->mNumVertices]; }
} if (curUV2s.size() == curVertices.size()) {
if (curUVs.size() == curVertices.size()) { curMesh->mTextureCoords[1] = new aiVector3D[curMesh->mNumVertices];
curMesh->mTextureCoords[0] = new aiVector3D[curMesh->mNumVertices]; }
} }
if (curUV2s.size() == curVertices.size()) { //break;
curMesh->mTextureCoords[1] = new aiVector3D[curMesh->mNumVertices];
} //case EXN_TEXT: {
} const char *sz = child.child_value();
break; if (textMeaning == 1) {
textMeaning = 0;
case EXN_TEXT:
{ // read vertices
const char* sz = reader->getNodeData(); do {
if (textMeaning == 1) { SkipSpacesAndLineEnd(&sz);
textMeaning = 0; aiVector3D temp;
aiColor4D c;
// read vertices
do { // Read the vertex position
SkipSpacesAndLineEnd(&sz); sz = fast_atoreal_move<float>(sz, (float &)temp.x);
aiVector3D temp;aiColor4D c; SkipSpaces(&sz);
// Read the vertex position sz = fast_atoreal_move<float>(sz, (float &)temp.y);
sz = fast_atoreal_move<float>(sz,(float&)temp.x); SkipSpaces(&sz);
SkipSpaces(&sz);
sz = fast_atoreal_move<float>(sz, (float &)temp.z);
sz = fast_atoreal_move<float>(sz,(float&)temp.y); SkipSpaces(&sz);
SkipSpaces(&sz); curVertices.push_back(temp);
sz = fast_atoreal_move<float>(sz,(float&)temp.z); // Read the vertex normals
SkipSpaces(&sz); sz = fast_atoreal_move<float>(sz, (float &)temp.x);
curVertices.push_back(temp); SkipSpaces(&sz);
// Read the vertex normals sz = fast_atoreal_move<float>(sz, (float &)temp.y);
sz = fast_atoreal_move<float>(sz,(float&)temp.x); SkipSpaces(&sz);
SkipSpaces(&sz);
sz = fast_atoreal_move<float>(sz, (float &)temp.z);
sz = fast_atoreal_move<float>(sz,(float&)temp.y); SkipSpaces(&sz);
SkipSpaces(&sz); curNormals.push_back(temp);
sz = fast_atoreal_move<float>(sz,(float&)temp.z); // read the vertex colors
SkipSpaces(&sz); uint32_t clr = strtoul16(sz, &sz);
curNormals.push_back(temp); ColorFromARGBPacked(clr, c);
// read the vertex colors if (!curColors.empty() && c != *(curColors.end() - 1))
uint32_t clr = strtoul16(sz,&sz); useColors = true;
ColorFromARGBPacked(clr,c);
curColors.push_back(c);
if (!curColors.empty() && c != *(curColors.end()-1)) SkipSpaces(&sz);
useColors = true;
// read the first UV coordinate set
curColors.push_back(c); sz = fast_atoreal_move<float>(sz, (float &)temp.x);
SkipSpaces(&sz); SkipSpaces(&sz);
sz = fast_atoreal_move<float>(sz, (float &)temp.y);
// read the first UV coordinate set SkipSpaces(&sz);
sz = fast_atoreal_move<float>(sz,(float&)temp.x); temp.z = 0.f;
SkipSpaces(&sz); temp.y = 1.f - temp.y; // DX to OGL
curUVs.push_back(temp);
sz = fast_atoreal_move<float>(sz,(float&)temp.y);
SkipSpaces(&sz); // read the (optional) second UV coordinate set
temp.z = 0.f; if (vertexFormat == 1) {
temp.y = 1.f - temp.y; // DX to OGL sz = fast_atoreal_move<float>(sz, (float &)temp.x);
curUVs.push_back(temp); SkipSpaces(&sz);
// read the (optional) second UV coordinate set sz = fast_atoreal_move<float>(sz, (float &)temp.y);
if (vertexFormat == 1) { temp.y = 1.f - temp.y; // DX to OGL
sz = fast_atoreal_move<float>(sz,(float&)temp.x); curUV2s.push_back(temp);
SkipSpaces(&sz); }
// read optional tangent and bitangent vectors
sz = fast_atoreal_move<float>(sz,(float&)temp.y); else if (vertexFormat == 2) {
temp.y = 1.f - temp.y; // DX to OGL // tangents
curUV2s.push_back(temp); sz = fast_atoreal_move<float>(sz, (float &)temp.x);
} SkipSpaces(&sz);
// read optional tangent and bitangent vectors
else if (vertexFormat == 2) { sz = fast_atoreal_move<float>(sz, (float &)temp.z);
// tangents SkipSpaces(&sz);
sz = fast_atoreal_move<float>(sz,(float&)temp.x);
SkipSpaces(&sz); sz = fast_atoreal_move<float>(sz, (float &)temp.y);
SkipSpaces(&sz);
sz = fast_atoreal_move<float>(sz,(float&)temp.z); temp.y *= -1.0f;
SkipSpaces(&sz); curTangents.push_back(temp);
sz = fast_atoreal_move<float>(sz,(float&)temp.y); // bitangents
SkipSpaces(&sz); sz = fast_atoreal_move<float>(sz, (float &)temp.x);
temp.y *= -1.0f; SkipSpaces(&sz);
curTangents.push_back(temp);
sz = fast_atoreal_move<float>(sz, (float &)temp.z);
// bitangents SkipSpaces(&sz);
sz = fast_atoreal_move<float>(sz,(float&)temp.x);
SkipSpaces(&sz); sz = fast_atoreal_move<float>(sz, (float &)temp.y);
SkipSpaces(&sz);
sz = fast_atoreal_move<float>(sz,(float&)temp.z); temp.y *= -1.0f;
SkipSpaces(&sz); curBitangents.push_back(temp);
}
sz = fast_atoreal_move<float>(sz,(float&)temp.y); }
SkipSpaces(&sz);
temp.y *= -1.0f; /* IMPORTANT: We assume that each vertex is specified in one
curBitangents.push_back(temp); line. So we can skip the rest of the line - unknown vertex
} elements are ignored.
} */
/* IMPORTANT: We assume that each vertex is specified in one while (SkipLine(&sz));
line. So we can skip the rest of the line - unknown vertex } else if (textMeaning == 2) {
elements are ignored. textMeaning = 0;
*/
// read indices
while (SkipLine(&sz)); aiFace *curFace = curMesh->mFaces;
} aiFace *const faceEnd = curMesh->mFaces + curMesh->mNumFaces;
else if (textMeaning == 2) {
textMeaning = 0; aiVector3D *pcV = curMesh->mVertices;
aiVector3D *pcN = curMesh->mNormals;
// read indices aiVector3D *pcT = curMesh->mTangents;
aiFace* curFace = curMesh->mFaces; aiVector3D *pcB = curMesh->mBitangents;
aiFace* const faceEnd = curMesh->mFaces + curMesh->mNumFaces; aiColor4D *pcC0 = curMesh->mColors[0];
aiVector3D *pcT0 = curMesh->mTextureCoords[0];
aiVector3D* pcV = curMesh->mVertices; aiVector3D *pcT1 = curMesh->mTextureCoords[1];
aiVector3D* pcN = curMesh->mNormals;
aiVector3D* pcT = curMesh->mTangents; unsigned int curIdx = 0;
aiVector3D* pcB = curMesh->mBitangents; unsigned int total = 0;
aiColor4D* pcC0 = curMesh->mColors[0]; while (SkipSpacesAndLineEnd(&sz)) {
aiVector3D* pcT0 = curMesh->mTextureCoords[0]; if (curFace >= faceEnd) {
aiVector3D* pcT1 = curMesh->mTextureCoords[1]; ASSIMP_LOG_ERROR("IRRMESH: Too many indices");
break;
unsigned int curIdx = 0; }
unsigned int total = 0; if (!curIdx) {
while(SkipSpacesAndLineEnd(&sz)) { curFace->mNumIndices = 3;
if (curFace >= faceEnd) { curFace->mIndices = new unsigned int[3];
ASSIMP_LOG_ERROR("IRRMESH: Too many indices"); }
break;
} unsigned int idx = strtoul10(sz, &sz);
if (!curIdx) { if (idx >= curVertices.size()) {
curFace->mNumIndices = 3; ASSIMP_LOG_ERROR("IRRMESH: Index out of range");
curFace->mIndices = new unsigned int[3]; idx = 0;
} }
unsigned int idx = strtoul10(sz,&sz); curFace->mIndices[curIdx] = total++;
if (idx >= curVertices.size()) {
ASSIMP_LOG_ERROR("IRRMESH: Index out of range"); *pcV++ = curVertices[idx];
idx = 0; if (pcN) *pcN++ = curNormals[idx];
} if (pcT) *pcT++ = curTangents[idx];
if (pcB) *pcB++ = curBitangents[idx];
curFace->mIndices[curIdx] = total++; if (pcC0) *pcC0++ = curColors[idx];
if (pcT0) *pcT0++ = curUVs[idx];
*pcV++ = curVertices[idx]; if (pcT1) *pcT1++ = curUV2s[idx];
if (pcN)*pcN++ = curNormals[idx];
if (pcT)*pcT++ = curTangents[idx]; if (++curIdx == 3) {
if (pcB)*pcB++ = curBitangents[idx]; ++curFace;
if (pcC0)*pcC0++ = curColors[idx]; curIdx = 0;
if (pcT0)*pcT0++ = curUVs[idx]; }
if (pcT1)*pcT1++ = curUV2s[idx]; }
if (++curIdx == 3) { if (curFace != faceEnd)
++curFace; ASSIMP_LOG_ERROR("IRRMESH: Not enough indices");
curIdx = 0;
} // Finish processing the mesh - do some small material workarounds
} if (curMatFlags & AI_IRRMESH_MAT_trans_vertex_alpha && !useColors) {
// Take the opacity value of the current material
if (curFace != faceEnd) // from the common vertex color alpha
ASSIMP_LOG_ERROR("IRRMESH: Not enough indices"); aiMaterial *mat = (aiMaterial *)curMat;
mat->AddProperty(&curColors[0].a, 1, AI_MATKEY_OPACITY);
// Finish processing the mesh - do some small material workarounds }
if (curMatFlags & AI_IRRMESH_MAT_trans_vertex_alpha && !useColors) { }
// Take the opacity value of the current material }
// from the common vertex color alpha }
aiMaterial* mat = (aiMaterial*)curMat;
mat->AddProperty(&curColors[0].a,1,AI_MATKEY_OPACITY); // End of the last buffer. A material and a mesh should be there
} if (curMat || curMesh) {
}} if (!curMat || !curMesh) {
break; ASSIMP_LOG_ERROR("IRRMESH: A buffer must contain a mesh and a material");
releaseMaterial(&curMat);
default: releaseMesh(&curMesh);
// GCC complains here ... } else {
break; materials.push_back(curMat);
meshes.push_back(curMesh);
}; }
} }
// End of the last buffer. A material and a mesh should be there if (materials.empty()) {
if (curMat || curMesh) { throw DeadlyImportError("IRRMESH: Unable to read a mesh from this file");
if ( !curMat || !curMesh) { }
ASSIMP_LOG_ERROR("IRRMESH: A buffer must contain a mesh and a material");
releaseMaterial( &curMat ); // now generate the output scene
releaseMesh( &curMesh ); pScene->mNumMeshes = (unsigned int)meshes.size();
} pScene->mMeshes = new aiMesh *[pScene->mNumMeshes];
else { for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
materials.push_back(curMat); pScene->mMeshes[i] = meshes[i];
meshes.push_back(curMesh);
} // clean this value ...
} pScene->mMeshes[i]->mNumUVComponents[3] = 0;
}
if (materials.empty())
throw DeadlyImportError("IRRMESH: Unable to read a mesh from this file"); pScene->mNumMaterials = (unsigned int)materials.size();
pScene->mMaterials = new aiMaterial *[pScene->mNumMaterials];
::memcpy(pScene->mMaterials, &materials[0], sizeof(void *) * pScene->mNumMaterials);
// now generate the output scene
pScene->mNumMeshes = (unsigned int)meshes.size(); pScene->mRootNode = new aiNode();
pScene->mMeshes = new aiMesh*[pScene->mNumMeshes]; pScene->mRootNode->mName.Set("<IRRMesh>");
for (unsigned int i = 0; i < pScene->mNumMeshes;++i) { pScene->mRootNode->mNumMeshes = pScene->mNumMeshes;
pScene->mMeshes[i] = meshes[i]; pScene->mRootNode->mMeshes = new unsigned int[pScene->mNumMeshes];
// clean this value ... for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
pScene->mMeshes[i]->mNumUVComponents[3] = 0; pScene->mRootNode->mMeshes[i] = i;
} }
pScene->mNumMaterials = (unsigned int)materials.size();
pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
::memcpy(pScene->mMaterials,&materials[0],sizeof(void*)*pScene->mNumMaterials);
pScene->mRootNode = new aiNode();
pScene->mRootNode->mName.Set("<IRRMesh>");
pScene->mRootNode->mNumMeshes = pScene->mNumMeshes;
pScene->mRootNode->mMeshes = new unsigned int[pScene->mNumMeshes];
for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
pScene->mRootNode->mMeshes[i] = i;
// clean up and return
delete reader;
AI_DEBUG_INVALIDATE_PTR(reader);
} }
#endif // !! ASSIMP_BUILD_NO_IRRMESH_IMPORTER #endif // !! ASSIMP_BUILD_NO_IRRMESH_IMPORTER

View File

@ -141,43 +141,55 @@ private:
}; // ! class CIrrXML_IOStreamReader }; // ! class CIrrXML_IOStreamReader
*/ */
class XmlNode { struct find_node_by_name_predicate {
public: std::string mName;
XmlNode() find_node_by_name_predicate(const std::string &name) :
: mNode(nullptr){ mName(name) {
// empty
}
XmlNode(pugi::xml_node *node)
: mNode(node) {
// empty // empty
} }
pugi::xml_node *getNode() const { bool operator()(pugi::xml_node node) const {
return mNode; return node.name() == mName;
} }
private:
pugi::xml_node *mNode;
}; };
class XmlParser {
template<class TNodeType>
class TXmlParser {
public: public:
XmlParser() : TXmlParser() :
mDoc(nullptr), mRoot(nullptr), mData() { mDoc(nullptr), mRoot(nullptr), mData() {
// empty // empty
} }
~XmlParser() { ~TXmlParser() {
clear(); clear();
} }
void clear() { void clear() {
mData.resize(0); mData.resize(0);
mRoot = nullptr;
delete mDoc; delete mDoc;
mDoc = nullptr; mDoc = nullptr;
} }
XmlNode *parse(IOStream *stream) { TNodeType *findNode(const std::string &name) {
if (name.empty()) {
return nullptr;
}
if (nullptr == mDoc) {
return nullptr;
}
find_node_by_name_predicate predicate(name);
pugi::xml_node node = mDoc->find_node(predicate);
if (node.empty()) {
return nullptr;
}
}
TNodeType *parse(IOStream *stream) {
if (nullptr == stream) { if (nullptr == stream) {
return nullptr; return nullptr;
} }
@ -187,8 +199,7 @@ public:
mDoc = new pugi::xml_document(); mDoc = new pugi::xml_document();
pugi::xml_parse_result result = mDoc->load_string(&mData[0]); pugi::xml_parse_result result = mDoc->load_string(&mData[0]);
if (result.status == pugi::status_ok) { if (result.status == pugi::status_ok) {
pugi::xml_node *root = &mDoc->root(); mRoot = &mDoc->root();
mRoot = new XmlNode(root);
} }
return mRoot; return mRoot;
@ -198,12 +209,18 @@ public:
return mDoc; return mDoc;
} }
TNodeType *getRootNode() const {
return mRoot;
}
private: private:
pugi::xml_document *mDoc; pugi::xml_document *mDoc;
XmlNode *mRoot; TNodeType *mRoot;
std::vector<char> mData; std::vector<char> mData;
}; };
using XmlParser = TXmlParser<pugi::xml_node>;
} // namespace Assimp } // namespace Assimp
#endif // !! INCLUDED_AI_IRRXML_WRAPPER #endif // !! INCLUDED_AI_IRRXML_WRAPPER