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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs,
  59. glm::mat4 MVP, unsigned int texture, TextureStorage store,
  60. gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
  61. Shader& shader = textureShader);
  62. static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs,
  63. std::vector<unsigned short>& indices, glm::mat4 MVP,
  64. unsigned int texture, TextureStorage store,
  65. gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
  66. Shader& shader = textureShader);
  67. static void drawGLBuffer(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs,
  68. Shader& shader = textureShader);
  69. static void drawGLOnly(std::vector<unsigned short>& indices, glm::mat4 MVP,
  70. unsigned int texture, TextureStorage store,
  71. gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
  72. Shader& shader = textureShader);
  73. static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
  74. glm::mat4 MVP, gl::GLenum mode = gl::GL_TRIANGLES,
  75. ShaderTexture* target = nullptr, Shader& shader = colorShader);
  76. static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
  77. std::vector<unsigned short>& indices, glm::mat4 MVP,
  78. gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
  79. Shader& shader = colorShader);
  80. static std::string getVersion(bool linked);
  81. private:
  82. int programID;
  83. std::vector<unsigned int> uniforms;
  84. ShaderBuffer vertexBuffer, otherBuffer, indexBuffer;
  85. static void bindProperBuffer(ShaderTexture* target);
  86. static Shader textureShader;
  87. static const char* textureShaderVertex;
  88. static const char* textureShaderFragment;
  89. static Shader colorShader;
  90. static const char* colorShaderVertex;
  91. static const char* colorShaderFragment;
  92. static unsigned int vertexArrayID;
  93. static bool lastBufferWasNotFramebuffer;
  94. };
  95. #endif