Open Source Tomb Raider Engine
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Shader.h 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/gl33.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 drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
  68. glm::mat4 MVP, gl::GLenum mode = gl::GL_TRIANGLES,
  69. ShaderTexture* target = nullptr, Shader& shader = colorShader);
  70. static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
  71. std::vector<unsigned short>& indices, glm::mat4 MVP,
  72. gl::GLenum mode = gl::GL_TRIANGLES, ShaderTexture* target = nullptr,
  73. Shader& shader = colorShader);
  74. static std::string getVersion(bool linked);
  75. private:
  76. int programID;
  77. std::vector<unsigned int> uniforms;
  78. ShaderBuffer vertexBuffer, otherBuffer, indexBuffer;
  79. static void bindProperBuffer(ShaderTexture* target);
  80. static Shader textureShader;
  81. static const char* textureShaderVertex;
  82. static const char* textureShaderFragment;
  83. static Shader colorShader;
  84. static const char* colorShaderVertex;
  85. static const char* colorShaderFragment;
  86. static unsigned int vertexArrayID;
  87. static bool lastBufferWasNotFramebuffer;
  88. };
  89. #endif