| 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 "glincl.h" |
|---|
| 11 | #include <vector> |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | // FORWARD DECLARATION |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | //! The Shader is a Class-wrapper around the OpenGL Shader Language (GLSL). |
|---|
| 19 | class Shader : public BaseObject |
|---|
| 20 | { |
|---|
| 21 | ObjectListDeclaration(Shader); |
|---|
| 22 | public: |
|---|
| 23 | class Uniform |
|---|
| 24 | { |
|---|
| 25 | public: |
|---|
| 26 | Uniform(const Shader* shader, const std::string& location) { this->uniform = glGetUniformLocationARB(shader->getProgram(), location.c_str()) ; } |
|---|
| 27 | Uniform(const Shader& shader, const std::string& location) { this->uniform = glGetUniformLocation(shader.getProgram(), location.c_str()) ; }; |
|---|
| 28 | Uniform(GLhandleARB shaderProgram, const std::string& location) { this->uniform = glGetUniformLocation(shaderProgram, location.c_str()) ; }; |
|---|
| 29 | |
|---|
| 30 | void set(float v0) const { glUniform1f(this->uniform, v0); } |
|---|
| 31 | void set(float v0, float v1) const { glUniform2f(this->uniform, v0, v1); } |
|---|
| 32 | void set(float v0, float v1, float v2) const { glUniform3f(this->uniform, v0, v1, v2); } |
|---|
| 33 | void set(float v0, float v1, float v2, float v3) const { glUniform4f(this->uniform, v0, v1, v2, v3); } |
|---|
| 34 | |
|---|
| 35 | void set(int v0) const { glUniform1i(this->uniform, v0); } |
|---|
| 36 | void set(int v0, int v1) const { glUniform2i(this->uniform, v0, v1); } |
|---|
| 37 | void set(int v0, int v1, int v2) const { glUniform3i(this->uniform, v0, v1, v2); } |
|---|
| 38 | void set(int v0, int v1, int v2, int v3) const { glUniform4i(this->uniform, v0, v1, v2, v3); } |
|---|
| 39 | |
|---|
| 40 | void setV(unsigned int count, float* vv) const; |
|---|
| 41 | void setV(unsigned int count, int* vv) const; |
|---|
| 42 | |
|---|
| 43 | private: |
|---|
| 44 | GLint uniform; |
|---|
| 45 | }; |
|---|
| 46 | |
|---|
| 47 | //! The Type of Shader. |
|---|
| 48 | typedef enum |
|---|
| 49 | { |
|---|
| 50 | None = 0, //!< No Type at all |
|---|
| 51 | Fragment = 1, //!< Fragment Shader. |
|---|
| 52 | Vertex = 2, //!< Vertex Shader. |
|---|
| 53 | Program = 4, //!< Compiled Shader Programm. |
|---|
| 54 | } Type; |
|---|
| 55 | |
|---|
| 56 | public: |
|---|
| 57 | Shader(const std::string& vertexShaderFile = "", const std::string& fragmentShaderFile = ""); |
|---|
| 58 | virtual ~Shader(); |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | Shader::Uniform getUniform(const std::string& location) { return Shader::Uniform(this, location); } |
|---|
| 62 | |
|---|
| 63 | GLhandleARB getProgram() const { return this->shaderProgram; } |
|---|
| 64 | GLhandleARB getVertexS() const { return this->vertexShader; } |
|---|
| 65 | GLhandleARB getFragmentS() const { return this->fragmentShader; } |
|---|
| 66 | |
|---|
| 67 | static bool checkShaderAbility(); |
|---|
| 68 | void activateShader(); |
|---|
| 69 | static void deactivateShader(); |
|---|
| 70 | |
|---|
| 71 | void debug() const; |
|---|
| 72 | |
|---|
| 73 | inline static bool isShaderActive() { return (Shader::storedShader != NULL) ? true : false; }; |
|---|
| 74 | inline static Shader* getActiveShader() { return Shader::storedShader; }; |
|---|
| 75 | inline static void suspendShader() { Shader* currShader = storedShader; if (storedShader!= NULL) { Shader::deactivateShader(); Shader::storedShader = currShader;} }; |
|---|
| 76 | inline static void restoreShader() { if (storedShader != NULL) storedShader->activateShader(); storedShader = NULL; }; |
|---|
| 77 | |
|---|
| 78 | static void printError(GLhandleARB program); |
|---|
| 79 | |
|---|
| 80 | private: |
|---|
| 81 | void bindShader(const char* name, const float* value, size_t size); |
|---|
| 82 | void linkShaderProgram(); |
|---|
| 83 | bool readShader(const std::string& fileName, std::string& output); |
|---|
| 84 | bool loadShaderProgramm(Shader::Type type, const std::string& fileName); |
|---|
| 85 | void deleteProgram(Shader::Type type); |
|---|
| 86 | |
|---|
| 87 | private: |
|---|
| 88 | std::string fragmentShaderFile; |
|---|
| 89 | std::string vertexShaderFile; |
|---|
| 90 | |
|---|
| 91 | GLhandleARB shaderProgram; |
|---|
| 92 | |
|---|
| 93 | GLhandleARB vertexShader; |
|---|
| 94 | GLhandleARB fragmentShader; |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | static Shader* storedShader; |
|---|
| 98 | }; |
|---|
| 99 | |
|---|
| 100 | #endif /* _SHADER_H */ |
|---|