[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" |
---|
[5261] | 10 | #include "glincl.h" |
---|
[7164] | 11 | #include <vector> |
---|
[1853] | 12 | |
---|
[5261] | 13 | |
---|
[8037] | 14 | // FORWARD DECLARATION |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | //! A class for ... |
---|
| 19 | class Shader : public BaseObject |
---|
[5261] | 20 | { |
---|
[8037] | 21 | public: |
---|
| 22 | class Uniform |
---|
| 23 | { |
---|
| 24 | public: |
---|
| 25 | Uniform(const Shader* shader, const std::string& location) { this->uniform = glGetUniformLocationARB(shader->getProgram(), location.c_str()) ; } |
---|
| 26 | Uniform(const Shader& shader, const std::string& location) { this->uniform = glGetUniformLocation(shader.getProgram(), location.c_str()) ; }; |
---|
| 27 | Uniform(GLhandleARB shaderProgram, const std::string& location) { this->uniform = glGetUniformLocation(shaderProgram, location.c_str()) ; }; |
---|
[5261] | 28 | |
---|
[8037] | 29 | void set(float v0) const { glUniform1f(this->uniform, v0); } |
---|
| 30 | void set(float v0, float v1) const { glUniform2f(this->uniform, v0, v1); } |
---|
| 31 | void set(float v0, float v1, float v2) const { glUniform3f(this->uniform, v0, v1, v2); } |
---|
| 32 | void set(float v0, float v1, float v2, float v3) const { glUniform4f(this->uniform, v0, v1, v2, v3); } |
---|
[5261] | 33 | |
---|
[8037] | 34 | void set(int v0) const { glUniform1i(this->uniform, v0); } |
---|
| 35 | void set(int v0, int v1) const { glUniform2i(this->uniform, v0, v1); } |
---|
| 36 | void set(int v0, int v1, int v2) const { glUniform3i(this->uniform, v0, v1, v2); } |
---|
| 37 | void set(int v0, int v1, int v2, int v3) const { glUniform4i(this->uniform, v0, v1, v2, v3); } |
---|
[3543] | 38 | |
---|
[8037] | 39 | void setV(unsigned int count, float* vv) const |
---|
| 40 | { |
---|
| 41 | switch (count) |
---|
| 42 | { |
---|
| 43 | case 1: |
---|
| 44 | glUniform1fv(this->uniform, 1, vv); |
---|
| 45 | break; |
---|
| 46 | case 2: |
---|
| 47 | glUniform2fv(this->uniform, 2, vv); |
---|
| 48 | break; |
---|
| 49 | case 3: |
---|
| 50 | glUniform3fv(this->uniform, 3, vv); |
---|
| 51 | break; |
---|
| 52 | case 4: |
---|
| 53 | glUniform4fv(this->uniform, 4, vv); |
---|
| 54 | break; |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | void setV(unsigned int count, int* vv) const |
---|
| 58 | { |
---|
| 59 | switch (count) |
---|
| 60 | { |
---|
| 61 | case 1: |
---|
| 62 | glUniform1iv(this->uniform, 1, vv); |
---|
| 63 | break; |
---|
| 64 | case 2: |
---|
| 65 | glUniform2iv(this->uniform, 2, vv); |
---|
| 66 | break; |
---|
| 67 | case 3: |
---|
| 68 | glUniform3iv(this->uniform, 3, vv); |
---|
| 69 | break; |
---|
| 70 | case 4: |
---|
| 71 | glUniform4iv(this->uniform, 4, vv); |
---|
| 72 | break; |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | private: |
---|
| 76 | GLint uniform; |
---|
| 77 | }; |
---|
[3543] | 78 | |
---|
[8037] | 79 | typedef enum |
---|
| 80 | { |
---|
| 81 | None = 0, |
---|
| 82 | Fragment = 1, |
---|
| 83 | Vertex = 2, |
---|
[1853] | 84 | |
---|
[8037] | 85 | Program = 4, |
---|
| 86 | } |
---|
| 87 | Type; |
---|
| 88 | |
---|
| 89 | public: |
---|
[7221] | 90 | Shader(const std::string& vertexShaderFile = "", const std::string& fragmentShaderFile = ""); |
---|
[5261] | 91 | virtual ~Shader(); |
---|
[7221] | 92 | static Shader* getShader(const std::string& vertexShaderFile, const std::string& fragmentShaderFile); |
---|
[5323] | 93 | static bool unload(Shader* shader); |
---|
[1853] | 94 | |
---|
[8037] | 95 | |
---|
| 96 | static bool checkShaderAbility(); |
---|
[5266] | 97 | void activateShader(); |
---|
[8255] | 98 | void bindShader(const char* name, const float* value, size_t size); |
---|
[5266] | 99 | static void deactivateShader(); |
---|
[3245] | 100 | |
---|
[8037] | 101 | Shader::Uniform getUniform(const std::string& location) { return Shader::Uniform(this, location); } |
---|
[5266] | 102 | |
---|
[8037] | 103 | bool loadShaderProgramm(Shader::Type type, const std::string& fileName); |
---|
| 104 | void deleteProgram(Shader::Type type); |
---|
[5317] | 105 | |
---|
[8037] | 106 | void linkShaderProgram(); |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | bool readShader(const std::string& fileName, std::string& output); |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | inline static bool shaderActive() { return (Shader::storedShader != NULL)? true : false; }; |
---|
[5323] | 113 | inline static Shader* getActiveShader() { return Shader::storedShader; }; |
---|
[5318] | 114 | inline static void suspendShader() { Shader* currShader = storedShader; if (storedShader!= NULL) { Shader::deactivateShader(); Shader::storedShader = currShader;} }; |
---|
[5317] | 115 | inline static void restoreShader() { if (storedShader != NULL) storedShader->activateShader(); storedShader = NULL; }; |
---|
| 116 | |
---|
| 117 | |
---|
[8037] | 118 | |
---|
| 119 | |
---|
| 120 | GLhandleARB getProgram() const { return this->shaderProgram; } |
---|
| 121 | GLhandleARB getVertexS() const { return this->vertexShader; } |
---|
| 122 | GLhandleARB getFragmentS() const { return this->fragmentShader; } |
---|
| 123 | |
---|
[5262] | 124 | void debug() const; |
---|
| 125 | |
---|
[8037] | 126 | static void printError(GLhandleARB program); |
---|
[5262] | 127 | |
---|
[5317] | 128 | |
---|
[8037] | 129 | private: |
---|
| 130 | std::string fragmentShaderFile; |
---|
| 131 | std::string vertexShaderFile; |
---|
| 132 | |
---|
| 133 | GLhandleARB shaderProgram; |
---|
| 134 | |
---|
| 135 | GLhandleARB vertexShader; |
---|
| 136 | GLhandleARB fragmentShader; |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | static Shader* storedShader; |
---|
[1853] | 140 | }; |
---|
| 141 | |
---|
[5261] | 142 | #endif /* _SHADER_H */ |
---|