Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
CommonConvert.h
Go to the documentation of this file.
1/* Copyright (C) 2025 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 <FCDocument/FCDExtra.h>
22#include <FCDocument/FCDocument.h>
23#include <FMath/FMMatrix44.h>
24#include <FUtils/FUError.h>
25#include <FUtils/Platforms.h>
26#include <cstddef>
27#include <exception>
28#include <libxml/tree.h>
29#include <memory>
30#include <string>
31#include <vector>
32
33class FCDControllerInstance;
34class FCDEntityInstance;
35class FCDSceneNode;
36class FCDSkinController;
37
38class Skeleton;
39
40class ColladaException : public std::exception
41{
42public:
43 ColladaException(const std::string& msg) : msg(msg) { }
44 ~ColladaException() throw() { }
45 virtual const char* what() const throw() { return msg.c_str(); }
46private:
47 std::string msg;
48};
49
51{
52 virtual ~OutputCB() { }
53 virtual void operator() (const char* data, unsigned int length)=0;
54};
55
56/**
57 * Standard error handler - logs FCollada messages using Log(), and also
58 * maintains a list of XML parser errors.
59 */
61{
62public:
63 FColladaErrorHandler(std::string& xmlErrors);
65
66private:
67 void OnError(FUError::Level errorLevel, uint32 errorCode, uint32 lineNumber);
68 std::string& xmlErrors;
69
71};
72
73/**
74 * Standard document loader. Based on FCDocument::LoadFromText, but allows
75 * access to <extra> nodes at the document level (i.e. directly in <COLLADA>).
76 */
78{
79public:
80 /**
81 * Loads the document from the given XML string. Should be the first function
82 * called on this object, and should only be called once.
83 * @throws ColladaException if unable to load.
84 */
85 void LoadFromText(const char* text);
86
87 /** Returns the FCDocument that was loaded. */
88 FCDocument* GetDocument() const { return document.get(); }
89
90 /** Returns the <extra> data from the <COLLADA> element. */
91 FCDExtra* GetExtra() const { return extra.get(); }
92
93private:
94 void ReadExtras(xmlNode* colladaNode);
95 std::unique_ptr<FCDocument> document;
96 std::unique_ptr<FCDExtra> extra;
97};
98
99/**
100 * Wrapper for code shared between the PMD and PSA converters. Loads the document
101 * and provides access to the relevant objects and values.
102 */
104{
105public:
106 CommonConvert(const char* text, std::string& xmlErrors);
108 const FColladaDocument& GetDocument() const { return m_Doc; }
109 FCDSceneNode& GetRoot() { return *m_Doc.GetDocument()->GetVisualSceneRoot(); }
110 FCDEntityInstance& GetInstance() { return *m_Instance; }
111 const FMMatrix44& GetEntityTransform() const { return m_EntityTransform; }
112 bool IsYUp() const { return m_YUp; }
113 bool IsXSI() const { return m_IsXSI; }
114
115private:
118 FCDEntityInstance* m_Instance;
120 bool m_YUp;
122};
123
124/** Throws a ColladaException unless the value is true. */
125#define REQUIRE(value, message) require_(__LINE__, value, "Assertion not satisfied", "failed requirement \"" message "\"")
126
127/** Throws a ColladaException unless the status is successful. */
128#define REQUIRE_SUCCESS(status) require_(__LINE__, status, "FCollada error", "Line " STRINGIFY(__LINE__))
129#define STRINGIFY(x) #x
130
131void require_(int line, bool value, const char* type, const char* message);
132
133/** Outputs a structure, using sizeof to get the size. */
134template<typename T> void write(OutputCB& output, const T& data)
135{
136 output((char*)&data, sizeof(T));
137}
138
139/**
140 * Tries to find a single suitable entity instance in the scene. Fails if there
141 * are none, or if there are too many and it's not clear which one should
142 * be converted.
143 *
144 * @param node root scene node to search under
145 * @param instance output - the found entity instance (if any)
146 * @param transform - the world-space transform of the found entity
147 *
148 * @return true if one was found
149 */
150bool FindSingleInstance(FCDSceneNode* node, FCDEntityInstance*& instance, FMMatrix44& transform);
151
152/**
153 * Like FCDSkinController::ReduceInfluences but works correctly.
154 * Additionally, multiple influences for the same joint-vertex pair are
155 * collapsed into a single influence.
156 */
157void SkinReduceInfluences(FCDSkinController* skin, size_t maxInfluenceCount, float minimumWeight);
158
159/**
160 * Fixes some occasional problems with the skeleton root definitions in a
161 * controller. (In particular, it's needed for models exported from XSI.)
162 * Should be called before extracting any joint information from the controller.
163 */
164void FixSkeletonRoots(FCDControllerInstance& controllerInstance);
165
166/**
167 * Finds the skeleton definition which best matches the given controller.
168 * @throws ColladaException if none is found.
169 */
170const Skeleton& FindSkeleton(const FCDControllerInstance& controllerInstance);
171
172/** Bone pose data */
174{
175 float translation[3];
176 float orientation[4];
177};
178
179/**
180 * Performs the standard transformations on bones, applying a scale matrix and
181 * moving them into the game's coordinate space.
182 */
183void TransformBones(std::vector<BoneTransform>& bones, const FMMatrix44& scaleTransform, bool yUp);
184
185extern FMMatrix44 FMMatrix44_Identity;
186
187#endif // INCLUDED_COMMONCONVERT
void require_(int line, bool value, const char *type, const char *message)
Definition: CommonConvert.cpp:43
const Skeleton & FindSkeleton(const FCDControllerInstance &controllerInstance)
Finds the skeleton definition which best matches the given controller.
Definition: CommonConvert.cpp:390
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:406
void write(OutputCB &output, const T &data)
Outputs a structure, using sizeof to get the size.
Definition: CommonConvert.h:134
FMMatrix44 FMMatrix44_Identity
void SkinReduceInfluences(FCDSkinController *skin, size_t maxInfluenceCount, float minimumWeight)
Like FCDSkinController::ReduceInfluences but works correctly.
Definition: CommonConvert.cpp:312
bool FindSingleInstance(FCDSceneNode *node, FCDEntityInstance *&instance, FMMatrix44 &transform)
Tries to find a single suitable entity instance in the scene.
Definition: CommonConvert.cpp:274
void FixSkeletonRoots(FCDControllerInstance &controllerInstance)
Fixes some occasional problems with the skeleton root definitions in a controller.
Definition: CommonConvert.cpp:372
Definition: CommonConvert.h:41
ColladaException(const std::string &msg)
Definition: CommonConvert.h:43
~ColladaException()
Definition: CommonConvert.h:44
std::string msg
Definition: CommonConvert.h:47
virtual const char * what() const
Definition: CommonConvert.h:45
Wrapper for code shared between the PMD and PSA converters.
Definition: CommonConvert.h:104
~CommonConvert()
Definition: CommonConvert.cpp:174
const FMMatrix44 & GetEntityTransform() const
Definition: CommonConvert.h:111
FCDEntityInstance * m_Instance
Definition: CommonConvert.h:118
bool m_YUp
Definition: CommonConvert.h:120
bool m_IsXSI
Definition: CommonConvert.h:121
FMMatrix44 m_EntityTransform
Definition: CommonConvert.h:119
FCDEntityInstance & GetInstance()
Definition: CommonConvert.h:110
FColladaDocument m_Doc
Definition: CommonConvert.h:117
bool IsXSI() const
Definition: CommonConvert.h:113
FCDSceneNode & GetRoot()
Definition: CommonConvert.h:109
const FColladaDocument & GetDocument() const
Definition: CommonConvert.h:108
bool IsYUp() const
Definition: CommonConvert.h:112
FColladaErrorHandler m_Err
Definition: CommonConvert.h:116
CommonConvert(const char *text, std::string &xmlErrors)
Definition: CommonConvert.cpp:147
Standard document loader.
Definition: CommonConvert.h:78
FCDocument * GetDocument() const
Returns the FCDocument that was loaded.
Definition: CommonConvert.h:88
std::unique_ptr< FCDExtra > extra
Definition: CommonConvert.h:96
std::unique_ptr< FCDocument > document
Definition: CommonConvert.h:95
void ReadExtras(xmlNode *colladaNode)
Definition: CommonConvert.cpp:122
FCDExtra * GetExtra() const
Returns the <extra> data from the <COLLADA> element.
Definition: CommonConvert.h:91
void LoadFromText(const char *text)
Loads the document from the given XML string.
Definition: CommonConvert.cpp:105
Standard error handler - logs FCollada messages using Log(), and also maintains a list of XML parser ...
Definition: CommonConvert.h:61
void OnError(FUError::Level errorLevel, uint32 errorCode, uint32 lineNumber)
Definition: CommonConvert.cpp:84
std::string & xmlErrors
Definition: CommonConvert.h:68
FColladaErrorHandler(std::string &xmlErrors)
Definition: CommonConvert.cpp:64
void operator=(FColladaErrorHandler)
~FColladaErrorHandler()
Definition: CommonConvert.cpp:75
Definition: StdSkeletons.h:52
#define T(string_literal)
Definition: secure_crt.cpp:81
Bone pose data.
Definition: CommonConvert.h:174
float translation[3]
Definition: CommonConvert.h:175
float orientation[4]
Definition: CommonConvert.h:176
Definition: CommonConvert.h:51
virtual void operator()(const char *data, unsigned int length)=0
virtual ~OutputCB()
Definition: CommonConvert.h:52