shaded boob

This commit is contained in:
malloc 2018-08-22 17:01:47 -05:00
parent b7a2e86477
commit 3eec99589d
2 changed files with 119 additions and 5 deletions

View file

@ -1,2 +1,67 @@
#include "_shader.hpp" #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<std::string> 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);
}
}

View file

@ -3,25 +3,74 @@
#include "common.hpp" #include "common.hpp"
#include <GL/glew.h> #include <GL/glew.h>
#include <cstdarg> #include <vector>
#include <exception>
#include <sstream>
#include <fstream>
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 { class Shader {
public: public:
Shader(); Shader();
bool Load(); bool Load();
void Start() const;
void Start(); inline GLint operator[] (std::vector::size_type index) {
void Stop(); return locations[index];
}
inline GLint Uniform(std::vector::size_type index) {
return locations[index];
}
void Stop() const;
void Unload(); void Unload();
protected: protected:
virtual bool PrepareLoad() = 0; virtual bool PrepareLoad() = 0;
virtual void PrepareUnload() {}; virtual void PrepareUnload() {};
bool AttachSource(); void AttachSource(const std::string& fileName, GLuint shaderType);
bool FetchUniforms(int count, GLint* ids, ...); void LinkProgram();
void LoadUniforms(std::vector<std::string> names);
private: private:
std::vector<GLint> locations;
std::vector<GLuint> shaders;
GLuint program; GLuint program;
bool loaded;
}; };
}
#endif #endif