goat3dgfx

view src/shader.h @ 15:7d6b667821cf

wrapped everything in the goatgfx namespace
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 30 Nov 2013 20:52:21 +0200
parents 1873dfd13f2d
children 6f82b9b6d6c3
line source
1 #ifndef SHADER_H_
2 #define SHADER_H_
4 #include <vector>
5 #include "vmath/vmath.h"
6 #include "opengl.h"
7 #include "dataset.h"
9 namespace goatgfx {
11 class Shader {
12 private:
13 unsigned int sdr;
14 unsigned int type;
15 char *name;
17 public:
18 Shader();
19 ~Shader();
21 unsigned int get_id() const;
23 void set_name(const char *name);
24 const char *get_name() const;
26 bool create(const char *src, unsigned int type);
27 void destroy();
29 bool load(const char *fname, unsigned int type);
30 };
32 #define VSDR(s) s, GL_VERTEX_SHADER
33 #define FSDR(s) s, GL_FRAGMENT_SHADER
34 #define PSDR(s) FSDR(s)
35 #define GSDR(s) s, GL_GEOMETRY_SHADER
36 #define TCSDR(s) s, GL_TESS_CONTROL_SHADER
37 #define TESDR(s) s, GL_TESS_EVALUATION_SHADER
39 class ShaderProg {
40 private:
41 unsigned int prog;
42 mutable bool must_link;
43 std::vector<Shader*> shaders;
45 struct StateLocCache { int sidx, loc; };
46 /** a cache of all st_ prefixed uniform locations and their corresponding
47 * index in the global uniform state vector (see unistate.h)
48 */
49 mutable std::vector<StateLocCache> stloc_cache;
51 void cache_state_uniforms() const;
52 void setup_state_uniforms() const;
54 public:
55 static ShaderProg *current;
57 ShaderProg();
58 ~ShaderProg();
60 /// returns the OpenGL object id for this shader program
61 unsigned int get_id() const;
63 /** takes a series of shaders, and constructs a program object by linking
64 * them together. Terminate with a null pointer (don't use 0!) */
65 bool create(Shader *sdr, ...);
66 /// same as above, but with a va_list instead of variable arguments.
67 bool create(Shader *sdr, va_list ap);
68 /** takes two shaders (vertex and pixel) and constructs a program object by
69 * linking them together. Either one can be null. */
70 bool create(Shader *vsdr, Shader *psdr);
72 /** takes a series of shader source/shader type pairs and constructs a program
73 * object by linking them together. Terminate with a null pointer (don't use 0!)
74 * You can use the VSDR, PSDR, GSDR, TCSDR, TESDR convenience macros for passing
75 * the pairs.
76 * Example: create(VSDR(vsrc0), VSDR(vsrc1), PSDR(psrc), NULL);
77 */
78 bool create(const char *src, unsigned int type, ...);
79 /// same as above, but with a va_list instead of variable arguments.
80 bool create(const char *src, unsigned int type, va_list ap);
81 /** takes two shaders source strings (vertex and pixel) and constructs
82 * a program object by linking them together. Either one can be null. */
83 bool create(const char *vsrc, const char *psrc);
85 void destroy();
87 /** takes a series of shader filename/shader type pairs, loads the shaders and
88 * constructs a program object by linking them together. Terminate with a null
89 * pointer (don't use 0!). You can use the VSDR, PSDR, GSDR, TCSDR, TESDR convenience
90 * macros for passing the pairs.
91 * Example: load(VSDR("vsdr1.glsl"), VSDR("vsdr2.glsl"), PSDR("pixel.glsl"), NULL);
92 */
93 bool load(const char *fname, unsigned int type, ...);
94 /// same as above, but with a va_list instead of variable arguments.
95 bool load(const char *fname, unsigned int type, va_list ap);
96 /** takes the filenames of two shader files (vertex and pixel), loads them and
97 * constructs a program object by linking them together. Either one can be null */
98 bool load(const char *vsrc, const char *psrc);
100 void add_shader(Shader *sdr);
101 bool link() const;
103 void bind() const;
105 int get_attrib_location(const char *name) const;
106 void set_attrib_location(const char *name, int loc) const;
108 int get_uniform_location(const char *name) const;
110 bool set_uniform(int loc, int val) const;
111 bool set_uniform(int loc, float val) const;
112 bool set_uniform(int loc, const Vector2 &v) const;
113 bool set_uniform(int loc, const Vector3 &v) const;
114 bool set_uniform(int loc, const Vector4 &v) const;
115 bool set_uniform(int loc, const Matrix3x3 &m) const;
116 bool set_uniform(int loc, const Matrix4x4 &m) const;
118 bool set_uniform(const char *name, int val) const;
119 bool set_uniform(const char *name, float val) const;
120 bool set_uniform(const char *name, const Vector2 &v) const;
121 bool set_uniform(const char *name, const Vector3 &v) const;
122 bool set_uniform(const char *name, const Vector4 &v) const;
123 bool set_uniform(const char *name, const Matrix3x3 &m) const;
124 bool set_uniform(const char *name, const Matrix4x4 &m) const;
126 friend void setup_unistate(const ShaderProg*);
127 };
129 class ShaderSet : public DataSet<Shader*> {
130 private:
131 unsigned int type;
133 public:
134 ShaderSet(unsigned int type);
135 };
137 } // namespace goatgfx
139 #endif // SHADER_H_