converted OggVorbisStream to use assman
[laserbrain_demo] / src / shader.h
1 #ifndef SHADER_H_
2 #define SHADER_H_
3
4 #include <vector>
5 #include <gmath/gmath.h>
6 #include "opengl.h"
7 #include "dataset.h"
8
9 class ShaderProg;
10
11
12 void bind_shader(const ShaderProg *sdr);
13 const ShaderProg *get_current_shader();
14
15
16 class Shader {
17 private:
18         unsigned int sdr;
19         unsigned int type;
20         char *name, *src;
21
22 public:
23         Shader();
24         ~Shader();
25
26         unsigned int get_id() const;
27         unsigned int get_type() const;
28
29         void set_name(const char *name);
30         const char *get_name() const;
31
32         void set_source(const char *src);
33         const char *get_source() const;
34
35         bool create(const char *src, unsigned int type);
36         void destroy();
37
38         bool load(const char *fname, unsigned int type);
39
40         friend bool load_shader(Shader*, const char*, unsigned int);
41 };
42
43 #define VSDR(s)         s, GL_VERTEX_SHADER
44 #define FSDR(s)         s, GL_FRAGMENT_SHADER
45 #define PSDR(s)         FSDR(s)
46 #define GSDR(s)         s, GL_GEOMETRY_SHADER
47 #define TCSDR(s)        s, GL_TESS_CONTROL_SHADER
48 #define TESDR(s)        s, GL_TESS_EVALUATION_SHADER
49
50 class ShaderProg {
51 private:
52         unsigned int prog;
53         mutable bool must_link;
54         std::vector<Shader*> shaders;
55
56         struct StateLocCache { int sidx, loc; };
57         /** a cache of all st_ prefixed uniform locations and their corresponding
58          * index in the global uniform state vector (see unistate.h)
59          */
60         mutable std::vector<StateLocCache> stloc_cache;
61
62         void cache_state_uniforms() const;
63         void setup_state_uniforms() const;
64
65 public:
66         static ShaderProg *current;
67
68         ShaderProg();
69         ~ShaderProg();
70
71         /// returns the OpenGL object id for this shader program
72         unsigned int get_id() const;
73
74         /** takes a series of shaders, and constructs a program object by linking
75          * them together. Terminate with a null pointer (don't use 0!) */
76         bool create(Shader *sdr, ...);
77         /// same as above, but with a va_list instead of variable arguments.
78         bool create(Shader *sdr, va_list ap);
79         /** takes two shaders (vertex and pixel) and constructs a program object by
80          * linking them together. Either one can be null. */
81         bool create(Shader *vsdr, Shader *psdr);
82
83         /** takes a series of shader source/shader type pairs and constructs a program
84          * object by linking them together. Terminate with a null pointer (don't use 0!)
85          * You can use the VSDR, PSDR, GSDR, TCSDR, TESDR convenience macros for passing
86          * the pairs.
87          * Example: create(VSDR(vsrc0), VSDR(vsrc1), PSDR(psrc), NULL);
88          */
89         bool create(const char *src, unsigned int type, ...);
90         /// same as above, but with a va_list instead of variable arguments.
91         bool create(const char *src, unsigned int type, va_list ap);
92         /** takes two shaders source strings (vertex and pixel) and constructs
93          * a program object by linking them together. Either one can be null. */
94         bool create(const char *vsrc, const char *psrc);
95
96         void destroy();
97
98         /** takes a series of shader filename/shader type pairs, loads the shaders and
99          * constructs a program object by linking them together. Terminate with a null
100          * pointer (don't use 0!). You can use the VSDR, PSDR, GSDR, TCSDR, TESDR convenience
101          * macros for passing the pairs.
102          * Example: load(VSDR("vsdr1.glsl"), VSDR("vsdr2.glsl"), PSDR("pixel.glsl"), NULL);
103          */
104         bool load(const char *fname, unsigned int type, ...);
105         /// same as above, but with a va_list instead of variable arguments.
106         bool load(const char *fname, unsigned int type, va_list ap);
107         /** takes the filenames of two shader files (vertex and pixel), loads them and
108          * constructs a program object by linking them together. Either one can be null */
109         bool load(const char *vsrc, const char *psrc);
110
111         void add_shader(Shader *sdr);
112         bool link() const;
113
114         void bind() const;
115
116         int get_attrib_location(const char *name) const;
117         void set_attrib_location(const char *name, int loc) const;
118
119         int get_uniform_location(const char *name) const;
120
121         bool set_uniform(int loc, int val) const;
122         bool set_uniform(int loc, float val) const;
123         bool set_uniform(int loc, const Vec2 &v) const;
124         bool set_uniform(int loc, const Vec3 &v) const;
125         bool set_uniform(int loc, const Vec4 &v) const;
126         bool set_uniform(int loc, const Mat3 &m) const;
127         bool set_uniform(int loc, const Mat4 &m) const;
128
129         bool set_uniform(const char *name, int val) const;
130         bool set_uniform(const char *name, float val) const;
131         bool set_uniform(const char *name, const Vec2 &v) const;
132         bool set_uniform(const char *name, const Vec3 &v) const;
133         bool set_uniform(const char *name, const Vec4 &v) const;
134         bool set_uniform(const char *name, const Mat3 &m) const;
135         bool set_uniform(const char *name, const Mat4 &m) const;
136
137         friend void setup_unistate(const ShaderProg*);
138 };
139
140 class ShaderSet : public DataSet<Shader*> {
141 private:
142         unsigned int type;
143
144 public:
145         ShaderSet(unsigned int type);
146 };
147
148 #endif  // SHADER_H_