Open Source Tomb Raider Engine
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Shader.h 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. int getAttrib(const char* name);
  52. void loadUniform(int uni, glm::vec2 vec);
  53. void loadUniform(int uni, glm::vec4 vec);
  54. void loadUniform(int uni, glm::mat4 mat);
  55. void loadUniform(int uni, int texture, TextureStorage store);
  56. static int initialize();
  57. static void shutdown();
  58. static void set2DState(bool on, bool depth = true);
  59. static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs,
  60. glm::mat4 MVP, unsigned int texture, TextureStorage store,
  61. gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
  62. Shader& shader = textureShader);
  63. static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs,
  64. std::vector<unsigned short>& indices, glm::mat4 MVP,
  65. unsigned int texture, TextureStorage store,
  66. gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
  67. Shader& shader = textureShader);
  68. static void drawGLBuffer(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs,
  69. Shader& shader = textureShader);
  70. static void drawGLOnly(std::vector<unsigned short>& indices, glm::mat4 MVP,
  71. unsigned int texture, TextureStorage store,
  72. gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
  73. Shader& shader = textureShader);
  74. static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
  75. glm::mat4 MVP, gl::GLenum mode = gl::GL_TRIANGLES,
  76. ShaderTexture* target = nullptr, Shader& shader = colorShader);
  77. static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
  78. std::vector<unsigned short>& indices, glm::mat4 MVP,
  79. gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
  80. Shader& shader = colorShader);
  81. static void drawGL(std::vector<glm::vec4>& vertices, std::vector<glm::vec3>& colors,
  82. gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
  83. Shader& shader = transformedColorShader);
  84. static void drawGL(std::vector<glm::vec4>& vertices, std::vector<glm::vec3>& colors,
  85. std::vector<unsigned short>& indices, gl::GLenum mode = gl::GL_TRIANGLES,
  86. ShaderTexture* target = nullptr, Shader& shader = transformedColorShader);
  87. static std::string getVersion(bool linked);
  88. private:
  89. int programID;
  90. std::vector<unsigned int> uniforms;
  91. ShaderBuffer vertexBuffer, otherBuffer, indexBuffer;
  92. static void bindProperBuffer(ShaderTexture* target);
  93. static Shader textureShader;
  94. static const char* textureShaderVertex;
  95. static const char* textureShaderFragment;
  96. static Shader colorShader;
  97. static const char* colorShaderVertex;
  98. static const char* colorShaderFragment;
  99. static Shader transformedColorShader;
  100. static const char* transformedColorShaderVertex;
  101. static const char* transformedColorShaderFragment;
  102. static unsigned int vertexArrayID;
  103. static bool lastBufferWasNotFramebuffer;
  104. };
  105. #endif