conworlds

view src/shader.h @ 13:283cdfa7dda2

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