| 1 | /*! |
|---|
| 2 | * @file shader.h |
|---|
| 3 | * @brief Definition of the Shader rendering class |
|---|
| 4 | */ |
|---|
| 5 | |
|---|
| 6 | #ifndef _SHADER_H |
|---|
| 7 | #define _SHADER_H |
|---|
| 8 | |
|---|
| 9 | #include "base_object.h" |
|---|
| 10 | #include "shader_data.h" |
|---|
| 11 | |
|---|
| 12 | // FORWARD DECLARATION |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | //! The Shader is a Class-wrapper around the OpenGL Shader Language (GLSL). |
|---|
| 17 | class Shader : public BaseObject |
|---|
| 18 | { |
|---|
| 19 | ObjectListDeclaration(Shader); |
|---|
| 20 | public: |
|---|
| 21 | class Uniform |
|---|
| 22 | { |
|---|
| 23 | public: |
|---|
| 24 | Uniform(const Shader& shader, const std::string& location) { this->uniform = glGetUniformLocation(shader.getProgram(), location.c_str()) ; }; |
|---|
| 25 | Uniform(GLhandleARB shaderProgram, const std::string& location) { this->uniform = glGetUniformLocation(shaderProgram, location.c_str()) ; }; |
|---|
| 26 | |
|---|
| 27 | void set(float v0) const { glUniform1f(this->uniform, v0); } |
|---|
| 28 | void set(float v0, float v1) const { glUniform2f(this->uniform, v0, v1); } |
|---|
| 29 | void set(float v0, float v1, float v2) const { glUniform3f(this->uniform, v0, v1, v2); } |
|---|
| 30 | void set(float v0, float v1, float v2, float v3) const { glUniform4f(this->uniform, v0, v1, v2, v3); } |
|---|
| 31 | |
|---|
| 32 | void set(int v0) const { glUniform1i(this->uniform, v0); } |
|---|
| 33 | void set(int v0, int v1) const { glUniform2i(this->uniform, v0, v1); } |
|---|
| 34 | void set(int v0, int v1, int v2) const { glUniform3i(this->uniform, v0, v1, v2); } |
|---|
| 35 | void set(int v0, int v1, int v2, int v3) const { glUniform4i(this->uniform, v0, v1, v2, v3); } |
|---|
| 36 | |
|---|
| 37 | void setV(unsigned int count, float* vv) const; |
|---|
| 38 | void setV(unsigned int count, int* vv) const; |
|---|
| 39 | |
|---|
| 40 | private: |
|---|
| 41 | GLint uniform; |
|---|
| 42 | }; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | public: |
|---|
| 46 | Shader(); |
|---|
| 47 | Shader(const std::string& vertexShaderFile, const std::string& fragmentShaderFile = ""); |
|---|
| 48 | |
|---|
| 49 | Shader& operator=(const Shader& shader) { this->data = shader.data; return *this; }; |
|---|
| 50 | const ShaderData::Pointer& dataPointer() const { return data; }; |
|---|
| 51 | void acquireData(const ShaderData::Pointer& pointer) { this->data = pointer; }; |
|---|
| 52 | |
|---|
| 53 | bool load(const std::string& vertexShaderFile, const std::string& fragmentShaderFile = "") |
|---|
| 54 | { return this->data->load(vertexShaderFile, fragmentShaderFile); } |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | Shader::Uniform getUniform(const std::string& location) { return Shader::Uniform(*this, location); } |
|---|
| 58 | |
|---|
| 59 | GLhandleARB getProgram() const { return this->data->getProgram(); } |
|---|
| 60 | GLhandleARB getVertexS() const { return this->data->getVertexS(); } |
|---|
| 61 | GLhandleARB getFragmentS() const { return this->data->getFragmentS(); } |
|---|
| 62 | |
|---|
| 63 | void activateShader() const; |
|---|
| 64 | static void deactivateShader(); |
|---|
| 65 | |
|---|
| 66 | void debug() const { this->data->debug(); }; |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | static bool checkShaderAbility(); |
|---|
| 70 | |
|---|
| 71 | inline static bool isShaderActive() { return (Shader::storedShader != NULL) ? true : false; }; |
|---|
| 72 | inline static const Shader* getActiveShader() { return Shader::storedShader; }; |
|---|
| 73 | |
|---|
| 74 | inline static void suspendShader() { const Shader* currShader = storedShader; if (storedShader!= NULL) { Shader::deactivateShader(); Shader::storedShader = currShader;} }; |
|---|
| 75 | inline static void restoreShader() { if (storedShader != NULL) storedShader->activateShader(); storedShader = NULL; }; |
|---|
| 76 | |
|---|
| 77 | private: |
|---|
| 78 | ShaderData::Pointer data; |
|---|
| 79 | |
|---|
| 80 | static const Shader* storedShader; |
|---|
| 81 | }; |
|---|
| 82 | |
|---|
| 83 | #endif /* _SHADER_H */ |
|---|