TDME2  1.9.200
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ShadowMapRenderShader.cpp
Go to the documentation of this file.
2 
3 #include <string>
4 #include <unordered_map>
5 #include <vector>
6 
7 #include <tdme/tdme.h>
13 #include <tdme/engine/Engine.h>
14 #include <tdme/engine/Timing.h>
15 #include <tdme/math/Matrix4x4.h>
17 
18 using std::string;
19 using std::unordered_map;
20 using std::vector;
21 
32 
33 ShadowMapRenderShader::ShadowMapRenderShader(Renderer* renderer): renderer(renderer)
34 {
38  auto threadCount = renderer->isSupportingMultithreadedRendering() == true?Engine::getThreadCount():1;
39  contexts.resize(threadCount);
40 }
41 
43 {
44  for (const auto& [shaderId, shader]: shaders) {
45  shader->unloadTextures();
46  delete shader;
47  }
48 }
49 
51 {
52  bool initialized = true;
53  for (const auto& [shaderId, shader]: shaders) {
54  initialized&= shader->isInitialized();
55  }
56  return initialized;
57 }
58 
60 {
61  for (const auto& [shaderId, shader]: shaders) {
62  shader->initialize();
63  }
64 }
65 
67 {
68  running = true;
69  this->engine = engine;
70 }
71 
73 {
74  running = false;
75  auto i = 0;
76  for (auto& shadowMappingShaderRenderContext: contexts) {
77  if (shadowMappingShaderRenderContext.implementation != nullptr) {
78  shadowMappingShaderRenderContext.implementation->unUseProgram(i);
79  }
80  shadowMappingShaderRenderContext.implementation = nullptr;
81  i++;
82  }
83  engine = nullptr;
84 }
85 
87 {
88  auto& shadowMappingShaderRenderContext = contexts[contextIdx];
89  if (shadowMappingShaderRenderContext.implementation == nullptr) return;
90  shadowMappingShaderRenderContext.implementation->updateMatrices(contextIdx);
91 }
92 
94  auto& shadowMappingShaderRenderContext = contexts[contextIdx];
95  if (shadowMappingShaderRenderContext.implementation == nullptr) return;
96  shadowMappingShaderRenderContext.implementation->updateTextureMatrix(renderer, contextIdx);
97 }
98 
100 {
101  auto& shadowMappingShaderRenderContext = contexts[contextIdx];
102  if (shadowMappingShaderRenderContext.implementation == nullptr) return;
103  shadowMappingShaderRenderContext.implementation->updateMaterial(renderer, contextIdx);
104 }
105 
106 void ShadowMapRenderShader::updateLight(int contextIdx, int32_t lightId) {
107  auto& shadowMappingShaderRenderContext = contexts[contextIdx];
108  if (shadowMappingShaderRenderContext.implementation == nullptr) return;
109  shadowMappingShaderRenderContext.implementation->updateLight(renderer, contextIdx, lightId);
110 }
111 
113  auto& shadowMappingShaderRenderContext = contexts[contextIdx];
114  if (shadowMappingShaderRenderContext.implementation == nullptr) return;
115  shadowMappingShaderRenderContext.implementation->updateShaderParameters(renderer, contextIdx);
116 }
117 
118 void ShadowMapRenderShader::bindTexture(int contextIdx, int32_t textureId)
119 {
120  auto& shadowMappingShaderRenderContext = contexts[contextIdx];
121  if (shadowMappingShaderRenderContext.implementation == nullptr) return;
122  shadowMappingShaderRenderContext.implementation->bindTexture(renderer, contextIdx, textureId);
123 }
124 
125 void ShadowMapRenderShader::setDepthBiasMVPMatrix(int contextIdx, const Matrix4x4& depthBiasMVPMatrix)
126 {
127  this->depthBiasMVPMatrix = depthBiasMVPMatrix;
128  auto& shadowMappingShaderRenderContext = contexts[contextIdx];
129  if (shadowMappingShaderRenderContext.implementation == nullptr) return;
130  shadowMappingShaderRenderContext.implementation->setDepthBiasMVPMatrix(contextIdx, this->depthBiasMVPMatrix);
131 }
132 
134  this->lightId = lightId;
135 }
136 
137 void ShadowMapRenderShader::setShader(int contextIdx, const string& id) {
138  // TODO: find a better solution for removing PBR- lighing prefix
139  string shaderId;
140  if (StringTools::startsWith(id, string("pbr-")) == true) {
141  shaderId = StringTools::substring(id, 4);
142  } else {
143  shaderId = id;
144  }
145 
146  //
147  auto& shadowMappingShaderRenderContext = contexts[contextIdx];
148  auto currentImplementation = shadowMappingShaderRenderContext.implementation;
149  auto shaderIt = shaders.find(shaderId);
150  if (shaderIt == shaders.end()) {
151  shaderIt = shaders.find("default");
152  }
153  auto nextImplementation = shaderIt->second;
154  if (currentImplementation != nextImplementation) {
155  if (currentImplementation != nullptr) currentImplementation->unUseProgram(contextIdx);
156  shadowMappingShaderRenderContext.implementation = nextImplementation;
157  shadowMappingShaderRenderContext.implementation->useProgram(engine, contextIdx);
158  }
159 
160  shadowMappingShaderRenderContext.implementation->setDepthBiasMVPMatrix(contextIdx, depthBiasMVPMatrix);
161  shadowMappingShaderRenderContext.implementation->setRenderLightId(lightId);
162 }
163 
164 void ShadowMapRenderShader::loadTextures(const string& pathName) {
165  for (const auto& [shaderId, shader]: shaders) {
166  shader->unloadTextures();
167  shader->loadTextures(pathName);
168  }
169 }
Engine main class.
Definition: Engine.h:131
static int getThreadCount()
Definition: Engine.h:670
Timing class.
Definition: Timing.h:16
void initialize()
Initialize shadow map render shader program.
unordered_map< string, ShadowMapRenderShaderImplementation * > shaders
void updateLight(int contextIdx, int32_t lightId)
Update light.
void setDepthBiasMVPMatrix(int contextIdx, const Matrix4x4 &depthBiasMVPMatrix)
Set up program depth bias mvp matrix.
void useProgram(Engine *engine)
Use shadow map render shader program.
void setShader(int contextIdx, const string &id)
Set shader.
void updateTextureMatrix(int contextIdx)
Update up texture matrix.
void updateShaderParameters(int contextIdx)
Update shader parameters.
void bindTexture(int contextIdx, int32_t textureId)
Bind texture.
void unUseProgram()
Unuse shadow map render shader program.
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
String tools class.
Definition: StringTools.h:22