Ignition Rendering

API Reference

5.0.0
BaseMesh.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 #ifndef IGNITION_RENDERING_BASE_BASEMESH_HH_
18 #define IGNITION_RENDERING_BASE_BASEMESH_HH_
19 
20 #include <map>
21 #include <string>
22 #include <unordered_map>
27 
28 namespace ignition
29 {
30  namespace rendering
31  {
32  inline namespace IGNITION_RENDERING_VERSION_NAMESPACE {
33  //
35  template <class T>
36  class BaseMesh :
37  public virtual Mesh,
38  public virtual T
39  {
40  protected: BaseMesh();
41 
42  public: virtual ~BaseMesh();
43 
44  // Documentation inherited.
45  public: virtual bool HasSkeleton() const override;
46 
47  // Documentation inherited.
49  SkeletonLocalTransforms() const override;
50 
51  // Documentation inherited.
52  public: virtual void SetSkeletonLocalTransforms(
53  const std::map<std::string, math::Matrix4d> &) override;
54 
55  // Documentation inherited.
56  public: virtual std::unordered_map<std::string, float> SkeletonWeights()
57  const override;
58 
59  // Documentation inherited.
60  public: virtual void SetSkeletonWeights(
62 
63  // Documentation inherited.
64  public: virtual void SetSkeletonAnimationEnabled(const std::string &_name,
65  bool _enabled, bool _loop = true, float _weight = 1.0) override;
66 
67  // Documentation inherited.
68  public: virtual bool SkeletonAnimationEnabled(const std::string &_name)
69  const override;
70 
71  // Documentation inherited.
72  public: virtual void UpdateSkeletonAnimation(
73  std::chrono::steady_clock::duration _time) override;
74 
75  public: virtual unsigned int SubMeshCount() const override;
76 
77  public: virtual bool HasSubMesh(ConstSubMeshPtr _subMesh) const override;
78 
79  public: virtual bool HasSubMeshName(const std::string &_name) const
80  override;
81 
82  public: virtual SubMeshPtr SubMeshByName(
83  const std::string &_name) const override;
84 
85  public: virtual SubMeshPtr SubMeshByIndex(unsigned int _index) const
86  override;
87 
88  // Documentation inherited.
89  public: virtual MaterialPtr Material() const override;
90 
91  public: virtual void SetMaterial(const std::string &_name,
92  bool _unique = true) override;
93 
94  public: virtual void SetMaterial(MaterialPtr _material,
95  bool _unique = true) override;
96 
97  public: virtual void PreRender() override;
98 
99  // Documentation inherited
100  public: virtual void Destroy() override;
101 
102  protected: virtual SubMeshStorePtr SubMeshes() const = 0;
103 
106  protected: bool ownsMaterial = false;
107 
109  protected: MaterialPtr material;
110  };
111 
113  template <class T>
114  class BaseSubMesh :
115  public virtual SubMesh,
116  public virtual T
117  {
118  protected: BaseSubMesh();
119 
120  public: virtual ~BaseSubMesh();
121 
122  // Documentation inherited
123  public: virtual MaterialPtr Material() const override;
124 
125  // Documentation inherited
126  public: virtual void SetMaterial(const std::string &_name,
127  bool _unique = true) override;
128 
129  // Documentation inherited
130  public: virtual void SetMaterial(MaterialPtr _material,
131  bool _unique = true) override;
132 
135  public: virtual void SetMaterialImpl(MaterialPtr _material) = 0;
136 
137  public: virtual void PreRender() override;
138 
139  // Documentation inherited
140  public: virtual void Destroy() override;
141 
144  protected: bool ownsMaterial = false;
145 
147  protected: MaterialPtr material;
148  };
149 
151  // BaseMesh
153  template <class T>
155  {
156  }
157 
159  template <class T>
161  {
162  }
163 
165  template <class T>
167  {
168  return false;
169  }
170 
172  template <class T>
175  {
177  return tmpMap;
178  }
179 
181  template <class T>
184  {
185  }
186 
188  template <class T>
190  {
192  return tmpMap;
193  }
194 
196  template <class T>
199  {
200  ignerr << "SetSkeletonWeights not supported for render engine: "
201  << this->Scene()->Engine()->Name() << std::endl;
202  }
203 
205  template <class T>
207  bool, float)
208  {
209  }
210 
212  template <class T>
214  {
215  return false;
216  }
217 
219  template <class T>
221  std::chrono::steady_clock::duration)
222  {
223  }
224 
226  template <class T>
227  unsigned int BaseMesh<T>::SubMeshCount() const
228  {
229  return this->SubMeshes()->Size();
230  }
231 
233  template <class T>
235  {
236  return this->SubMeshes()->Contains(_subMesh);
237  }
238 
240  template <class T>
241  bool BaseMesh<T>::HasSubMeshName(const std::string &_name) const
242  {
243  return this->SubMeshes()->ContainsName(_name);
244  }
245 
247  template <class T>
249  {
250  return this->SubMeshes()->GetByName(_name);
251  }
252 
254  template <class T>
255  SubMeshPtr BaseMesh<T>::SubMeshByIndex(unsigned int _index) const
256  {
257  return this->SubMeshes()->GetByIndex(_index);
258  }
259 
261  template <class T>
263  {
264  unsigned int count = this->SubMeshCount();
265  return (count > 0) ? this->SubMeshByIndex(0)->Material() :
266  MaterialPtr();
267  }
268 
270  template <class T>
271  void BaseMesh<T>::SetMaterial(const std::string &_name, bool _unique)
272  {
273  MaterialPtr mat = this->Scene()->Material(_name);
274  if (mat) this->SetMaterial(mat, _unique);
275  }
276 
278  template <class T>
279  void BaseMesh<T>::SetMaterial(MaterialPtr _material, bool _unique)
280  {
281  // todo(anyone) take ownership of reference _material if _unique
282  // and destroy the reference material when the mesh is destroyed
283  unsigned int count = this->SubMeshCount();
284  _material = (_unique && count > 0) ? _material->Clone() : _material;
285 
286  for (unsigned int i = 0; i < count; ++i)
287  {
288  SubMeshPtr subMesh = this->SubMeshByIndex(i);
289  subMesh->SetMaterial(_material, false);
290  }
291 
292  if (this->material && this->ownsMaterial)
293  this->Scene()->DestroyMaterial(this->material);
294 
295  this->ownsMaterial = _unique;
296  this->material = _material;
297  }
298 
300  template <class T>
302  {
303  unsigned int count = this->SubMeshCount();
304 
305  for (unsigned int i = 0; i < count; ++i)
306  {
307  SubMeshPtr subMesh = this->SubMeshByIndex(i);
308  subMesh->PreRender();
309  }
310 
311  T::PreRender();
312  }
313 
315  template <class T>
317  {
318  T::Destroy();
319  this->SubMeshes()->DestroyAll();
320  if (this->material && this->ownsMaterial)
321  this->Scene()->DestroyMaterial(this->material);
322  this->material.reset();
323  }
324 
326  // BaseSubMesh
328  template <class T>
330  {
331  }
332 
334  template <class T>
336  {
337  }
338 
340  template <class T>
342  {
343  T::Destroy();
344  if (this->material && this->ownsMaterial)
345  this->Scene()->DestroyMaterial(this->material);
346  this->material.reset();
347  }
348 
349 
351  template <class T>
352  void BaseSubMesh<T>::SetMaterial(const std::string &_name, bool _unique)
353  {
354  MaterialPtr mat = this->Scene()->Material(_name);
355  if (mat) this->SetMaterial(mat, _unique);
356  }
357 
359  template <class T>
360  void BaseSubMesh<T>::SetMaterial(MaterialPtr _material, bool _unique)
361  {
362  _material = (_unique) ? _material->Clone() : _material;
363 
364  MaterialPtr origMaterial = this->material;
365  bool origUnique = this->ownsMaterial;
366 
367  this->SetMaterialImpl(_material);
368 
369  if (origMaterial && origUnique)
370  this->Scene()->DestroyMaterial(origMaterial);
371 
372  this->material = _material;
373  this->ownsMaterial = _unique;
374  }
375 
377  template <class T>
379  {
380  return this->material;
381  }
382 
384  template <class T>
386  {
387  T::PreRender();
388  if (this->Material())
389  this->Material()->PreRender();
390  }
391  }
392  }
393 }
394 #endif
virtual bool SkeletonAnimationEnabled(const std::string &_name) const override
Get whether a skeleton animation is enabled or not.
Definition: BaseMesh.hh:213
virtual void SetSkeletonLocalTransforms(const std::map< std::string, math::Matrix4d > &) override
Set transforms for the skeleton.
Definition: BaseMesh.hh:182
virtual void DestroyMaterial(MaterialPtr _material)=0
Unregister and destroy a material.
virtual ~BaseMesh()
Definition: BaseMesh.hh:160
virtual SubMeshPtr SubMeshByIndex(unsigned int _index) const override
Get sub-mesh at given index.
Definition: BaseMesh.hh:255
Definition: BaseMesh.hh:114
virtual void SetMaterial(const std::string &_name, bool _unique=true) override
Set the materials of this SubMesh. The specified material will be retrieved from the parent Scene...
Definition: BaseMesh.hh:352
virtual bool HasSubMeshName(const std::string &_name) const override
Determine if has sub-mesh with given name.
Definition: BaseMesh.hh:241
MaterialPtr material
Pointer to currently assigned material.
Definition: BaseMesh.hh:147
virtual bool HasSkeleton() const override
Check whether the mesh has skeleton.
Definition: BaseMesh.hh:166
T endl(T... args)
virtual unsigned int SubMeshCount() const override
Get the sub-mesh count.
Definition: BaseMesh.hh:227
virtual void SetMaterial(const std::string &_name, bool _unique=true) override
Set the materials of this Geometry. The specified material will be retrieved from the parent Scene...
Definition: BaseMesh.hh:271
virtual bool HasSubMesh(ConstSubMeshPtr _subMesh) const override
Determine if has given sub-mesh.
Definition: BaseMesh.hh:234
virtual void PreRender() override
Prepare this object and any of its children for rendering. This should be called for each object in a...
Definition: BaseMesh.hh:385
virtual std::map< std::string, math::Matrix4d > SkeletonLocalTransforms() const override
Get the skeleton local transforms.
Definition: BaseMesh.hh:174
STL class.
STL class.
virtual MaterialPtr Material() const override
Get the material of this geometry.
Definition: BaseMesh.hh:262
virtual void PreRender() override
Prepare this object and any of its children for rendering. This should be called for each object in a...
Definition: BaseMesh.hh:301
virtual std::unordered_map< std::string, float > SkeletonWeights() const override
Get skeleton node weight.
Definition: BaseMesh.hh:189
Manages a single scene-graph. This class updates scene-wide properties and holds the root scene node...
Definition: Scene.hh:49
virtual RenderEngine * Engine() const =0
Get the creating render-engine of the scene.
Definition: BaseMesh.hh:36
virtual MaterialPtr Material() const override
Get the currently assigned material.
Definition: BaseMesh.hh:378
virtual void SetSkeletonWeights(const std::unordered_map< std::string, float > &) override
Set skeleton node weight.
Definition: BaseMesh.hh:197
virtual ~BaseSubMesh()
Definition: BaseMesh.hh:335
BaseSubMesh()
Definition: BaseMesh.hh:329
virtual void Destroy() override
Destroy any resources associated with this object. Invoking any other functions after destroying an o...
Definition: BaseMesh.hh:316
virtual void Destroy() override
Destroy any resources associated with this object. Invoking any other functions after destroying an o...
Definition: BaseMesh.hh:341
virtual std::string Name() const =0
Get name of the render-engine.
virtual void PreRender()=0
Prepare this object and any of its children for rendering. This should be called for each object in a...
#define ignerr
shared_ptr< Material > MaterialPtr
Definition: RenderTypes.hh:166
Represents a collection of mesh geometries.
Definition: Mesh.hh:36
virtual void SetSkeletonAnimationEnabled(const std::string &_name, bool _enabled, bool _loop=true, float _weight=1.0) override
Set whether a skeleton animation should be enabled or not.
Definition: BaseMesh.hh:206
MaterialPtr material
Pointer to currently assigned material.
Definition: BaseMesh.hh:109
Represents a surface material of a Geometry.
Definition: Material.hh:47
BaseMesh()
Definition: BaseMesh.hh:154
virtual SubMeshPtr SubMeshByName(const std::string &_name) const override
Get sub-mesh with given name.
Definition: BaseMesh.hh:248
virtual void UpdateSkeletonAnimation(std::chrono::steady_clock::duration _time) override
Play the active skeleton animation to the specified time.
Definition: BaseMesh.hh:220
Represents a single mesh geometry.
Definition: Mesh.hh:126
virtual MaterialPtr Material(const std::string &_name) const =0
Get material registered under the given name. If no material is registered under the given name...