TDME2  1.9.200
DeferredLightingShaderTerrainImplementation.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/Texture.h>
15 
16 using std::string;
17 using std::to_string;
18 
29 
30 bool DeferredLightingShaderTerrainImplementation::isSupported(Renderer* renderer) {
31  return renderer->isDeferredShadingAvailable() == true;
32 }
33 
35 {
36 }
37 
39  return "defer_terrain";
40 }
41 
43 {
44  auto shaderVersion = renderer->getShaderVersion();
45 
46  // lighting
47  // vertex shader
50  "shader/" + shaderVersion + "/lighting/specular",
51  "render_vertexshader.vert",
52  "#define LIGHT_COUNT " + to_string(Engine::LIGHTS_MAX) + "\n#define HAVE_TERRAIN_SHADER\n#define HAVE_DEPTH_FOG\n" + additionalDefinitions
53  );
54  if (vertexShaderId == 0) return;
55 
56  // fragment shader
59  "shader/" + shaderVersion + "/lighting/specular",
60  "defer_fragmentshader.frag",
61  "#define LIGHT_COUNT " + to_string(Engine::LIGHTS_MAX) + "\n#define HAVE_TERRAIN_SHADER\n#define HAVE_DEPTH_FOG\n" + additionalDefinitions,
62  FileSystem::getInstance()->getContentAsString(
63  "shader/" + shaderVersion + "/functions",
64  "terrain.inc.glsl"
65  ) + "\n" +
66  FileSystem::getInstance()->getContentAsString(
67  "shader/" + shaderVersion + "/functions/specular",
68  "specular_lighting.inc.glsl"
69  ) + "\n"
70  );
71  if (fragmentShaderId == 0) return;
72 
73  // create, attach and link program
77 
78  //
80  if (initialized == false) return;
81 
82  //
83  initialized = false;
84 
86 
88  if (uniformGrasTextureUnit == -1) return;
89 
91  if (uniformDirtTextureUnit == -1) return;
92 
94  if (uniformSnowTextureUnit == -1) return;
95 
97  if (uniformStoneTextureUnit == -1) return;
98 
99  //
100  loadTextures(".");
101 
102  //
103  initialized = true;
104 }
105 
107 }
108 
111 
112  //
113  auto currentTextureUnit = renderer->getTextureUnit(contextIdx);
115  renderer->bindTexture(contextIdx, grasTextureId);
117  renderer->bindTexture(contextIdx, dirtTextureId);
119  renderer->bindTexture(contextIdx, snowTextureId);
121  renderer->bindTexture(contextIdx, stoneTextureId);
123  renderer->setTextureUnit(contextIdx, currentTextureUnit);
124 
125  //
130 }
131 
133  //
134  auto currentTextureUnit = renderer->getTextureUnit(contextIdx);
136  renderer->bindTexture(contextIdx, renderer->ID_NONE);
138  renderer->bindTexture(contextIdx, renderer->ID_NONE);
140  renderer->bindTexture(contextIdx, renderer->ID_NONE);
142  renderer->bindTexture(contextIdx, renderer->ID_NONE);
143  renderer->setTextureUnit(contextIdx, currentTextureUnit);
144 
145  //
147 }
148 
152 }
153 
155 }
156 
158  if (grasTexture != nullptr) {
161  grasTexture = nullptr;
163  }
164  if (dirtTexture != nullptr) {
167  dirtTexture = nullptr;
169  }
170  if (snowTexture != nullptr) {
173  snowTexture = nullptr;
175  }
176  if (stoneTexture != nullptr) {
179  stoneTexture = nullptr;
181  }
182 }
183 
185  //
186  grasTextureId = Engine::getInstance()->getTextureManager()->addTexture(grasTexture = TextureReader::read(pathName + "/resources/engine/textures", "terrain_gras.png"), renderer->CONTEXTINDEX_DEFAULT);
187  dirtTextureId = Engine::getInstance()->getTextureManager()->addTexture(dirtTexture = TextureReader::read(pathName + "/resources/engine/textures", "terrain_dirt.png"), renderer->CONTEXTINDEX_DEFAULT);
188  snowTextureId = Engine::getInstance()->getTextureManager()->addTexture(snowTexture = TextureReader::read(pathName + "/resources/engine/textures", "terrain_snow.png"), renderer->CONTEXTINDEX_DEFAULT);
189  stoneTextureId = Engine::getInstance()->getTextureManager()->addTexture(stoneTexture = TextureReader::read(pathName + "/resources/engine/textures", "terrain_stone.png"), renderer->CONTEXTINDEX_DEFAULT);
190 }
Engine main class.
Definition: Engine.h:131
static Engine * getInstance()
Returns engine instance.
Definition: Engine.h:612
static TextureManager * getTextureManager()
Definition: Engine.h:627
Texture entity.
Definition: Texture.h:24
const string & getId() const
Definition: Texture.h:172
virtual void updateMatrices(Renderer *renderer, int contextIdx) override
Update matrices to program.
virtual void useProgram(Engine *engine, int contextIdx) override
Use lighting program.
virtual void updateShaderParameters(Renderer *renderer, int contextIdx) override
Update shader parameters.
virtual void updateMatrices(Renderer *renderer, int contextIdx) override
Update matrices to program.
virtual void unUseProgram(int contextIdx) override
Unuse lighting program.
virtual void useProgram(Engine *engine, int contextIdx) override
Use lighting program.
void removeTexture(Texture *texture)
Removes a texture from 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 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 int32_t getTextureUnit(int contextIdx)=0
Get texture unit.
virtual void setProgramUniformInteger(int contextIdx, int32_t uniformId, int32_t value)=0
Set up a integer uniform value.
virtual void bindTexture(int contextIdx, int32_t textureId)=0
Binds a texture with given id or unbinds when using ID_NONE.
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.
const array< float, 16 > & getArray() const
Definition: Matrix4x4.h:611
File system singleton class.
Definition: FileSystem.h:17
virtual void releaseReference()
Releases a reference, thus decrementing the counter and delete it if reference counter is zero.
Definition: Reference.h:38