From 3eec99589dadfa842af9d21d18cf871ca2d9a635 Mon Sep 17 00:00:00 2001 From: malloc Date: Wed, 22 Aug 2018 17:01:47 -0500 Subject: [PATCH] shaded boob --- src/client/shaders/_shader.cpp | 65 ++++++++++++++++++++++++++++++++++ src/client/shaders/_shader.hpp | 59 +++++++++++++++++++++++++++--- 2 files changed, 119 insertions(+), 5 deletions(-) diff --git a/src/client/shaders/_shader.cpp b/src/client/shaders/_shader.cpp index 2bf08d8..d3de410 100644 --- a/src/client/shaders/_shader.cpp +++ b/src/client/shaders/_shader.cpp @@ -1,2 +1,67 @@ #include "_shader.hpp" +sosc::Shader::Shader() : loaded(false) {} + +bool sosc::Shader::Load() { + if(this->loaded) + return true; + + program = glCreateProgram(); + if(!PrepareLoad()) + return false; + + this->loaded = true; + return true; +} + +void sosc::Shader::Start() const { + if(!this->loaded) + return; + + glUseProgram(this->program); +} + +void sosc::Shader::Stop() const { + if(!this->loaded) + return; + + glUseProgram(0); +} + +void sosc::Shader::Unload() { + if(!this->loaded) + return; + + PrepareUnload(); + glDeleteProgram(this->program); + this->loaded = false; +} + +void sosc::Shader::AttachSource + (const std::string& fileName, GLuint shaderType) +{ + if(this->loaded) + return; + + +} + +void sosc::Shader::LinkProgram() { + glLinkProgram(this->program); + for(const auto& shader : this->shaders) + glDeleteShader(shader); +} + +void sosc::Shader::LoadUniforms(std::vector names) { + if(this->loaded) + return; + + GLint id; + this->locations.clear(); + for(const auto& name : names) { + if((id = glGetUniformLocation(this->program, name.c_str())) == -1) + throw ShaderUniformException(name); + + this->locations.push_back(id); + } +} \ No newline at end of file diff --git a/src/client/shaders/_shader.hpp b/src/client/shaders/_shader.hpp index 90b338c..16b59e7 100644 --- a/src/client/shaders/_shader.hpp +++ b/src/client/shaders/_shader.hpp @@ -3,25 +3,74 @@ #include "common.hpp" #include -#include +#include +#include +#include +#include + +namespace sosc { +struct ShaderCompilationException : public std::exception { +public: + ShaderCompilationException + (const std::string& shaderName, const std::string& errorInfo) + { + std::stringstream ss; + ss << "Error in '" << shaderName << "': " << errorInfo; + this->errorInfo = ss.str(); + } + + const char* what() noexcept { + return this->errorInfo.c_str(); + } +private: + std::string errorInfo; +}; + +struct ShaderUniformException : public std::exception { +public: + explicit ShaderUniformException(const std::string& uniformName) { + std::stringstream ss; + ss << "Could not find uniform '" << uniformName << "'."; + this->errorInfo = ss.str(); + } + + const char* what() noexcept { + return this->errorInfo.c_str(); + } +private: + std::string errorInfo; +}; class Shader { public: Shader(); + bool Load(); + void Start() const; - void Start(); - void Stop(); + inline GLint operator[] (std::vector::size_type index) { + return locations[index]; + } + inline GLint Uniform(std::vector::size_type index) { + return locations[index]; + } + + void Stop() const; void Unload(); protected: virtual bool PrepareLoad() = 0; virtual void PrepareUnload() {}; - bool AttachSource(); - bool FetchUniforms(int count, GLint* ids, ...); + void AttachSource(const std::string& fileName, GLuint shaderType); + void LinkProgram(); + void LoadUniforms(std::vector names); private: + std::vector locations; + std::vector shaders; GLuint program; + bool loaded; }; +} #endif