28 #define CHECK_GL_ERROR(cmd) \ 30 Shader::reportGLError(#cmd, __FILE__, __LINE__) 31 #define REPORT_GL_ERROR(str) Shader::reportGLError(str, __FILE__, __LINE__) 37 typedef util::Vector<1, GLint> ivec1;
38 typedef util::Vector<2, GLint> ivec2;
39 typedef util::Vector<3, GLint> ivec3;
40 typedef util::Vector<4, GLint> ivec4;
41 typedef util::Vector<1, GLfloat> vec1;
42 typedef util::Vector<2, GLfloat> vec2;
43 typedef util::Vector<3, GLfloat> vec3;
44 typedef util::Vector<4, GLfloat> vec4;
45 typedef util::Matrix<2, 2, GLfloat> mat2;
46 typedef util::Matrix<3, 3, GLfloat> mat3;
47 typedef util::Matrix<4, 4, GLfloat> mat4;
68 virtual void setUniform(GLint location)
const = 0;
69 virtual void setAttrib(GLuint location)
const = 0;
72 virtual std::istream& read(std::istream& s) = 0;
73 virtual std::ostream& write(std::ostream& s)
const = 0;
74 virtual Value* copy() = 0;
77 template <
typename T>
class ValueImpl :
public Value {
79 typedef typename T::value_type value_type;
80 typedef void (*uniform_fct)(GLint, GLsizei,
const value_type*);
81 typedef void (*attrib_fct)(GLuint,
const value_type*);
83 ValueImpl(uniform_fct ufct, attrib_fct afct,
const T* v,
int count)
87 , glVertexAttrib(afct)
91 ValueImpl(uniform_fct ufct, attrib_fct afct)
94 , glVertexAttrib(afct)
98 ValueImpl(
const ValueImpl& copy)
101 , glUniform(copy.glUniform)
102 , glVertexAttrib(copy.glVertexAttrib)
106 virtual Value* copy() {
107 return new ValueImpl(*
this);
110 virtual void setUniform(GLint location)
const {
111 glUniform(location, (GLint)value.size(), value[0].c_data());
113 virtual void setAttrib(GLuint location)
const {
114 glVertexAttrib(location, value[0].c_data());
127 virtual std::istream& read(std::istream& s)
133 virtual std::ostream& write(std::ostream& s)
const 138 std::vector<T> value;
139 uniform_fct glUniform;
140 attrib_fct glVertexAttrib;
157 value = copy.value->copy();
160 template <
typename T>
167 template <
typename T>
168 explicit GLSLValue(
const std::vector<T>& val)
174 template <
typename T>
175 explicit GLSLValue(
const T* val,
int count)
178 setValue(val, count);
189 value = copy.value->copy();
193 void setUniform(GLint location)
const {
194 value->setUniform(location);
196 void setAttrib(GLuint location)
const {
197 value->setAttrib(location);
199 std::istream& read(std::istream& s);
200 std::ostream& write(std::ostream& s)
const;
204 void setValue(
const GLint* value,
int count);
205 void setValue(
const ivec1* value,
int count);
206 void setValue(
const ivec2* value,
int count);
207 void setValue(
const ivec3* value,
int count);
208 void setValue(
const ivec4* value,
int count);
209 void setValue(
const GLfloat* value,
int count);
210 void setValue(
const vec1* value,
int count);
211 void setValue(
const vec2* value,
int count);
212 void setValue(
const vec3* value,
int count);
213 void setValue(
const vec4* value,
int count);
214 void setValue(
const mat2* value,
int count);
215 void setValue(
const mat3* value,
int count);
216 void setValue(
const mat4* value,
int count);
218 void setValue(
const GLint& value) {
221 void setValue(
const ivec1& value) {
224 void setValue(
const ivec2& value) {
227 void setValue(
const ivec3& value) {
230 void setValue(
const ivec4& value) {
233 void setValue(
const GLfloat& value) {
236 void setValue(
const vec1& value) {
239 void setValue(
const vec2& value) {
242 void setValue(
const vec3& value) {
245 void setValue(
const vec4& value) {
248 void setValue(
const mat2& value) {
251 void setValue(
const mat3& value) {
254 void setValue(
const mat4& value) {
271 inline std::ostream& operator<<(std::ostream& s,
const GLSLValue& ut) {
275 inline std::istream& operator>>(std::istream& s,
GLSLValue& ut) {
281 enum ActiveTextures {
300 Shader(
int verbosity = 1);
302 static void activeTexture(ActiveTextures at) {
303 glActiveTexture(at + GL_TEXTURE0);
306 static void activeTexture(
int at) {
307 glActiveTexture(at + GL_TEXTURE0);
312 _initialized =
false;
319 static bool stopUsingShaders();
321 static bool reportGLError(
const char*
id,
const char* file,
int line);
322 static bool reportGLError(
const QString&
id,
const char* file,
int line);
324 bool isVertexShaderCode(
unsigned int pos)
const {
325 return vertex_shaders_code[pos].second;
328 bool isFragmentShaderCode(
unsigned int pos)
const {
329 return fragment_shaders_code[pos].second;
332 const QString& getVertexShader(
unsigned int pos)
const {
333 return vertex_shaders_code[pos].first;
336 const QString& getFragmentShader(
unsigned int pos)
const {
337 return fragment_shaders_code[pos].first;
340 void addVertexShaderCode(
const QString& code);
341 bool changeVertexShaderCode(
int pos,
const QString& code);
342 void removeVertexShaderCode(
const QString& code);
343 void addFragmentShaderCode(
const QString& code);
344 bool changeFragmentShaderCode(
int pos,
const QString& code);
345 void removeFragmentShaderCode(
const QString& code);
347 void addVertexShader(
const QString& filename);
348 bool changeVertexShader(
int pos,
const QString& filename);
349 void removeVertexShader(
const QString& filename);
350 void addFragmentShader(
const QString& filename);
351 bool changeFragmentShader(
int pos,
const QString& filename);
352 void removeFragmentShader(
const QString& filename);
353 void setVerbosity(
int verb) {
357 QString shaderTypeName(GLenum shader_type);
359 GLuint compileShaderFile(GLenum shader_type,
QString filename);
360 GLuint compileShader(GLenum shader_type,
QString content);
362 void printProgramInfoLog(GLuint
object);
363 void printShaderInfoLog(GLuint
object);
366 bool hasShaders()
const {
370 GLuint program()
const {
374 void setupUniforms();
376 GLuint attribLocation(
const QString& name);
379 void setAttrib(GLuint loc,
const GLSLValue& value);
384 bool initialized()
const {
389 void loadUniform(GLint program,
const QString& name,
const GLSLValue& value);
394 typedef std::pair<QString, bool> code_t;
395 std::vector<code_t> vertex_shaders_code, fragment_shaders_code;
397 std::vector<GLuint> vertex_shaders, fragment_shaders;
399 std::vector<QString> uniform_names, model_uniform_names;
400 std::vector<GLSLValue> uniform_values, model_uniform_values;
Defines the util::Parms class.
Definition: Shader.hpp:279
Graphical (or textual) assertion utility.
This namespace contains all the API of MorphoGraphX.
Definition: Assert.hpp:26
Definition: Shader.hpp:63
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...