Open Source Tomb Raider Engine
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Shader.h 3.3KB

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