From 9733137499a84f44c29d06d2551d41a903de1112 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 28 Aug 2012 13:34:09 +0300 Subject: [PATCH] Don't expose the shader type enum Instead take an object oriented approach. --- source/ambientocclusion.cpp | 4 ++-- source/bloom.cpp | 4 ++-- source/colorcurve.cpp | 3 +-- source/postprocessor.cpp | 2 +- source/program.cpp | 12 ++++++------ source/shader.cpp | 28 +++++++++++++++++++++++----- source/shader.h | 32 +++++++++++++++++++++----------- 7 files changed, 56 insertions(+), 29 deletions(-) diff --git a/source/ambientocclusion.cpp b/source/ambientocclusion.cpp index e22e0391..de937cf6 100644 --- a/source/ambientocclusion.cpp +++ b/source/ambientocclusion.cpp @@ -69,11 +69,11 @@ AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float depth_ratio): quad(get_fullscreen_quad()) { occlude_shader.attach_shader(get_fullscreen_vertex_shader()); - occlude_shader.attach_shader_owned(new Shader(FRAGMENT_SHADER, occlude_fs)); + occlude_shader.attach_shader_owned(new FragmentShader(occlude_fs)); occlude_shader.link(); combine_shader.attach_shader(get_fullscreen_vertex_shader()); - combine_shader.attach_shader_owned(new Shader(FRAGMENT_SHADER, combine_fs)); + combine_shader.attach_shader_owned(new FragmentShader(combine_fs)); combine_shader.link(); occlusion.storage(GL::RGB, w, h); diff --git a/source/bloom.cpp b/source/bloom.cpp index 8a6ddbcd..b73ea160 100644 --- a/source/bloom.cpp +++ b/source/bloom.cpp @@ -43,11 +43,11 @@ Bloom::Bloom(unsigned w, unsigned h): quad(get_fullscreen_quad()) { blur_shader.attach_shader(get_fullscreen_vertex_shader()); - blur_shader.attach_shader_owned(new Shader(FRAGMENT_SHADER, blur_fs)); + blur_shader.attach_shader_owned(new FragmentShader(blur_fs)); blur_shader.link(); combine_shader.attach_shader(get_fullscreen_vertex_shader()); - combine_shader.attach_shader_owned(new Shader(FRAGMENT_SHADER, combine_fs)); + combine_shader.attach_shader_owned(new FragmentShader(combine_fs)); combine_shader.link(); blur_shdata[0].uniform("delta", 1.0f/w, 0.0f); diff --git a/source/colorcurve.cpp b/source/colorcurve.cpp index 95c82491..d008593e 100644 --- a/source/colorcurve.cpp +++ b/source/colorcurve.cpp @@ -43,8 +43,7 @@ ColorCurve::ColorCurve(): quad(get_fullscreen_quad()) { shprog.attach_shader(get_fullscreen_vertex_shader()); - Shader *fs = new Shader(FRAGMENT_SHADER, fragment_src); - shprog.attach_shader_owned(fs); + shprog.attach_shader_owned(new FragmentShader(fragment_src)); shprog.link(); set_peak(0.2); diff --git a/source/postprocessor.cpp b/source/postprocessor.cpp index add8b1ed..6c9408cb 100644 --- a/source/postprocessor.cpp +++ b/source/postprocessor.cpp @@ -21,7 +21,7 @@ namespace GL { Shader &PostProcessor::get_fullscreen_vertex_shader() { - static Shader shader(VERTEX_SHADER, fullscreen_vs_source); + static VertexShader shader(fullscreen_vs_source); return shader; } diff --git a/source/program.cpp b/source/program.cpp index 729018a1..84b3cdd1 100644 --- a/source/program.cpp +++ b/source/program.cpp @@ -128,8 +128,8 @@ Program::Program(const string &vert, const string &frag) { init(); - attach_shader_owned(new Shader(VERTEX_SHADER, vert)); - attach_shader_owned(new Shader(FRAGMENT_SHADER, frag)); + attach_shader_owned(new VertexShader(vert)); + attach_shader_owned(new FragmentShader(frag)); link(); } @@ -179,8 +179,8 @@ void Program::add_standard_shaders(const StandardFeatures &features) string flags = features.create_flags(); string vertex_src = process_standard_source(standard_vertex_src, flags); string fragment_src = process_standard_source(standard_fragment_src, flags); - attach_shader_owned(new Shader(VERTEX_SHADER, vertex_src)); - attach_shader_owned(new Shader(FRAGMENT_SHADER, fragment_src)); + attach_shader_owned(new VertexShader(vertex_src)); + attach_shader_owned(new FragmentShader(fragment_src)); } string Program::process_standard_source(const char **source, const string &flags) @@ -477,7 +477,7 @@ void Program::Loader::attribute(unsigned i, const string &n) void Program::Loader::fragment_shader(const string &src) { - obj.attach_shader_owned(new Shader(FRAGMENT_SHADER, src)); + obj.attach_shader_owned(new FragmentShader(src)); } void Program::Loader::standard() @@ -489,7 +489,7 @@ void Program::Loader::standard() void Program::Loader::vertex_shader(const string &src) { - obj.attach_shader_owned(new Shader(VERTEX_SHADER, src)); + obj.attach_shader_owned(new VertexShader(src)); } diff --git a/source/shader.cpp b/source/shader.cpp index b0cf5a3e..0da1a6e9 100644 --- a/source/shader.cpp +++ b/source/shader.cpp @@ -9,12 +9,12 @@ using namespace std; namespace Msp { namespace GL { -Shader::Shader(ShaderType t) +Shader::Shader(GLenum t) { init(t); } -Shader::Shader(ShaderType t, const string &src) +Shader::Shader(GLenum t, const string &src) { init(t); @@ -22,13 +22,13 @@ Shader::Shader(ShaderType t, const string &src) compile(); } -void Shader::init(ShaderType t) +void Shader::init(GLenum t) { compiled = false; - if(t==FRAGMENT_SHADER) + if(t==GL_FRAGMENT_SHADER) static Require _req(ARB_fragment_shader); - else if(t==VERTEX_SHADER) + else if(t==GL_VERTEX_SHADER) static Require _req(ARB_vertex_shader); id = glCreateShader(t); @@ -74,5 +74,23 @@ string Shader::get_info_log() const return log; } + +VertexShader::VertexShader(): + Shader(GL_VERTEX_SHADER) +{ } + +VertexShader::VertexShader(const string &src): + Shader(GL_VERTEX_SHADER, src) +{ } + + +FragmentShader::FragmentShader(): + Shader(GL_FRAGMENT_SHADER) +{ } + +FragmentShader::FragmentShader(const string &src): + Shader(GL_FRAGMENT_SHADER, src) +{ } + } // namespace GL } // namespace Msp diff --git a/source/shader.h b/source/shader.h index c1fd6b22..63af9b3a 100644 --- a/source/shader.h +++ b/source/shader.h @@ -7,25 +7,19 @@ namespace Msp { namespace GL { -enum ShaderType -{ - FRAGMENT_SHADER = GL_FRAGMENT_SHADER_ARB, - VERTEX_SHADER = GL_VERTEX_SHADER_ARB -}; - class Shader { private: unsigned id; bool compiled; -public: - Shader(ShaderType t); - Shader(ShaderType t, const std::string &); +protected: + Shader(GLenum t); + Shader(GLenum t, const std::string &); private: - void init(ShaderType); + void init(GLenum); public: - ~Shader(); + virtual ~Shader(); void source(unsigned count, const char **str, const int *len); void source(const std::string &str); @@ -36,6 +30,22 @@ public: std::string get_info_log() const; }; + +class VertexShader: public Shader +{ +public: + VertexShader(); + VertexShader(const std::string &); +}; + + +class FragmentShader: public Shader +{ +public: + FragmentShader(); + FragmentShader(const std::string &); +}; + } // namespace GL } // namespace Msp -- 2.43.0