goat3dgfx

view src/shader.h @ 20:d9c8cd19c606

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