Torque3D/Engine/lib/assimp/code/AssetLib/Collada/ColladaParser.h

320 lines
12 KiB
C
Raw Normal View History

2022-04-26 11:56:24 -05:00
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------
2026-06-09 12:46:56 -05:00
Copyright (c) 2006-2026, assimp team
2022-04-26 11:56:24 -05:00
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------
*/
/** @file ColladaParser.h
* @brief Defines the parser helper class for the collada loader
*/
#pragma once
#ifndef AI_COLLADAPARSER_H_INC
#define AI_COLLADAPARSER_H_INC
#include "ColladaHelper.h"
#include <assimp/ai_assert.h>
#include <assimp/XmlParser.h>
#include <map>
namespace Assimp {
class ZipArchiveIOSystem;
// ------------------------------------------------------------------------------------------
/** Parser helper class for the Collada loader.
*
* Does all the XML reading and builds internal data structures from it,
* but leaves the resolving of all the references to the loader.
*/
class ColladaParser {
friend class ColladaLoader;
2026-06-09 12:46:56 -05:00
public:
/// Map for generic metadata as aiString.
using StringMetaData = std::map<std::string, aiString>;
2022-04-26 11:56:24 -05:00
2026-06-09 12:46:56 -05:00
/// Constructor from XML file.
2022-04-26 11:56:24 -05:00
ColladaParser(IOSystem *pIOHandler, const std::string &pFile);
2026-06-09 12:46:56 -05:00
/// Destructor
2022-04-26 11:56:24 -05:00
~ColladaParser();
2026-06-09 12:46:56 -05:00
/// Attempts to read the ZAE manifest and returns the DAE to open
2022-04-26 11:56:24 -05:00
static std::string ReadZaeManifest(ZipArchiveIOSystem &zip_archive);
2026-06-09 12:46:56 -05:00
/// Reads the contents of the file
2022-04-26 11:56:24 -05:00
void ReadContents(XmlNode &node);
2026-06-09 12:46:56 -05:00
/// Reads the structure of the file
2022-04-26 11:56:24 -05:00
void ReadStructure(XmlNode &node);
2026-06-09 12:46:56 -05:00
/// Reads asset information such as coordinate system information and legal blah
2022-04-26 11:56:24 -05:00
void ReadAssetInfo(XmlNode &node);
2026-06-09 12:46:56 -05:00
/// Reads contributor information such as author and legal blah
2022-04-26 11:56:24 -05:00
void ReadContributorInfo(XmlNode &node);
2026-06-09 12:46:56 -05:00
/// Reads the animation library
2022-04-26 11:56:24 -05:00
void ReadAnimationLibrary(XmlNode &node);
2026-06-09 12:46:56 -05:00
/// Reads the animation clip library
2022-04-26 11:56:24 -05:00
void ReadAnimationClipLibrary(XmlNode &node);
2026-06-09 12:46:56 -05:00
/// Unwrap controllers dependency hierarchy
2022-04-26 11:56:24 -05:00
void PostProcessControllers();
2026-06-09 12:46:56 -05:00
/// Re-build animations from animation clip library, if present, otherwise combine single-channel animations
2022-04-26 11:56:24 -05:00
void PostProcessRootAnimations();
2026-06-09 12:46:56 -05:00
/// Reads an animation into the given parent structure
2022-04-26 11:56:24 -05:00
void ReadAnimation(XmlNode &node, Collada::Animation *pParent);
2026-06-09 12:46:56 -05:00
/// Reads the skeleton controller library
2022-04-26 11:56:24 -05:00
void ReadControllerLibrary(XmlNode &node);
2026-06-09 12:46:56 -05:00
/// Reads a controller into the given mesh structure
2022-04-26 11:56:24 -05:00
void ReadController(XmlNode &node, Collada::Controller &pController);
2026-06-09 12:46:56 -05:00
/// Reads the image library contents
void ReadImageLibrary(const XmlNode &node);
2022-04-26 11:56:24 -05:00
2026-06-09 12:46:56 -05:00
/// Reads an image entry into the given image
void ReadImage(const XmlNode &node, Collada::Image &pImage) const;
2022-04-26 11:56:24 -05:00
2026-06-09 12:46:56 -05:00
/// Reads the material library
2022-04-26 11:56:24 -05:00
void ReadMaterialLibrary(XmlNode &node);
2026-06-09 12:46:56 -05:00
/// Reads the camera library
2022-04-26 11:56:24 -05:00
void ReadCameraLibrary(XmlNode &node);
2026-06-09 12:46:56 -05:00
/// Reads the light library
2022-04-26 11:56:24 -05:00
void ReadLightLibrary(XmlNode &node);
2026-06-09 12:46:56 -05:00
/// Reads the effect library
2022-04-26 11:56:24 -05:00
void ReadEffectLibrary(XmlNode &node);
2026-06-09 12:46:56 -05:00
/// Reads an effect entry into the given effect
2022-04-26 11:56:24 -05:00
void ReadEffect(XmlNode &node, Collada::Effect &pEffect);
2026-06-09 12:46:56 -05:00
/// Reads an COMMON effect profile
2022-04-26 11:56:24 -05:00
void ReadEffectProfileCommon(XmlNode &node, Collada::Effect &pEffect);
2026-06-09 12:46:56 -05:00
/// Read sampler properties
2022-04-26 11:56:24 -05:00
void ReadSamplerProperties(XmlNode &node, Collada::Sampler &pSampler);
2026-06-09 12:46:56 -05:00
/// Reads an effect entry containing a color or a texture defining that color
2022-04-26 11:56:24 -05:00
void ReadEffectColor(XmlNode &node, aiColor4D &pColor, Collada::Sampler &pSampler);
2026-06-09 12:46:56 -05:00
/// Reads an effect entry containing a float
2022-04-26 11:56:24 -05:00
void ReadEffectFloat(XmlNode &node, ai_real &pFloat);
2026-06-09 12:46:56 -05:00
/// Reads an effect parameter specification of any kind
2022-04-26 11:56:24 -05:00
void ReadEffectParam(XmlNode &node, Collada::EffectParam &pParam);
2026-06-09 12:46:56 -05:00
/// Reads the geometry library contents
2022-04-26 11:56:24 -05:00
void ReadGeometryLibrary(XmlNode &node);
2026-06-09 12:46:56 -05:00
/// Reads a geometry from the geometry library.
2022-04-26 11:56:24 -05:00
void ReadGeometry(XmlNode &node, Collada::Mesh &pMesh);
2026-06-09 12:46:56 -05:00
/// Reads a mesh from the geometry library
2022-04-26 11:56:24 -05:00
void ReadMesh(XmlNode &node, Collada::Mesh &pMesh);
2026-06-09 12:46:56 -05:00
/// Reads a source element - a combination of raw data and an accessor defining
///things that should not be definable. Yes, that's another rant.
2022-04-26 11:56:24 -05:00
void ReadSource(XmlNode &node);
2026-06-09 12:46:56 -05:00
/// Reads a data array holding a number of elements, and stores it in the global library.
/// Currently supported are array of floats and arrays of strings.
2022-04-26 11:56:24 -05:00
void ReadDataArray(XmlNode &node);
2026-06-09 12:46:56 -05:00
/// Reads an accessor and stores it in the global library under the given ID -
/// accessors use the ID of the parent <source> element
2022-04-26 11:56:24 -05:00
void ReadAccessor(XmlNode &node, const std::string &pID);
2026-06-09 12:46:56 -05:00
/// Reads input declarations of per-vertex mesh data into the given mesh
2022-04-26 11:56:24 -05:00
void ReadVertexData(XmlNode &node, Collada::Mesh &pMesh);
2026-06-09 12:46:56 -05:00
/// Reads input declarations of per-index mesh data into the given mesh
2022-04-26 11:56:24 -05:00
void ReadIndexData(XmlNode &node, Collada::Mesh &pMesh);
2026-06-09 12:46:56 -05:00
/// Reads a single input channel element and stores it in the given array, if valid
2022-04-26 11:56:24 -05:00
void ReadInputChannel(XmlNode &node, std::vector<Collada::InputChannel> &poChannels);
2026-06-09 12:46:56 -05:00
/// Reads a <p> primitive index list and assembles the mesh data into the given mesh
2022-04-26 11:56:24 -05:00
size_t ReadPrimitives(XmlNode &node, Collada::Mesh &pMesh, std::vector<Collada::InputChannel> &pPerIndexChannels,
size_t pNumPrimitives, const std::vector<size_t> &pVCount, Collada::PrimitiveType pPrimType);
2026-06-09 12:46:56 -05:00
/// Copies the data for a single primitive into the mesh, based on the InputChannels
2022-04-26 11:56:24 -05:00
void CopyVertex(size_t currentVertex, size_t numOffsets, size_t numPoints, size_t perVertexOffset,
Collada::Mesh &pMesh, std::vector<Collada::InputChannel> &pPerIndexChannels,
size_t currentPrimitive, const std::vector<size_t> &indices);
2026-06-09 12:46:56 -05:00
/// Reads one triangle of a tristrip into the mesh
2022-04-26 11:56:24 -05:00
void ReadPrimTriStrips(size_t numOffsets, size_t perVertexOffset, Collada::Mesh &pMesh,
std::vector<Collada::InputChannel> &pPerIndexChannels, size_t currentPrimitive, const std::vector<size_t> &indices);
2026-06-09 12:46:56 -05:00
/// Extracts a single object from an input channel and stores it in the appropriate mesh data array
2022-04-26 11:56:24 -05:00
void ExtractDataObjectFromChannel(const Collada::InputChannel &pInput, size_t pLocalIndex, Collada::Mesh &pMesh);
2026-06-09 12:46:56 -05:00
/// Reads the library of node hierarchies and scene parts
2022-04-26 11:56:24 -05:00
void ReadSceneLibrary(XmlNode &node);
2026-06-09 12:46:56 -05:00
/// Reads a scene node's contents including children and stores it in the given node
2022-04-26 11:56:24 -05:00
void ReadSceneNode(XmlNode &node, Collada::Node *pNode);
2026-06-09 12:46:56 -05:00
/// Reads a mesh reference in a node and adds it to the node's mesh list
2022-04-26 11:56:24 -05:00
void ReadNodeGeometry(XmlNode &node, Collada::Node *pNode);
2026-06-09 12:46:56 -05:00
/// Reads the collada scene
2022-04-26 11:56:24 -05:00
void ReadScene(XmlNode &node);
2026-06-09 12:46:56 -05:00
/// Processes bind_vertex_input and bind elements
2022-04-26 11:56:24 -05:00
void ReadMaterialVertexInputBinding(XmlNode &node, Collada::SemanticMappingTable &tbl);
2026-06-09 12:46:56 -05:00
/// Reads embedded textures from a ZAE archive
2022-04-26 11:56:24 -05:00
void ReadEmbeddedTextures(ZipArchiveIOSystem &zip_archive);
protected:
2026-06-09 12:46:56 -05:00
/// Converts a path read from a collada file to the usual representation
static void UriDecodePath(aiString &ss);
/// Calculates the resulting transformation from all the given transform steps
2022-04-26 11:56:24 -05:00
aiMatrix4x4 CalculateResultTransform(const std::vector<Collada::Transform> &pTransforms) const;
2026-06-09 12:46:56 -05:00
/// Determines the input data type for the given semantic string
2022-04-26 11:56:24 -05:00
Collada::InputType GetTypeForSemantic(const std::string &pSemantic);
2026-06-09 12:46:56 -05:00
/// Finds the item in the given library by its reference, throws if not found
2022-04-26 11:56:24 -05:00
template <typename Type>
const Type &ResolveLibraryReference(const std::map<std::string, Type> &pLibrary, const std::string &pURL) const;
2026-06-09 12:46:56 -05:00
private:
/// Filename, for a verbose error message
2022-04-26 11:56:24 -05:00
std::string mFileName;
2026-06-09 12:46:56 -05:00
/// XML reader, member for everyday use
2022-04-26 11:56:24 -05:00
XmlParser mXmlParser;
2026-06-09 12:46:56 -05:00
/// All data arrays found in the file by ID. Might be referred to by actually
/// everyone. Collada, you are a steaming pile of indirection.
2022-04-26 11:56:24 -05:00
using DataLibrary = std::map<std::string, Collada::Data> ;
DataLibrary mDataLibrary;
2026-06-09 12:46:56 -05:00
/// Same for accessors which define how the data in a data array is accessed.
2022-04-26 11:56:24 -05:00
using AccessorLibrary = std::map<std::string, Collada::Accessor> ;
AccessorLibrary mAccessorLibrary;
2026-06-09 12:46:56 -05:00
/// Mesh library: mesh by ID
2022-04-26 11:56:24 -05:00
using MeshLibrary = std::map<std::string, Collada::Mesh *>;
MeshLibrary mMeshLibrary;
2026-06-09 12:46:56 -05:00
/// node library: root node of the hierarchy part by ID
2022-04-26 11:56:24 -05:00
using NodeLibrary = std::map<std::string, Collada::Node *>;
NodeLibrary mNodeLibrary;
2026-06-09 12:46:56 -05:00
/// Image library: stores texture properties by ID
2022-04-26 11:56:24 -05:00
using ImageLibrary = std::map<std::string, Collada::Image> ;
ImageLibrary mImageLibrary;
2026-06-09 12:46:56 -05:00
/// Effect library: surface attributes by ID
2022-04-26 11:56:24 -05:00
using EffectLibrary = std::map<std::string, Collada::Effect> ;
EffectLibrary mEffectLibrary;
2026-06-09 12:46:56 -05:00
/// Material library: surface material by ID
2022-04-26 11:56:24 -05:00
using MaterialLibrary = std::map<std::string, Collada::Material> ;
MaterialLibrary mMaterialLibrary;
2026-06-09 12:46:56 -05:00
/// Light library: surface light by ID
2022-04-26 11:56:24 -05:00
using LightLibrary = std::map<std::string, Collada::Light> ;
LightLibrary mLightLibrary;
2026-06-09 12:46:56 -05:00
/// Camera library: surface material by ID
2022-04-26 11:56:24 -05:00
using CameraLibrary = std::map<std::string, Collada::Camera> ;
CameraLibrary mCameraLibrary;
2026-06-09 12:46:56 -05:00
/// Controller library: joint controllers by ID
2022-04-26 11:56:24 -05:00
using ControllerLibrary = std::map<std::string, Collada::Controller> ;
ControllerLibrary mControllerLibrary;
2026-06-09 12:46:56 -05:00
/// Animation library: animation references by ID
2022-04-26 11:56:24 -05:00
using AnimationLibrary = std::map<std::string, Collada::Animation *> ;
AnimationLibrary mAnimationLibrary;
2026-06-09 12:46:56 -05:00
/// Animation clip library: clip animation references by ID
2022-04-26 11:56:24 -05:00
using AnimationClipLibrary = std::vector<std::pair<std::string, std::vector<std::string>>> ;
AnimationClipLibrary mAnimationClipLibrary;
2026-06-09 12:46:56 -05:00
/// Pointer to the root node. Don't delete, it just points to one of the nodes in the node library.
2022-04-26 11:56:24 -05:00
Collada::Node *mRootNode;
2026-06-09 12:46:56 -05:00
/// Root animation container
2022-04-26 11:56:24 -05:00
Collada::Animation mAnims;
2026-06-09 12:46:56 -05:00
/// Size unit: how large compared to a meter
2022-04-26 11:56:24 -05:00
ai_real mUnitSize;
2026-06-09 12:46:56 -05:00
/// Which is the up vector
2022-04-26 11:56:24 -05:00
enum { UP_X,
UP_Y,
UP_Z } mUpDirection;
2026-06-09 12:46:56 -05:00
/// Asset metadata (global for scene)
2022-04-26 11:56:24 -05:00
StringMetaData mAssetMetaData;
2026-06-09 12:46:56 -05:00
/// Collada file format version
2022-04-26 11:56:24 -05:00
Collada::FormatVersion mFormat;
};
// ------------------------------------------------------------------------------------------------
// Finds the item in the given library by its reference, throws if not found
template <typename Type>
const Type &ColladaParser::ResolveLibraryReference(const std::map<std::string, Type> &pLibrary, const std::string &pURL) const {
typename std::map<std::string, Type>::const_iterator it = pLibrary.find(pURL);
if (it == pLibrary.end()) {
throw DeadlyImportError("Unable to resolve library reference \"", pURL, "\".");
}
return it->second;
}
} // end of namespace Assimp
#endif // AI_COLLADAPARSER_H_INC