Gazebo Rendering

API Reference

9.0.0
BaseMaterial.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_BASEMATERIAL_HH_
18#define GZ_RENDERING_BASE_BASEMATERIAL_HH_
19
20#include <memory>
21#include <string>
22
23#include "gz/common/Console.hh"
24
26#include "gz/rendering/Scene.hh"
28
29namespace gz
30{
31 namespace rendering
32 {
33 inline namespace GZ_RENDERING_VERSION_NAMESPACE {
34 //
37
38 template <class T>
40 public virtual Material,
41 public virtual T
42 {
43 protected: BaseMaterial();
44
45 public: virtual ~BaseMaterial();
46
47 // Documentation inherited
48 public: virtual MaterialPtr Clone(const std::string &_name = "") const
49 override;
50
51 // Documentation inherited
52 public: virtual void SetAmbient(const double _r, const double _g,
53 const double _b, const double _a = 1.0) override;
54
55 // Documentation inherited
56 public: virtual void SetAmbient(const math::Color &_color) override;
57
58 // Documentation inherited
59 public: virtual void SetDiffuse(const double _r, const double _g,
60 const double _b, const double _a = 1.0) override;
61
62 // Documentation inherited
63 public: virtual void SetDiffuse(const math::Color &_color) override;
64
65 // Documentation inherited
66 public: virtual void SetSpecular(const double _r, const double _g,
67 const double _b, const double _a = 1.0) override;
68
69 // Documentation inherited
70 public: virtual void SetSpecular(const math::Color &_color) override;
71
72 // Documentation inherited
73 public: virtual void SetEmissive(const double _r, const double _g,
74 const double _b, const double _a = 1.0) override;
75
76 // Documentation inherited
77 public: virtual void SetEmissive(const math::Color &_color) override;
78
79 // Documentation inherited
80 public: virtual void SetTransparency(const double _transparency) override;
81
82 // Documentation inherited
83 public: virtual void SetAlphaFromTexture(bool _enabled,
84 double _alpha = 0.5, bool _twoSided = true) override;
85
86 // Documentation inherited
87 public: bool TextureAlphaEnabled() const override;
88
89 // Documentation inherited
90 public: double AlphaThreshold() const override;
91
92 // Documentation inherited
93 public: bool TwoSidedEnabled() const override;
94
95 // Documentation inherited
96 public: virtual void SetShininess(const double _shininess) override;
97
98 // Documentation inherited
99 public: virtual void SetReflectivity(const double _reflectivity) override;
100
101 // Documentation inherited
102 public: virtual void SetCastShadows(const bool _castShadows) override;
103
104 // Documentation inherited
105 public: virtual void SetReceiveShadows(const bool _receiveShadows)
106 override;
107
108 // Documentation inherited
109 public: virtual void SetReflectionEnabled(const bool _enabled) override;
110
111 // Documentation inherited
112 public: virtual void SetLightingEnabled(const bool _enabled) override;
113
114 // Documentation inherited.
115 public: virtual void SetDepthCheckEnabled(bool _enabled) override;
116
117 // Documentation inherited.
118 public: virtual void SetDepthWriteEnabled(bool _enabled) override;
119
120 // Documentation inherited
121 public: virtual math::Color Ambient() const override;
122
123 // Documentation inherited
124 public: virtual math::Color Diffuse() const override;
125
126 // Documentation inherited
127 public: virtual math::Color Specular() const override;
128
129 // Documentation inherited
130 public: virtual math::Color Emissive() const override;
131
132 // Documentation inherited
133 public: virtual double Transparency() const override;
134
135 // Documentation inherited
136 public: virtual double Reflectivity() const override;
137
138 // Documentation inherited
139 public: virtual double Shininess() const override;
140
141 // Documentation inherited
142 public: virtual bool CastShadows() const override;
143
144 // Documentation inherited
145 public: virtual bool ReceiveShadows() const override;
146
147 // Documentation inherited
148 public: virtual bool LightingEnabled() const override;
149
150 // Documentation inherited
151 public: virtual bool DepthCheckEnabled() const override;
152
153 // Documentation inherited
154 public: virtual bool DepthWriteEnabled() const override;
155
156 // Documentation inherited
157 public: virtual bool ReflectionEnabled() const override;
158
159 // Documentation inherited
160 public: virtual bool HasTexture() const override;
161
162 // Documentation inherited
163 public: virtual std::string Texture() const override;
164
165 // Documentation inherited
166 public: virtual void SetTexture(const std::string &_texture,
167 const std::shared_ptr<const common::Image> &_img) override;
168
169 // Documentation inherited
171 const override;
172
173 // Documentation inherited
174 public: virtual void ClearTexture() override;
175
176 // Documentation inherited
177 public: virtual bool HasNormalMap() const override;
178
179 // Documentation inherited
180 public: virtual std::string NormalMap() const override;
181
182 // Documentation inherited
184 const override;
185
186 // Documentation inherited
187 public: virtual void SetNormalMap(const std::string &_normalMap,
188 const std::shared_ptr<const common::Image> &_img) override;
189
190 // Documentation inherited
191 public: virtual void ClearNormalMap() override;
192
193 // Documentation inherited
194 public: virtual bool HasRoughnessMap() const override;
195
196 // Documentation inherited
197 public: virtual std::string RoughnessMap() const override;
198
199 // Documentation inherited
201 const override;
202
203 // Documentation inherited
204 public: virtual void SetRoughnessMap(const std::string &_roughnessMap,
205 const std::shared_ptr<const common::Image> &_img) override;
206
207 // Documentation inherited
208 public: virtual void ClearRoughnessMap() override;
209
210 // Documentation inherited
211 public: virtual bool HasMetalnessMap() const override;
212
213 // Documentation inherited
214 public: virtual std::string MetalnessMap() const override;
215
216 // Documentation inherited
218 const override;
219
220 // Documentation inherited
221 public: virtual void SetMetalnessMap(const std::string &_metalnessMap,
222 const std::shared_ptr<const common::Image> &_img) override;
223
224 // Documentation inherited
225 public: virtual void ClearMetalnessMap() override;
226
227 // Documentation inherited
228 public: virtual bool HasEnvironmentMap() const override;
229
230 // Documentation inherited
231 public: virtual std::string EnvironmentMap() const override;
232
233 // Documentation inherited
235 const override;
236
237 // Documentation inherited
238 public: virtual void SetEnvironmentMap(const std::string &_environmentMap,
239 const std::shared_ptr<const common::Image> &_img) override;
240
241 // Documentation inherited
242 public: virtual void ClearEnvironmentMap() override;
243
244 // Documentation inherited
245 public: virtual bool HasEmissiveMap() const override;
246
247 // Documentation inherited
248 public: virtual std::string EmissiveMap() const override;
249
250 // Documentation inherited
252 const override;
253
254 // Documentation inherited
255 public: virtual void SetEmissiveMap(const std::string &_emissiveMap,
256 const std::shared_ptr<const common::Image> &_img) override;
257
258 // Documentation inherited
259 public: virtual void ClearEmissiveMap() override;
260
261 // Documentation inherited
262 public: virtual bool HasLightMap() const override;
263
264 // Documentation inherited
265 public: virtual std::string LightMap() const override;
266
267 // Documentation inherited
268 public: virtual unsigned int LightMapTexCoordSet() const override;
269
270 // Documentation inherited
271 public: virtual void SetLightMap(const std::string &_lightMap,
273 unsigned int _uvSet = 0u) override;
274
275 // Documentation inherited
277 const override;
278
279 // Documentation inherited
280 public: virtual void ClearLightMap() override;
281
282 // Documentation inherited
283 public: virtual void SetRenderOrder(const float _renderOrder) override;
284
285 // Documentation inherited
286 public: virtual float RenderOrder() const override;
287
288 // Documentation inherited
289 public: virtual void SetRoughness(const float _roughness) override;
290
291 // Documentation inherited
292 public: virtual float Roughness() const override;
293
294 // Documentation inherited
295 public: virtual void SetMetalness(const float _metalness) override;
296
297 // Documentation inherited
298 public: virtual float Metalness() const override;
299
300 // Documentation inherited
301 public: virtual MaterialType Type() const override;
302
303 public: virtual void SetShaderType(enum ShaderType _type) override
304 {
305 (void)_type;
306 // no op
307 }
308
309 // Documentation inherited
310 public: virtual enum ShaderType ShaderType() const override
311 {
312 return ST_PIXEL;
313 }
314
315 // Documentation inherited.
316 // \sa Material::SetDepthMaterial()
317 public: virtual void SetDepthMaterial(const double far,
318 const double near) override;
319
320 // Documentation inherited.
321 // \sa Material::VertexShader() const
322 public: virtual std::string VertexShader() const override;
323
324 // Documentation inherited.
325 // \sa Material::VertexShaderParams()
326 public: virtual ShaderParamsPtr VertexShaderParams() override;
327
328 // Documentation inherited.
329 // \sa Material::SetVertexShader(const std::string &)
330 public: virtual void SetVertexShader(const std::string &_path) override;
331
332 // Documentation inherited.
333 // \sa Material::FragmentShader() const
334 public: virtual std::string FragmentShader() const override;
335
336 // Documentation inherited.
337 // \sa Material::FragmentShaderParams()
338 public: virtual ShaderParamsPtr FragmentShaderParams() override;
339
340 // Documentation inherited.
341 // \sa Material::SetFragmentShader(const std::string &)
342 public: virtual void SetFragmentShader(const std::string &_path) override;
343
344 // Documentation inherited.
345 public: virtual void CopyFrom(ConstMaterialPtr _material) override;
346
347 // Documentation inherited.
348 public: virtual void CopyFrom(const common::Material &_material) override;
349
350 // Documentation inherited.
351 public: virtual void PreRender() override;
352
353 protected: virtual void Reset();
354
357
360
363
366
368 protected: double transparency = 0.0;
369
371 protected: bool textureAlphaEnabled = false;
372
374 protected: double alphaThreshold = 0.5;
375
377 protected: bool twoSidedEnabled = false;
378
380 protected: double renderOrder = 0.0;
381
383 protected: double shininess = 0.0;
384
386 protected: double reflectivity = 0.0;
387
389 protected: bool lightingEnabled = false;
390
392 protected: bool depthCheckEnabled = true;
393
395 protected: bool depthWriteEnabled = true;
396
398 protected: bool reflectionEnabled = false;
399
401 protected: bool receiveShadows = true;
402
404 protected: bool castShadows = true;
405 };
406
408 template <class T>
412
414 template <class T>
418
420 template <class T>
421 void BaseMaterial<T>::SetAmbient(const double _r, const double _g,
422 const double _b, const double _a)
423 {
424 this->SetAmbient(math::Color(_r, _g, _b, _a));
425 }
426
428 template <class T>
430 {
431 this->ambient = _color;
432 }
433
435 template <class T>
436 void BaseMaterial<T>::SetDiffuse(const double _r, const double _g,
437 const double _b, const double _a)
438 {
439 this->SetDiffuse(math::Color(_r, _g, _b, _a));
440 }
441
443 template <class T>
445 {
446 this->diffuse = _color;
447 }
448
450 template <class T>
451 void BaseMaterial<T>::SetSpecular(const double _r, const double _g,
452 const double _b, const double _a)
453 {
454 this->SetSpecular(math::Color(_r, _g, _b, _a));
455 }
456
458 template <class T>
460 {
461 this->specular = _color;
462 }
463
465 template <class T>
466 void BaseMaterial<T>::SetEmissive(const double _r, const double _g,
467 const double _b, const double _a)
468 {
469 this->SetEmissive(math::Color(_r, _g, _b, _a));
470 }
471
473 template <class T>
475 {
476 this->emissive = _color;
477 }
478
480 template <class T>
481 void BaseMaterial<T>::SetShininess(const double _shininess)
482 {
483 this->shininess = _shininess;
484 }
485
487 template <class T>
488 void BaseMaterial<T>::SetTransparency(const double _transparency)
489 {
490 this->transparency = _transparency;
491 }
492
494 template <class T>
495 void BaseMaterial<T>::SetAlphaFromTexture(bool _enabled, double _alpha,
496 bool _twoSided)
497 {
498 this->textureAlphaEnabled = _enabled;
499 this->alphaThreshold = _alpha;
500 this->twoSidedEnabled = _twoSided;
501 }
502
504 template <class T>
506 {
507 return this->textureAlphaEnabled;
508 }
509
511 template <class T>
513 {
514 return this->alphaThreshold;
515 }
516
518 template <class T>
520 {
521 return this->twoSidedEnabled;
522 }
523
525 template <class T>
526 void BaseMaterial<T>::SetReflectivity(const double _reflectivity)
527 {
528 this->reflectivity = _reflectivity;
529 }
530
532 template <class T>
534 {
535 this->reflectionEnabled = _enabled;
536 }
537
539 template <class T>
540 void BaseMaterial<T>::SetLightingEnabled(const bool _enabled)
541 {
542 this->lightingEnabled = _enabled;
543 }
544
546 template <class T>
548 {
549 this->depthCheckEnabled = _enabled;
550 }
551
553 template <class T>
555 {
556 this->depthWriteEnabled = _enabled;
557 }
558
560 template <class T>
561 void BaseMaterial<T>::SetCastShadows(const bool _castShadows)
562 {
563 this->castShadows = _castShadows;
564 }
565
567 template <class T>
568 void BaseMaterial<T>::SetReceiveShadows(const bool _receive)
569 {
570 this->receiveShadows = _receive;
571 }
572
574 template <class T>
575 void BaseMaterial<T>::SetRenderOrder(const float _renderorder)
576 {
577 this->renderOrder = _renderorder;
578 }
579
581 template <class T>
583 {
584 return this->ambient;
585 }
586
588 template <class T>
590 {
591 return this->diffuse;
592 }
593
595 template <class T>
597 {
598 return this->specular;
599 }
600
602 template <class T>
604 {
605 return this->emissive;
606 }
607
609 template <class T>
611 {
612 return this->shininess;
613 }
614
616 template <class T>
618 {
619 return this->transparency;
620 }
621
623 template <class T>
625 {
626 return this->renderOrder;
627 }
628
630 template <class T>
632 {
633 return this->reflectivity;
634 }
635
637 template <class T>
639 {
640 return this->castShadows;
641 }
642
644 template <class T>
646 {
647 return this->receiveShadows;
648 }
649
651 template <class T>
653 {
654 return this->lightingEnabled;
655 }
656
658 template <class T>
660 {
661 return this->depthCheckEnabled;
662 }
663
665 template <class T>
667 {
668 return this->depthWriteEnabled;
669 }
670
672 template <class T>
674 {
675 return this->reflectionEnabled;
676 }
677
679 template <class T>
681 {
682 return MT_CLASSIC;
683 }
684
686 template <class T>
688 {
689 return std::string();
690 }
691
693 template <class T>
695 {
696 return nullptr;
697 }
698
700 template <class T>
702 {
703 // no op
704 }
705
707 template <class T>
709 {
710 return std::string();
711 }
712
714 template <class T>
719
721 template <class T>
723 {
724 // no op
725 }
726
728 template <class T>
730 {
731 return false;
732 }
733
735 template <class T>
737 {
738 return std::string();
739 }
740
742 template <class T>
745 {
746 // no op
747 }
748
750 template <class T>
755
757 template <class T>
759 {
760 // no op
761 }
762
764 template <class T>
766 {
767 return false;
768 }
769
771 template <class T>
773 {
774 return std::string();
775 }
776
778 template <class T>
783
785 template <class T>
788 {
789 // no op
790 }
791
793 template <class T>
795 {
796 // no op
797 }
798
800 template <class T>
802 {
803 return false;
804 }
805
807 template <class T>
809 {
810 return std::string();
811 }
812
814 template <class T>
820
822 template <class T>
825 {
826 // no op
827 }
828
830 template <class T>
832 {
833 // no op
834 }
835
837 template <class T>
839 {
840 return false;
841 }
842
844 template <class T>
846 {
847 return std::string();
848 }
849
851 template <class T>
857
859 template <class T>
862 {
863 // no op
864 }
865
867 template <class T>
869 {
870 // no op
871 }
872
874 template <class T>
876 {
877 return false;
878 }
879
881 template <class T>
883 {
884 return std::string();
885 }
886
888 template <class T>
894
896 template <class T>
902
904 template <class T>
906 {
907 // no op
908 }
909
911 template <class T>
913 {
914 return false;
915 }
916
918 template <class T>
920 {
921 return std::string();
922 }
923
925 template <class T>
931
933 template <class T>
936 {
937 // no op
938 }
939
941 template <class T>
943 {
944 // no op
945 }
946
948 template <class T>
950 {
951 return false;
952 }
953
955 template <class T>
957 {
958 return std::string();
959 }
960
962 template <class T>
964 {
965 return 0u;
966 }
967
969 template <class T>
972 unsigned int)
973 {
974 // no op
975 }
976
978 template <class T>
983
985 template <class T>
987 {
988 // no op
989 }
990
992 template <class T>
994 {
995 // no op
996 }
997
999 template <class T>
1001 {
1002 return 0.0f;
1003 }
1004
1006 template <class T>
1008 {
1009 // no op
1010 }
1011
1013 template <class T>
1015 {
1016 return 0.0f;
1017 }
1018
1020 template <class T>
1022 {
1023 auto baseShared = this->shared_from_this();
1024
1025 auto thisShared =
1027
1028 MaterialPtr material = T::Scene()->CreateMaterial(_name);
1029 material->CopyFrom(thisShared);
1030 return material;
1031 }
1032
1034 template <class T>
1036 {
1037 this->SetLightingEnabled(_material->LightingEnabled());
1038 this->SetAmbient(_material->Ambient());
1039 this->SetDiffuse(_material->Diffuse());
1040 this->SetSpecular(_material->Specular());
1041 this->SetEmissive(_material->Emissive());
1042 this->SetRenderOrder(_material->RenderOrder());
1043 this->SetShininess(_material->Shininess());
1044 this->SetAlphaFromTexture(_material->TextureAlphaEnabled(),
1045 _material->AlphaThreshold(), _material->TwoSidedEnabled());
1046 // override transparency / blend setting after setting alpha from texture
1047 this->SetTransparency(_material->Transparency());
1048 // override depth check / depth write after setting transparency
1049 this->SetDepthCheckEnabled(_material->DepthCheckEnabled());
1050 this->SetDepthWriteEnabled(_material->DepthWriteEnabled());
1051 this->SetReflectivity(_material->Reflectivity());
1052 this->SetCastShadows(_material->CastShadows());
1053 this->SetReceiveShadows(_material->ReceiveShadows());
1054 this->SetReflectionEnabled(_material->ReflectionEnabled());
1055 this->SetTexture(_material->Texture(), _material->TextureData());
1056 this->SetNormalMap(_material->NormalMap(), _material->NormalMapData());
1057 this->SetRoughnessMap(_material->RoughnessMap(),
1058 _material->RoughnessMapData());
1059 this->SetMetalnessMap(_material->MetalnessMap(),
1060 _material->MetalnessMapData());
1061 this->SetRoughness(_material->Roughness());
1062 this->SetMetalness(_material->Metalness());
1063 this->SetEnvironmentMap(_material->EnvironmentMap(),
1064 _material->EnvironmentMapData());
1065 this->SetEmissiveMap(_material->EmissiveMap(),
1066 _material->EmissiveMapData());
1067 this->SetLightMap(_material->LightMap(), _material->LightMapData(),
1068 _material->LightMapTexCoordSet());
1069 this->SetShaderType(_material->ShaderType());
1070 this->SetVertexShader(_material->VertexShader());
1071 this->SetFragmentShader(_material->FragmentShader());
1072 }
1073
1075 template <class T>
1077 {
1078 this->SetLightingEnabled(_material.Lighting());
1079 this->SetAmbient(_material.Ambient());
1080 this->SetDiffuse(_material.Diffuse());
1081 this->SetSpecular(_material.Specular());
1082 this->SetEmissive(_material.Emissive());
1083 this->SetShininess(_material.Shininess());
1084 this->SetTransparency(_material.Transparency());
1085 this->SetAlphaFromTexture(_material.TextureAlphaEnabled(),
1086 _material.AlphaThreshold(), _material.TwoSidedEnabled());
1087 this->SetRenderOrder(_material.RenderOrder());
1088 // TODO(anyone): update common::Material
1089 this->SetReflectivity(0);
1090 this->SetTexture(_material.TextureImage(), _material.TextureData());
1091 // TODO(anyone): update common::Material
1092 this->SetCastShadows(true);
1093 // TODO(anyone): update common::Material
1094 this->SetReceiveShadows(true);
1095 // TODO(anyone): update common::Material
1096 this->SetReflectionEnabled(true);
1097 // TODO(anyone): update common::Material
1098 this->ClearNormalMap();
1099 // TODO(anyone): update common::Material
1100 this->SetShaderType(ST_PIXEL);
1101
1102 const common::Pbr *pbrMat = _material.PbrMaterial();
1103 if (!pbrMat)
1104 pbrMat = &kDefaultPbr;
1105 this->SetNormalMap(pbrMat->NormalMap(), pbrMat->NormalMapData());
1106 this->SetRoughnessMap(pbrMat->RoughnessMap(), pbrMat->RoughnessMapData());
1107 this->SetMetalnessMap(pbrMat->MetalnessMap(), pbrMat->MetalnessMapData());
1108 this->SetRoughness(pbrMat->Roughness());
1109 this->SetMetalness(pbrMat->Metalness());
1110 // TODO(anyone): update when pbrMat has EnvironmentMapData API
1111 this->SetEnvironmentMap(pbrMat->EnvironmentMap(), nullptr);
1112 this->SetEmissiveMap(pbrMat->EmissiveMap(), pbrMat->EmissiveMapData());
1113 this->SetLightMap(pbrMat->LightMap(), pbrMat->LightMapData(),
1114 pbrMat->LightMapTexCoordSet());
1115 }
1116
1118 template <class T>
1120 {
1121 // do nothing
1122 }
1123
1125 template <class T>
1126 void BaseMaterial<T>::SetDepthMaterial(const double /*far*/,
1127 const double /*near*/)
1128 {
1129 // do nothing
1130 }
1131
1133 template <class T>
1135 {
1136 this->SetLightingEnabled(true);
1137 this->SetDepthCheckEnabled(true);
1138 this->SetDepthWriteEnabled(true);
1139 this->SetAmbient(0.3, 0.3, 0.3);
1140 this->SetDiffuse(1.0, 1.0, 1.0);
1141 this->SetSpecular(0.2, 0.2, 0.2);
1142 this->SetEmissive(0, 0, 0);
1143 this->SetRenderOrder(0);
1144 this->SetShininess(1.5);
1145 this->SetTransparency(0);
1146 this->SetReflectivity(0);
1147 this->SetCastShadows(true);
1148 this->SetReceiveShadows(true);
1149 this->SetReflectionEnabled(true);
1150 this->ClearTexture();
1151 this->ClearNormalMap();
1152 this->ClearRoughnessMap();
1153 this->ClearMetalnessMap();
1154 this->ClearEmissiveMap();
1155 this->ClearLightMap();
1156 this->SetRoughness(kDefaultPbr.Roughness());
1157 this->SetMetalness(kDefaultPbr.Metalness());
1158 this->SetShaderType(ST_PIXEL);
1159 }
1160 }
1161 }
1162}
1163#endif