conworlds
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/shader.h Sun Aug 24 09:41:24 2014 +0300 1.3 @@ -0,0 +1,150 @@ 1.4 +#ifndef SHADER_H_ 1.5 +#define SHADER_H_ 1.6 + 1.7 +#include <vector> 1.8 +#include <string> 1.9 +#include "vmath/vmath.h" 1.10 +#include "opengl.h" 1.11 +#include "dataset.h" 1.12 + 1.13 +class ShaderProg; 1.14 + 1.15 + 1.16 +void bind_shader(const ShaderProg *sdr); 1.17 +const ShaderProg *get_current_shader(); 1.18 + 1.19 + 1.20 +class Shader { 1.21 +private: 1.22 + unsigned int sdr; 1.23 + unsigned int type; 1.24 + std::string name, src; 1.25 + 1.26 +public: 1.27 + Shader(); 1.28 + ~Shader(); 1.29 + 1.30 + unsigned int get_id() const; 1.31 + 1.32 + void set_name(const char *name); 1.33 + const char *get_name() const; 1.34 + 1.35 + bool create(const char *src, unsigned int type); 1.36 + void destroy(); 1.37 + 1.38 + bool load(const char *fname, unsigned int type); 1.39 + 1.40 + 1.41 + // these functions are only meant to be used by the ShaderSet 1.42 + static Shader *create_shader(); 1.43 + static bool load_shader(Shader *sdr, const char *fname); 1.44 + static bool done_shader(Shader *sdr, unsigned int type); 1.45 + static void destroy_shader(Shader *sdr); 1.46 +}; 1.47 + 1.48 +#define VSDR(s) s, GL_VERTEX_SHADER 1.49 +#define FSDR(s) s, GL_FRAGMENT_SHADER 1.50 +#define PSDR(s) FSDR(s) 1.51 +#define GSDR(s) s, GL_GEOMETRY_SHADER 1.52 +#define TCSDR(s) s, GL_TESS_CONTROL_SHADER 1.53 +#define TESDR(s) s, GL_TESS_EVALUATION_SHADER 1.54 + 1.55 +class ShaderProg { 1.56 +private: 1.57 + unsigned int prog; 1.58 + mutable bool must_link; 1.59 + std::vector<Shader*> shaders; 1.60 + 1.61 + struct StateLocCache { int sidx, loc; }; 1.62 + /** a cache of all st_ prefixed uniform locations and their corresponding 1.63 + * index in the global uniform state vector (see unistate.h) 1.64 + */ 1.65 + mutable std::vector<StateLocCache> stloc_cache; 1.66 + 1.67 + void cache_state_uniforms() const; 1.68 + void setup_state_uniforms() const; 1.69 + 1.70 +public: 1.71 + static ShaderProg *current; 1.72 + 1.73 + ShaderProg(); 1.74 + ~ShaderProg(); 1.75 + 1.76 + /// returns the OpenGL object id for this shader program 1.77 + unsigned int get_id() const; 1.78 + 1.79 + /** takes a series of shaders, and constructs a program object by linking 1.80 + * them together. Terminate with a null pointer (don't use 0!) */ 1.81 + bool create(Shader *sdr, ...); 1.82 + /// same as above, but with a va_list instead of variable arguments. 1.83 + bool create(Shader *sdr, va_list ap); 1.84 + /** takes two shaders (vertex and pixel) and constructs a program object by 1.85 + * linking them together. Either one can be null. */ 1.86 + bool create(Shader *vsdr, Shader *psdr); 1.87 + 1.88 + /** takes a series of shader source/shader type pairs and constructs a program 1.89 + * object by linking them together. Terminate with a null pointer (don't use 0!) 1.90 + * You can use the VSDR, PSDR, GSDR, TCSDR, TESDR convenience macros for passing 1.91 + * the pairs. 1.92 + * Example: create(VSDR(vsrc0), VSDR(vsrc1), PSDR(psrc), NULL); 1.93 + */ 1.94 + bool create(const char *src, unsigned int type, ...); 1.95 + /// same as above, but with a va_list instead of variable arguments. 1.96 + bool create(const char *src, unsigned int type, va_list ap); 1.97 + /** takes two shaders source strings (vertex and pixel) and constructs 1.98 + * a program object by linking them together. Either one can be null. */ 1.99 + bool create(const char *vsrc, const char *psrc); 1.100 + 1.101 + void destroy(); 1.102 + 1.103 + /** takes a series of shader filename/shader type pairs, loads the shaders and 1.104 + * constructs a program object by linking them together. Terminate with a null 1.105 + * pointer (don't use 0!). You can use the VSDR, PSDR, GSDR, TCSDR, TESDR convenience 1.106 + * macros for passing the pairs. 1.107 + * Example: load(VSDR("vsdr1.glsl"), VSDR("vsdr2.glsl"), PSDR("pixel.glsl"), NULL); 1.108 + */ 1.109 + bool load(const char *fname, unsigned int type, ...); 1.110 + /// same as above, but with a va_list instead of variable arguments. 1.111 + bool load(const char *fname, unsigned int type, va_list ap); 1.112 + /** takes the filenames of two shader files (vertex and pixel), loads them and 1.113 + * constructs a program object by linking them together. Either one can be null */ 1.114 + bool load(const char *vsrc, const char *psrc); 1.115 + 1.116 + void add_shader(Shader *sdr); 1.117 + bool link() const; 1.118 + 1.119 + void bind() const; 1.120 + 1.121 + int get_attrib_location(const char *name) const; 1.122 + void set_attrib_location(const char *name, int loc) const; 1.123 + 1.124 + int get_uniform_location(const char *name) const; 1.125 + 1.126 + bool set_uniform(int loc, int val) const; 1.127 + bool set_uniform(int loc, float val) const; 1.128 + bool set_uniform(int loc, const Vector2 &v) const; 1.129 + bool set_uniform(int loc, const Vector3 &v) const; 1.130 + bool set_uniform(int loc, const Vector4 &v) const; 1.131 + bool set_uniform(int loc, const Matrix3x3 &m) const; 1.132 + bool set_uniform(int loc, const Matrix4x4 &m) const; 1.133 + 1.134 + bool set_uniform(const char *name, int val) const; 1.135 + bool set_uniform(const char *name, float val) const; 1.136 + bool set_uniform(const char *name, const Vector2 &v) const; 1.137 + bool set_uniform(const char *name, const Vector3 &v) const; 1.138 + bool set_uniform(const char *name, const Vector4 &v) const; 1.139 + bool set_uniform(const char *name, const Matrix3x3 &m) const; 1.140 + bool set_uniform(const char *name, const Matrix4x4 &m) const; 1.141 + 1.142 + friend void setup_unistate(const ShaderProg*); 1.143 +}; 1.144 + 1.145 +class ShaderSet : public DataSet<Shader*> { 1.146 +private: 1.147 + unsigned int type; 1.148 + 1.149 +public: 1.150 + ShaderSet(unsigned int type); 1.151 +}; 1.152 + 1.153 +#endif // SHADER_H_