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