Shader.cpp 2.6 KB

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