Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Shader.h 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*!
  2. * \file include/system/Shader.h
  3. * \brief OpenGL Shader Implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #ifndef _SHADER_H_
  8. #define _SHADER_H_
  9. #include <string>
  10. #include <vector>
  11. #include "TextureManager.h"
  12. class ShaderBuffer {
  13. public:
  14. ShaderBuffer() : created(false), buffer(0), boundSize(0) { }
  15. ~ShaderBuffer();
  16. void bufferData(int elem, int size, void* data);
  17. template<typename T>
  18. void bufferData(std::vector<T> v)
  19. { bufferData(v.size(), sizeof(T), &v[0]); }
  20. void bindBuffer();
  21. void bindBuffer(int location, int size);
  22. void unbind(int location);
  23. unsigned int getBuffer() { assert(created); return buffer; }
  24. int getSize() { return boundSize; }
  25. private:
  26. bool created;
  27. unsigned int buffer;
  28. int boundSize;
  29. };
  30. class ShaderTexture {
  31. public:
  32. ShaderTexture(int w = 512, int h = 512);
  33. ~ShaderTexture();
  34. void clear();
  35. void bind();
  36. int getTexture() { return texture; }
  37. private:
  38. int width, height;
  39. unsigned int framebuffer, depth;
  40. int texture;
  41. };
  42. class Shader {
  43. public:
  44. Shader() : programID(-1) { }
  45. ~Shader();
  46. int compile(const char* vertex, const char* fragment);
  47. void use();
  48. int addUniform(const char* name);
  49. unsigned int getUniform(int n);
  50. void loadUniform(int uni, glm::vec2 vec);
  51. void loadUniform(int uni, glm::vec4 vec);
  52. void loadUniform(int uni, glm::mat4 mat);
  53. void loadUniform(int uni, int texture, TextureStorage store);
  54. static int initialize();
  55. static void shutdown();
  56. static void set2DState(bool on, bool depth = true);
  57. static void drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, glm::vec4 color, unsigned int texture,
  58. TextureStorage store = TextureStorage::SYSTEM, GLenum = GL_TRIANGLES,
  59. ShaderTexture* target = nullptr, Shader& shader = textShader);
  60. static void drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, unsigned int texture, glm::mat4 MVP,
  61. TextureStorage store = TextureStorage::GAME, ShaderTexture* target = nullptr,
  62. Shader& shader = textureShader);
  63. static void drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, ShaderBuffer& indices,
  64. unsigned int texture, glm::mat4 MVP,
  65. TextureStorage store = TextureStorage::GAME, ShaderTexture* target = nullptr,
  66. Shader& shader = textureShader);
  67. static void drawGL(ShaderBuffer& vertices, ShaderBuffer& colors, glm::mat4 MVP,
  68. GLenum mode = GL_TRIANGLES, ShaderTexture* target = nullptr,
  69. Shader& shader = colorShader);
  70. static void drawGL(ShaderBuffer& vertices, ShaderBuffer& colors, ShaderBuffer& indices,
  71. glm::mat4 MVP, GLenum mode = GL_TRIANGLES, ShaderTexture* target = nullptr,
  72. Shader& shader = colorShader);
  73. static std::string getVersion(bool linked);
  74. private:
  75. int programID;
  76. std::vector<unsigned int> uniforms;
  77. static Shader textShader;
  78. static const char* textShaderVertex;
  79. static const char* textShaderFragment;
  80. static Shader textureShader;
  81. static const char* textureShaderVertex;
  82. static const char* textureShaderFragment;
  83. static Shader colorShader;
  84. static const char* colorShaderVertex;
  85. static const char* colorShaderFragment;
  86. static unsigned int vertexArrayID;
  87. };
  88. #endif