TDME2 1.9.121
PostProcessingShaderLightScatteringImplementation.cpp
Go to the documentation of this file.
1#include <string>
2
3#include <tdme/tdme.h>
9#include <tdme/math/Math.h>
10
11using std::string;
12
18
19bool PostProcessingShaderLightScatteringImplementation::isSupported(Renderer* renderer) {
20 return renderer->getShaderVersion() == "gl3";
21}
22
24{
25}
26
28{
29 auto shaderVersion = renderer->getShaderVersion();
30
31 // fragment shader
34 "shader/" + shaderVersion + "/postprocessing",
35 "light_scattering_fragmentshader.frag",
36 "#define LIGHT_COUNT " + to_string(Engine::LIGHTS_MAX) + "\n"
37 );
38 if (fragmentShaderId == 0) return;
39
40 // vertex shader
43 "shader/" + shaderVersion + "/postprocessing",
44 "light_scattering_vertexshader.vert",
45 "#define LIGHT_COUNT " + to_string(Engine::LIGHTS_MAX) + "\n"
46 );
47 if (vertexShaderId == 0) return;
48
49 // create, attach and link program
53
54 //
56
57 // register shader
58 if (initialized == false) return;
59
60 // lights
61 for (auto i = 0; i < Engine::LIGHTS_MAX; i++) {
62 uniformLightEnabled[i] = renderer->getProgramUniformLocation(programId, "lights[" + to_string(i) +"].enabled");
63 uniformLightPosition[i] = renderer->getProgramUniformLocation(programId, "lights[" + to_string(i) +"].position");
64 uniformLightIntensity[i] = renderer->getProgramUniformLocation(programId, "lights[" + to_string(i) +"].intensity");
65 }
66
67 //
69 Engine::ShaderType::SHADERTYPE_POSTPROCESSING,
70 "light_scattering"
71 );
72}
73
75 int _width = engine->getScaledWidth() != -1?engine->getScaledWidth():engine->getWidth();
76 int _height = engine->getScaledHeight() != -1?engine->getScaledHeight():engine->getHeight();
77 for (auto i = 0; i < engine->getLightCount(); i++) {
78 auto light = engine->getLightAt(i);
79 if (light->isEnabled() == false || light->isRenderSource() == false) {
81 continue;
82 }
83 Vector2 lightSourcePosition2D;
84 Vector3 lightSourcePosition = Vector3(light->getPosition().getX(), light->getPosition().getY(), light->getPosition().getZ());
85 if (light->getPosition().getW() > Math::EPSILON) lightSourcePosition.scale(1.0f / light->getPosition().getW());
86 engine->computeScreenCoordinateByWorldCoordinate(lightSourcePosition, lightSourcePosition2D);
87 lightSourcePosition2D.setX(Math::clamp(lightSourcePosition2D.getX() / static_cast<float>(_width), 0.0f, 1.0f));
88 lightSourcePosition2D.setY(Math::clamp(1.0f - (lightSourcePosition2D.getY() / static_cast<float>(_height)), 0.0f, 1.0f));
89 float intensity = 1.0f;
90 float _intensity = 1.0f;
91 if (lightSourcePosition2D.getX() < 0.4f) _intensity = lightSourcePosition2D.getX() / 0.4f;
92 if (_intensity < intensity) intensity = _intensity;
93 if (lightSourcePosition2D.getX() > 0.6f) _intensity = (1.0f - lightSourcePosition2D.getX()) / 0.4f;
94 if (_intensity < intensity) intensity = _intensity;
95 if (lightSourcePosition2D.getY() < 0.4f) _intensity = lightSourcePosition2D.getY() / 0.4f;
96 if (_intensity < intensity) intensity = _intensity;
97 if (lightSourcePosition2D.getY() > 0.6f) _intensity = (1.0f - lightSourcePosition2D.getY()) / 0.4f;
98 if (_intensity < intensity) intensity = _intensity;
99 if (intensity < Math::EPSILON) {
101 } else {
102 if (renderer->getRendererType() == Renderer::RENDERERTYPE_VULKAN) {
103 lightSourcePosition2D = Vector2(lightSourcePosition2D.getX(), 1.0f - lightSourcePosition2D.getY());
104 }
106 renderer->setProgramUniformFloatVec2(contextIdx, uniformLightPosition[i], lightSourcePosition2D.getArray());
107 renderer->setProgramUniformFloat(contextIdx, uniformLightIntensity[i], intensity * 0.6f);
108 }
109 }
110}
Engine main class.
Definition: Engine.h:122
int32_t getLightCount()
Definition: Engine.h:941
Light * getLightAt(int32_t idx)
Returns light at idx (0 <= idx < 8)
Definition: Engine.h:950
int32_t getWidth()
Definition: Engine.h:865
static void registerShader(ShaderType type, const string &shaderId, const map< string, ShaderParameter > &parameterDefaults={})
Register shader.
Definition: Engine.cpp:2122
int32_t getHeight()
Definition: Engine.h:872
bool computeScreenCoordinateByWorldCoordinate(const Vector3 &worldCoordinate, Vector2 &screenCoordinate, int width=-1, int height=-1)
Convert screen coordinate by world coordinate.
Definition: Engine.cpp:1894
static constexpr int LIGHTS_MAX
Definition: Engine.h:173
int32_t getScaledWidth()
Definition: Engine.h:879
int32_t getScaledHeight()
Definition: Engine.h:886
Shader parameter model class.
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 int32_t createProgram(int type)=0
Creates a shader program.
virtual void setProgramUniformFloatVec2(int contextIdx, int32_t uniformId, const array< float, 2 > &data)=0
Set up a float vec2 uniform value.
virtual void setProgramUniformInteger(int contextIdx, int32_t uniformId, int32_t value)=0
Set up a integer uniform value.
virtual void attachShaderToProgram(int32_t programId, int32_t shaderId)=0
Attaches a shader to a program.
virtual int32_t getProgramUniformLocation(int32_t programId, const string &name)=0
Returns location of given uniform variable.
virtual void setProgramUniformFloat(int contextIdx, int32_t uniformId, float value)=0
Set up a float uniform value.
Standard math functions.
Definition: Math.h:21
2D vector 2 class
Definition: Vector2.h:19
float getY() const
Definition: Vector2.h:111
float getX() const
Definition: Vector2.h:94
Vector2 & setX(float x)
set X
Definition: Vector2.h:103
array< float, 2 > & getArray() const
Definition: Vector2.h:330
Vector2 & setY(float y)
set Y
Definition: Vector2.h:119
3D vector 3 class
Definition: Vector3.h:22
Vector3 & scale(float scale)
Scale this vector.
Definition: Vector3.h:349