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