TDME2  1.9.200
SpecularMaterialProperties.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 #include <tdme/math/Math.h>
11 
12 using std::string;
13 
18 using tdme::math::Math;
20 
21 SpecularMaterialProperties::SpecularMaterialProperties()
22 {
23  ambientColor.set(0.2f, 0.2f, 0.2f, 0.0f);
24  diffuseColor.set(0.8f, 0.8f, 0.8f, 1.0f);
25  specularColor.set(0.0f, 0.0f, 0.0f, 0.0f);
26  emissionColor.set(0.0f, 0.0f, 0.0f, 0.0f);
27  shininess = 0.0f;
28  reflection = 0.0f;
29  diffuseTexture = nullptr;
33  specularTexture = nullptr;
34  normalTexture = nullptr;
35  textureAtlasSize = 1;
36 }
37 
41  if (normalTexture != nullptr) normalTexture->releaseReference();
42 }
43 
45  if (this->diffuseTexture == diffuseTexture) return;
46  if (this->diffuseTexture != nullptr) this->diffuseTexture->releaseReference();
47  diffuseTexturePathName.clear();
51  this->diffuseTexture = diffuseTexture;
53 }
54 
55 void SpecularMaterialProperties::setDiffuseTexture(const string& pathName, const string& fileName, const string& transparencyPathName, const string& transparencyFileName)
56 {
58  diffuseTexture = nullptr;
59  // load diffuse texture
60  diffuseTexturePathName = pathName;
61  diffuseTextureFileName = fileName;
62  diffuseTransparencyTexturePathName = transparencyPathName;
63  diffuseTransparencyTextureFileName = transparencyFileName;
64  if (diffuseTransparencyTextureFileName.size() > 0) {
65  diffuseTexture = TextureReader::read2(
70  true,
71  textureAtlasSize > 1?false:true
72  );
74  } else {
75  diffuseTexture = TextureReader::read(
78  true,
79  textureAtlasSize > 1?false:true
80  );
82  }
84 }
85 
87 {
89  auto diffuseTextureMaskedTransparencyTest = true;
90  if (diffuseTexture != nullptr && diffuseTexture->getRGBDepthBitsPerPixel() == 32) {
91  auto textureData = diffuseTexture->getRGBTextureData();
92  for (auto i = 0; i < diffuseTexture->getTextureWidth() * diffuseTexture->getTextureHeight(); i++) {
93  auto alpha = textureData.get(i * 4 + 3);
94  if (alpha != 255) diffuseTextureTransparency = true;
95  if (alpha > 5 || alpha < 250) diffuseTextureMaskedTransparencyTest = false;
96  }
97  }
98  if (diffuseTextureMaskedTransparency == false) diffuseTextureMaskedTransparency = diffuseTextureMaskedTransparencyTest;
99 }
100 
102  if (this->specularTexture == specularTexture) return;
103  if (this->specularTexture != nullptr) this->specularTexture->releaseReference();
104  specularTexturePathName.clear();
106  this->specularTexture = specularTexture;
107 }
108 
109 void SpecularMaterialProperties::setSpecularTexture(const string& pathName, const string& fileName)
110 {
112  specularTexture = nullptr;
113  specularTexturePathName = pathName;
114  specularTextureFileName = fileName;
115  specularTexture = TextureReader::read(pathName, fileName);
116 }
117 
119  if (this->normalTexture == normalTexture) return;
120  if (this->normalTexture != nullptr) this->normalTexture->releaseReference();
121  normalTexturePathName.clear();
123  this->normalTexture = normalTexture;
124 }
125 
126 void SpecularMaterialProperties::setNormalTexture(const string& pathName, const string& fileName)
127 {
128  if (normalTexture != nullptr) normalTexture->releaseReference();
129  normalTexturePathName = pathName;
130  normalTextureFileName = fileName;
131  normalTexture = TextureReader::read(pathName, fileName);
132 }
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
void setAtlasSize(uint16_t atlasSize)
Set atlas size.
Definition: Texture.h:377
Represents specular material properties.
void checkDiffuseTextureTransparency()
Checks and set ups diffuse texture transparency.
void setSpecularTexture(Texture *specularTexture)
Set up a specular texture by the texture itself.
void setDiffuseTexture(Texture *diffuseTexture)
Set up a diffuse texture by the texture itself.
void setNormalTexture(Texture *normalTexture)
Set up a normal texture by the texture itself.
Standard math functions.
Definition: Math.h:19
uint8_t get(int64_t position) const
Definition: Buffer.h:107
Byte buffer class.
Definition: ByteBuffer.h:27
virtual void releaseReference()
Releases a reference, thus decrementing the counter and delete it if reference counter is zero.
Definition: Reference.h:38