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