Shader.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <fstream>
  2. #include "rendering/Shader.h"
  3. #include "utils/Logger.h"
  4. #include "wrapper/GL.h"
  5. Shader::Shader() : vertex(0), geometry(0), fragment(0), program(0) {
  6. }
  7. Shader::~Shader() {
  8. GL::deleteShader(vertex);
  9. GL::deleteShader(geometry);
  10. GL::deleteShader(fragment);
  11. GL::deleteProgram(program);
  12. }
  13. Error Shader::compile(const char* vertexPath, const char* geometryPath,
  14. const char* fragmentPath) {
  15. if(vertexPath != nullptr) {
  16. Error error = compile(vertexPath, vertex, GL::VERTEX_SHADER);
  17. if(error.has()) {
  18. return error;
  19. }
  20. }
  21. if(geometryPath != nullptr) {
  22. Error error = compile(geometryPath, geometry, GL::GEOMETRY_SHADER);
  23. if(error.has()) {
  24. return error;
  25. }
  26. }
  27. if(fragmentPath != nullptr) {
  28. Error error = compile(fragmentPath, fragment, GL::FRAGMENT_SHADER);
  29. if(error.has()) {
  30. return error;
  31. }
  32. }
  33. program = GL::createProgram();
  34. if(vertexPath != nullptr) {
  35. GL::attachShader(program, vertex);
  36. }
  37. if(geometryPath != nullptr) {
  38. GL::attachShader(program, geometry);
  39. }
  40. if(fragmentPath != nullptr) {
  41. GL::attachShader(program, fragment);
  42. }
  43. GL::linkProgram(program);
  44. Error error = GL::getError("cannot link");
  45. if(error.has()) {
  46. return error;
  47. }
  48. return GL::getLinkerError(program);
  49. }
  50. Error Shader::compile(const char* path, GL::Shader& s, GL::ShaderType st) {
  51. List<char> code;
  52. Error error = readFile(code, path);
  53. if(error.has()) {
  54. return error;
  55. }
  56. return compile(s, code, st);
  57. }
  58. Error Shader::readFile(List<char>& code, const char* path) const {
  59. std::ifstream in;
  60. in.open(path);
  61. if(!in.good()) {
  62. return {"cannot read file"};
  63. }
  64. while(true) {
  65. int c = in.get();
  66. if(c == EOF) {
  67. break;
  68. }
  69. code.add(c);
  70. }
  71. code.add('\0');
  72. return {};
  73. }
  74. Error Shader::compile(GL::Shader& s, const List<char>& code,
  75. GL::ShaderType st) {
  76. s = GL::createShader(st);
  77. GL::compileShader(s, code.begin());
  78. Error error = GL::getError("compile error");
  79. if(error.has()) {
  80. return error;
  81. }
  82. return GL::getCompileError(s);
  83. }
  84. void Shader::use() const {
  85. GL::useProgram(program);
  86. }
  87. void Shader::setMatrix(const char* name, const float* data) {
  88. GL::setMatrix(program, name, data);
  89. }
  90. void Shader::setInt(const char* name, int data) {
  91. GL::setInt(program, name, data);
  92. }
  93. void Shader::setFloat(const char* name, float data) {
  94. GL::setFloat(program, name, data);
  95. }
  96. void Shader::setVector(const char* name, const Vector2& v) {
  97. GL::set2Float(program, name, &(v[0]));
  98. }
  99. void Shader::setVector(const char* name, const Vector3& v) {
  100. GL::set3Float(program, name, &(v[0]));
  101. }
  102. void Shader::setVector(const char* name, const Vector4& v) {
  103. GL::set4Float(program, name, &(v[0]));
  104. }