Shader.cpp 2.6 KB

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