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