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 | //! A class for ... |
---|
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 | { |
---|
42 | switch (count) |
---|
43 | { |
---|
44 | case 1: |
---|
45 | glUniform1fv(this->uniform, 1, vv); |
---|
46 | break; |
---|
47 | case 2: |
---|
48 | glUniform2fv(this->uniform, 2, vv); |
---|
49 | break; |
---|
50 | case 3: |
---|
51 | glUniform3fv(this->uniform, 3, vv); |
---|
52 | break; |
---|
53 | case 4: |
---|
54 | glUniform4fv(this->uniform, 4, vv); |
---|
55 | break; |
---|
56 | } |
---|
57 | } |
---|
58 | void setV(unsigned int count, int* vv) const |
---|
59 | { |
---|
60 | switch (count) |
---|
61 | { |
---|
62 | case 1: |
---|
63 | glUniform1iv(this->uniform, 1, vv); |
---|
64 | break; |
---|
65 | case 2: |
---|
66 | glUniform2iv(this->uniform, 2, vv); |
---|
67 | break; |
---|
68 | case 3: |
---|
69 | glUniform3iv(this->uniform, 3, vv); |
---|
70 | break; |
---|
71 | case 4: |
---|
72 | glUniform4iv(this->uniform, 4, vv); |
---|
73 | break; |
---|
74 | } |
---|
75 | } |
---|
76 | private: |
---|
77 | GLint uniform; |
---|
78 | }; |
---|
79 | |
---|
80 | typedef enum |
---|
81 | { |
---|
82 | None = 0, |
---|
83 | Fragment = 1, |
---|
84 | Vertex = 2, |
---|
85 | |
---|
86 | Program = 4, |
---|
87 | } |
---|
88 | Type; |
---|
89 | |
---|
90 | public: |
---|
91 | Shader(const std::string& vertexShaderFile = "", const std::string& fragmentShaderFile = ""); |
---|
92 | virtual ~Shader(); |
---|
93 | static Shader* getShader(const std::string& vertexShaderFile, const std::string& fragmentShaderFile); |
---|
94 | static bool unload(Shader* shader); |
---|
95 | |
---|
96 | |
---|
97 | static bool checkShaderAbility(); |
---|
98 | void activateShader(); |
---|
99 | void bindShader(const char* name, const float* value, size_t size); |
---|
100 | static void deactivateShader(); |
---|
101 | |
---|
102 | Shader::Uniform getUniform(const std::string& location) { return Shader::Uniform(this, location); } |
---|
103 | |
---|
104 | bool loadShaderProgramm(Shader::Type type, const std::string& fileName); |
---|
105 | void deleteProgram(Shader::Type type); |
---|
106 | |
---|
107 | void linkShaderProgram(); |
---|
108 | |
---|
109 | |
---|
110 | bool readShader(const std::string& fileName, std::string& output); |
---|
111 | |
---|
112 | |
---|
113 | inline static bool shaderActive() { return (Shader::storedShader != NULL)? true : false; }; |
---|
114 | inline static Shader* getActiveShader() { return Shader::storedShader; }; |
---|
115 | inline static void suspendShader() { Shader* currShader = storedShader; if (storedShader!= NULL) { Shader::deactivateShader(); Shader::storedShader = currShader;} }; |
---|
116 | inline static void restoreShader() { if (storedShader != NULL) storedShader->activateShader(); storedShader = NULL; }; |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | |
---|
121 | GLhandleARB getProgram() const { return this->shaderProgram; } |
---|
122 | GLhandleARB getVertexS() const { return this->vertexShader; } |
---|
123 | GLhandleARB getFragmentS() const { return this->fragmentShader; } |
---|
124 | |
---|
125 | void debug() const; |
---|
126 | |
---|
127 | static void printError(GLhandleARB program); |
---|
128 | |
---|
129 | |
---|
130 | private: |
---|
131 | std::string fragmentShaderFile; |
---|
132 | std::string vertexShaderFile; |
---|
133 | |
---|
134 | GLhandleARB shaderProgram; |
---|
135 | |
---|
136 | GLhandleARB vertexShader; |
---|
137 | GLhandleARB fragmentShader; |
---|
138 | |
---|
139 | |
---|
140 | static Shader* storedShader; |
---|
141 | }; |
---|
142 | |
---|
143 | #endif /* _SHADER_H */ |
---|