Ignition Rendering

API Reference

5.1.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(
54  override;
55 
56  // Documentation inherited.
57  public: virtual std::unordered_map<std::string, float> SkeletonWeights()
58  const override;
59 
60  // Documentation inherited.
61  public: virtual void SetSkeletonWeights(
63  override;
64 
65  // Documentation inherited.
66  public: virtual void SetSkeletonAnimationEnabled(const std::string &_name,
67  bool _enabled, bool _loop = true, float _weight = 1.0) override;
68 
69  // Documentation inherited.
70  public: virtual bool SkeletonAnimationEnabled(const std::string &_name)
71  const override;
72 
73  // Documentation inherited.
74  public: virtual void UpdateSkeletonAnimation(
75  std::chrono::steady_clock::duration _time) override;
76 
77  public: virtual unsigned int SubMeshCount() const override;
78 
79  public: virtual bool HasSubMesh(ConstSubMeshPtr _subMesh) const override;
80 
81  public: virtual bool HasSubMeshName(const std::string &_name) const
82  override;
83 
84  public: virtual SubMeshPtr SubMeshByName(
85  const std::string &_name) const override;
86 
87  public: virtual SubMeshPtr SubMeshByIndex(unsigned int _index) const
88  override;
89 
90  // Documentation inherited.
91  public: virtual MaterialPtr Material() const override;
92 
93  // Documentation inherited.
94  public: virtual void SetMaterial(const std::string &_name,
95  bool _unique = true) override;
96 
97  // Documentation inherited.
98  public: virtual void SetMaterial(MaterialPtr _material,
99  bool _unique = true) override;
100 
101  public: virtual void PreRender() override;
102 
103  // Documentation inherited
104  public: virtual void Destroy() override;
105 
106  protected: virtual SubMeshStorePtr SubMeshes() const = 0;
107 
110  protected: bool ownsMaterial = false;
111 
113  protected: MaterialPtr material;
114  };
115 
117  template <class T>
118  class BaseSubMesh :
119  public virtual SubMesh,
120  public virtual T
121  {
122  protected: BaseSubMesh();
123 
124  public: virtual ~BaseSubMesh();
125 
126  // Documentation inherited
127  public: virtual MaterialPtr Material() const override;
128 
129  // Documentation inherited
130  public: virtual void SetMaterial(const std::string &_name,
131  bool _unique = true) override;
132 
133  // Documentation inherited
134  public: virtual void SetMaterial(MaterialPtr _material,
135  bool _unique = true) override;
136 
139  public: virtual void SetMaterialImpl(MaterialPtr _material) = 0;
140 
141  public: virtual void PreRender() override;
142 
143  // Documentation inherited
144  public: virtual void Destroy() override;
145 
148  protected: bool ownsMaterial = false;
149 
151  protected: MaterialPtr material;
152  };
153 
155  // BaseMesh
157  template <class T>
159  {
160  }
161 
163  template <class T>
165  {
166  }
167 
169  template <class T>
171  {
172  return false;
173  }
174 
176  template <class T>
179  {
181  return tmpMap;
182  }
183 
185  template <class T>
188  {
189  }
190 
192  template <class T>
194  {
196  return tmpMap;
197  }
198 
200  template <class T>
203  {
204  ignerr << "SetSkeletonWeights not supported for render engine: "
205  << this->Scene()->Engine()->Name() << std::endl;
206  }
207 
209  template <class T>
211  bool, float)
212  {
213  }
214 
216  template <class T>
218  {
219  return false;
220  }
221 
223  template <class T>
225  std::chrono::steady_clock::duration)
226  {
227  }
228 
230  template <class T>
231  unsigned int BaseMesh<T>::SubMeshCount() const
232  {
233  return this->SubMeshes()->Size();
234  }
235 
237  template <class T>
239  {
240  return this->SubMeshes()->Contains(_subMesh);
241  }
242 
244  template <class T>
245  bool BaseMesh<T>::HasSubMeshName(const std::string &_name) const
246  {
247  return this->SubMeshes()->ContainsName(_name);
248  }
249 
251  template <class T>
253  {
254  return this->SubMeshes()->GetByName(_name);
255  }
256 
258  template <class T>
259  SubMeshPtr BaseMesh<T>::SubMeshByIndex(unsigned int _index) const
260  {
261  return this->SubMeshes()->GetByIndex(_index);
262  }
263 
265  template <class T>
267  {
268  unsigned int count = this->SubMeshCount();
269  return (count > 0) ? this->SubMeshByIndex(0)->Material() :
270  MaterialPtr();
271  }
272 
274  template <class T>
275  void BaseMesh<T>::SetMaterial(const std::string &_name, bool _unique)
276  {
277  MaterialPtr mat = this->Scene()->Material(_name);
278  if (mat) this->SetMaterial(mat, _unique);
279  }
280 
282  template <class T>
283  void BaseMesh<T>::SetMaterial(MaterialPtr _material, bool _unique)
284  {
285  // todo(anyone) take ownership of reference _material if _unique
286  // and destroy the reference material when the mesh is destroyed
287  unsigned int count = this->SubMeshCount();
288  _material = (_unique && count > 0) ? _material->Clone() : _material;
289 
290  for (unsigned int i = 0; i < count; ++i)
291  {
292  SubMeshPtr subMesh = this->SubMeshByIndex(i);
293  subMesh->SetMaterial(_material, false);
294  }
295 
296  if (this->material && this->ownsMaterial)
297  this->Scene()->DestroyMaterial(this->material);
298 
299  this->ownsMaterial = _unique;
300  this->material = _material;
301  }
302 
304  template <class T>
306  {
307  unsigned int count = this->SubMeshCount();
308 
309  for (unsigned int i = 0; i < count; ++i)
310  {
311  SubMeshPtr subMesh = this->SubMeshByIndex(i);
312  subMesh->PreRender();
313  }
314 
315  T::PreRender();
316  }
317 
319  template <class T>
321  {
322  T::Destroy();
323  this->SubMeshes()->DestroyAll();
324  if (this->material && this->ownsMaterial)
325  this->Scene()->DestroyMaterial(this->material);
326  this->material.reset();
327  }
328 
330  // BaseSubMesh
332  template <class T>
334  {
335  }
336 
338  template <class T>
340  {
341  }
342 
344  template <class T>
346  {
347  T::Destroy();
348  if (this->material && this->ownsMaterial)
349  this->Scene()->DestroyMaterial(this->material);
350  this->material.reset();
351  }
352 
353 
355  template <class T>
356  void BaseSubMesh<T>::SetMaterial(const std::string &_name, bool _unique)
357  {
358  MaterialPtr mat = this->Scene()->Material(_name);
359  if (mat) this->SetMaterial(mat, _unique);
360  }
361 
363  template <class T>
364  void BaseSubMesh<T>::SetMaterial(MaterialPtr _material, bool _unique)
365  {
366  _material = (_unique) ? _material->Clone() : _material;
367 
368  MaterialPtr origMaterial = this->material;
369  bool origUnique = this->ownsMaterial;
370 
371  this->SetMaterialImpl(_material);
372 
373  if (origMaterial && origUnique)
374  this->Scene()->DestroyMaterial(origMaterial);
375 
376  this->material = _material;
377  this->ownsMaterial = _unique;
378  }
379 
381  template <class T>
383  {
384  return this->material;
385  }
386 
388  template <class T>
390  {
391  T::PreRender();
392  if (this->Material())
393  this->Material()->PreRender();
394  }
395  }
396  }
397 }
398 #endif
virtual bool SkeletonAnimationEnabled(const std::string &_name) const override
Get whether a skeleton animation is enabled or not.
Definition: BaseMesh.hh:217
virtual void DestroyMaterial(MaterialPtr _material)=0
Unregister and destroy a material.
virtual ~BaseMesh()
Definition: BaseMesh.hh:164
virtual SubMeshPtr SubMeshByIndex(unsigned int _index) const override
Get sub-mesh at given index.
Definition: BaseMesh.hh:259
virtual void SetSkeletonWeights(const std::unordered_map< std::string, float > &_weights) override
Set skeleton node weight.
Definition: BaseMesh.hh:201
Definition: BaseMesh.hh:118
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:356
virtual bool HasSubMeshName(const std::string &_name) const override
Determine if has sub-mesh with given name.
Definition: BaseMesh.hh:245
MaterialPtr material
Pointer to currently assigned material.
Definition: BaseMesh.hh:151
virtual bool HasSkeleton() const override
Check whether the mesh has skeleton.
Definition: BaseMesh.hh:170
T endl(T... args)
virtual unsigned int SubMeshCount() const override
Get the sub-mesh count.
Definition: BaseMesh.hh:231
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:275
virtual bool HasSubMesh(ConstSubMeshPtr _subMesh) const override
Determine if has given sub-mesh.
Definition: BaseMesh.hh:238
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:389
virtual std::map< std::string, math::Matrix4d > SkeletonLocalTransforms() const override
Get the skeleton local transforms.
Definition: BaseMesh.hh:178
STL class.
STL class.
virtual MaterialPtr Material() const override
Get the material of this geometry.
Definition: BaseMesh.hh:266
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:305
virtual std::unordered_map< std::string, float > SkeletonWeights() const override
Get skeleton node weight.
Definition: BaseMesh.hh:193
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:382
virtual ~BaseSubMesh()
Definition: BaseMesh.hh:339
BaseSubMesh()
Definition: BaseMesh.hh:333
virtual void Destroy() override
Destroy any resources associated with this object. Invoking any other functions after destroying an o...
Definition: BaseMesh.hh:320
virtual void Destroy() override
Destroy any resources associated with this object. Invoking any other functions after destroying an o...
Definition: BaseMesh.hh:345
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
virtual void SetSkeletonLocalTransforms(const std::map< std::string, math::Matrix4d > &_tfs) override
Set transforms for the skeleton.
Definition: BaseMesh.hh:186
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:210
MaterialPtr material
Pointer to currently assigned material.
Definition: BaseMesh.hh:113
Represents a surface material of a Geometry.
Definition: Material.hh:47
shared_ptr< Material > MaterialPtr
Shared pointer to Material.
Definition: RenderTypes.hh:166
BaseMesh()
Definition: BaseMesh.hh:158
virtual SubMeshPtr SubMeshByName(const std::string &_name) const override
Get sub-mesh with given name.
Definition: BaseMesh.hh:252
virtual void UpdateSkeletonAnimation(std::chrono::steady_clock::duration _time) override
Play the active skeleton animation to the specified time.
Definition: BaseMesh.hh:224
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...