OpenClonk
StdMeshLoader.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
5  *
6  * Distributed under the terms of the ISC license; see accompanying file
7  * "COPYING" for details.
8  *
9  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10  * See accompanying file "TRADEMARK" for details.
11  *
12  * To redistribute this file separately, substitute the full license texts
13  * for the above references.
14  */
15 
16 // A loader for the OGRE .mesh binary file format
17 #ifndef INC_StdMeshLoader
18 #define INC_StdMeshLoader
19 
20 #include "lib/StdMesh.h"
21 
22 namespace OgreToClonk
23 {
31 }
32 
34 {
35 public:
36  class StdMeshXML;
37  class LoaderException : public std::runtime_error { public: LoaderException(const char *msg) : std::runtime_error(msg) {} };
38 
39  // filename is only used to show it in error messages. The model is
40  // loaded from src. Throws LoaderException.
41  static StdMesh *LoadMeshBinary(const char *sourcefile, size_t size, const StdMeshMatManager &mat_mgr, StdMeshSkeletonLoader &loader, const char *filename = nullptr);
42  static StdMesh *LoadMeshXml(const char *sourcefile, size_t size, const StdMeshMatManager &mat_mgr, StdMeshSkeletonLoader &loader, const char *filename = nullptr);
43 };
44 
45 // Interface to load skeleton files. Given a filename occuring in the
46 // mesh file, this should load the skeleton file from wherever the mesh file
47 // was loaded from, for example from a C4Group. Return default-construted
48 // StdStrBuf with nullptr data in case of error.
50 {
51 public:
52  virtual ~StdMeshSkeletonLoader() = default;
53 
54  void StoreSkeleton(const char* groupname, const char* filename, std::shared_ptr<StdMeshSkeleton> skeleton);
55  void RemoveSkeleton(const StdCopyStrBuf& filepath);
56  void RemoveSkeleton(const char* groupname, const char* filename);
57  void RemoveSkeletonsInGroup(const char* groupname);
58 
59  virtual StdMeshSkeleton* GetSkeletonByDefinition(const char* definition) const = 0;
60  std::shared_ptr<StdMeshSkeleton> GetSkeletonByName(const StdStrBuf& name) const;
61  void LoadSkeletonXml(const char* groupname, const char* filename, const char *sourcefile, size_t size);
62  void LoadSkeletonBinary(const char* groupname, const char* filename, const char *sourcefile, size_t size);
63 
65 
66  static void MakeFullSkeletonPath(StdCopyStrBuf &out, const char* groupname, const char* filename)
67  {
68  out = StdCopyStrBuf(groupname);
69  out.AppendBackslash();
70  out.Append(filename);
71  out.ToLowerCase();
72  }
73 
74  void Clear()
75  {
76  Skeletons.clear();
77  AppendtoSkeletons.clear();
78  IncludeSkeletons.clear();
79  }
80 
81  // Allows to iterate over loaded skeletons:
82  typedef std::map<StdCopyStrBuf, std::shared_ptr<StdMeshSkeleton>> SkeletonMap;
83  typedef SkeletonMap::const_iterator skeleton_iterator;
84 
85  skeleton_iterator skeletons_begin() const { return Skeletons.begin(); }
86  skeleton_iterator skeletons_end() const { return Skeletons.end(); }
87 
88 private:
89  void AddSkeleton(const StdCopyStrBuf& filepath, std::shared_ptr<StdMeshSkeleton> skeleton);
90  void DoResetSkeletons();
91  void DoAppendSkeletons();
92  void DoIncludeSkeletons();
93 
94  SkeletonMap Skeletons;
95 
96  // was a map <pointer to skeleton, id> first, but that lead to problems with reloading:
97  // the old skeleton was still in the map, because the new skeleton was a different object of course
98  // so instead of finding the old skeleton it seemed simpler to just save a key that can be overloaded
99 
100  std::map<StdCopyStrBuf, StdCopyStrBuf> AppendtoSkeletons; // key is the filepath, value is the id to append to
101  std::map<StdCopyStrBuf, StdCopyStrBuf> IncludeSkeletons; // key is the filepath, value is the definition to include
102 };
103 
104 #define DEFINE_EXCEPTION(_cls, _text) class _cls : public StdMeshLoader::LoaderException { public: _cls(const char *msg = _text) : LoaderException(msg) {} }
105 
106 namespace Ogre
107 {
108  DEFINE_EXCEPTION(InsufficientData, "Premature end of data stream");
109  DEFINE_EXCEPTION(MultipleSingletonChunks, "A singleton chunk was found multiple times");
110  namespace Mesh
111  {
112  DEFINE_EXCEPTION(InvalidVersion, "Mesh header does not contain the expected version");
113  DEFINE_EXCEPTION(SharedVertexGeometryForbidden, "A CID_Geometry chunk was found in a submesh with shared vertices");
114  DEFINE_EXCEPTION(InvalidSubmeshOp, "The render operation of a CID_Submesh_Op chunk was invalid");
115  DEFINE_EXCEPTION(InvalidVertexType, "The vertex type of a CID_Geometry_Vertex_Decl_Element chunk was invalid");
116  DEFINE_EXCEPTION(InvalidVertexSemantic, "The vertex semantic of a CID_Geometry_Vertex_Decl_Element chunk was invalid");
117  DEFINE_EXCEPTION(InvalidVertexDeclaration, "The vertex declaration of a CID_Geometry chunk was invalid");
118  DEFINE_EXCEPTION(InvalidMaterial, "The material referenced by a mesh or submesh is not defined");
119  DEFINE_EXCEPTION(VertexNotFound, "A specified vertex was not found");
120  DEFINE_EXCEPTION(EmptyBoundingBox, "Bounding box is empty");
121  DEFINE_EXCEPTION(NotImplemented, "The requested operation is not implemented");
122  }
123  namespace Skeleton
124  {
125  DEFINE_EXCEPTION(InvalidVersion, "Skeleton header does not contain the expected version");
126  DEFINE_EXCEPTION(IdNotUnique, "An element with an unique ID appeared multiple times");
127  DEFINE_EXCEPTION(BoneNotFound, "A specified bone was not found");
128  DEFINE_EXCEPTION(MissingMasterBone, "The skeleton does not have a master bone");
129  DEFINE_EXCEPTION(MultipleBoneTracks, "An animation has multiple tracks for one bone");
130  }
131 }
132 
133 #undef DEFINE_EXCEPTION
134 
135 #endif
LoaderException(const char *msg)
Definition: StdMeshLoader.h:37
static StdMesh * LoadMeshXml(const char *sourcefile, size_t size, const StdMeshMatManager &mat_mgr, StdMeshSkeletonLoader &loader, const char *filename=nullptr)
static StdMesh * LoadMeshBinary(const char *sourcefile, size_t size, const StdMeshMatManager &mat_mgr, StdMeshSkeletonLoader &loader, const char *filename=nullptr)
virtual StdMeshSkeleton * GetSkeletonByDefinition(const char *definition) const =0
void LoadSkeletonBinary(const char *groupname, const char *filename, const char *sourcefile, size_t size)
static void MakeFullSkeletonPath(StdCopyStrBuf &out, const char *groupname, const char *filename)
Definition: StdMeshLoader.h:66
virtual ~StdMeshSkeletonLoader()=default
skeleton_iterator skeletons_end() const
Definition: StdMeshLoader.h:86
std::map< StdCopyStrBuf, std::shared_ptr< StdMeshSkeleton > > SkeletonMap
Definition: StdMeshLoader.h:82
void StoreSkeleton(const char *groupname, const char *filename, std::shared_ptr< StdMeshSkeleton > skeleton)
std::shared_ptr< StdMeshSkeleton > GetSkeletonByName(const StdStrBuf &name) const
skeleton_iterator skeletons_begin() const
Definition: StdMeshLoader.h:85
SkeletonMap::const_iterator skeleton_iterator
Definition: StdMeshLoader.h:83
void RemoveSkeletonsInGroup(const char *groupname)
void LoadSkeletonXml(const char *groupname, const char *filename, const char *sourcefile, size_t size)
void RemoveSkeleton(const StdCopyStrBuf &filepath)
void ToLowerCase()
Definition: StdBuf.cpp:385
void AppendBackslash()
Definition: StdBuf.cpp:248
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:519
DEFINE_EXCEPTION(InvalidVersion, "Mesh header does not contain the expected version")
DEFINE_EXCEPTION(InvalidVersion, "Skeleton header does not contain the expected version")
DEFINE_EXCEPTION(InsufficientData, "Premature end of data stream")
StdMeshVector TransformVector(const StdMeshVector &vector)
StdMeshVector TransformScaleVector(const StdMeshVector &vector)
StdMeshQuaternion TransformQuaternion(const StdMeshQuaternion &quaternion)
StdSubMesh::Vertex TransformVertex(const StdSubMesh::Vertex &vertex)
StdMeshVector TransformPseudoVector(const StdMeshVector &vector)
StdMeshTransformation TransformTransformation(const StdMeshTransformation &trans)
StdMeshVector TransformNormalVector(const StdMeshVector &vector)