goat3dgfx

view src/shader.h @ 5:18879c956eb1

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