Open Source Tomb Raider Engine
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Shader.h 3.3KB

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