Gazebo Rendering

API Reference

9.4.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 the same material is being set, return early and don't try
312 // to destroy the material. We still need call SetMaterial on the
313 // submeshes in case the user changed some material properties
314 // before setting it back.
315 if (!_unique && _material == this->material)
316 return;
317
318 if (this->material && this->ownsMaterial)
319 this->Scene()->DestroyMaterial(this->material);
320
321 this->ownsMaterial = _unique;
322 this->material = _material;
323 }
324
326 template <class T>
328 {
329 unsigned int count = this->SubMeshCount();
330
331 for (unsigned int i = 0; i < count; ++i)
332 {
333 SubMeshPtr subMesh = this->SubMeshByIndex(i);
334 subMesh->PreRender();
335 }
336
337 T::PreRender();
338 }
339
341 template <class T>
343 {
344 if (!this->Scene())
345 {
346 gzerr << "Cloning a mesh failed because the mesh to be "
347 << "cloned does not belong to a scene.\n";
348 return nullptr;
349 }
350 else if (this->meshDescriptor.meshName.empty())
351 {
352 gzerr << "Cloning a geometry failed because the name of the mesh is "
353 << "missing.\n";
354 return nullptr;
355 }
356
357 auto result = this->Scene()->CreateMesh(this->meshDescriptor);
358 if (result)
359 {
360 if (this->Material())
361 {
362 // this call will set the material for the mesh and its submeshes
363 result->SetMaterial(this->Material());
364 }
365 else
366 {
367 // if the mesh doesn't have a material, clone any existing submesh
368 // materials
369 for (unsigned int i = 0; i < this->SubMeshCount(); ++i)
370 {
371 auto existingSubMeshMaterial = this->SubMeshByIndex(i)->Material();
372 if (existingSubMeshMaterial)
373 result->SubMeshByIndex(i)->SetMaterial(existingSubMeshMaterial);
374 }
375 }
376 }
377
378 return result;
379 }
380
382 template <class T>
384 {
385 return this->meshDescriptor;
386 }
387
389 template <class T>
391 {
392 this->meshDescriptor = _desc;
393 }
394
396 template <class T>
398 {
399 T::Destroy();
400 this->SubMeshes()->DestroyAll();
401 if (this->material && this->ownsMaterial)
402 this->Scene()->DestroyMaterial(this->material);
403 this->material.reset();
404 this->meshDescriptor = MeshDescriptor();
405 }
406
408 // BaseSubMesh
410 template <class T>
414
416 template <class T>
420
422 template <class T>
424 {
425 T::Destroy();
426 if (this->material && this->ownsMaterial)
427 this->Scene()->DestroyMaterial(this->material);
428 this->material.reset();
429 }
430
431
433 template <class T>
434 void BaseSubMesh<T>::SetMaterial(const std::string &_name, bool _unique)
435 {
436 MaterialPtr mat = this->Scene()->Material(_name);
437 if (mat) this->SetMaterial(mat, _unique);
438 }
439
441 template <class T>
442 void BaseSubMesh<T>::SetMaterial(MaterialPtr _material, bool _unique)
443 {
444 _material = (_unique) ? _material->Clone() : _material;
445
446 MaterialPtr origMaterial = this->material;
447 bool origUnique = this->ownsMaterial;
448
449 this->SetMaterialImpl(_material);
450
451 // If the same material is being set, return early and don't try
452 // to destroy the material. We still need to call SetMaterialImpl
453 // above in case the user changed some material properties
454 // before setting it back.
455 if (!_unique && _material == origMaterial)
456 return;
457
458 if (origMaterial && origUnique)
459 this->Scene()->DestroyMaterial(origMaterial);
460
461 this->material = _material;
462 this->ownsMaterial = _unique;
463 }
464
466 template <class T>
468 {
469 return this->material;
470 }
471
473 template <class T>
475 {
476 T::PreRender();
477 if (this->Material())
478 this->Material()->PreRender();
479 }
480 }
481 }
482}
483#endif