Gazebo Rendering

API Reference

9.3.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 GZ_RENDERING_BASE_BASEMESH_HH_
18#define GZ_RENDERING_BASE_BASEMESH_HH_
19
20#include <map>
21#include <string>
22#include <unordered_map>
23
24#include <gz/common/Console.hh>
25
26#include "gz/rendering/Mesh.hh"
30
31namespace gz
32{
33 namespace rendering
34 {
35 inline namespace GZ_RENDERING_VERSION_NAMESPACE {
36 //
38 template <class T>
39 class BaseMesh :
40 public virtual Mesh,
41 public virtual T
42 {
43 protected: BaseMesh();
44
45 public: virtual ~BaseMesh();
46
47 // Documentation inherited.
48 public: virtual bool HasSkeleton() const override;
49
50 // Documentation inherited.
52 SkeletonLocalTransforms() const override;
53
54 // Documentation inherited.
55 public: virtual void SetSkeletonLocalTransforms(
57 override;
58
59 // Documentation inherited.
61 const override;
62
63 // Documentation inherited.
64 public: virtual void SetSkeletonWeights(
66 override;
67
68 // Documentation inherited.
69 public: virtual void SetSkeletonAnimationEnabled(const std::string &_name,
70 bool _enabled, bool _loop = true, float _weight = 1.0) override;
71
72 // Documentation inherited.
73 public: virtual bool SkeletonAnimationEnabled(const std::string &_name)
74 const override;
75
76 // Documentation inherited.
77 public: virtual void UpdateSkeletonAnimation(
78 std::chrono::steady_clock::duration _time) override;
79
80 public: virtual unsigned int SubMeshCount() const override;
81
82 public: virtual bool HasSubMesh(ConstSubMeshPtr _subMesh) const override;
83
84 public: virtual bool HasSubMeshName(const std::string &_name) const
85 override;
86
87 public: virtual SubMeshPtr SubMeshByName(
88 const std::string &_name) const override;
89
90 public: virtual SubMeshPtr SubMeshByIndex(unsigned int _index) const
91 override;
92
93 // Documentation inherited.
94 public: virtual MaterialPtr Material() const override;
95
96 // Documentation inherited.
97 public: virtual void SetMaterial(const std::string &_name,
98 bool _unique = true) override;
99
100 // Documentation inherited.
101 public: virtual void SetMaterial(MaterialPtr _material,
102 bool _unique = true) override;
103
104 public: virtual void PreRender() override;
105
106 // Documentation inherited.
107 public: virtual GeometryPtr Clone() const override;
108
109 // Documentation inherited.
110 public: void SetDescriptor(const MeshDescriptor &_desc) override;
111
112 // Documentation inherited.
113 public: const MeshDescriptor &Descriptor() const override;
114
115 // Documentation inherited
116 public: virtual void Destroy() override;
117
118 protected: virtual SubMeshStorePtr SubMeshes() const = 0;
119
122 protected: bool ownsMaterial = false;
123
126
129 };
130
132 template <class T>
134 public virtual SubMesh,
135 public virtual T
136 {
137 protected: BaseSubMesh();
138
139 public: virtual ~BaseSubMesh();
140
141 // Documentation inherited
142 public: virtual MaterialPtr Material() const override;
143
144 // Documentation inherited
145 public: virtual void SetMaterial(const std::string &_name,
146 bool _unique = true) override;
147
148 // Documentation inherited
149 public: virtual void SetMaterial(MaterialPtr _material,
150 bool _unique = true) override;
151
154 public: virtual void SetMaterialImpl(MaterialPtr _material) = 0;
155
156 public: virtual void PreRender() override;
157
158 // Documentation inherited
159 public: virtual void Destroy() override;
160
163 protected: bool ownsMaterial = false;
164
167 };
168
170 // BaseMesh
172 template <class T>
176
178 template <class T>
182
184 template <class T>
186 {
187 return false;
188 }
189
191 template <class T>
194 {
196 return tmpMap;
197 }
198
200 template <class T>
205
207 template <class T>
213
215 template <class T>
218 {
219 gzerr << "SetSkeletonWeights not supported for render engine: "
220 << this->Scene()->Engine()->Name() << std::endl;
221 }
222
224 template <class T>
226 bool, float)
227 {
228 }
229
231 template <class T>
233 {
234 return false;
235 }
236
238 template <class T>
240 std::chrono::steady_clock::duration)
241 {
242 }
243
245 template <class T>
246 unsigned int BaseMesh<T>::SubMeshCount() const
247 {
248 return this->SubMeshes()->Size();
249 }
250
252 template <class T>
254 {
255 return this->SubMeshes()->Contains(_subMesh);
256 }
257
259 template <class T>
261 {
262 return this->SubMeshes()->ContainsName(_name);
263 }
264
266 template <class T>
268 {
269 return this->SubMeshes()->GetByName(_name);
270 }
271
273 template <class T>
274 SubMeshPtr BaseMesh<T>::SubMeshByIndex(unsigned int _index) const
275 {
276 return this->SubMeshes()->GetByIndex(_index);
277 }
278
280 template <class T>
282 {
283 unsigned int count = this->SubMeshCount();
284 return (count > 0) ? this->SubMeshByIndex(0)->Material() :
285 MaterialPtr();
286 }
287
289 template <class T>
290 void BaseMesh<T>::SetMaterial(const std::string &_name, bool _unique)
291 {
292 MaterialPtr mat = this->Scene()->Material(_name);
293 if (mat) this->SetMaterial(mat, _unique);
294 }
295
297 template <class T>
298 void BaseMesh<T>::SetMaterial(MaterialPtr _material, bool _unique)
299 {
300 // todo(anyone) take ownership of reference _material if _unique
301 // and destroy the reference material when the mesh is destroyed
302 unsigned int count = this->SubMeshCount();
303 _material = (_unique && count > 0) ? _material->Clone() : _material;
304
305 for (unsigned int i = 0; i < count; ++i)
306 {
307 SubMeshPtr subMesh = this->SubMeshByIndex(i);
308 subMesh->SetMaterial(_material, false);
309 }
310
311 if (this->material && this->ownsMaterial)
312 this->Scene()->DestroyMaterial(this->material);
313
314 this->ownsMaterial = _unique;
315 this->material = _material;
316 }
317
319 template <class T>
321 {
322 unsigned int count = this->SubMeshCount();
323
324 for (unsigned int i = 0; i < count; ++i)
325 {
326 SubMeshPtr subMesh = this->SubMeshByIndex(i);
327 subMesh->PreRender();
328 }
329
330 T::PreRender();
331 }
332
334 template <class T>
336 {
337 if (!this->Scene())
338 {
339 gzerr << "Cloning a mesh failed because the mesh to be "
340 << "cloned does not belong to a scene.\n";
341 return nullptr;
342 }
343 else if (this->meshDescriptor.meshName.empty())
344 {
345 gzerr << "Cloning a geometry failed because the name of the mesh is "
346 << "missing.\n";
347 return nullptr;
348 }
349
350 auto result = this->Scene()->CreateMesh(this->meshDescriptor);
351 if (result)
352 {
353 if (this->Material())
354 {
355 // this call will set the material for the mesh and its submeshes
356 result->SetMaterial(this->Material());
357 }
358 else
359 {
360 // if the mesh doesn't have a material, clone any existing submesh
361 // materials
362 for (unsigned int i = 0; i < this->SubMeshCount(); ++i)
363 {
364 auto existingSubMeshMaterial = this->SubMeshByIndex(i)->Material();
365 if (existingSubMeshMaterial)
366 result->SubMeshByIndex(i)->SetMaterial(existingSubMeshMaterial);
367 }
368 }
369 }
370
371 return result;
372 }
373
375 template <class T>
377 {
378 return this->meshDescriptor;
379 }
380
382 template <class T>
384 {
385 this->meshDescriptor = _desc;
386 }
387
389 template <class T>
391 {
392 T::Destroy();
393 this->SubMeshes()->DestroyAll();
394 if (this->material && this->ownsMaterial)
395 this->Scene()->DestroyMaterial(this->material);
396 this->material.reset();
397 this->meshDescriptor = MeshDescriptor();
398 }
399
401 // BaseSubMesh
403 template <class T>
407
409 template <class T>
413
415 template <class T>
417 {
418 T::Destroy();
419 if (this->material && this->ownsMaterial)
420 this->Scene()->DestroyMaterial(this->material);
421 this->material.reset();
422 }
423
424
426 template <class T>
427 void BaseSubMesh<T>::SetMaterial(const std::string &_name, bool _unique)
428 {
429 MaterialPtr mat = this->Scene()->Material(_name);
430 if (mat) this->SetMaterial(mat, _unique);
431 }
432
434 template <class T>
435 void BaseSubMesh<T>::SetMaterial(MaterialPtr _material, bool _unique)
436 {
437 _material = (_unique) ? _material->Clone() : _material;
438
439 MaterialPtr origMaterial = this->material;
440 bool origUnique = this->ownsMaterial;
441
442 this->SetMaterialImpl(_material);
443
444 if (origMaterial && origUnique)
445 this->Scene()->DestroyMaterial(origMaterial);
446
447 this->material = _material;
448 this->ownsMaterial = _unique;
449 }
450
452 template <class T>
454 {
455 return this->material;
456 }
457
459 template <class T>
461 {
462 T::PreRender();
463 if (this->Material())
464 this->Material()->PreRender();
465 }
466 }
467 }
468}
469#endif