Shader.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <fstream>
  2. #include <iostream>
  3. #include "wrapper/Shader.h"
  4. #include "wrapper/GL.h"
  5. Shader::Shader(const char* vertexPath, const char* fragmentPath) : vertexShader(0), fragmentShader(0), program(0) {
  6. if(readFileAndCompile(vertexPath, vertexShader, GL_VERTEX_SHADER) ||
  7. readFileAndCompile(fragmentPath, fragmentShader, GL_FRAGMENT_SHADER)) {
  8. return;
  9. }
  10. program = glCreateProgram();
  11. glAttachShader(program, vertexShader);
  12. glAttachShader(program, fragmentShader);
  13. glLinkProgram(program);
  14. if(GL::checkAndPrintError("cannot link")) {
  15. return;
  16. }
  17. GLint linked;
  18. glGetProgramiv(program, GL_LINK_STATUS, &linked);
  19. if(!linked) {
  20. ErrorLog log;
  21. glGetProgramInfoLog(program, log.getLength(), nullptr, log.begin());
  22. std::cout << "linker log: " << log.begin() << "\n";
  23. return;
  24. }
  25. }
  26. Shader::~Shader() {
  27. glDeleteShader(vertexShader);
  28. glDeleteShader(fragmentShader);
  29. glDeleteProgram(program);
  30. }
  31. bool Shader::readFileAndCompile(const char* path, GLuint& shader, GLenum shaderType) {
  32. std::cout << "shader: " << path << '\n';
  33. Code code;
  34. if(readFile(code, path)) {
  35. return true;
  36. }
  37. return compile(shader, code, shaderType);
  38. }
  39. bool Shader::readFile(Code& code, const char* path) const {
  40. std::ifstream in;
  41. in.open(path);
  42. if(!in.good()) {
  43. std::cout << "cannot read file\n";
  44. return true;
  45. }
  46. in.get(code.begin(), code.getLength(), EOF);
  47. return false;
  48. }
  49. bool Shader::hasError() const {
  50. return vertexShader == 0 || fragmentShader == 0 || program == 0;
  51. }
  52. bool Shader::compile(GLuint& shader, const Code& code, GLenum shaderType) {
  53. shader = glCreateShader(shaderType);
  54. const GLchar* buffer = code.begin();
  55. glShaderSource(shader, 1, &buffer, nullptr);
  56. glCompileShader(shader);
  57. if(GL::checkAndPrintError("compile error")) {
  58. return true;
  59. }
  60. GLint compiled;
  61. glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
  62. if(!compiled) {
  63. ErrorLog log;
  64. glGetShaderInfoLog(shader, log.getLength(), nullptr, log.begin());
  65. std::cout << "compiler log: " << log.begin() << "\n";
  66. return true;
  67. }
  68. return false;
  69. }
  70. void Shader::use() const {
  71. glUseProgram(program);
  72. }
  73. void Shader::setMatrix(const GLchar* name, const GLfloat* data) {
  74. glUniformMatrix4fv(glGetUniformLocation(program, name), 1, GL_TRUE, data);
  75. }
  76. void Shader::setInt(const GLchar* name, GLint data) {
  77. glUniform1i(glGetUniformLocation(program, name), data);
  78. }
  79. void Shader::setFloat(const GLchar* name, GLfloat data) {
  80. glUniform1f(glGetUniformLocation(program, name), data);
  81. }