18 #define CHECK_GL_ERROR(cmd) \
20 Shader::reportGLError(#cmd, __FILE__, __LINE__)
21 #define REPORT_GL_ERROR(str) Shader::reportGLError(str, __FILE__, __LINE__)
27 typedef util::Vector<1, GLint> ivec1;
28 typedef util::Vector<2, GLint> ivec2;
29 typedef util::Vector<3, GLint> ivec3;
30 typedef util::Vector<4, GLint> ivec4;
31 typedef util::Vector<1, GLfloat> vec1;
32 typedef util::Vector<2, GLfloat> vec2;
33 typedef util::Vector<3, GLfloat> vec3;
34 typedef util::Vector<4, GLfloat> vec4;
35 typedef util::Matrix<2, 2, GLfloat> mat2;
36 typedef util::Matrix<3, 3, GLfloat> mat3;
37 typedef util::Matrix<4, 4, GLfloat> mat4;
58 virtual void setUniform(GLint location)
const = 0;
59 virtual void setAttrib(GLuint location)
const = 0;
62 virtual std::istream& read(std::istream& s) = 0;
63 virtual std::ostream& write(std::ostream& s)
const = 0;
64 virtual Value* copy() = 0;
67 template <
typename T>
class ValueImpl :
public Value {
69 typedef typename T::value_type value_type;
70 typedef void (*uniform_fct)(GLint, GLsizei,
const value_type*);
71 typedef void (*attrib_fct)(GLuint,
const value_type*);
73 ValueImpl(uniform_fct ufct, attrib_fct afct,
const T* v,
int count)
77 , glVertexAttrib(afct)
81 ValueImpl(uniform_fct ufct, attrib_fct afct)
84 , glVertexAttrib(afct)
88 ValueImpl(
const ValueImpl& copy)
91 , glUniform(copy.glUniform)
92 , glVertexAttrib(copy.glVertexAttrib)
96 virtual Value* copy() {
97 return new ValueImpl(*
this);
100 virtual void setUniform(GLint location)
const {
101 glUniform(location, (GLint)value.size(), value[0].c_data());
103 virtual void setAttrib(GLuint location)
const {
104 glVertexAttrib(location, value[0].c_data());
117 virtual std::istream& read(std::istream& s)
123 virtual std::ostream& write(std::ostream& s)
const
128 std::vector<T> value;
129 uniform_fct glUniform;
130 attrib_fct glVertexAttrib;
147 value = copy.value->copy();
150 template <
typename T>
157 template <
typename T>
158 explicit GLSLValue(
const std::vector<T>& val)
164 template <
typename T>
165 explicit GLSLValue(
const T* val,
int count)
168 setValue(val, count);
179 value = copy.value->copy();
183 void setUniform(GLint location)
const {
184 value->setUniform(location);
186 void setAttrib(GLuint location)
const {
187 value->setAttrib(location);
189 std::istream& read(std::istream& s);
190 std::ostream& write(std::ostream& s)
const;
194 void setValue(
const GLint* value,
int count);
195 void setValue(
const ivec1* value,
int count);
196 void setValue(
const ivec2* value,
int count);
197 void setValue(
const ivec3* value,
int count);
198 void setValue(
const ivec4* value,
int count);
199 void setValue(
const GLfloat* value,
int count);
200 void setValue(
const vec1* value,
int count);
201 void setValue(
const vec2* value,
int count);
202 void setValue(
const vec3* value,
int count);
203 void setValue(
const vec4* value,
int count);
204 void setValue(
const mat2* value,
int count);
205 void setValue(
const mat3* value,
int count);
206 void setValue(
const mat4* value,
int count);
208 void setValue(
const GLint& value) {
211 void setValue(
const ivec1& value) {
214 void setValue(
const ivec2& value) {
217 void setValue(
const ivec3& value) {
220 void setValue(
const ivec4& value) {
223 void setValue(
const GLfloat& value) {
226 void setValue(
const vec1& value) {
229 void setValue(
const vec2& value) {
232 void setValue(
const vec3& value) {
235 void setValue(
const vec4& value) {
238 void setValue(
const mat2& value) {
241 void setValue(
const mat3& value) {
244 void setValue(
const mat4& value) {
261 inline std::ostream& operator<<(std::ostream& s,
const GLSLValue& ut) {
265 inline std::istream& operator>>(std::istream& s, GLSLValue& ut) {
271 enum ActiveTextures {
290 Shader(
int verbosity = 1);
292 static void activeTexture(ActiveTextures at) {
293 glActiveTexture(at + GL_TEXTURE0);
296 static void activeTexture(
int at) {
297 glActiveTexture(at + GL_TEXTURE0);
302 _initialized =
false;
309 static bool stopUsingShaders();
311 static bool reportGLError(
const char*
id,
const char* file,
int line);
312 static bool reportGLError(
const QString&
id,
const char* file,
int line);
314 bool isVertexShaderCode(
unsigned int pos)
const {
315 return vertex_shaders_code[pos].second;
318 bool isFragmentShaderCode(
unsigned int pos)
const {
319 return fragment_shaders_code[pos].second;
322 const QString& getVertexShader(
unsigned int pos)
const {
323 return vertex_shaders_code[pos].first;
326 const QString& getFragmentShader(
unsigned int pos)
const {
327 return fragment_shaders_code[pos].first;
330 void addVertexShaderCode(
const QString& code);
331 bool changeVertexShaderCode(
int pos,
const QString& code);
332 void removeVertexShaderCode(
const QString& code);
333 void addFragmentShaderCode(
const QString& code);
334 bool changeFragmentShaderCode(
int pos,
const QString& code);
335 void removeFragmentShaderCode(
const QString& code);
337 void addVertexShader(
const QString& filename);
338 bool changeVertexShader(
int pos,
const QString& filename);
339 void removeVertexShader(
const QString& filename);
340 void addFragmentShader(
const QString& filename);
341 bool changeFragmentShader(
int pos,
const QString& filename);
342 void removeFragmentShader(
const QString& filename);
343 void setVerbosity(
int verb) {
347 QString shaderTypeName(GLenum shader_type);
349 GLuint compileShaderFile(GLenum shader_type,
QString filename);
350 GLuint compileShader(GLenum shader_type,
QString content);
352 void printProgramInfoLog(GLuint
object);
353 void printShaderInfoLog(GLuint
object);
356 bool hasShaders()
const {
360 GLuint program()
const {
364 void setupUniforms();
369 void setAttrib(GLuint loc,
const GLSLValue& value);
374 bool initialized()
const {
379 void loadUniform(GLint program,
const QString& name,
const GLSLValue& value);
384 typedef std::pair<QString, bool> code_t;
385 std::vector<code_t> vertex_shaders_code, fragment_shaders_code;
387 std::vector<GLuint> vertex_shaders, fragment_shaders;
389 std::vector<QString> uniform_names, model_uniform_names;
390 std::vector<GLSLValue> uniform_values, model_uniform_values;
AttribBase(const QString &name) const QString & name()
Default constructor of named attribute.
Definition: Attributes.hpp:54
Defines the util::Parms class.
Definition: Shader.hpp:269
Graphical (or textual) assertion utility.
Namespace containing all the utility classes.
Definition: Vector.hpp:37
Definition: Shader.hpp:53
Class representing a fixed-size matrix.
Definition: Matrix.hpp:34
Defines the Matrix class template This file is shared by cuda, do not include headers that nvcc can't...
Defines the Vector class template This file is shared by cuda, do not include headers that nvcc can't...