Open Source Tomb Raider Engine
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/gl.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 void drawGL(std::vector<glm::vec4>& vertices, std::vector<glm::vec3>& colors,
  81. gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
  82. Shader& shader = transformedColorShader);
  83. static void drawGL(std::vector<glm::vec4>& vertices, std::vector<glm::vec3>& colors,
  84. std::vector<unsigned short>& indices, gl::GLenum mode = gl::GL_TRIANGLES,
  85. ShaderTexture* target = nullptr, Shader& shader = transformedColorShader);
  86. static std::string getVersion(bool linked);
  87. private:
  88. int programID;
  89. std::vector<unsigned int> uniforms;
  90. ShaderBuffer vertexBuffer, otherBuffer, indexBuffer;
  91. static void bindProperBuffer(ShaderTexture* target);
  92. static Shader textureShader;
  93. static const char* textureShaderVertex;
  94. static const char* textureShaderFragment;
  95. static Shader colorShader;
  96. static const char* colorShaderVertex;
  97. static const char* colorShaderFragment;
  98. static Shader transformedColorShader;
  99. static const char* transformedColorShaderVertex;
  100. static const char* transformedColorShaderFragment;
  101. static unsigned int vertexArrayID;
  102. static bool lastBufferWasNotFramebuffer;
  103. };
  104. #endif