From fbd2e5d9a802db4fb5e056705ec599ac423e09be Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 27 Oct 2019 01:30:34 +0200 Subject: Create Shader class as a base for vertex and pixel shader --- src/RenderBackend/OpenGL/VertexShader.cpp | 111 +++--------------------------- 1 file changed, 8 insertions(+), 103 deletions(-) (limited to 'src/RenderBackend/OpenGL/VertexShader.cpp') diff --git a/src/RenderBackend/OpenGL/VertexShader.cpp b/src/RenderBackend/OpenGL/VertexShader.cpp index ac9a159..19705b6 100644 --- a/src/RenderBackend/OpenGL/VertexShader.cpp +++ b/src/RenderBackend/OpenGL/VertexShader.cpp @@ -2,62 +2,16 @@ #include "../../../include/RenderBackend/OpenGL/opengl.hpp" #include "../../../include/RenderBackend/OpenGL/CompiledShader.hpp" -using namespace std; - -namespace amalgine -{ - VertexShaderTooManyAttributes::VertexShaderTooManyAttributes(i32 maxVertexAttributes) : - runtime_error(string("Attempting to define more than ") + to_string(maxVertexAttributes) + " vertex attributes") - { - - } - - VertexShaderAttributeAlreadyDefined::VertexShaderAttributeAlreadyDefined(const std::string &attributeName) : - runtime_error(string("A vertex attribute with the name ") + attributeName + " has already been defined") - { - - } - - VertexShaderInvalidAttributeName::VertexShaderInvalidAttributeName(const std::string &attributeName) : - runtime_error(string("Vertex attribute name ") + attributeName + " is invalid") - { - - } - - VertexShaderFunctionAlreadyDefined::VertexShaderFunctionAlreadyDefined(const std::string &funcName) : - runtime_error(string("Vertex shader function already defined: ") + funcName) - { - - } - - VertexShader::VertexShader() : locationCounter(0), mainFuncDefined(false) - { - glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVertexAttribs); - writeHeader("#version 330 core\n\n"); - } - - const string& VertexShader::getInputAttributeName(i32 attributeIndex) const - { - assert(attributeIndex < vertexAttributeNames.size()); - return vertexAttributeNames[attributeIndex].name; - } - - AttributeType VertexShader::getInputAttributeType(i32 attributeIndex) const +namespace amalgine { + VertexShader::VertexShader() { - assert(attributeIndex < vertexAttributeNames.size()); - return getAttributeTypeByName(vertexAttributeNames[attributeIndex].typeName); + glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxAttribs); } - - ShaderInputVec2 VertexShader::defineInputVec2(const std::string &name) - { - i32 attributeIndex = defineInputVariable(name, "vec2"); - return ShaderInputVec2(this, attributeIndex); - } - + void VertexShader::defineMain(VertexShaderMainFunc mainFunc) { if(mainFuncDefined) - throw VertexShaderFunctionAlreadyDefined("main"); + throw ShaderFunctionAlreadyDefined("main"); writeBody("void main() {\n"); ShaderVec4 glPosition = mainFunc(); @@ -66,60 +20,11 @@ namespace amalgine writeBody(glPosition.getOutput()); writeBody(";\n}\n\n"); } - - i32 VertexShader::defineInputVariable(const string &variableName, const char *typeName) - { - if(!isShaderVariableNameValid(variableName.c_str())) - throw VertexShaderInvalidAttributeName(variableName); - - if(locationCounter + 1 > maxVertexAttribs) - throw VertexShaderTooManyAttributes(maxVertexAttribs); - - if(vertexAttributes.find(variableName) != vertexAttributes.end()) - throw VertexShaderAttributeAlreadyDefined(variableName); - - i32 attributeIndex = locationCounter; - vertexAttributes[variableName] = locationCounter; - ShaderAttribute shaderAttribute; - shaderAttribute.name = variableName; - shaderAttribute.typeName = typeName; - vertexAttributeNames.push_back(shaderAttribute); - - writeHeader("layout(location = "); - writeHeader(to_string(locationCounter)); - ++locationCounter; - writeHeader(") in "); - writeHeader(typeName); - writeHeader(" "); - writeHeader(variableName); - writeHeader(";\n"); - return attributeIndex; - } - - string VertexShader::build() const - { - std::string result; - result.reserve(header.size() + 2 + body.size()); - result += header; - result += "\n"; - result += body; - return result; - } - - void VertexShader::writeHeader(const string &code) - { - header += code; - } - - void VertexShader::writeBody(const string &code) - { - body += code; - } - + Result VertexShader::compile() { GLuint vertexShaderId = glCreateShader(GL_VERTEX_SHADER); - string verterShaderSource = build(); + std::string verterShaderSource = build(); const char *verterShaderSourcePtr = verterShaderSource.c_str(); printf("Vertex shader:\n%s\n", verterShaderSourcePtr); glShaderSource(vertexShaderId, 1, &verterShaderSourcePtr, NULL); @@ -127,7 +32,7 @@ namespace amalgine GLint status; glGetShaderiv(vertexShaderId, GL_COMPILE_STATUS, &status); - string compileLog = getShaderCompileLog((u32)vertexShaderId); + std::string compileLog = getShaderCompileLog((u32)vertexShaderId); if(status == GL_TRUE) { if(!compileLog.empty()) -- cgit v1.2.3