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

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