TDME2 1.9.121
DeferredLightingRenderShader.cpp
Go to the documentation of this file.
2
3#include <tdme/tdme.h>
10#include <tdme/math/Math.h>
15
17
32
33DeferredLightingRenderShader::DeferredLightingRenderShader(Renderer* renderer)
34{
35 this->renderer = renderer;
36 initialized = false;
37 isRunning = false;
38}
39
41{
42}
43
45{
46 return initialized;
47}
48
50{
51 auto shaderVersion = renderer->getShaderVersion();
54 "shader/" + shaderVersion + "/framebuffer",
55 "render_vertexshader.vert",
56 "#define HAVE_DEPTH_FOG"
57 );
58 if (vertexShaderId == 0) return;
59
62 "shader/" + shaderVersion + "/framebuffer",
63 "deferred_lighting_fragmentshader.frag",
64 string() +
65 "#define HAVE_DEPTH_FOG\n" +
66 "#define LIGHT_COUNT " + to_string(Engine::LIGHTS_MAX) + "\n#define USE_PUNCTUAL\n#define MATERIAL_METALLICROUGHNESS\n#define USE_IBL\n",
67 FileSystem::getInstance()->getContentAsString(
68 "shader/" + shaderVersion + "/functions/specular",
69 "specular_lighting.inc.glsl"
70 ) +
71 "\n\n" +
72 FileSystem::getInstance()->getContentAsString(
73 "shader/" + shaderVersion + "/functions/pbr",
74 "tonemapping.inc.glsl"
75 ) +
76 "\n\n" +
77 FileSystem::getInstance()->getContentAsString(
78 "shader/" + shaderVersion + "/functions/pbr",
79 "functions.inc.glsl"
80 ) +
81 "\n\n" +
82 FileSystem::getInstance()->getContentAsString(
83 "shader/" + shaderVersion + "/functions/pbr",
84 "pbr_lighting.inc.glsl"
85 ) +
86 "\n\n"
87 );
88 if (fragmentShaderId == 0) return;
89
96 }
97 if (renderer->linkProgram(programId) == false) return;
98
99 // uniforms
100 // geometry buffers
102 if (uniformGeometryBufferTextureId1 == -1) return;
104 if (uniformGeometryBufferTextureId2 == -1) return;
106 if (uniformGeometryBufferTextureId3 == -1) return;
107 // color buffers
109 if (uniformColorBufferTextureUnit1 == -1) return;
111 if (uniformColorBufferTextureUnit2 == -1) return;
113 if (uniformColorBufferTextureUnit3 == -1) return;
115 if (uniformColorBufferTextureUnit4 == -1) return;
117 if (uniformColorBufferTextureUnit5 == -1) return;
118 // depth buffer
120 if (uniformDepthBufferTextureUnit == -1) return;
121
122 // specular lights
123 for (auto i = 0; i < Engine::LIGHTS_MAX; i++) {
124 uniformSpecularLightEnabled[i] = renderer->getProgramUniformLocation(programId, "specularLights[" + to_string(i) +"].enabled");
125 if (uniformSpecularLightEnabled[i] == -1) return;
126 uniformSpecularLightAmbient[i] = renderer->getProgramUniformLocation(programId,"specularLights[" + to_string(i) + "].ambient");
127 if (uniformSpecularLightAmbient[i] == -1) return;
128 uniformSpecularLightDiffuse[i] = renderer->getProgramUniformLocation(programId, "specularLights[" + to_string(i) + "].diffuse");
129 if (uniformSpecularLightDiffuse[i] == -1) return;
130 uniformSpecularLightSpecular[i] = renderer->getProgramUniformLocation(programId, "specularLights[" + to_string(i) + "].specular");
131 if (uniformSpecularLightSpecular[i] == -1) return;
132 uniformSpecularLightPosition[i] = renderer->getProgramUniformLocation(programId, "specularLights[" + to_string(i) + "].position");
133 if (uniformSpecularLightPosition[i] == -1) return;
134 uniformSpecularLightSpotDirection[i] = renderer->getProgramUniformLocation(programId, "specularLights[" + to_string(i) + "].spotDirection");
135 if (uniformSpecularLightSpotDirection[i] == -1) return;
136 uniformSpecularLightSpotExponent[i] = renderer->getProgramUniformLocation(programId, "specularLights[" + to_string(i) + "].spotExponent");
137 if (uniformSpecularLightSpotExponent[i] == -1) return;
138 uniformSpecularLightSpotCosCutoff[i] = renderer->getProgramUniformLocation(programId, "specularLights[" + to_string(i) + "].spotCosCutoff");
139 if (uniformSpecularLightSpotCosCutoff[i] == -1) return;
140 uniformSpecularLightConstantAttenuation[i] = renderer->getProgramUniformLocation(programId, "specularLights[" + to_string(i) + "].constantAttenuation");
141 if (uniformSpecularLightConstantAttenuation[i] == -1) return;
142 uniformSpecularLightLinearAttenuation[i] = renderer->getProgramUniformLocation(programId, "specularLights[" + to_string(i) + "].linearAttenuation");
143 if (uniformSpecularLightLinearAttenuation[i] == -1) return;
144 uniformSpecularLightQuadraticAttenuation[i] = renderer->getProgramUniformLocation(programId, "specularLights[" + to_string(i) + "].quadraticAttenuation");
145 if (uniformSpecularLightQuadraticAttenuation[i] == -1) return;
146 uniformSpecularLightRadius[i] = renderer->getProgramUniformLocation(programId, "specularLights[" + to_string(i) + "].radius");
147 if (uniformSpecularLightRadius[i] == -1) return;
148 }
149
150 // camera matrix
152 if (uniformCameraMatrix == -1) return;
153
154 // PBR
156 if (uniformCamera == -1) return;
157
158 // lights
159 for (auto i = 0; i < Engine::LIGHTS_MAX; i++) {
160 uniformPBRLightEnabled[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].enabled");
161 if (uniformPBRLightEnabled[i] == -1) return;
162 //uniformLightAmbient[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].ambient");
163 //if (uniformLightAmbient[i] == -1) return;
164 uniformPBRLightDirection[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].direction");
165 if (uniformPBRLightDirection[i] == -1) return;
166 uniformPBRLightRange[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].range");
167 if (uniformPBRLightRange[i] == -1) return;
168 uniformPBRLightColor[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].color");
169 if (uniformPBRLightColor[i] == -1) return;
170 uniformPBRLightIntensity[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].intensity");
171 if (uniformPBRLightIntensity[i] == -1) return;
172 uniformPBRLightPosition[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].position");
173 if (uniformPBRLightPosition[i] == -1) return;
174 uniformPBRLightInnerConeCos[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].innerConeCos");
175 if (uniformPBRLightInnerConeCos[i] == -1) return;
176 uniformPBRLightOuterConeCos[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].outerConeCos");
177 if (uniformPBRLightOuterConeCos[i] == -1) return;
178 uniformPBRLightType[i] = renderer->getProgramUniformLocation(programId, "u_PBRLights[" + to_string(i) + "].type");
179 if (uniformPBRLightType[i] == -1) return;
180 }
181
182 // IBL
184 if (uniformDiffuseEnvSampler == -1) return;
186 if (uniformSpecularEnvSampler == -1) return;
188 if (uniformbrdfLUT == -1) return;
189
190 //
191 string environmentType = "studio_grey";
194 "pbr-environment-diffuse",
195 TextureReader::read("resources/engine/environments/" + environmentType + "/diffuse", "diffuse_left.png"),
196 TextureReader::read("resources/engine/environments/" + environmentType + "/diffuse", "diffuse_right.png"),
197 TextureReader::read("resources/engine/environments/" + environmentType + "/diffuse", "diffuse_top.png"),
198 TextureReader::read("resources/engine/environments/" + environmentType + "/diffuse", "diffuse_bottom.png"),
199 TextureReader::read("resources/engine/environments/" + environmentType + "/diffuse", "diffuse_front.png"),
200 TextureReader::read("resources/engine/environments/" + environmentType + "/diffuse", "diffuse_back.png"),
202 );
205 "pbr-environment-specular",
206 TextureReader::read("resources/engine/environments/" + environmentType + "/specular", "specular_left.png"),
207 TextureReader::read("resources/engine/environments/" + environmentType + "/specular", "specular_right.png"),
208 TextureReader::read("resources/engine/environments/" + environmentType + "/specular", "specular_top.png"),
209 TextureReader::read("resources/engine/environments/" + environmentType + "/specular", "specular_bottom.png"),
210 TextureReader::read("resources/engine/environments/" + environmentType + "/specular", "specular_front.png"),
211 TextureReader::read("resources/engine/environments/" + environmentType + "/specular", "specular_back.png"),
213 );
214 texturebrdfLUT = Engine::getInstance()->getTextureManager()->addTexture(TextureReader::read("resources/engine/environments", "brdfLUT.png"), renderer->CONTEXTINDEX_DEFAULT);
215
216 //
217 initialized = true;
218}
219
221{
222 auto contextIdx = renderer->CONTEXTINDEX_DEFAULT;
223 renderer->useProgram(contextIdx, programId);
234
235 // Specular
236 for (auto lightId = 0; lightId < Engine::LIGHTS_MAX; lightId++) {
237 auto light = engine->getLightAt(lightId);
238 renderer->setProgramUniformInteger(contextIdx, uniformSpecularLightEnabled[lightId], light->isEnabled() == true?1:0);
239 if (light->isEnabled() == false) continue;
240 renderer->setProgramUniformFloatVec4(contextIdx, uniformSpecularLightAmbient[lightId], light->getAmbient().getArray());
241 renderer->setProgramUniformFloatVec4(contextIdx, uniformSpecularLightDiffuse[lightId], light->getDiffuse().getArray());
242 renderer->setProgramUniformFloatVec4(contextIdx, uniformSpecularLightSpecular[lightId], light->getSpecular().getArray());
243 renderer->setProgramUniformFloatVec4(contextIdx, uniformSpecularLightPosition[lightId], light->getPosition().getArray());
244 renderer->setProgramUniformFloatVec3(contextIdx, uniformSpecularLightSpotDirection[lightId], light->getSpotDirection().getArray());
245 renderer->setProgramUniformFloat(contextIdx, uniformSpecularLightSpotExponent[lightId], light->getSpotExponent());
246 renderer->setProgramUniformFloat(contextIdx, uniformSpecularLightSpotCosCutoff[lightId], static_cast<float>(Math::cos(Math::PI / 180.0f * light->getSpotCutOff())));
247 renderer->setProgramUniformFloat(contextIdx, uniformSpecularLightConstantAttenuation[lightId], light->getConstantAttenuation());
248 renderer->setProgramUniformFloat(contextIdx, uniformSpecularLightLinearAttenuation[lightId], light->getLinearAttenuation());
249 renderer->setProgramUniformFloat(contextIdx, uniformSpecularLightQuadraticAttenuation[lightId], light->getQuadraticAttenuation());
250 renderer->setProgramUniformFloat(contextIdx, uniformSpecularLightRadius[lightId], light->getRadius());
251 }
253
254 // PBR
256
257 // IBL
261 renderer->setTextureUnit(contextIdx, 9);
263 renderer->setTextureUnit(contextIdx, 10);
265 renderer->setTextureUnit(contextIdx, 11);
266 renderer->bindTexture(contextIdx, texturebrdfLUT);
267
268 // lights
269 for (auto lightId = 0; lightId < Engine::LIGHTS_MAX; lightId++) {
270 auto light = engine->getLightAt(lightId);
271 if (uniformPBRLightEnabled[lightId] != -1) renderer->setProgramUniformInteger(contextIdx, uniformPBRLightEnabled[lightId], light->isEnabled() == true?1:0);
272 if (light->isEnabled() == false) continue;
273 auto& ambient = light->getAmbient().getArray();
274 auto& diffuse = light->getDiffuse().getArray();
275 auto& position = light->getPosition().getArray();
276 // renderer->setProgramUniformFloatVec3(context, uniformLightAmbient[lightId], {{ ambient[0], ambient[1], ambient[2] }});
277 renderer->setProgramUniformFloatVec3(contextIdx, uniformPBRLightDirection[lightId], light->getSpotDirection().getArray());
278 renderer->setProgramUniformFloat(contextIdx, uniformPBRLightRange[lightId], 0.0f);
279 renderer->setProgramUniformFloatVec3(contextIdx, uniformPBRLightColor[lightId], {{ diffuse[0], diffuse[1], diffuse[2] }});
280 renderer->setProgramUniformFloat(contextIdx, uniformPBRLightIntensity[lightId], 1.0f);
281 renderer->setProgramUniformFloatVec3(contextIdx, uniformPBRLightPosition[lightId],{{ position[0], position[1], position[2] }});
282 if (uniformPBRLightType[lightId] != -1) renderer->setProgramUniformInteger(contextIdx, uniformPBRLightType[lightId], 0);
283 }
284
285 //
286 isRunning = true;
287}
288
290{
291 auto contextIdx = renderer->CONTEXTINDEX_DEFAULT;
292 auto textureUnit = renderer->getTextureUnit(contextIdx);
293 // PBR
294 // IBL
295 renderer->setTextureUnit(contextIdx, 9);
296 renderer->bindTexture(contextIdx, renderer->ID_NONE);
297 renderer->setTextureUnit(contextIdx, 10);
298 renderer->bindTexture(contextIdx, renderer->ID_NONE);
299 renderer->setTextureUnit(contextIdx, 11);
300 renderer->bindTexture(contextIdx, renderer->ID_NONE);
301 renderer->setTextureUnit(contextIdx, textureUnit);
302
303 //
304 isRunning = false;
305}
const Matrix4x4 & getModelViewMatrix() const
Definition: Camera.h:243
Engine main class.
Definition: Engine.h:122
Light * getLightAt(int32_t idx)
Returns light at idx (0 <= idx < 8)
Definition: Engine.h:950
static TextureManager * getTextureManager()
Definition: Engine.h:564
static constexpr int LIGHTS_MAX
Definition: Engine.h:173
Camera * getCamera()
Definition: Engine.h:907
static Engine * getInstance()
Returns engine instance.
Definition: Engine.h:554
Geometry buffer class.
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.
TextureManager_TextureManaged * addTexture(const string &id, bool &created)
Adds a 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 int32_t loadShader(int32_t type, const string &pathName, const string &fileName, const string &definitions=string(), const string &functions=string())=0
Loads a shader.
virtual void setProgramUniformFloatVec4(int contextIdx, int32_t uniformId, const array< float, 4 > &data)=0
Set up a float vec4 uniform value.
virtual int32_t createProgram(int type)=0
Creates a shader program.
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 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:503
virtual void attachShaderToProgram(int32_t programId, int32_t shaderId)=0
Attaches a shader to a program.
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.
Buffers used to transfer data between main memory to graphics board memory.
Definition: ObjectBuffer.h:23
Standard math functions.
Definition: Math.h:21
array< float, 16 > & getArray() const
Returns array data.
Definition: Matrix4x4.h:616
array< float, 3 > & getArray() const
Definition: Vector3.h:171
File system singleton class.
Definition: FileSystem.h:14
Byte buffer class.
Definition: ByteBuffer.h:24
Float buffer class.
Definition: FloatBuffer.h:18