OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
StdMeshLoaderXml.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 
17 // A loader for the OGRE .mesh XML file format
18 
19 #include "C4Include.h"
20 #include "lib/StdMesh.h"
21 #include "lib/StdMeshLoader.h"
22 #include <tinyxml.h>
23 
24 // Helper class to load things from an XML file with error checking
26 {
27 public:
28  StdMeshXML(const char* filename, const char* xml_data);
29 
30  const char* RequireStrAttribute(TiXmlElement* element, const char* attribute) const;
31  int RequireIntAttribute(TiXmlElement* element, const char* attribute) const;
32  float RequireFloatAttribute(TiXmlElement* element, const char* attribute) const;
33 
34  TiXmlElement* RequireFirstChild(TiXmlElement* element, const char* child);
35 
36  void LoadGeometry(StdMesh& mesh, std::vector<StdSubMesh::Vertex>& vertices, TiXmlElement* boneassignments_elem);
37  void LoadBoneAssignments(StdMesh& mesh, std::vector<StdSubMesh::Vertex>& vertices, TiXmlElement* boneassignments_elem);
38 
39  void Error(const StdStrBuf& message, TiXmlElement* element) const;
40  void Error(const StdStrBuf& message, int row) const;
41 
42 private:
43  TiXmlDocument Document;
44  StdStrBuf FileName;
45 };
46 
47 StdMeshLoader::StdMeshXML::StdMeshXML(const char* filename, const char* xml_data):
48  FileName(filename)
49 {
50  Document.Parse(xml_data);
51  if (Document.Error())
52  Error(StdStrBuf(Document.ErrorDesc()), Document.ErrorRow());
53 }
54 
55 const char* StdMeshLoader::StdMeshXML::RequireStrAttribute(TiXmlElement* element, const char* attribute) const
56 {
57  const char* value = element->Attribute(attribute);
58  if (!value) Error(FormatString("Element '%s' does not have attribute '%s'", element->Value(), attribute), element);
59  return value;
60 }
61 
62 int StdMeshLoader::StdMeshXML::RequireIntAttribute(TiXmlElement* element, const char* attribute) const
63 {
64  int retval;
65  if (element->QueryIntAttribute(attribute, &retval) != TIXML_SUCCESS)
66  Error(FormatString("Element '%s' does not have integer attribute '%s'", element->Value(), attribute), element);
67  return retval;
68 }
69 
70 float StdMeshLoader::StdMeshXML::RequireFloatAttribute(TiXmlElement* element, const char* attribute) const
71 {
72  float retval = 0;
73  if (element->QueryFloatAttribute(attribute, &retval) != TIXML_SUCCESS)
74  Error(FormatString("Element '%s' does not have floating point attribute '%s'", element->Value(), attribute), element);
75  return retval;
76 }
77 
78 TiXmlElement* StdMeshLoader::StdMeshXML::RequireFirstChild(TiXmlElement* element, const char* child)
79 {
80  TiXmlElement* retval;
81 
82  if (element)
83  {
84  retval = element->FirstChildElement(child);
85  if (!retval)
86  Error(FormatString("Element '%s' does not contain '%s' child", element->Value(), child), element);
87  }
88  else
89  {
90  retval = Document.RootElement();
91  if (strcmp(retval->Value(), child) != 0)
92  Error(FormatString("Root element is not '%s'", child), retval);
93  }
94 
95  return retval;
96 }
97 
98 void StdMeshLoader::StdMeshXML::Error(const StdStrBuf& message, TiXmlElement* element) const
99 {
100  Error(message, element->Row());
101 }
102 
103 void StdMeshLoader::StdMeshXML::Error(const StdStrBuf& message, int row) const
104 {
105  throw StdMeshLoader::LoaderException(FormatString("%s:%u: %s", FileName.getData(), row, message.getData()).getData());
106 }
107 
108 void StdMeshLoader::StdMeshXML::LoadGeometry(StdMesh& mesh, std::vector<StdSubMesh::Vertex>& vertices, TiXmlElement* geometry_elem)
109 {
110  // Check whether mesh has any vertices so far -- we need this later for
111  // initialization of bounding box and bounding sphere.
112  bool hasVertices = false;
113  if(!mesh.SharedVertices.empty()) hasVertices = true;
114  for(auto & SubMesh : mesh.SubMeshes)
115  if(!SubMesh.Vertices.empty())
116  hasVertices = true;
117 
118  int VertexCount = RequireIntAttribute(geometry_elem, "vertexcount");
119  vertices.resize(VertexCount);
120 
121  static const unsigned int POSITIONS = 1;
122  static const unsigned int NORMALS = 2;
123  static const unsigned int TEXCOORDS = 4;
124 
125  // Individual vertex attributes can be split up in multiple vertex buffers
126  unsigned int loaded_attributes = 0;
127  for(TiXmlElement* buffer_elem = geometry_elem->FirstChildElement("vertexbuffer"); buffer_elem != nullptr; buffer_elem = buffer_elem->NextSiblingElement("vertexbuffer"))
128  {
129  unsigned int attributes = 0;
130  if(buffer_elem->Attribute("positions")) attributes |= POSITIONS;
131  if(buffer_elem->Attribute("normals")) attributes |= NORMALS;
132  if(buffer_elem->Attribute("texture_coords")) attributes |= TEXCOORDS;
133 
134  unsigned int i;
135  TiXmlElement* vertex_elem;
136  for (vertex_elem = buffer_elem->FirstChildElement("vertex"), i = 0; vertex_elem != nullptr && i < vertices.size(); vertex_elem = vertex_elem->NextSiblingElement("vertex"), ++i)
137  {
138  if(attributes & POSITIONS)
139  {
140  TiXmlElement* position_elem = RequireFirstChild(vertex_elem, "position");
141 
142  vertices[i].x = RequireFloatAttribute(position_elem, "x");
143  vertices[i].y = RequireFloatAttribute(position_elem, "y");
144  vertices[i].z = RequireFloatAttribute(position_elem, "z");
145  }
146 
147  if(attributes & NORMALS)
148  {
149  TiXmlElement* normal_elem = RequireFirstChild(vertex_elem, "normal");
150 
151  vertices[i].nx = RequireFloatAttribute(normal_elem, "x");
152  vertices[i].ny = RequireFloatAttribute(normal_elem, "y");
153  vertices[i].nz = RequireFloatAttribute(normal_elem, "z");
154  }
155 
156  if(attributes & TEXCOORDS)
157  {
158  // FIXME: The Ogre format supports denoting multiple texture coordinates, but the rendering code only supports one
159  // currently only the first set is read, any additional ones are ignored
160  TiXmlElement* texcoord_elem = RequireFirstChild(vertex_elem, "texcoord");
161  vertices[i].u = RequireFloatAttribute(texcoord_elem, "u");
162  vertices[i].v = RequireFloatAttribute(texcoord_elem, "v");
163  }
164 
165  vertices[i] = OgreToClonk::TransformVertex(vertices[i]);
166 
167  if (attributes & POSITIONS)
168  {
169  const float d = std::sqrt(vertices[i].x*vertices[i].x
170  + vertices[i].y*vertices[i].y
171  + vertices[i].z*vertices[i].z);
172 
173  // Construct BoundingBox
174  StdMeshBox& BoundingBox = mesh.BoundingBox;
175  if (i == 0 && !hasVertices)
176  {
177  // First vertex
178  BoundingBox.x1 = BoundingBox.x2 = vertices[i].x;
179  BoundingBox.y1 = BoundingBox.y2 = vertices[i].y;
180  BoundingBox.z1 = BoundingBox.z2 = vertices[i].z;
181  mesh.BoundingRadius = d;
182  }
183  else
184  {
185  BoundingBox.x1 = std::min(vertices[i].x, BoundingBox.x1);
186  BoundingBox.x2 = std::max(vertices[i].x, BoundingBox.x2);
187  BoundingBox.y1 = std::min(vertices[i].y, BoundingBox.y1);
188  BoundingBox.y2 = std::max(vertices[i].y, BoundingBox.y2);
189  BoundingBox.z1 = std::min(vertices[i].z, BoundingBox.z1);
190  BoundingBox.z2 = std::max(vertices[i].z, BoundingBox.z2);
191  mesh.BoundingRadius = std::max(mesh.BoundingRadius, d);
192  }
193  }
194  }
195 
196  if(vertex_elem != nullptr)
197  Error(FormatString("Too many vertices in vertexbuffer"), buffer_elem);
198  if(i < vertices.size())
199  Error(FormatString("Not enough vertices in vertexbuffer"), buffer_elem);
200 
201  loaded_attributes |= attributes;
202  }
203 
204  static const unsigned int REQUIRED_ATTRIBUTES = POSITIONS | NORMALS | TEXCOORDS;
205  if((loaded_attributes & REQUIRED_ATTRIBUTES) != REQUIRED_ATTRIBUTES)
206  Error(FormatString("Not all required vertex attributes (positions, normals, texcoords) present in mesh geometry"), geometry_elem);
207 }
208 
209 void StdMeshLoader::StdMeshXML::LoadBoneAssignments(StdMesh& mesh, std::vector<StdSubMesh::Vertex>& vertices, TiXmlElement* boneassignments_elem)
210 {
211  for (TiXmlElement* vertexboneassignment_elem = boneassignments_elem->FirstChildElement("vertexboneassignment"); vertexboneassignment_elem != nullptr; vertexboneassignment_elem = vertexboneassignment_elem->NextSiblingElement("vertexboneassignment"))
212  {
213  int BoneID = RequireIntAttribute(vertexboneassignment_elem, "boneindex");
214  int VertexIndex = RequireIntAttribute(vertexboneassignment_elem, "vertexindex");
215  float weight = RequireFloatAttribute(vertexboneassignment_elem, "weight");
216 
217  if (VertexIndex < 0 || static_cast<unsigned int>(VertexIndex) >= vertices.size())
218  Error(FormatString("Vertex index in bone assignment (%d) is out of range", VertexIndex), vertexboneassignment_elem);
219 
220  // maybe not needed, see comment below
221  const StdMeshBone* bone = nullptr;
222  for (unsigned int i = 0; !bone && i < mesh.GetSkeleton().GetNumBones(); ++i)
223  if (mesh.GetSkeleton().GetBone(i).ID == BoneID)
224  bone = &mesh.GetSkeleton().GetBone(i);
225 
226  if (!bone) Error(FormatString("There is no such bone with ID %d", BoneID), vertexboneassignment_elem);
227 
228  // Find first bone assignment with a zero weight (i.e. is unused)
229  StdSubMesh::Vertex& vertex = vertices[VertexIndex];
230  // Check quickly if all weight slots are used
231  if (vertex.bone_weight[StdMeshVertex::MaxBoneWeightCount - 1] != 0)
232  {
233  Error(FormatString("Vertex %d is influenced by more than %d bones", VertexIndex, static_cast<int>(StdMeshVertex::MaxBoneWeightCount)), vertexboneassignment_elem);
234  }
235  for (size_t weight_index = 0; weight_index < StdMeshVertex::MaxBoneWeightCount; ++weight_index)
236  {
237  if (vertex.bone_weight[weight_index] == 0)
238  {
239  vertex.bone_weight[weight_index] = weight;
240  vertex.bone_index[weight_index] = bone->Index;
241  break;
242  }
243  }
244  }
245 
246  // Normalize vertex bone assignment weights (this is not guaranteed in the
247  // Ogre file format).
248  for (auto & vertex : vertices)
249  {
250  float sum = 0.0;
251  for (float weight : vertex.bone_weight)
252  sum += weight;
253  if (sum != 0)
254  for (float &weight : vertex.bone_weight)
255  weight /= sum;
256  else
257  vertex.bone_weight[0] = 1.0f;
258  }
259 }
260 
261 StdMesh *StdMeshLoader::LoadMeshXml(const char* xml_data, size_t size, const StdMeshMatManager& manager, StdMeshSkeletonLoader& skel_loader, const char* filename)
262 {
263  StdMeshXML xml(filename ? filename : "<unknown>", xml_data);
264 
265  std::unique_ptr<StdMesh> mesh(new StdMesh);
266 
267  TiXmlElement* mesh_elem = xml.RequireFirstChild(nullptr, "mesh");
268 
269  // Load shared geometry, if any
270  TiXmlElement* sharedgeometry_elem = mesh_elem->FirstChildElement("sharedgeometry");
271  if(sharedgeometry_elem != nullptr)
272  xml.LoadGeometry(*mesh, mesh->SharedVertices, sharedgeometry_elem);
273 
274  TiXmlElement* submeshes_elem = xml.RequireFirstChild(mesh_elem, "submeshes");
275 
276  TiXmlElement* submesh_elem_base = xml.RequireFirstChild(submeshes_elem, "submesh");
277  for (TiXmlElement* submesh_elem = submesh_elem_base; submesh_elem != nullptr; submesh_elem = submesh_elem->NextSiblingElement("submesh"))
278  {
279  mesh->SubMeshes.push_back(StdSubMesh());
280  StdSubMesh& submesh = mesh->SubMeshes.back();
281 
282  const char* material = xml.RequireStrAttribute(submesh_elem, "material");
283  submesh.Material = manager.GetMaterial(material);
284  if (!submesh.Material)
285  xml.Error(FormatString("There is no such material named '%s'", material), submesh_elem);
286 
287  const char* usesharedvertices = submesh_elem->Attribute("usesharedvertices");
288  const std::vector<StdMesh::Vertex>* vertices;
289  if(!usesharedvertices || strcmp(usesharedvertices, "true") != 0)
290  {
291  TiXmlElement* geometry_elem = xml.RequireFirstChild(submesh_elem, "geometry");
292  xml.LoadGeometry(*mesh, submesh.Vertices, geometry_elem);
293  vertices = &submesh.Vertices;
294  }
295  else
296  {
297  if(mesh->SharedVertices.empty())
298  xml.Error(StdCopyStrBuf("Submesh specifies to use shared vertices but there is no shared geometry"), submesh_elem);
299  vertices = &mesh->SharedVertices;
300  }
301 
302  TiXmlElement* faces_elem = xml.RequireFirstChild(submesh_elem, "faces");
303  int FaceCount = xml.RequireIntAttribute(faces_elem, "count");
304  submesh.Faces.resize(FaceCount);
305 
306  unsigned int i = 0;
307  for (TiXmlElement* face_elem = faces_elem->FirstChildElement("face"); face_elem != nullptr && i < submesh.Faces.size(); face_elem = face_elem->NextSiblingElement("face"), ++i)
308  {
309  int v[3];
310 
311  v[0] = xml.RequireIntAttribute(face_elem, "v1");
312  v[1] = xml.RequireIntAttribute(face_elem, "v2");
313  v[2] = xml.RequireIntAttribute(face_elem, "v3");
314 
315  for (unsigned int j = 0; j < 3; ++j)
316  {
317  if (v[j] < 0 || static_cast<unsigned int>(v[j]) >= vertices->size())
318  xml.Error(FormatString("Vertex index v%u (%d) is out of range", j+1, v[j]), face_elem);
319  submesh.Faces[i].Vertices[j] = v[j];
320  }
321  }
322  }
323 
324  // We allow bounding box to be empty if it's only due to Z direction since
325  // this is what goes inside the screen in Clonk.
326  if(mesh->BoundingBox.x1 == mesh->BoundingBox.x2 || mesh->BoundingBox.y1 == mesh->BoundingBox.y2)
327  xml.Error(StdCopyStrBuf("Bounding box is empty"), mesh_elem);
328 
329  // Read skeleton, if any
330  TiXmlElement* skeletonlink_elem = mesh_elem->FirstChildElement("skeletonlink");
331  if (skeletonlink_elem)
332  {
333  const char* name = xml.RequireStrAttribute(skeletonlink_elem, "name");
334  StdCopyStrBuf xml_filename(name); xml_filename.Append(".xml");
335 
336  StdCopyStrBuf skeleton_filename;
337  StdMeshSkeletonLoader::MakeFullSkeletonPath(skeleton_filename, filename, xml_filename.getData());
338 
339  mesh->Skeleton = skel_loader.GetSkeletonByName(skeleton_filename);
340  if (!mesh->Skeleton) xml.Error(FormatString("Failed to load '%s'", skeleton_filename.getData()), skeletonlink_elem);
341 
342  // Vertex<->Bone assignments for shared geometry
343  if (sharedgeometry_elem)
344  {
345  TiXmlElement* boneassignments_elem = xml.RequireFirstChild(mesh_elem, "boneassignments");
346  xml.LoadBoneAssignments(*mesh, mesh->SharedVertices, boneassignments_elem);
347  }
348 
349  // Vertex<->Bone assignments for all vertices (need to go through SubMeshes again...)
350  unsigned int submesh_index = 0;
351  for (TiXmlElement* submesh_elem = submesh_elem_base; submesh_elem != nullptr; submesh_elem = submesh_elem->NextSiblingElement("submesh"), ++submesh_index)
352  {
353  StdSubMesh& submesh = mesh->SubMeshes[submesh_index];
354  if (!submesh.Vertices.empty())
355  {
356  TiXmlElement* boneassignments_elem = xml.RequireFirstChild(submesh_elem, "boneassignments");
357  xml.LoadBoneAssignments(*mesh, submesh.Vertices, boneassignments_elem);
358  }
359  }
360  }
361  else
362  {
363  // Mesh has no skeleton
364  // Bone assignements do not make sense then, as the
365  // actual bones are defined in the skeleton file.
366  for (TiXmlElement* submesh_elem = submesh_elem_base; submesh_elem != nullptr; submesh_elem = submesh_elem->NextSiblingElement("submesh"))
367  {
368  TiXmlElement* boneassignments_elem = submesh_elem->FirstChildElement("boneassignments");
369  if (boneassignments_elem)
370  xml.Error(StdStrBuf("Mesh has bone assignments, but no skeleton"), boneassignments_elem);
371  }
372 
373  TiXmlElement* boneassignments_elem = mesh_elem->FirstChildElement("boneassignments");
374  if (boneassignments_elem)
375  xml.Error(StdStrBuf("Mesh has bone assignments, but no skeleton"), boneassignments_elem);
376  }
377 
378  return mesh.release();
379 }
380 
381 void StdMeshSkeletonLoader::LoadSkeletonXml(const char* groupname, const char* filename, const char *sourcefile, size_t size)
382 {
383  if (sourcefile == nullptr)
384  {
385  throw Ogre::InsufficientData(FormatString("Failed to load '%s/%s'", groupname, filename).getData());
386  }
387 
388  std::shared_ptr<StdMeshLoader::StdMeshXML> skeleton(new StdMeshLoader::StdMeshXML(filename, sourcefile));
389 
390  TiXmlElement* skeleton_elem = skeleton->RequireFirstChild(nullptr, "skeleton");
391  TiXmlElement* bones_elem = skeleton->RequireFirstChild(skeleton_elem, "bones");
392 
393  // Read bones. Don't insert into Master bone table yet, as the master bone
394  // table is sorted hierarchically, and we will read the hierarchy only
395  // afterwards.
396  std::vector<StdMeshBone*> bones;
397  for (TiXmlElement* bone_elem = bones_elem->FirstChildElement("bone"); bone_elem != nullptr; bone_elem = bone_elem->NextSiblingElement("bone"))
398  {
399  StdMeshBone* bone = new StdMeshBone;
400  bones.push_back(bone);
401 
402  bone->ID = skeleton->RequireIntAttribute(bone_elem, "id");
403  bone->Name = skeleton->RequireStrAttribute(bone_elem, "name");
404  // TODO: Make sure ID and name are unique
405 
406  bone->Parent = nullptr;
407  // Index of bone will be set when building Master Bone Table later
408 
409  TiXmlElement* position_elem = skeleton->RequireFirstChild(bone_elem, "position");
410  TiXmlElement* rotation_elem = skeleton->RequireFirstChild(bone_elem, "rotation");
411  TiXmlElement* axis_elem = skeleton->RequireFirstChild(rotation_elem, "axis");
412 
413  StdMeshVector d, r;
414  d.x = skeleton->RequireFloatAttribute(position_elem, "x");
415  d.y = skeleton->RequireFloatAttribute(position_elem, "y");
416  d.z = skeleton->RequireFloatAttribute(position_elem, "z");
417  float angle = skeleton->RequireFloatAttribute(rotation_elem, "angle");
418  r.x = skeleton->RequireFloatAttribute(axis_elem, "x");
419  r.y = skeleton->RequireFloatAttribute(axis_elem, "y");
420  r.z = skeleton->RequireFloatAttribute(axis_elem, "z");
421 
423  bone->Transformation.rotate = StdMeshQuaternion::AngleAxis(angle, r);
424  bone->Transformation.translate = d;
425 
426  // We need this later for applying animations, and attaching meshes, therefore cache it here
427  bone->InverseTransformation = StdMeshTransformation::Inverse(bone->Transformation);
428  }
429 
430  // Bone hierarchy
431  TiXmlElement* bonehierarchy_elem = skeleton->RequireFirstChild(skeleton_elem, "bonehierarchy");
432  for (TiXmlElement* boneparent_elem = bonehierarchy_elem->FirstChildElement("boneparent"); boneparent_elem != nullptr; boneparent_elem = boneparent_elem->NextSiblingElement("boneparent"))
433  {
434  const char* child_name = skeleton->RequireStrAttribute(boneparent_elem, "bone");
435  const char* parent_name = skeleton->RequireStrAttribute(boneparent_elem, "parent");
436 
437  // Lookup the two bones
438  StdMeshBone* child = nullptr;
439  StdMeshBone* parent = nullptr;
440  for (unsigned int i = 0; i < bones.size() && (!child || !parent); ++i)
441  {
442  if (!child && bones[i]->Name == child_name)
443  child = bones[i];
444  if (!parent && bones[i]->Name == parent_name)
445  parent = bones[i];
446  }
447 
448  if (!child) skeleton->Error(FormatString("There is no such bone with name '%s'", child_name), boneparent_elem);
449  if (!parent) skeleton->Error(FormatString("There is no such bone with name '%s'", parent_name), boneparent_elem);
450 
451  child->Parent = parent;
452  parent->Children.push_back(child);
453  }
454 
455  std::shared_ptr<StdMeshSkeleton> Skeleton(new StdMeshSkeleton);
456 
457  // Fill master bone table in hierarchical order:
458  for (auto & bone : bones)
459  if (bone->Parent == nullptr)
460  Skeleton->AddMasterBone(bone);
461 
462  // Load Animations
463  TiXmlElement* animations_elem = skeleton_elem->FirstChildElement("animations");
464  if (animations_elem)
465  {
466  for (TiXmlElement* animation_elem = animations_elem->FirstChildElement("animation"); animation_elem != nullptr; animation_elem = animation_elem->NextSiblingElement("animation"))
467  {
468  StdCopyStrBuf name(skeleton->RequireStrAttribute(animation_elem, "name"));
469  if (Skeleton->Animations.find(name) != Skeleton->Animations.end())
470  skeleton->Error(FormatString("There is already an animation with name '%s'", name.getData()), animation_elem);
471 
472  StdMeshAnimation& animation = Skeleton->Animations.insert(std::make_pair(name, StdMeshAnimation())).first->second;
473  animation.Name = name;
474  animation.Length = skeleton->RequireFloatAttribute(animation_elem, "length");
475  animation.Tracks.resize(Skeleton->GetNumBones());
476  animation.OriginSkeleton = &(*Skeleton);
477 
478  TiXmlElement* tracks_elem = skeleton->RequireFirstChild(animation_elem, "tracks");
479  for (TiXmlElement* track_elem = tracks_elem->FirstChildElement("track"); track_elem != nullptr; track_elem = track_elem->NextSiblingElement("track"))
480  {
481  const char* bone_name = skeleton->RequireStrAttribute(track_elem, "bone");
482  StdMeshBone* bone = nullptr;
483  for (unsigned int i = 0; !bone && i < Skeleton->GetNumBones(); ++i)
484  if (Skeleton->Bones[i]->Name == bone_name)
485  bone = Skeleton->Bones[i];
486  if (!bone) skeleton->Error(FormatString("There is no such bone with name '%s'", bone_name), track_elem);
487 
488  if (animation.Tracks[bone->Index] != nullptr) skeleton->Error(FormatString("There is already a track for bone '%s' in animation '%s'", bone_name, animation.Name.getData()), track_elem);
489 
490  StdMeshTrack* track = new StdMeshTrack;
491  animation.Tracks[bone->Index] = track;
492 
493  TiXmlElement* keyframes_elem = skeleton->RequireFirstChild(track_elem, "keyframes");
494  for (TiXmlElement* keyframe_elem = keyframes_elem->FirstChildElement("keyframe"); keyframe_elem != nullptr; keyframe_elem = keyframe_elem->NextSiblingElement("keyframe"))
495  {
496  float time = skeleton->RequireFloatAttribute(keyframe_elem, "time");
497  StdMeshKeyFrame& frame = track->Frames[time];
498 
499  TiXmlElement* translate_elem = keyframe_elem->FirstChildElement("translate");
500  TiXmlElement* rotate_elem = keyframe_elem->FirstChildElement("rotate");
501  TiXmlElement* scale_elem = keyframe_elem->FirstChildElement("scale");
502 
503  StdMeshVector d, s, r;
504  d.x = d.y = d.z = 0.0f;
506  r.x = r.y = 0.0f; r.z = 1.0f;
507  float angle = 0.0f;
508 
509  if (translate_elem)
510  {
511  d.x = skeleton->RequireFloatAttribute(translate_elem, "x");
512  d.y = skeleton->RequireFloatAttribute(translate_elem, "y");
513  d.z = skeleton->RequireFloatAttribute(translate_elem, "z");
514  }
515 
516  if (rotate_elem)
517  {
518  TiXmlElement* axis_elem = skeleton->RequireFirstChild(rotate_elem, "axis");
519  angle = skeleton->RequireFloatAttribute(rotate_elem, "angle");
520  r.x = skeleton->RequireFloatAttribute(axis_elem, "x");
521  r.y = skeleton->RequireFloatAttribute(axis_elem, "y");
522  r.z = skeleton->RequireFloatAttribute(axis_elem, "z");
523  }
524 
525  if (scale_elem)
526  {
527  s.x = skeleton->RequireFloatAttribute(scale_elem, "x");
528  s.y = skeleton->RequireFloatAttribute(scale_elem, "y");
529  s.z = skeleton->RequireFloatAttribute(scale_elem, "z");
530  }
531 
532  frame.Transformation.scale = s;
533  frame.Transformation.rotate = StdMeshQuaternion::AngleAxis(angle, r);
534  frame.Transformation.translate = bone->InverseTransformation.rotate * (bone->InverseTransformation.scale * d);
536  }
537  }
538  }
539  }
540 
541  // is there even any xml file that we load from?
542  // it looks like this could never work: if the mesh has no skeleton, then the code below will fail because of a null pointer...
543 
544  // Apply parent transformation to each bone transformation. We need to
545  // do this late since animation keyframe computation needs the bone
546  // transformations, not bone+parent.
547  for (unsigned int i = 0; i < Skeleton->GetNumBones(); ++i)
548  {
549  // Apply parent transformation
550  if (Skeleton->Bones[i]->Parent)
551  Skeleton->Bones[i]->Transformation = Skeleton->Bones[i]->Parent->Transformation * OgreToClonk::TransformTransformation(Skeleton->Bones[i]->Transformation);
552  else
553  Skeleton->Bones[i]->Transformation = OgreToClonk::TransformTransformation(Skeleton->Bones[i]->Transformation);
554 
555  // Update inverse
556  Skeleton->Bones[i]->InverseTransformation = StdMeshTransformation::Inverse(Skeleton->Bones[i]->Transformation);
557  }
558 
559  StoreSkeleton(groupname, filename, Skeleton);
560 }
const char * getData() const
Definition: StdBuf.h:442
float y2
Definition: StdMesh.h:147
#define z
int ID
Definition: StdMesh.h:33
float bone_weight[MaxBoneWeightCount]
Definition: StdMeshMath.h:44
std::shared_ptr< StdMeshSkeleton > GetSkeletonByName(const StdStrBuf &name) const
float x2
Definition: StdMesh.h:147
StdCopyStrBuf Name
Definition: StdMesh.h:97
float z1
Definition: StdMesh.h:146
StdMeshTransformation Transformation
Definition: StdMesh.h:37
float RequireFloatAttribute(TiXmlElement *element, const char *attribute) const
int RequireIntAttribute(TiXmlElement *element, const char *attribute) const
const StdMeshSkeleton & GetSkeleton() const
Definition: StdMesh.h:203
float z2
Definition: StdMesh.h:147
StdMeshTransformation TransformTransformation(const StdMeshTransformation &trans)
const StdMeshBone & GetBone(size_t i) const
Definition: StdMesh.h:116
void LoadGeometry(StdMesh &mesh, std::vector< StdSubMesh::Vertex > &vertices, TiXmlElement *boneassignments_elem)
TiXmlElement * RequireFirstChild(TiXmlElement *element, const char *child)
const char * RequireStrAttribute(TiXmlElement *element, const char *attribute) const
float y1
Definition: StdMesh.h:146
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:519
static StdMeshVector UnitScale()
Definition: StdMeshMath.cpp:29
uint16_t bone_index[MaxBoneWeightCount]
Definition: StdMeshMath.h:45
void Error(const StdStrBuf &message, TiXmlElement *element) const
StdCopyStrBuf Name
Definition: StdMesh.h:34
static const size_t MaxBoneWeightCount
Definition: StdMeshMath.h:37
float x1
Definition: StdMesh.h:146
StdMeshTransformation InverseTransformation
Definition: StdMesh.h:39
unsigned int Index
Definition: StdMesh.h:32
StdMeshXML(const char *filename, const char *xml_data)
const StdMeshMaterial * GetMaterial(const char *material_name) const
void LoadBoneAssignments(StdMesh &mesh, std::vector< StdSubMesh::Vertex > &vertices, TiXmlElement *boneassignments_elem)
static StdMesh * LoadMeshXml(const char *sourcefile, size_t size, const StdMeshMatManager &mat_mgr, StdMeshSkeletonLoader &loader, const char *filename=nullptr)
static void MakeFullSkeletonPath(StdCopyStrBuf &out, const char *groupname, const char *filename)
Definition: StdMeshLoader.h:66
float Length
Definition: StdMesh.h:98
#define s
size_t GetNumBones() const
Definition: StdMesh.h:117
StdMeshTransformation Transformation
Definition: StdMesh.h:68
StdSubMesh::Vertex TransformVertex(const StdSubMesh::Vertex &vertex)
void LoadSkeletonXml(const char *groupname, const char *filename, const char *sourcefile, size_t size)
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270