OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
Shader.h
Go to the documentation of this file.
1 
8 #ifndef _SHADER_H_
9 #define _SHADER_H_
10 
11 #include <vector>
12 
13 #include "TextureManager.h"
14 
15 class ShaderBuffer {
16  public:
17  ShaderBuffer() : created(false), buffer(0), boundSize(0) { }
18  ~ShaderBuffer();
19 
20  void bufferData(int elem, int size, void* data);
21 
22  template<typename T>
23  void bufferData(std::vector<T> v)
24  { bufferData(v.size(), sizeof(T), &v[0]); }
25 
26  void bindBuffer();
27  void bindBuffer(int location, int size);
28  void unbind(int location);
29 
30  unsigned int getBuffer() { assert(created); return buffer; }
31  int getSize() { return boundSize; }
32 
33  private:
34  bool created;
35  unsigned int buffer;
36  int boundSize;
37 };
38 
40  public:
41  ShaderTexture(int w = 512, int h = 512);
43 
44  void clear();
45  void bind();
46  int getTexture() { return texture; }
47 
48  private:
49  int width, height;
50  unsigned int framebuffer, depth;
51  int texture;
52 };
53 
54 class Shader {
55  public:
56  Shader() : programID(-1) { }
57  ~Shader();
58 
59  int compile(const char* vertex, const char* fragment);
60  void use();
61 
62  int addUniform(const char* name);
63  unsigned int getUniform(int n);
64 
65  void loadUniform(int uni, glm::vec2 vec);
66  void loadUniform(int uni, glm::vec4 vec);
67  void loadUniform(int uni, glm::mat4 mat);
68  void loadUniform(int uni, int texture, TextureStorage store);
69 
70  static int initialize();
71  static void shutdown();
72 
73  static void set2DState(bool on, bool depth = true);
74 
75  static void drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, glm::vec4 color, unsigned int texture,
76  TextureStorage store = TextureStorage::SYSTEM, unsigned int mode = GL_TRIANGLES,
77  ShaderTexture* target = nullptr, Shader& shader = textShader);
78 
79  static void drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, unsigned int texture, glm::mat4 MVP,
80  TextureStorage store = TextureStorage::GAME, ShaderTexture* target = nullptr,
81  Shader& shader = textureShader);
82  static void drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, ShaderBuffer& indices,
83  unsigned int texture, glm::mat4 MVP,
84  TextureStorage store = TextureStorage::GAME, ShaderTexture* target = nullptr,
85  Shader& shader = textureShader);
86 
87  static void drawGL(ShaderBuffer& vertices, ShaderBuffer& colors, glm::mat4 MVP,
88  unsigned int mode = GL_TRIANGLES, ShaderTexture* target = nullptr,
89  Shader& shader = colorShader);
90  static void drawGL(ShaderBuffer& vertices, ShaderBuffer& colors, ShaderBuffer& indices,
91  glm::mat4 MVP, unsigned int mode = GL_TRIANGLES, ShaderTexture* target = nullptr,
92  Shader& shader = colorShader);
93 
94  private:
95  int programID;
96  std::vector<unsigned int> uniforms;
97 
99  static const char* textShaderVertex;
100  static const char* textShaderFragment;
101 
103  static const char* textureShaderVertex;
104  static const char* textureShaderFragment;
105 
107  static const char* colorShaderVertex;
108  static const char* colorShaderFragment;
109 
110  static unsigned int vertexArrayID;
111 };
112 
113 #endif
114 
ShaderTexture(int w=512, int h=512)
Definition: Shader.cpp:56
static const char * textureShaderFragment
Definition: Shader.h:104
void bufferData(int elem, int size, void *data)
Definition: Shader.cpp:18
static Shader textShader
Definition: Shader.h:98
Shader()
Definition: Shader.h:56
int width
Definition: Shader.h:49
unsigned int buffer
Definition: Shader.h:35
static const char * textShaderVertex
Definition: Shader.h:99
int height
Definition: Shader.h:49
int getSize()
Definition: Shader.h:31
static void shutdown()
Definition: Shader.cpp:262
void bindBuffer()
Definition: Shader.cpp:29
void loadUniform(int uni, glm::vec2 vec)
Definition: Shader.cpp:118
void bufferData(std::vector< T > v)
Definition: Shader.h:23
int compile(const char *vertex, const char *fragment)
Definition: Shader.cpp:139
int texture
Definition: Shader.h:51
int programID
Definition: Shader.h:95
~Shader()
Definition: Shader.cpp:96
unsigned int depth
Definition: Shader.h:50
static int initialize()
Definition: Shader.cpp:218
static const char * colorShaderFragment
Definition: Shader.h:108
bool created
Definition: Shader.h:34
#define assert(x)
Definition: global.h:124
static Shader colorShader
Definition: Shader.h:106
static const char * textureShaderVertex
Definition: Shader.h:103
static void drawGL(ShaderBuffer &vertices, ShaderBuffer &uvs, glm::vec4 color, unsigned int texture, TextureStorage store=TextureStorage::SYSTEM, unsigned int mode=GL_TRIANGLES, ShaderTexture *target=nullptr, Shader &shader=textShader)
Definition: Shader.cpp:278
static const char * textShaderFragment
Definition: Shader.h:100
unsigned int getBuffer()
Definition: Shader.h:30
static Shader textureShader
Definition: Shader.h:102
static const char * colorShaderVertex
Definition: Shader.h:107
TextureStorage
void clear()
Definition: Shader.cpp:84
unsigned int framebuffer
Definition: Shader.h:50
Definition: Shader.h:54
ShaderBuffer()
Definition: Shader.h:17
int addUniform(const char *name)
Definition: Shader.cpp:101
void use()
Definition: Shader.cpp:134
Texture Registry.
~ShaderTexture()
Definition: Shader.cpp:77
static unsigned int vertexArrayID
Definition: Shader.h:110
int getTexture()
Definition: Shader.h:46
~ShaderBuffer()
Definition: Shader.cpp:13
int boundSize
Definition: Shader.h:36
void bind()
Definition: Shader.cpp:89
static void set2DState(bool on, bool depth=true)
Definition: Shader.cpp:266
std::vector< unsigned int > uniforms
Definition: Shader.h:96
void unbind(int location)
Definition: Shader.cpp:49
unsigned int getUniform(int n)
Definition: Shader.cpp:112