Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
CommonConvert.h
Go to the documentation of this file.
1/* Copyright (C) 2021 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_COMMONCONVERT
19#define INCLUDED_COMMONCONVERT
20
21#include <exception>
22#include <string>
23#include <memory>
24#include <vector>
25
26class FCDEntityInstance;
27class FCDSceneNode;
28class FCDSkinController;
29class FMMatrix44;
30
31class Skeleton;
32
33class ColladaException : public std::exception
34{
35public:
36 ColladaException(const std::string& msg) : msg(msg) { }
37 ~ColladaException() throw() { }
38 virtual const char* what() const throw() { return msg.c_str(); }
39private:
40 std::string msg;
41};
42
44{
45 virtual ~OutputCB() { }
46 virtual void operator() (const char* data, unsigned int length)=0;
47};
48
49/**
50 * Standard error handler - logs FCollada messages using Log(), and also
51 * maintains a list of XML parser errors.
52 */
54{
55public:
56 FColladaErrorHandler(std::string& xmlErrors);
58
59private:
60 void OnError(FUError::Level errorLevel, uint32 errorCode, uint32 lineNumber);
61 std::string& xmlErrors;
62
64};
65
66/**
67 * Standard document loader. Based on FCDocument::LoadFromText, but allows
68 * access to <extra> nodes at the document level (i.e. directly in <COLLADA>).
69 */
71{
72public:
73 /**
74 * Loads the document from the given XML string. Should be the first function
75 * called on this object, and should only be called once.
76 * @throws ColladaException if unable to load.
77 */
78 void LoadFromText(const char* text);
79
80 /** Returns the FCDocument that was loaded. */
81 FCDocument* GetDocument() const { return document.get(); }
82
83 /** Returns the <extra> data from the <COLLADA> element. */
84 FCDExtra* GetExtra() const { return extra.get(); }
85
86private:
87 void ReadExtras(xmlNode* colladaNode);
88 std::unique_ptr<FCDocument> document;
89 std::unique_ptr<FCDExtra> extra;
90};
91
92/**
93 * Wrapper for code shared between the PMD and PSA converters. Loads the document
94 * and provides access to the relevant objects and values.
95 */
97{
98public:
99 CommonConvert(const char* text, std::string& xmlErrors);
101 const FColladaDocument& GetDocument() const { return m_Doc; }
102 FCDSceneNode& GetRoot() { return *m_Doc.GetDocument()->GetVisualSceneRoot(); }
103 FCDEntityInstance& GetInstance() { return *m_Instance; }
104 const FMMatrix44& GetEntityTransform() const { return m_EntityTransform; }
105 bool IsYUp() const { return m_YUp; }
106 bool IsXSI() const { return m_IsXSI; }
107
108private:
111 FCDEntityInstance* m_Instance;
113 bool m_YUp;
115};
116
117/** Throws a ColladaException unless the value is true. */
118#define REQUIRE(value, message) require_(__LINE__, value, "Assertion not satisfied", "failed requirement \"" message "\"")
119
120/** Throws a ColladaException unless the status is successful. */
121#define REQUIRE_SUCCESS(status) require_(__LINE__, status, "FCollada error", "Line " STRINGIFY(__LINE__))
122#define STRINGIFY(x) #x
123
124void require_(int line, bool value, const char* type, const char* message);
125
126/** Outputs a structure, using sizeof to get the size. */
127template<typename T> void write(OutputCB& output, const T& data)
128{
129 output((char*)&data, sizeof(T));
130}
131
132/**
133 * Tries to find a single suitable entity instance in the scene. Fails if there
134 * are none, or if there are too many and it's not clear which one should
135 * be converted.
136 *
137 * @param node root scene node to search under
138 * @param instance output - the found entity instance (if any)
139 * @param transform - the world-space transform of the found entity
140 *
141 * @return true if one was found
142 */
143bool FindSingleInstance(FCDSceneNode* node, FCDEntityInstance*& instance, FMMatrix44& transform);
144
145/**
146 * Like FCDSkinController::ReduceInfluences but works correctly.
147 * Additionally, multiple influences for the same joint-vertex pair are
148 * collapsed into a single influence.
149 */
150void SkinReduceInfluences(FCDSkinController* skin, size_t maxInfluenceCount, float minimumWeight);
151
152/**
153 * Fixes some occasional problems with the skeleton root definitions in a
154 * controller. (In particular, it's needed for models exported from XSI.)
155 * Should be called before extracting any joint information from the controller.
156 */
157void FixSkeletonRoots(FCDControllerInstance& controllerInstance);
158
159/**
160 * Finds the skeleton definition which best matches the given controller.
161 * @throws ColladaException if none is found.
162 */
163const Skeleton& FindSkeleton(const FCDControllerInstance& controllerInstance);
164
165/** Bone pose data */
167{
168 float translation[3];
169 float orientation[4];
170};
171
172/**
173 * Performs the standard transformations on bones, applying a scale matrix and
174 * moving them into the game's coordinate space.
175 */
176void TransformBones(std::vector<BoneTransform>& bones, const FMMatrix44& scaleTransform, bool yUp);
177
178extern FMMatrix44 FMMatrix44_Identity;
179
180#endif // INCLUDED_COMMONCONVERT
void require_(int line, bool value, const char *type, const char *message)
Definition: CommonConvert.cpp:35
const Skeleton & FindSkeleton(const FCDControllerInstance &controllerInstance)
Finds the skeleton definition which best matches the given controller.
Definition: CommonConvert.cpp:382
void TransformBones(std::vector< BoneTransform > &bones, const FMMatrix44 &scaleTransform, bool yUp)
Performs the standard transformations on bones, applying a scale matrix and moving them into the game...
Definition: CommonConvert.cpp:398
void write(OutputCB &output, const T &data)
Outputs a structure, using sizeof to get the size.
Definition: CommonConvert.h:127
FMMatrix44 FMMatrix44_Identity
void SkinReduceInfluences(FCDSkinController *skin, size_t maxInfluenceCount, float minimumWeight)
Like FCDSkinController::ReduceInfluences but works correctly.
Definition: CommonConvert.cpp:304
bool FindSingleInstance(FCDSceneNode *node, FCDEntityInstance *&instance, FMMatrix44 &transform)
Tries to find a single suitable entity instance in the scene.
Definition: CommonConvert.cpp:266
void FixSkeletonRoots(FCDControllerInstance &controllerInstance)
Fixes some occasional problems with the skeleton root definitions in a controller.
Definition: CommonConvert.cpp:364
Definition: CommonConvert.h:34
ColladaException(const std::string &msg)
Definition: CommonConvert.h:36
~ColladaException()
Definition: CommonConvert.h:37
std::string msg
Definition: CommonConvert.h:40
virtual const char * what() const
Definition: CommonConvert.h:38
Wrapper for code shared between the PMD and PSA converters.
Definition: CommonConvert.h:97
~CommonConvert()
Definition: CommonConvert.cpp:166
const FMMatrix44 & GetEntityTransform() const
Definition: CommonConvert.h:104
FCDEntityInstance * m_Instance
Definition: CommonConvert.h:111
bool m_YUp
Definition: CommonConvert.h:113
bool m_IsXSI
Definition: CommonConvert.h:114
FMMatrix44 m_EntityTransform
Definition: CommonConvert.h:112
FCDEntityInstance & GetInstance()
Definition: CommonConvert.h:103
FColladaDocument m_Doc
Definition: CommonConvert.h:110
bool IsXSI() const
Definition: CommonConvert.h:106
FCDSceneNode & GetRoot()
Definition: CommonConvert.h:102
const FColladaDocument & GetDocument() const
Definition: CommonConvert.h:101
bool IsYUp() const
Definition: CommonConvert.h:105
FColladaErrorHandler m_Err
Definition: CommonConvert.h:109
CommonConvert(const char *text, std::string &xmlErrors)
Definition: CommonConvert.cpp:139
Standard document loader.
Definition: CommonConvert.h:71
FCDocument * GetDocument() const
Returns the FCDocument that was loaded.
Definition: CommonConvert.h:81
std::unique_ptr< FCDExtra > extra
Definition: CommonConvert.h:89
std::unique_ptr< FCDocument > document
Definition: CommonConvert.h:88
void ReadExtras(xmlNode *colladaNode)
Definition: CommonConvert.cpp:114
FCDExtra * GetExtra() const
Returns the <extra> data from the <COLLADA> element.
Definition: CommonConvert.h:84
void LoadFromText(const char *text)
Loads the document from the given XML string.
Definition: CommonConvert.cpp:97
Standard error handler - logs FCollada messages using Log(), and also maintains a list of XML parser ...
Definition: CommonConvert.h:54
void OnError(FUError::Level errorLevel, uint32 errorCode, uint32 lineNumber)
Definition: CommonConvert.cpp:76
std::string & xmlErrors
Definition: CommonConvert.h:61
FColladaErrorHandler(std::string &xmlErrors)
Definition: CommonConvert.cpp:56
void operator=(FColladaErrorHandler)
~FColladaErrorHandler()
Definition: CommonConvert.cpp:67
Definition: StdSkeletons.h:51
#define T(string_literal)
Definition: secure_crt.cpp:77
Bone pose data.
Definition: CommonConvert.h:167
float translation[3]
Definition: CommonConvert.h:168
float orientation[4]
Definition: CommonConvert.h:169
Definition: CommonConvert.h:44
virtual void operator()(const char *data, unsigned int length)=0
virtual ~OutputCB()
Definition: CommonConvert.h:45