aboutsummaryrefslogtreecommitdiff
path: root/src/RenderBackend/OpenGL
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2017-12-27 23:48:41 +0100
committerdec05eba <dec05eba@protonmail.com>2021-11-18 15:21:48 +0100
commitff4daae11db0ab811cac66e262d289a4107bba4a (patch)
tree7b62216d578a59b940dada8e794355dfb7ff3273 /src/RenderBackend/OpenGL
parent2d8b61bf9fc36e933a0091e1098e62faf143412c (diff)
Add uniform (shader global variable)
Diffstat (limited to 'src/RenderBackend/OpenGL')
-rw-r--r--src/RenderBackend/OpenGL/PixelShader.cpp18
-rw-r--r--src/RenderBackend/OpenGL/ShaderProgram.cpp21
-rw-r--r--src/RenderBackend/OpenGL/ShaderVec.cpp6
3 files changed, 45 insertions, 0 deletions
diff --git a/src/RenderBackend/OpenGL/PixelShader.cpp b/src/RenderBackend/OpenGL/PixelShader.cpp
index 8aa4d9a..f263513 100644
--- a/src/RenderBackend/OpenGL/PixelShader.cpp
+++ b/src/RenderBackend/OpenGL/PixelShader.cpp
@@ -49,6 +49,24 @@ namespace amalgine
return ShaderOutputVec4(this, attributeIndex);
}
+ // TODO: Generate warning if global variable is defined but not assigned to?
+ ShaderGlobalVec3 PixelShader::defineGlobalVec3(const std::string &name)
+ {
+ if(!isShaderVariableNameValid(name.c_str()))
+ throw PixelShaderInvalidAttributeName(name);
+
+ if(globalAttributes.find(name) != globalAttributes.end())
+ throw PixelShaderAttributeAlreadyDefined(name);
+
+ writeHeader("uniform vec3 ");
+ writeHeader(name);
+ writeHeader(";\n");
+
+ ShaderGlobalVec3 globalVec(name);
+ globalAttributes[name] = globalVec.getVecObject();
+ return ShaderGlobalVec3(globalVec);
+ }
+
i32 PixelShader::defineOutputVariable(const string &variableName, const char *typeName)
{
if(!isShaderVariableNameValid(variableName.c_str()))
diff --git a/src/RenderBackend/OpenGL/ShaderProgram.cpp b/src/RenderBackend/OpenGL/ShaderProgram.cpp
index 40f5b6c..aceda3b 100644
--- a/src/RenderBackend/OpenGL/ShaderProgram.cpp
+++ b/src/RenderBackend/OpenGL/ShaderProgram.cpp
@@ -12,6 +12,12 @@ namespace amalgine
}
+ ShaderProgramNonExistingGlobalVariable::ShaderProgramNonExistingGlobalVariable(const char *variableName) :
+ runtime_error(string("There is no shader global variable with the name '") + variableName + "'")
+ {
+
+ }
+
ShaderProgram::ShaderProgram() : built(false)
{
shaderProgramId = glCreateProgram();
@@ -84,4 +90,19 @@ namespace amalgine
throw ShaderProgramUsedBeforeBuilt();
}
}
+
+ ShaderProgramGlobalVec3 ShaderProgram::getGlobalVec3(const char *name)
+ {
+ if(built)
+ {
+ GLint uniformId = glGetUniformLocation(shaderProgramId, name);
+ if(uniformId == -1)
+ throw ShaderProgramNonExistingGlobalVariable(name);
+ return ShaderProgramGlobalVec3(this, uniformId);
+ }
+ else
+ {
+ throw ShaderProgramUsedBeforeBuilt();
+ }
+ }
}
diff --git a/src/RenderBackend/OpenGL/ShaderVec.cpp b/src/RenderBackend/OpenGL/ShaderVec.cpp
index 3bbe0be..dd3bbe5 100644
--- a/src/RenderBackend/OpenGL/ShaderVec.cpp
+++ b/src/RenderBackend/OpenGL/ShaderVec.cpp
@@ -20,6 +20,12 @@ namespace amalgine
return AttributeType::NONE;
}
+ void ShaderProgramGlobalVec3::set(f32 x, f32 y, f32 z)
+ {
+ shaderProgram->use();
+ glUniform3f(uniformId, x, y, z);
+ }
+
const string& ShaderInputVec2::getName() const
{
return vertexShader->getInputAttributeName(attributeIndex);