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