TDME2  1.9.200
PBRMaterialProperties.cpp
Go to the documentation of this file.
2 
3 #include <string>
4 
5 #include <tdme/tdme.h>
6 #include <tdme/engine/Texture.h>
8 #include <tdme/engine/Color4.h>
9 
10 using std::string;
11 
16 
17 PBRMaterialProperties::PBRMaterialProperties()
18 {
19  baseColorFactor.set(1.0f, 1.0f, 1.0f, 1.0f);
20  baseColorTexture = nullptr;
24  metallicFactor = 1.0f;
25  roughnessFactor = 1.0f;
26  metallicRoughnessTexture = nullptr;
27  normalScale = 1.0f;
28  normalTexture = nullptr;
29  emissiveTexture = nullptr;
30  emissiveFactor.set(1.0f, 1.0f, 1.0f, 1.0f);
31  exposure = 1.0f;
32 }
33 
37  if (normalTexture != nullptr) normalTexture->releaseReference();
39 }
40 
42  if (this->baseColorTexture == baseColorTexture) return;
43  if (this->baseColorTexture != nullptr) this->baseColorTexture->releaseReference();
46  this->baseColorTexture = baseColorTexture;
48 }
49 
50 void PBRMaterialProperties::setBaseColorTexture(const string& pathName, const string& fileName)
51 {
53  baseColorTexturePathName = pathName;
54  baseColorTextureFileName = fileName;
55  baseColorTexture = TextureReader::read(pathName, fileName);
57 }
58 
60  if (this->metallicRoughnessTexture == metallicRoughnessTexture) return;
61  if (this->metallicRoughnessTexture != nullptr) this->metallicRoughnessTexture->releaseReference();
64  this->metallicRoughnessTexture = metallicRoughnessTexture;
65 }
66 
67 void PBRMaterialProperties::setMetallicRoughnessTexture(const string& pathName, const string& fileName)
68 {
72  metallicRoughnessTexture = TextureReader::read(pathName, fileName);
73 }
74 
76  if (this->normalTexture == normalTexture) return;
77  if (this->normalTexture != nullptr) this->normalTexture->releaseReference();
78  normalTexturePathName.clear();
80  this->normalTexture = normalTexture;
81 }
82 
83 void PBRMaterialProperties::setNormalTexture(const string& pathName, const string& fileName)
84 {
85  if (normalTexture != nullptr) normalTexture->releaseReference();
86  normalTexturePathName = pathName;
87  normalTextureFileName = fileName;
88  normalTexture = TextureReader::read(pathName, fileName);
89 }
90 
92  if (this->emissiveTexture == emissiveTexture) return;
93  if (this->emissiveTexture != nullptr) this->emissiveTexture->releaseReference();
96  this->emissiveTexture = emissiveTexture;
97 }
98 
99 void PBRMaterialProperties::setEmissiveTexture(const string& pathName, const string& fileName) {
101  emissiveTexturePathName = pathName;
102  emissiveTextureFileName = fileName;
103  emissiveTexture = TextureReader::read(pathName, fileName);
104 }
105 
107 {
109  auto baseColorTextureMaskedTransparencyTest = true;
110  if (baseColorTexture != nullptr && baseColorTexture->getRGBDepthBitsPerPixel() == 32) {
111  auto textureData = baseColorTexture->getRGBTextureData();
112  for (auto i = 0; i < baseColorTexture->getTextureWidth() * baseColorTexture->getTextureHeight(); i++) {
113  auto alpha = textureData.get(i * 4 + 3);
114  if (alpha != 255) baseColorTextureTransparency = true;
115  if (alpha > 5 || alpha < 250) baseColorTextureMaskedTransparencyTest = false;
116  }
117  }
118  if (baseColorTextureMaskedTransparency == false) baseColorTextureMaskedTransparency = baseColorTextureMaskedTransparencyTest;
119 }
Color 4 definition class.
Definition: Color4.h:18
void set(float r, float g, float b, float a)
Sets this color by its components.
Definition: Color4.h:66
Texture entity.
Definition: Texture.h:24
ByteBuffer getRGBTextureData()
Definition: Texture.h:253
uint8_t getRGBDepthBitsPerPixel() const
Definition: Texture.h:186
const string & getId() const
Definition: Texture.h:172
uint16_t getTextureHeight() const
Definition: Texture.h:211
uint16_t getTextureWidth() const
Definition: Texture.h:218
Represents specular material properties.
void setBaseColorTexture(Texture *baseColorTexture)
Set up a base color texture by the texture itself.
void setEmissiveTexture(Texture *emissiveTexture)
Set up a emissive texture by the texture itself.
void setMetallicRoughnessTexture(Texture *metallicRoughnessTexture)
Set up a metallic roughness texture by the texture itself.
void setNormalTexture(Texture *normalTexture)
Set up a normal texture by the texture itself.
void checkBaseColorTextureTransparency()
Checks and set ups base color texture transparency.
uint8_t get(int64_t position) const
Definition: Buffer.h:107
virtual void releaseReference()
Releases a reference, thus decrementing the counter and delete it if reference counter is zero.
Definition: Reference.h:38