TDME2  1.9.200
ShadowMapRenderShaderBaseImplementation.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/Timing.h>
13 #include <tdme/math/Math.h>
14 #include <tdme/math/Matrix4x4.h>
15 #include <tdme/math/Vector3.h>
18 
19 using std::to_string;
20 
29 using tdme::math::Math;
34 
35 ShadowMapRenderShaderBaseImplementation::ShadowMapRenderShaderBaseImplementation(Renderer* renderer)
36 {
37  this->renderer = renderer;
38  initialized = false;
39 }
40 
42 {
43 }
44 
46 {
47  return initialized;
48 }
49 
51 {
52  auto shaderVersion = renderer->getShaderVersion();
53 
54  // map inputs to attributes
60  }
61  // link
62  if (renderer->linkProgram(programId) == false)
63  return;
64 
65  // uniforms
68  if (uniformTextureAtlasSize == -1) return;
70  if (uniformTextureAtlasPixelDimension == -1) return;
72  if (uniformTextureUnit == -1) return;
74  if (renderUniformTexturePixelWidth == -1) return;
76  if (renderUniformTexturePixelHeight == -1) return;
78  if (renderUniformDepthBiasMVPMatrix == -1) return;
79  if (renderer->isInstancedRenderingAvailable() == true) {
81  if (renderUniformProjectionMatrix == -1) return;
83  if (renderUniformCameraMatrix == -1) return;
84  } else {
86  if (renderUniformMVPMatrix == -1) return;
88  if (renderUniformNormalMatrix == -1) return;
90  }
92  if (uniformTextureMatrix == -1) return;
94  if (uniformDiffuseTextureUnit == -1) return;
96  if (uniformDiffuseTextureAvailable == -1) return;
102  if (renderUniformLightDirection == -1) return;
103  if (shaderVersion != "gl2") {
104  if (renderer->isInstancedRenderingAvailable() == false) {
106  if (renderUniformMVMatrix == -1) return;
107  }
109  if (renderUniformLightPosition == -1) return;
111  if (renderUniformLightSpotExponent == -1) return;
113  if (renderUniformLightSpotCosCutoff == -1) return;
115  if (renderUniformLightConstantAttenuation == -1) return;
117  if (renderUniformLightLinearAttenuation == -1) return;
119  if (renderUniformLightQuadraticAttenuation == -1) return;
120  }
121 
122  //
124 
125  //
126  initialized = true;
127 }
128 
130 {
131  renderer->useProgram(contextIdx, programId);
134  renderer->setProgramUniformInteger(contextIdx, uniformDiffuseTextureUnit, LightingShaderConstants::SPECULAR_TEXTUREUNIT_DIFFUSE);
136  if (renderUniformTime != -1) renderer->setProgramUniformFloat(contextIdx, renderUniformTime, static_cast<float>(engine->getTiming()->getTotalTime()) / 1000.0f);
138 }
139 
141 {
142 }
143 
145  if (renderer->isInstancedRenderingAvailable() == true) {
148  } else {
149  Matrix4x4 mvMatrix;
150  Matrix4x4 mvpMatrix;
151  Matrix4x4 normalMatrix;
152  Vector3 modelTranslation;
153  // model view matrix
155  // object to screen matrix
156  mvpMatrix.set(mvMatrix).multiply(renderer->getProjectionMatrix());
157  // normal matrix
158  normalMatrix.set(renderer->getModelViewMatrix()).invert().transpose();
159  // model translation
160  renderer->getModelViewMatrix().getTranslation(modelTranslation);
161  // upload
166  }
167 }
168 
171 }
172 
174 {
175  auto material = renderer->getSpecularMaterial(contextIdx);
176  renderer->setProgramUniformInteger(contextIdx, uniformDiffuseTextureMaskedTransparency, material.diffuseTextureMaskedTransparency);
177  renderer->setProgramUniformFloat(contextIdx, uniformDiffuseTextureMaskedTransparencyThreshold, material.diffuseTextureMaskedTransparencyThreshold);
178  renderer->setProgramUniformInteger(contextIdx, uniformTextureAtlasSize, material.textureAtlasSize);
179  renderer->setProgramUniformFloatVec2(contextIdx, uniformTextureAtlasPixelDimension, material.textureAtlasPixelDimension);
180 }
181 
182 void ShadowMapRenderShaderBaseImplementation::updateLight(Renderer* renderer, int contextIdx, int32_t lightId) {
183  if (lightId != this->lightId) {
184  return;
185  }
186 
187  const auto& light = renderer->getLight(contextIdx, lightId);
188  auto lightPosition = Vector3(light.position[0], light.position[1], light.position[2]);
189  auto lightSpotDirection = Vector3(light.spotDirection[0], light.spotDirection[1], light.spotDirection[2]);
190  if (renderUniformLightPosition != -1) renderer->setProgramUniformFloatVec3(contextIdx, renderUniformLightPosition, lightPosition.getArray());
191  if (renderUniformLightDirection != -1) renderer->setProgramUniformFloatVec3(contextIdx, renderUniformLightDirection, lightSpotDirection.getArray());
197  if (renderer->isInstancedRenderingAvailable() == true) {
200  }
203 }
204 
205 void ShadowMapRenderShaderBaseImplementation::bindTexture(Renderer* renderer, int contextIdx, int32_t textureId)
206 {
207  switch (renderer->getTextureUnit(contextIdx)) {
208  case LightingShaderConstants::SPECULAR_TEXTUREUNIT_DIFFUSE:
209  renderer->setProgramUniformInteger(contextIdx, uniformDiffuseTextureAvailable, textureId == 0 ? 0 : 1);
210  break;
211  }
212 }
213 
214 void ShadowMapRenderShaderBaseImplementation::setDepthBiasMVPMatrix(int contextIdx, const Matrix4x4& depthBiasMVPMatrix)
215 {
217 }
218 
220  this->lightId = lightId;
221 }
222 
224 }
225 
227 }
Engine main class.
Definition: Engine.h:131
static int32_t getShadowMapWidth()
Definition: Engine.h:730
static int32_t getShadowMapHeight()
Definition: Engine.h:737
static int32_t getShadowMapRenderLookUps()
Definition: Engine.h:744
Timing * getTiming()
Definition: Engine.h:1064
Timing class.
Definition: Timing.h:16
int64_t getTotalTime()
Definition: Timing.h:63
Interface to lighting shader program.
Matrix3x3 & getTextureMatrix(int contextIdx)
Get texture matrix.
Definition: Renderer.h:579
virtual void setProgramUniformFloatVec2(int contextIdx, int32_t uniformId, const array< float, 2 > &data)=0
Set up a float vec2 uniform value.
virtual int32_t getTextureUnit(int contextIdx)=0
Get texture unit.
virtual void setProgramAttributeLocation(int32_t programId, int32_t location, const string &name)=0
Bind attribute to a input location.
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 setProgramUniformFloatMatrix3x3(int contextIdx, int32_t uniformId, const array< float, 9 > &value)=0
Set up a float matrix 3x3 uniform value.
Renderer_SpecularMaterial & getSpecularMaterial(int contextIdx)
Get specular material.
Definition: Renderer.h:1157
void setLighting(int contextIdx, int32_t lighting)
Set current lighting model.
Definition: Renderer.h:504
virtual bool isInstancedRenderingAvailable()=0
Checks if instanced rendering is available.
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.
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
virtual void updateLight(Renderer *renderer, int contextIdx, int32_t lightId) override
Update light.
virtual void updateTextureMatrix(Renderer *renderer, int contextIdx) override
Update texture matrix.
virtual void updateMaterial(Renderer *renderer, int contextIdx) override
Update material.
virtual void bindTexture(Renderer *renderer, int contextIdx, int32_t textureId) override
Bind texture.
virtual void unUseProgram(int contextIdx) override
Un use shadow map render shader program.
virtual void useProgram(Engine *engine, int contextIdx) override
Use shadow map render shader program.
virtual void setDepthBiasMVPMatrix(int contextIdx, const Matrix4x4 &depthBiasMVPMatrix) override
Set up program depth bias mvp matrix.
Standard math functions.
Definition: Math.h:19
const array< float, 9 > & getArray() const
Definition: Matrix3x3.h:369
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
Matrix4x4 & invert()
Inverts this matrix.
Definition: Matrix4x4.h:479
Matrix4x4 & transpose()
Transposes this matrix.
Definition: Matrix4x4.h:453
const array< float, 16 > & getArray() const
Definition: Matrix4x4.h:611
void getTranslation(Vector3 &translation) const
Get translation.
Definition: Matrix4x4.h:433
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
File system singleton class.
Definition: FileSystem.h:17