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 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <glm/mat4x4.hpp>
  11. #include <glm/vec2.hpp>
  12. #include <glm/vec4.hpp>
  13. #include "TextureManager.h"
  14. class ShaderBuffer {
  15. public:
  16. ShaderBuffer() : buffer(0), created(false) { }
  17. ~ShaderBuffer();
  18. void bufferData(int elem, int size, void* data);
  19. template<typename T>
  20. void bufferData(std::vector<T> v)
  21. { bufferData(v.size(), sizeof(T), &v[0]); }
  22. void bindBuffer();
  23. void bindBuffer(int location, int size);
  24. void unbind(int location);
  25. private:
  26. unsigned int buffer;
  27. bool created;
  28. };
  29. class Shader {
  30. public:
  31. Shader() : programID(-1) { }
  32. ~Shader();
  33. int compile(const char* vertex, const char* fragment);
  34. void use();
  35. int addUniform(const char* name);
  36. unsigned int getUniform(int n);
  37. void addBuffer(int n = 1);
  38. unsigned int getBuffer(int n);
  39. template<typename T>
  40. void bufferData(int buff, std::vector<T> v)
  41. { bufferData(buff, v.size(), sizeof(T), &v[0]); }
  42. void bufferData(int buff, int elem, int size, void* d);
  43. void loadUniform(int uni, glm::vec2 vec);
  44. void loadUniform(int uni, glm::vec4 vec);
  45. void loadUniform(int uni, glm::mat4 mat);
  46. void loadUniform(int uni, int texture, TextureManager::TextureStorage store, int slot = 0);
  47. void bindBuffer(int buff);
  48. void bindBuffer(int buff, int location, int size);
  49. void disableAttribs();
  50. static int initialize();
  51. static void shutdown();
  52. static void drawGL(std::vector<glm::vec2>& vertices, std::vector<glm::vec2>& uvs,
  53. glm::vec4 color, unsigned int texture);
  54. static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs,
  55. std::vector<unsigned short>& indices, glm::mat4 MVP, unsigned int texture);
  56. static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
  57. std::vector<unsigned short>& indices, glm::mat4 MVP, int mode = GL_TRIANGLES);
  58. private:
  59. int programID;
  60. std::vector<unsigned int> uniforms;
  61. std::vector<unsigned int> buffers;
  62. std::vector<bool> attribs;
  63. static Shader textShader;
  64. static const char* textShaderVertex;
  65. static const char* textShaderFragment;
  66. static Shader textureShader;
  67. static const char* textureShaderVertex;
  68. static const char* textureShaderFragment;
  69. static Shader colorShader;
  70. static const char* colorShaderVertex;
  71. static const char* colorShaderFragment;
  72. static unsigned int vertexArrayID;
  73. };
  74. #endif