DEL: Boilerplate Shader Uniform Code
All checks were successful
Tests / test (push) Successful in 2m31s
All checks were successful
Tests / test (push) Successful in 2m31s
This commit is contained in:
parent
31d27a8243
commit
67fb1be3b5
@ -5,36 +5,23 @@
|
||||
#include "GUIShader.h"
|
||||
|
||||
void GUIShader::loadTransformationMatrix(glm::mat4 matrix) {
|
||||
ShaderProgram::loadMatrix(location_transformationMatrix, matrix);
|
||||
ShaderProgram::loadMatrix(getUniformLocation("transformationMatrix"), matrix);
|
||||
}
|
||||
|
||||
void GUIShader::loadShaderEffect(float brightness, glm::vec3 tintColor, float tintStrength) {
|
||||
ShaderProgram::loadFloat(location_brightness, brightness);
|
||||
ShaderProgram::loadVector(location_tintColor, tintColor);
|
||||
ShaderProgram::loadFloat(location_tintStrength, tintStrength);
|
||||
ShaderProgram::loadFloat(getUniformLocation("brightness"), brightness);
|
||||
ShaderProgram::loadVector(getUniformLocation("tintColor"), tintColor);
|
||||
ShaderProgram::loadFloat(getUniformLocation("tintStrength"), tintStrength);
|
||||
}
|
||||
|
||||
void GUIShader::loadForegroundTexture(int textureBankIndex, bool useTexture) {
|
||||
ShaderProgram::loadBoolean(location_usesForgroundTexture, useTexture);
|
||||
ShaderProgram::loadInt(location_foregroundTextureSampler, textureBankIndex);
|
||||
ShaderProgram::loadBoolean(getUniformLocation("hasForeground"), useTexture);
|
||||
ShaderProgram::loadInt(getUniformLocation("guiTexture"), textureBankIndex);
|
||||
}
|
||||
|
||||
void GUIShader::loadBackgroundTexture(int textureBankIndex, bool useTexture) {
|
||||
ShaderProgram::loadBoolean(location_usesBackgroundTexture, useTexture);
|
||||
ShaderProgram::loadInt(location_backgroundTextureSampler, textureBankIndex);
|
||||
}
|
||||
|
||||
void GUIShader::getAllUniformLocations() {
|
||||
location_transformationMatrix = ShaderProgram::getUniformLocation("transformationMatrix");
|
||||
|
||||
location_brightness = ShaderProgram::getUniformLocation("brightness");
|
||||
location_tintColor = ShaderProgram::getUniformLocation("tintColor");
|
||||
location_tintStrength = ShaderProgram::getUniformLocation("tintStrength");
|
||||
|
||||
location_foregroundTextureSampler = ShaderProgram::getUniformLocation("guiTexture");
|
||||
location_backgroundTextureSampler = ShaderProgram::getUniformLocation("backgroundTexture");
|
||||
location_usesForgroundTexture = ShaderProgram::getUniformLocation("hasForeground");
|
||||
location_usesBackgroundTexture = ShaderProgram::getUniformLocation("hasBackground");
|
||||
ShaderProgram::loadBoolean(getUniformLocation("hasBackground"), useTexture);
|
||||
ShaderProgram::loadInt(getUniformLocation("backgroundTexture"), textureBankIndex);
|
||||
}
|
||||
|
||||
void GUIShader::bindAttributes() const {
|
||||
|
||||
@ -11,7 +11,6 @@ class GUIShader: public ShaderProgram {
|
||||
public:
|
||||
GUIShader() : ShaderProgram(VERTEX_FILE, FRAGMENT_FILE) {
|
||||
GUIShader::bindAttributes();
|
||||
GUIShader::getAllUniformLocations();
|
||||
};
|
||||
void loadTransformationMatrix(glm::mat4 matrix);
|
||||
void loadShaderEffect(float brightness, glm::vec3 tintColor, float tintStrength);
|
||||
@ -23,18 +22,7 @@ private:
|
||||
inline static const std::string VERTEX_FILE = "assets/shaders/guiVertexShader.glsl";
|
||||
inline static const std::string FRAGMENT_FILE = "assets/shaders/guiFragmentShader.glsl";
|
||||
|
||||
int location_transformationMatrix;
|
||||
int location_brightness;
|
||||
int location_tintColor;
|
||||
int location_tintStrength;
|
||||
|
||||
int location_usesForgroundTexture;
|
||||
int location_foregroundTextureSampler;
|
||||
int location_usesBackgroundTexture;
|
||||
int location_backgroundTextureSampler;
|
||||
|
||||
protected:
|
||||
void getAllUniformLocations() override;
|
||||
void bindAttributes() const override;
|
||||
};
|
||||
|
||||
|
||||
@ -5,22 +5,17 @@
|
||||
#include "MinimapShader.h"
|
||||
|
||||
void MinimapShader::loadTransformationMatrix(const glm::mat4 &matrix) {
|
||||
ShaderProgram::loadMatrix(location_transformationMatrix, matrix);
|
||||
ShaderProgram::loadMatrix(getUniformLocation("transformationMatrix"), matrix);
|
||||
}
|
||||
|
||||
void MinimapShader::loadViewProjectionMatrix(const glm::mat4 &matrix) {
|
||||
ShaderProgram::loadMatrix(location_viewProjectionMatrix, matrix);
|
||||
ShaderProgram::loadMatrix(getUniformLocation("viewProjectionMatrix"), matrix);
|
||||
}
|
||||
|
||||
void MinimapShader::loadColor(glm::vec3 color) {
|
||||
ShaderProgram::loadVector(location_color, color);
|
||||
ShaderProgram::loadVector(getUniformLocation("color"), color);
|
||||
}
|
||||
|
||||
void MinimapShader::getAllUniformLocations() {
|
||||
location_color = getUniformLocation("color");
|
||||
location_transformationMatrix = getUniformLocation("transformationMatrix");
|
||||
location_viewProjectionMatrix = getUniformLocation("viewProjectionMatrix");
|
||||
}
|
||||
|
||||
void MinimapShader::bindAttributes() const {
|
||||
bindAttribute(0, "position");
|
||||
|
||||
@ -11,22 +11,17 @@ class MinimapShader: public ShaderProgram {
|
||||
public:
|
||||
MinimapShader() : ShaderProgram(VERTEX_FILE, FRAGMENT_FILE) {
|
||||
MinimapShader::bindAttributes();
|
||||
MinimapShader::getAllUniformLocations();
|
||||
|
||||
}
|
||||
|
||||
void loadTransformationMatrix(const glm::mat4 &matrix);
|
||||
void loadViewProjectionMatrix(const glm::mat4 &matrix);
|
||||
void loadColor(glm::vec3 color);
|
||||
private:
|
||||
int location_transformationMatrix;
|
||||
int location_viewProjectionMatrix;
|
||||
int location_color;
|
||||
|
||||
protected:
|
||||
inline static const std::string VERTEX_FILE = "assets/shaders/minimapVertexShader.glsl";
|
||||
inline static const std::string FRAGMENT_FILE = "assets/shaders/minimapFragmentShader.glsl";
|
||||
|
||||
void getAllUniformLocations() override;
|
||||
void bindAttributes() const override;
|
||||
};
|
||||
|
||||
|
||||
@ -44,10 +44,6 @@ void ShaderProgram::bindAttribute(int attribute, const std::string& variableName
|
||||
glBindAttribLocation(programID, attribute, variableName.c_str());
|
||||
}
|
||||
|
||||
int ShaderProgram::getUniformLocation(const std::string &uniformName) const {
|
||||
return glGetUniformLocation(programID, uniformName.c_str());
|
||||
}
|
||||
|
||||
void ShaderProgram::loadFloat(int location, float value) {
|
||||
glUniform1f(location, value);
|
||||
}
|
||||
@ -68,6 +64,17 @@ void ShaderProgram::loadInt(int location, int value) {
|
||||
glUniform1i(location, value);
|
||||
}
|
||||
|
||||
int ShaderProgram::getUniformLocation(const std::string &name) {
|
||||
auto it = uniformLocations.find(name);
|
||||
if (it != uniformLocations.end()) {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
GLint loc = glGetUniformLocation(programID, name.c_str());
|
||||
uniformLocations[name] = loc;
|
||||
return loc;
|
||||
}
|
||||
|
||||
|
||||
int ShaderProgram::loadShader(const std::string& file, int type) {
|
||||
std::ifstream shaderFile(file);
|
||||
|
||||
@ -26,16 +26,15 @@ protected:
|
||||
|
||||
virtual void bindAttributes() const = 0;
|
||||
|
||||
[[nodiscard]] int getUniformLocation(const std::string& uniformName) const;
|
||||
virtual void getAllUniformLocations() = 0;
|
||||
|
||||
void loadFloat(int location, float value);
|
||||
void loadVector(int location, glm::vec3 vector);
|
||||
void loadBoolean(int location, bool value);
|
||||
void loadMatrix(int location, glm::mat4 matrix);
|
||||
void loadInt(int location, int value);
|
||||
int getUniformLocation(const std::string& name);
|
||||
private:
|
||||
static int loadShader(const std::string& file, int type);
|
||||
std::unordered_map<std::string, GLint> uniformLocations;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -6,36 +6,35 @@
|
||||
|
||||
StaticShader::StaticShader() : ShaderProgram(VERTEX_FILE, FRAGMENT_FILE) {
|
||||
StaticShader::bindAttributes();
|
||||
StaticShader::getAllUniformLocations();
|
||||
}
|
||||
|
||||
void StaticShader::loadTransformationMatrix(glm::mat4 matrix) {
|
||||
loadMatrix(location_transformationMatrix, matrix);
|
||||
loadMatrix(getUniformLocation("transformationMatrix"), matrix);
|
||||
}
|
||||
|
||||
void StaticShader::loadProjectionMatrix(glm::mat4 matrix) {
|
||||
loadMatrix(location_projectionMatrix, matrix);
|
||||
loadMatrix(getUniformLocation("projectionMatrix"), matrix);
|
||||
}
|
||||
|
||||
void StaticShader::loadViewMatrix(glm::mat4 matrix) {
|
||||
loadMatrix(location_viewMatrix, matrix);
|
||||
loadMatrix(getUniformLocation("viewMatrix"), matrix);
|
||||
}
|
||||
|
||||
void StaticShader::loadLight(glm::vec3 position, glm::vec3 color) {
|
||||
loadVector(location_lightPosition, position);
|
||||
loadVector(location_lightColor, color);
|
||||
loadVector(getUniformLocation("lightPosition"), position);
|
||||
loadVector(getUniformLocation("lightColor"), color);
|
||||
}
|
||||
|
||||
void StaticShader::loadGhostMode(bool ghostMode) {
|
||||
loadBoolean(location_ghostMode, ghostMode);
|
||||
loadBoolean(getUniformLocation("ghostMode"), ghostMode);
|
||||
}
|
||||
|
||||
void StaticShader::loadGhostColor(glm::vec3 ghostColor) {
|
||||
loadVector(location_ghostColor, ghostColor);
|
||||
loadVector(getUniformLocation("ghostColor"), ghostColor);
|
||||
}
|
||||
|
||||
void StaticShader::loadPulse(float pulse) {
|
||||
loadFloat(location_pulse, pulse);
|
||||
loadFloat(getUniformLocation("pulse"), pulse);
|
||||
}
|
||||
|
||||
void StaticShader::bindAttributes() const {
|
||||
@ -44,16 +43,4 @@ void StaticShader::bindAttributes() const {
|
||||
bindAttribute(2, "normal");
|
||||
}
|
||||
|
||||
void StaticShader::getAllUniformLocations() {
|
||||
location_transformationMatrix = getUniformLocation("transformationMatrix");
|
||||
location_projectionMatrix = getUniformLocation("projectionMatrix");
|
||||
location_viewMatrix = getUniformLocation("viewMatrix");
|
||||
location_lightPosition = getUniformLocation("lightPosition");
|
||||
location_lightColor = getUniformLocation("lightColor");
|
||||
|
||||
location_ghostColor = getUniformLocation("ghostColor");
|
||||
location_pulse = getUniformLocation("pulse");
|
||||
location_ghostMode = getUniformLocation("ghostMode");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -22,19 +22,8 @@ private:
|
||||
inline static const std::string VERTEX_FILE = "assets/shaders/vertexShader.glsl";
|
||||
inline static const std::string FRAGMENT_FILE = "assets/shaders/fragmentShader.glsl";
|
||||
|
||||
int location_transformationMatrix;
|
||||
int location_projectionMatrix;
|
||||
int location_viewMatrix;
|
||||
int location_lightPosition;
|
||||
int location_lightColor;
|
||||
|
||||
int location_ghostColor;
|
||||
int location_pulse;
|
||||
int location_ghostMode;
|
||||
|
||||
protected:
|
||||
void bindAttributes() const override;
|
||||
void getAllUniformLocations() override;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -6,29 +6,28 @@
|
||||
|
||||
TerrainShader::TerrainShader(): ShaderProgram(VERTEX_FILE, FRAGMENT_FILE) {
|
||||
TerrainShader::bindAttributes();
|
||||
TerrainShader::getAllUniformLocations();
|
||||
}
|
||||
|
||||
void TerrainShader::loadTransformationMatrix(glm::mat4 matrix) {
|
||||
loadMatrix(location_transformationMatrix, matrix);
|
||||
loadMatrix(getUniformLocation("transformationMatrix"), matrix);
|
||||
}
|
||||
|
||||
void TerrainShader::loadProjectionMatrix(glm::mat4 matrix) {
|
||||
loadMatrix(location_projectionMatrix, matrix);
|
||||
loadMatrix(getUniformLocation("projectionMatrix"), matrix);
|
||||
}
|
||||
|
||||
void TerrainShader::loadViewMatrix(glm::mat4 matrix) {
|
||||
loadMatrix(location_viewMatrix, matrix);
|
||||
loadMatrix(getUniformLocation("viewMatrix"), matrix);
|
||||
}
|
||||
|
||||
void TerrainShader::loadLight(glm::vec3 position, glm::vec3 color) {
|
||||
loadVector(location_lightPosition, position);
|
||||
loadVector(location_lightColor, color);
|
||||
loadVector(getUniformLocation("lightPosition"), position);
|
||||
loadVector(getUniformLocation("lightColor"), color);
|
||||
}
|
||||
|
||||
void TerrainShader::loadHighlight(bool isHighlighted, glm::vec3 color) {
|
||||
loadBoolean(location_isHighlighted, isHighlighted);
|
||||
loadVector(location_highlightColor, color);
|
||||
loadBoolean(getUniformLocation("isHighlighted"), isHighlighted);
|
||||
loadVector(getUniformLocation("highLightColor"), color);
|
||||
}
|
||||
|
||||
|
||||
@ -38,15 +37,4 @@ void TerrainShader::bindAttributes() const {
|
||||
bindAttribute(2, "normal");
|
||||
}
|
||||
|
||||
void TerrainShader::getAllUniformLocations() {
|
||||
location_transformationMatrix = getUniformLocation("transformationMatrix");
|
||||
location_projectionMatrix = getUniformLocation("projectionMatrix");
|
||||
location_viewMatrix = getUniformLocation("viewMatrix");
|
||||
location_lightPosition = getUniformLocation("lightPosition");
|
||||
location_lightColor = getUniformLocation("lightColor");
|
||||
|
||||
location_isHighlighted = getUniformLocation("isHighlighted");
|
||||
location_highlightColor = getUniformLocation("highLightColor");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -18,19 +18,8 @@ public:
|
||||
private:
|
||||
inline static const std::string VERTEX_FILE = "assets/shaders/terrainVertexShader.glsl";
|
||||
inline static const std::string FRAGMENT_FILE = "assets/shaders/terrainFragmentShader.glsl";
|
||||
|
||||
int location_transformationMatrix;
|
||||
int location_projectionMatrix;
|
||||
int location_viewMatrix;
|
||||
int location_lightPosition;
|
||||
int location_lightColor;
|
||||
|
||||
int location_isHighlighted;
|
||||
int location_highlightColor;
|
||||
|
||||
protected:
|
||||
void bindAttributes() const override;
|
||||
void getAllUniformLocations() override;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -5,17 +5,13 @@
|
||||
#include "TextShader.h"
|
||||
|
||||
void TextShader::loadProjectionMatrix(glm::mat4 matrix) {
|
||||
ShaderProgram::loadMatrix(location_projectionMatrix, matrix);
|
||||
ShaderProgram::loadMatrix(getUniformLocation("projectionMatrix"), matrix);
|
||||
}
|
||||
|
||||
void TextShader::loadTextColor(glm::vec3 color) {
|
||||
ShaderProgram::loadVector(location_textColor, color);
|
||||
ShaderProgram::loadVector(getUniformLocation("textColor"), color);
|
||||
}
|
||||
|
||||
void TextShader::getAllUniformLocations() {
|
||||
location_projectionMatrix = ShaderProgram::getUniformLocation("projectionMatrix");
|
||||
location_textColor = ShaderProgram::getUniformLocation("textColor");
|
||||
}
|
||||
|
||||
void TextShader::bindAttributes() const {
|
||||
ShaderProgram::bindAttribute(0, "vertices");
|
||||
|
||||
@ -11,20 +11,16 @@ class TextShader : public ShaderProgram {
|
||||
public :
|
||||
TextShader() : ShaderProgram(VERTEX_FILE, FRAGMENT_FILE) {
|
||||
TextShader::bindAttributes();
|
||||
TextShader::getAllUniformLocations();
|
||||
}
|
||||
void loadProjectionMatrix(glm::mat4 matrix);
|
||||
void loadTextColor(glm::vec3 color);
|
||||
|
||||
protected:
|
||||
void getAllUniformLocations() override;
|
||||
void bindAttributes() const override;
|
||||
private:
|
||||
inline static const std::string VERTEX_FILE = "assets/shaders/textVertexShader.glsl";
|
||||
inline static const std::string FRAGMENT_FILE = "assets/shaders/textFragmentShader.glsl";
|
||||
|
||||
int location_projectionMatrix;
|
||||
int location_textColor;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -5,15 +5,15 @@
|
||||
#include "WorldSpriteShader.h"
|
||||
|
||||
void WorldSpriteShader::loadProjectionViewMatrix(const glm::mat4 &matrix) {
|
||||
loadMatrix(location_projectionViewMatrix, matrix);
|
||||
loadMatrix(getUniformLocation("projectionViewMatrix"), matrix);
|
||||
}
|
||||
|
||||
void WorldSpriteShader::loadModelMatrix(const glm::mat4 &matrix) {
|
||||
loadMatrix(location_modelMatrix, matrix);
|
||||
loadMatrix(getUniformLocation("modelMatrix"), matrix);
|
||||
}
|
||||
|
||||
void WorldSpriteShader::loadAlpha(float alpha) {
|
||||
loadFloat(location_alpha, alpha);
|
||||
loadFloat(getUniformLocation("alpha"), alpha);
|
||||
}
|
||||
|
||||
void WorldSpriteShader::bindAttributes() const {
|
||||
@ -21,8 +21,3 @@ void WorldSpriteShader::bindAttributes() const {
|
||||
bindAttribute(1, "texCoords");
|
||||
}
|
||||
|
||||
void WorldSpriteShader::getAllUniformLocations() {
|
||||
location_projectionViewMatrix = getUniformLocation("projectionViewMatrix");
|
||||
location_modelMatrix = getUniformLocation("modelMatrix");
|
||||
location_alpha = getUniformLocation("alpha");
|
||||
}
|
||||
|
||||
@ -11,7 +11,6 @@ class WorldSpriteShader : public ShaderProgram {
|
||||
public:
|
||||
WorldSpriteShader() : ShaderProgram(VERTEX_FILE, FRAGMENT_FILE) {
|
||||
WorldSpriteShader::bindAttributes();
|
||||
WorldSpriteShader::getAllUniformLocations();
|
||||
}
|
||||
|
||||
void loadProjectionViewMatrix(const glm::mat4 &matrix);
|
||||
@ -22,13 +21,8 @@ private:
|
||||
inline static const std::string VERTEX_FILE = "assets/shaders/worldSpriteVertexShader.glsl";
|
||||
inline static const std::string FRAGMENT_FILE = "assets/shaders/worldSpriteFragmentShader.glsl";
|
||||
|
||||
int location_projectionViewMatrix;
|
||||
int location_modelMatrix;
|
||||
int location_alpha;
|
||||
|
||||
protected:
|
||||
void bindAttributes() const override;
|
||||
void getAllUniformLocations() override;
|
||||
|
||||
};
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user