Shader.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <fstream>
  2. #include "rendering/Shader.h"
  3. #include "utils/Logger.h"
  4. Shader::Shader() : shaders(0), program(0) {
  5. }
  6. Shader::~Shader() {
  7. for(GL::Shader shader : shaders) {
  8. GL::deleteShader(shader);
  9. }
  10. GL::deleteProgram(program);
  11. }
  12. static bool endsWith(const char* path, int length, const char* ending) {
  13. int endingLength = strlen(ending);
  14. if(length < endingLength) {
  15. return false;
  16. }
  17. return strcmp(path + (length - endingLength), ending) == 0;
  18. }
  19. GL::ShaderType Shader::getShaderType(const char* path) const {
  20. int length = strlen(path);
  21. if(endsWith(path, length, ".vs")) {
  22. return GL::VERTEX_SHADER;
  23. } else if(endsWith(path, length, ".fs")) {
  24. return GL::FRAGMENT_SHADER;
  25. } else if(endsWith(path, length, ".gs")) {
  26. return GL::GEOMETRY_SHADER;
  27. } else if(endsWith(path, length, ".tcs")) {
  28. return GL::TESSELATION_CONTROL_SHADER;
  29. } else if(endsWith(path, length, ".tes")) {
  30. return GL::TESSELATION_EVALUATION_SHADER;
  31. }
  32. return GL::NO_SHADER;
  33. }
  34. Error Shader::readAndCompile(const char* path, GL::Shader& s,
  35. GL::ShaderType st) {
  36. List<char> code;
  37. Error error = readFile(code, path);
  38. if(error.has()) {
  39. return error;
  40. }
  41. return compileType(s, code, st);
  42. }
  43. Error Shader::readFile(List<char>& code, const char* path) const {
  44. std::ifstream in;
  45. in.open(path);
  46. if(!in.good()) {
  47. return {"cannot read file"};
  48. }
  49. while(true) {
  50. int c = in.get();
  51. if(c == EOF) {
  52. break;
  53. }
  54. code.add(c);
  55. }
  56. code.add('\0');
  57. return {};
  58. }
  59. Error Shader::compileType(GL::Shader& s, const List<char>& code,
  60. GL::ShaderType st) {
  61. s = GL::createShader(st);
  62. GL::compileShader(s, code.begin());
  63. Error error = GL::getError("compile error");
  64. if(error.has()) {
  65. return error;
  66. }
  67. return GL::getCompileError(s);
  68. }
  69. void Shader::use() const {
  70. GL::useProgram(program);
  71. }
  72. void Shader::setMatrix(const char* name, const float* data) {
  73. GL::setMatrix(program, name, data);
  74. }
  75. void Shader::setMatrix(const char* name, const Matrix& m) {
  76. setMatrix(name, m.getValues());
  77. }
  78. void Shader::setInt(const char* name, int data) {
  79. GL::setInt(program, name, data);
  80. }
  81. void Shader::setFloat(const char* name, float data) {
  82. GL::setFloat(program, name, data);
  83. }
  84. void Shader::setVector(const char* name, const Vector2& v) {
  85. GL::set2Float(program, name, &(v[0]));
  86. }
  87. void Shader::setVector(const char* name, const Vector3& v) {
  88. GL::set3Float(program, name, &(v[0]));
  89. }
  90. void Shader::setVector(const char* name, const Vector4& v) {
  91. GL::set4Float(program, name, &(v[0]));
  92. }