TDME2  1.9.200
ShadowMapCreationShader.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/math/Matrix4x4.h>
16 
17 using std::string;
18 using std::unordered_map;
19 using std::vector;
20 
31 
32 ShadowMapCreationShader::ShadowMapCreationShader(Renderer* renderer): renderer(renderer)
33 {
37  auto threadCount = renderer->isSupportingMultithreadedRendering() == true?Engine::getThreadCount():1;
38  contexts.resize(threadCount);
39 }
40 
42  for (const auto& [shaderId, shader]: shaders) {
43  shader->unloadTextures();
44  delete shader;
45  }
46 }
47 
49 {
50  bool initialized = true;
51  for (const auto& [shaderId, shader]: shaders) {
52  initialized&= shader->isInitialized();
53  }
54  return initialized;
55 }
56 
58 {
59  for (const auto& [shaderId, shader]: shaders) {
60  shader->initialize();
61  }
62 }
63 
65 {
66  running = true;
67  this->engine = engine;
68 }
69 
71 {
72  running = false;
73  auto i = 0;
74  for (auto& shadowMappingShaderPreContext: contexts) {
75  if (shadowMappingShaderPreContext.implementation != nullptr) {
76  shadowMappingShaderPreContext.implementation->unUseProgram(i);
77  }
78  shadowMappingShaderPreContext.implementation = nullptr;
79  i++;
80  }
81  engine = nullptr;
82 }
83 
85 {
86  auto& shadowMappingShaderPreContext = contexts[contextIdx];
87  if (shadowMappingShaderPreContext.implementation == nullptr) return;
88  shadowMappingShaderPreContext.implementation->updateMatrices(contextIdx);
89 }
90 
92  auto& shadowMappingShaderPreContext = contexts[contextIdx];
93  if (shadowMappingShaderPreContext.implementation == nullptr) return;
94  shadowMappingShaderPreContext.implementation->updateTextureMatrix(renderer, contextIdx);
95 }
96 
98 {
99  auto& shadowMappingShaderPreContext = contexts[contextIdx];
100  if (shadowMappingShaderPreContext.implementation == nullptr) return;
101  shadowMappingShaderPreContext.implementation->updateMaterial(renderer, contextIdx);
102 }
103 
105  auto& shadowMappingShaderPreContext = contexts[contextIdx];
106  if (shadowMappingShaderPreContext.implementation == nullptr) return;
107  shadowMappingShaderPreContext.implementation->updateShaderParameters(renderer, contextIdx);
108 }
109 
110 void ShadowMapCreationShader::bindTexture(int contextIdx, int32_t textureId)
111 {
112  auto& shadowMappingShaderPreContext = contexts[contextIdx];
113  if (shadowMappingShaderPreContext.implementation == nullptr) return;
114  shadowMappingShaderPreContext.implementation->bindTexture(renderer, contextIdx, textureId);
115 }
116 
117 void ShadowMapCreationShader::setShader(int contextIdx, const string& id) {
118  // TODO: find a better solution for removing PBR- lighing prefix
119  string shaderId;
120  if (StringTools::startsWith(id, string("pbr-")) == true) {
121  shaderId = StringTools::substring(id, 4);
122  } else {
123  shaderId = id;
124  }
125 
126  //
127  auto& shadowMappingShaderPreContext = contexts[contextIdx];
128  auto currentImplementation = shadowMappingShaderPreContext.implementation;
129  auto shaderIt = shaders.find(shaderId);
130  if (shaderIt == shaders.end()) {
131  shaderIt = shaders.find("default");
132  }
133  auto nextImplementation = shaderIt->second;
134  if (currentImplementation != nextImplementation) {
135  if (currentImplementation != nullptr) currentImplementation->unUseProgram(contextIdx);
136  shadowMappingShaderPreContext.implementation = nextImplementation;
137  shadowMappingShaderPreContext.implementation->useProgram(engine, contextIdx);
138  }
139 }
140 
141 void ShadowMapCreationShader::loadTextures(const string& pathName) {
142  for (const auto& [shaderId, shader]: shaders) {
143  shader->unloadTextures();
144  shader->loadTextures(pathName);
145  }
146 }
Engine main class.
Definition: Engine.h:131
static int getThreadCount()
Definition: Engine.h:670
void useProgram(Engine *engine)
Use shadow map creation shader program.
void setShader(int contextIdx, const string &id)
Set shader.
void updateShaderParameters(int contextIdx)
Update shader parameters.
unordered_map< string, ShadowMapCreationShaderImplementation * > shaders
void bindTexture(int contextIdx, int32_t textureId)
Bind texture.
void unUseProgram()
Unuse shadow map creation shader program.
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
String tools class.
Definition: StringTools.h:22