jGL
Loading...
Searching...
No Matches
glShader.h
Go to the documentation of this file.
1#ifndef GLSHADER
2#define GLSHADER
3
4#include <jGL/OpenGL/gl.h>
5#include <jGL/shader.h>
6
7namespace jGL::GL
8{
9
14 struct glShader : public Shader
15 {
16
23 glShader(const char * v, const char * f)
24 : Shader(v, f), program(0), compiled(false), used(false)
25 {}
26
31 : Shader(), program(0), compiled(false), used(false)
32 {}
33
40 glShader(std::string path, std::string name)
41 : Shader(path, name), program(0), compiled(false), used(false)
42 {}
43
45
50 void create();
51
56 void release();
57
62 void compile();
63
68 void use();
69
76 bool isCompiled(){return compiled;}
77
84 bool isProgram(){return glIsProgram(program);}
85
86 private:
87
88 GLuint program;
89 bool compiled;
90 bool used;
91
92 inline const GLuint location(const char * name) const
93 {
94 return glGetUniformLocation(program, name);
95 }
96
97 // cannot have spec in class scope https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85282
98 // also cannot use partial spec workaround because non-class, non-variable partial
99 // specialization is not allowed
100 // https://stackoverflow.com/questions/8061456/c-function-template-partial-specialization
101 // so the template dispatches to these guys
102
103 void setValue(jGLUniform<int> * u, int value)
104 {
105 u->value = value;
106 if (isCompiled())
107 {
108 use();
109 glUniform1i(location(u->name.c_str()), u->value);
110 }
111
112 }
113
114 void setValue(jGLUniform<Sampler2D> * u, Sampler2D value)
115 {
116 u->value = value;
117 if (isCompiled())
118 {
119 use();
120 glUniform1i(location(u->name.c_str()), u->value.texture);
121 }
122
123 }
124
125 void setValue(jGLUniform<float> * u, float value)
126 {
127 u->value = value;
128 if (isCompiled())
129 {
130 use();
131 glUniform1f(location(u->name.c_str()), u->value);
132 }
133
134 }
135
136 void setValue(jGLUniform<glm::vec2> * u, glm::vec2 value)
137 {
138 u->value = value;
139 if (isCompiled())
140 {
141 use();
142 glUniform2f(location(u->name.c_str()), u->value.x, u->value.y);
143 }
144
145 }
146
147 void setValue(jGLUniform<glm::vec4> * u, glm::vec4 value)
148 {
149 u->value = value;
150 if (isCompiled())
151 {
152 use();
153 glUniform4f(location(u->name.c_str()), u->value.x, u->value.y, u->value.z, u->value.w);
154 }
155
156 }
157
158 void setValue(jGLUniform<glm::mat4> * u, glm::mat4 value)
159 {
160 u->value = value;
161 if (isCompiled())
162 {
163 use();
164 glUniformMatrix4fv(location(u->name.c_str()), 1, false, glm::value_ptr(u->value));
165 }
166 }
167
168 };
169
170}
171#endif /* GLSHADER */
Definition gl.h:41
std::string name
Definition uniform.h:17
An OpenGL implementation of Shader.
Definition glShader.h:15
bool isCompiled()
Checks if the glShader is compiled.
Definition glShader.h:76
glShader(std::string path, std::string name)
Construct a glShader from source given by paths.
Definition glShader.h:40
~glShader()
Definition glShader.h:44
glShader()
Construct an empty glShader.
Definition glShader.h:30
void release()
Destroy the shader program.
Definition glShader.cpp:15
glShader(const char *v, const char *f)
Construct a glShader from a vertex and fragment source.
Definition glShader.h:23
void create()
Create the shader program.
Definition glShader.cpp:6
void use()
Use the shader program (and compile if required).
Definition glShader.cpp:119
bool isProgram()
Checks if an OpenGL program is created.
Definition glShader.h:84
void compile()
Compile the shader program.
Definition glShader.cpp:24
Generic shader program with vertex and fragment shader.
Definition shader.h:44
Definition uniform.h:22
T value
Definition uniform.h:27