TDME2  1.9.200
LightingShaderPBRBaseImplementation.cpp
Go to the documentation of this file.
2 
3 #include <string>
4 
5 #include <tdme/tdme.h>
11 #include <tdme/engine/Engine.h>
12 #include <tdme/engine/Texture.h>
13 #include <tdme/engine/Timing.h>
14 #include <tdme/math/Matrix4x4.h>
15 #include <tdme/math/Vector3.h>
16 #include <tdme/utilities/Console.h>
17 #include <tdme/utilities/Float.h>
18 
19 using std::string;
20 using std::to_string;
21 
35 
36 LightingShaderPBRBaseImplementation::LightingShaderPBRBaseImplementation(Renderer* renderer)
37 {
38  this->renderer = renderer;
39  initialized = false;
40 }
41 
43 {
44  return initialized;
45 }
46 
48 {
49 
50  // link program
51  if (renderer->linkProgram(programId) == false) return;
52 
53  // uniforms
55  if (uniformBaseColorFactor == -1) return;
57  if (uniformBaseColorSampler == -1) return;
59  if (uniformBaseColorSamplerAvailable == -1) return;
61  if (uniformAlphaCutoffEnabled == -1) return;
63  if (uniformAlphaCutoff == -1) return;
66  if (uniformExposure == -1) return;
68  if (uniformMetallicFactor == -1) return;
70  if (uniformMetallicRoughnessSampler == -1) return;
74  if (uniformRoughnessFactor == -1) return;
76  if (uniformNormalSampler == -1) return;
78  if (uniformNormalSamplerAvailable == -1) return;
80  if (uniformNormalScale == -1) return;
82  // TODO
83  // if (uniformEmissiveSampler == -1) return;
85  // TODO
86  // if (uniformEmissiveSamplerAvailable == -1) return;
88  // TODO
89  // if (uniformEmissiveFactor == -1) return;
91  if (uniformViewProjectionMatrix == -1) return;
92  for (auto i = 0; i < Engine::LIGHTS_MAX; i++) {
93  uniformLightEnabled[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].enabled");
94  uniformLightAmbient[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].ambient");
95  uniformLightDirection[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].direction");
96  uniformLightRange[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].range");
97  uniformLightColor[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].color");
98  uniformLightIntensity[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].intensity");
99  uniformLightPosition[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].position");
100  uniformLightInnerConeCos[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].innerConeCos");
101  uniformLightOuterConeCos[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].outerConeCos");
102  uniformLightType[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].type");
103  }
104 
105  // IBL
110 
111  //
112  loadTextures(".");
113 
114  //
115  initialized = true;
116 }
117 
119 {
120  renderer->useProgram(contextIdx, programId);
121  renderer->setLighting(contextIdx, renderer->LIGHTING_PBR);
134  renderer->bindTexture(contextIdx, texturebrdfLUT);
135  renderer->setTextureUnit(contextIdx, 0);
136 }
137 
139 {
141  renderer->bindTexture(contextIdx, renderer->ID_NONE);
143  renderer->bindTexture(contextIdx, renderer->ID_NONE);
145  renderer->bindTexture(contextIdx, renderer->ID_NONE);
147  renderer->bindTexture(contextIdx, renderer->ID_NONE);
149  renderer->bindTexture(contextIdx, renderer->ID_NONE);
151  renderer->bindTexture(contextIdx, renderer->ID_NONE);
153  renderer->bindTexture(contextIdx, renderer->ID_NONE);
154  renderer->setTextureUnit(contextIdx, 0);
155 }
156 
158 {
159 }
160 
162 {
163  auto material = renderer->getPBRMaterial(contextIdx);
164  renderer->setProgramUniformFloatVec4(contextIdx, uniformBaseColorFactor, material.baseColorFactor);
165  renderer->setProgramUniformFloat(contextIdx, uniformExposure, material.exposure);
166  renderer->setProgramUniformFloat(contextIdx, uniformMetallicFactor, material.metallicFactor);
167  renderer->setProgramUniformFloat(contextIdx, uniformRoughnessFactor, material.roughnessFactor);
168  renderer->setProgramUniformFloatVec3(contextIdx, uniformEmissiveFactor, material.emissiveFactor);
169  renderer->setProgramUniformFloat(contextIdx, uniformNormalScale, material.normalScale);
170  renderer->setProgramUniformInteger(contextIdx, uniformAlphaCutoffEnabled, material.baseColorTextureMaskedTransparency);
171  renderer->setProgramUniformFloat(contextIdx, uniformAlphaCutoff, material.baseColorTextureMaskedTransparency == 0?0.0f:material.baseColorTextureMaskedTransparencyThreshold);
172 }
173 
174 void LightingShaderPBRBaseImplementation::updateLight(Renderer* renderer, int contextIdx, int32_t lightId)
175 {
176  const auto& light = renderer->getLight(contextIdx, lightId);
177  if (uniformLightEnabled[lightId] != -1) renderer->setProgramUniformInteger(contextIdx, uniformLightEnabled[lightId], light.enabled);
178  if (light.enabled == 0) return;
179  if (uniformLightAmbient[lightId] != -1)
181  contextIdx,
182  uniformLightAmbient[lightId],
183  {{
184  light.ambient[0],
185  light.ambient[1],
186  light.ambient[2]
187  }}
188  );
189  if (uniformLightDirection[lightId] != -1) renderer->setProgramUniformFloatVec3(contextIdx, uniformLightDirection[lightId], light.spotDirection);
190  if (uniformLightRange[lightId] != -1) renderer->setProgramUniformFloat(contextIdx, uniformLightRange[lightId], 0.0f);
191  if (uniformLightColor[lightId] != -1)
193  contextIdx,
194  uniformLightColor[lightId],
195  {{
196  light.diffuse[0],
197  light.diffuse[1],
198  light.diffuse[2]
199  }}
200  );
201  if (uniformLightIntensity[lightId] != -1) renderer->setProgramUniformFloat(contextIdx, uniformLightIntensity[lightId], 1.0f);
202  if (uniformLightPosition[lightId] != -1) renderer->setProgramUniformFloatVec3(contextIdx, uniformLightPosition[lightId],{{ light.position[0], light.position[1], light.position[2] }});
203  if (uniformLightType[lightId] != -1) renderer->setProgramUniformInteger(contextIdx, uniformLightType[lightId], 0);
204 }
205 
207 {
208  // set up camera position and view projection matrices
209  // matrices
210  Matrix4x4 vpMatrix;
211  // object to screen matrix
213  // upload matrices
216 }
217 
219 }
220 
222 }
223 
224 void LightingShaderPBRBaseImplementation::bindTexture(Renderer* renderer, int contextIdx, int32_t textureId)
225 {
226  switch (renderer->getTextureUnit(contextIdx)) {
229  break;
232  break;
234  if (uniformNormalSamplerAvailable != -1) renderer->setProgramUniformInteger(contextIdx, uniformNormalSamplerAvailable, textureId == 0 ? 0 : 1);
235  break;
238  break;
239  }
240 }
241 
243  //
244  for (auto i = 0; i < envDiffuseTextures.size(); i++) {
245  if (envDiffuseTextures[i] == nullptr) continue;
246  envDiffuseTextures[i]->releaseReference();
247  envDiffuseTextures[i] = nullptr;
248  }
249  for (auto i = 0; i < envSpecularTextures.size(); i++) {
250  if (envSpecularTextures[i] == nullptr) continue;
251  envSpecularTextures[i]->releaseReference();
252  envSpecularTextures[i] = nullptr;
253  }
254  //
255  Engine::getInstance()->getTextureManager()->removeTexture("pbr-environment-diffuse");
256  Engine::getInstance()->getTextureManager()->removeTexture("pbr-environment-specular");
257 }
258 
260  string environmentType = "studio_grey";
263  "pbr-environment-diffuse",
264  envDiffuseTextures[0] = TextureReader::read("resources/engine/environments/" + environmentType + "/diffuse", "diffuse_left.png"),
265  envDiffuseTextures[1] = TextureReader::read("resources/engine/environments/" + environmentType + "/diffuse", "diffuse_right.png"),
266  envDiffuseTextures[2] = TextureReader::read("resources/engine/environments/" + environmentType + "/diffuse", "diffuse_top.png"),
267  envDiffuseTextures[3] = TextureReader::read("resources/engine/environments/" + environmentType + "/diffuse", "diffuse_bottom.png"),
268  envDiffuseTextures[4] = TextureReader::read("resources/engine/environments/" + environmentType + "/diffuse", "diffuse_front.png"),
269  envDiffuseTextures[5] = TextureReader::read("resources/engine/environments/" + environmentType + "/diffuse", "diffuse_back.png"),
271  );
274  "pbr-environment-specular",
275  envSpecularTextures[0] = TextureReader::read("resources/engine/environments/" + environmentType + "/specular", "specular_left.png"),
276  envSpecularTextures[1] = TextureReader::read("resources/engine/environments/" + environmentType + "/specular", "specular_right.png"),
277  envSpecularTextures[2] = TextureReader::read("resources/engine/environments/" + environmentType + "/specular", "specular_top.png"),
278  envSpecularTextures[3] = TextureReader::read("resources/engine/environments/" + environmentType + "/specular", "specular_bottom.png"),
279  envSpecularTextures[4] = TextureReader::read("resources/engine/environments/" + environmentType + "/specular", "specular_front.png"),
280  envSpecularTextures[5] = TextureReader::read("resources/engine/environments/" + environmentType + "/specular", "specular_back.png"),
282  );
283 }
Engine main class.
Definition: Engine.h:131
static Engine * getInstance()
Returns engine instance.
Definition: Engine.h:612
static TextureManager * getTextureManager()
Definition: Engine.h:627
static BRDFLUTShader * getBRDFLUTShader()
Definition: Engine.h:483
Texture entity.
Definition: Texture.h:24
Timing class.
Definition: Timing.h:16
virtual void updateLight(Renderer *renderer, int contextIdx, int32_t lightId) override
Update light to program.
virtual void updateTextureMatrix(Renderer *renderer, int contextIdx) override
Update texture matrix to program.
virtual void updateMaterial(Renderer *renderer, int contextIdx) override
Update material to program.
virtual void updateMatrices(Renderer *renderer, int contextIdx) override
Update matrices to program.
virtual void bindTexture(Renderer *renderer, int contextIdx, int32_t textureId) override
Bind texture.
virtual void updateEffect(Renderer *renderer, int contextIdx) override
Update effect to program.
virtual void useProgram(Engine *engine, int contextIdx) override
Use lighting program.
virtual void updateShaderParameters(Renderer *renderer, int contextIdx) override=0
Update shader parameters.
void removeTexture(Texture *texture)
Removes a texture from manager.
int32_t addCubeMapTexture(const string &id, Texture *textureLeft, Texture *textureRight, Texture *textureTop, Texture *textureBottom, Texture *textureFront, Texture *textureBack, int contextIdx=0)
Adds a cube map texture to manager.
virtual void setTextureUnit(int contextIdx, int32_t textureUnit)=0
Sets up texture unit.
virtual void bindCubeMapTexture(int contextIdx, int32_t textureId)=0
Binds a cube map texture with given id or unbinds when using ID_NONE.
virtual void setProgramUniformFloatVec4(int contextIdx, int32_t uniformId, const array< float, 4 > &data)=0
Set up a float vec4 uniform value.
virtual int32_t getTextureUnit(int contextIdx)=0
Get texture unit.
virtual void setProgramUniformInteger(int contextIdx, int32_t uniformId, int32_t value)=0
Set up a integer uniform value.
virtual bool linkProgram(int32_t programId)=0
Links attached shaders to a program.
virtual void bindTexture(int contextIdx, int32_t textureId)=0
Binds a texture with given id or unbinds when using ID_NONE.
void setLighting(int contextIdx, int32_t lighting)
Set current lighting model.
Definition: Renderer.h:504
virtual void setProgramUniformFloatMatrix4x4(int contextIdx, int32_t uniformId, const array< float, 16 > &value)=0
Set up a float matrix 4x4 uniform value.
virtual int32_t getProgramUniformLocation(int32_t programId, const string &name)=0
Returns location of given uniform variable.
Renderer_PBRMaterial & getPBRMaterial(int contextIdx)
Get PBR material.
Definition: Renderer.h:1167
virtual void useProgram(int contextIdx, int32_t programId)=0
Use shader program.
virtual void setProgramUniformFloatVec3(int contextIdx, int32_t uniformId, const array< float, 3 > &data)=0
Set up a float vec3 uniform value.
virtual void setProgramUniformFloat(int contextIdx, int32_t uniformId, float value)=0
Set up a float uniform value.
Renderer_Light & getLight(int contextIdx, int32_t lightIdx)
Get light.
Definition: Renderer.h:1114
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
Vector3 multiply(const Vector3 &vector3) const
Multiplies this matrix with vector3.
Definition: Matrix4x4.h:225
Matrix4x4 & set(float r0c0, float r0c1, float r0c2, float r0c3, float r1c0, float r1c1, float r1c2, float r1c3, float r2c0, float r2c1, float r2c2, float r2c3, float r3c0, float r3c1, float r3c2, float r3c3)
Sets this matrix by its components.
Definition: Matrix4x4.h:108
const array< float, 16 > & getArray() const
Definition: Matrix4x4.h:611
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
const array< float, 3 > & getArray() const
Definition: Vector3.h:366
Console class.
Definition: Console.h:29
Float class.
Definition: Float.h:27