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