shader.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "shader.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <spdlog/spdlog.h>
  6. #include <glad/glad.h>
  7. #include <glm/gtc/matrix_transform.hpp>
  8. #include <glm/gtc/type_ptr.hpp>
  9. Shader::Shader(const char* vertexPath, const char* fragmentPath)
  10. {
  11. std::string vertexCode;
  12. std::string fragmentCode;
  13. std::ifstream vShaderFile;
  14. std::ifstream fShaderFile;
  15. // ensure ifstream objects can throw exceptions:
  16. vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
  17. fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
  18. try
  19. {
  20. // open files
  21. vShaderFile.open(vertexPath);
  22. fShaderFile.open(fragmentPath);
  23. std::stringstream vShaderStream, fShaderStream;
  24. // read file's buffer contents into streams
  25. vShaderStream << vShaderFile.rdbuf();
  26. fShaderStream << fShaderFile.rdbuf();
  27. // close file handlers
  28. vShaderFile.close();
  29. fShaderFile.close();
  30. // convert stream into string
  31. vertexCode = vShaderStream.str();
  32. fragmentCode = fShaderStream.str();
  33. }
  34. catch (std::ifstream::failure e)
  35. {
  36. spdlog::error("SHADER::FILE_NOT_SUCCESFULLY_READ");
  37. return;
  38. }
  39. const char* vShaderCode = vertexCode.c_str();
  40. const char* fShaderCode = fragmentCode.c_str();
  41. unsigned int vertex, fragment;
  42. int success;
  43. char infoLog[512];
  44. // vertex Shader
  45. vertex = glCreateShader(GL_VERTEX_SHADER);
  46. glShaderSource(vertex, 1, &vShaderCode, NULL);
  47. glCompileShader(vertex);
  48. // print compile errors if any
  49. glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
  50. if (!success)
  51. {
  52. glGetShaderInfoLog(vertex, 512, NULL, infoLog);
  53. spdlog::error("SHADER::VERTEX::COMPILATION_FAILED {}", infoLog);
  54. return;
  55. };
  56. // similiar for Fragment Shader
  57. fragment = glCreateShader(GL_FRAGMENT_SHADER);
  58. glShaderSource(fragment, 1, &fShaderCode, NULL);
  59. glCompileShader(fragment);
  60. // print compile errors if any
  61. glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
  62. if (!success)
  63. {
  64. glGetShaderInfoLog(vertex, 512, NULL, infoLog);
  65. spdlog::error("SHADER::FRAGMENT::COMPILATION_FAILED {}", infoLog);
  66. return;
  67. };
  68. // shader Program
  69. ID = glCreateProgram();
  70. glAttachShader(ID, vertex);
  71. glAttachShader(ID, fragment);
  72. glLinkProgram(ID);
  73. // print linking errors if any
  74. glGetProgramiv(ID, GL_LINK_STATUS, &success);
  75. if (!success)
  76. {
  77. glGetProgramInfoLog(ID, 512, NULL, infoLog);
  78. spdlog::error("SHADER::PROGRAM::LINKING_FAILED: {}", infoLog);
  79. return;
  80. }
  81. // delete the shaders as they're linked into our program now and no longer necessary
  82. glDeleteShader(vertex);
  83. glDeleteShader(fragment);
  84. compiled = true;
  85. }
  86. void Shader::use()
  87. {
  88. if (!compiled) {
  89. spdlog::error("Shader not compiler. Exiting");
  90. return;
  91. }
  92. glUseProgram(ID);
  93. }
  94. void Shader::set4f(const std::string& uniform, float val1, float val2, float val3, float val4) {
  95. glUniform4f(glGetUniformLocation(ID, uniform.c_str()), val1, val2, val3, val4);
  96. }
  97. void Shader::setFloat(const char* name, float value, bool useShader)
  98. {
  99. if (useShader)
  100. this->use();
  101. glUniform1f(glGetUniformLocation(this->ID, name), value);
  102. }
  103. void Shader::setInteger(const char* name, int value, bool useShader)
  104. {
  105. if (useShader)
  106. this->use();
  107. glUniform1i(glGetUniformLocation(this->ID, name), value);
  108. }
  109. void Shader::setVector2f(const char* name, float x, float y, bool useShader)
  110. {
  111. if (useShader)
  112. this->use();
  113. glUniform2f(glGetUniformLocation(this->ID, name), x, y);
  114. }
  115. void Shader::setVector2f(const char* name, const glm::vec2& value, bool useShader)
  116. {
  117. if (useShader)
  118. this->use();
  119. glUniform2f(glGetUniformLocation(this->ID, name), value.x, value.y);
  120. }
  121. void Shader::setVector3f(const char* name, float x, float y, float z, bool useShader)
  122. {
  123. if (useShader)
  124. this->use();
  125. glUniform3f(glGetUniformLocation(this->ID, name), x, y, z);
  126. }
  127. void Shader::setVector3f(const char* name, const glm::vec3& value, bool useShader)
  128. {
  129. if (useShader)
  130. this->use();
  131. glUniform3f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z);
  132. }
  133. void Shader::setVector4f(const char* name, float x, float y, float z, float w, bool useShader)
  134. {
  135. if (useShader)
  136. this->use();
  137. glUniform4f(glGetUniformLocation(this->ID, name), x, y, z, w);
  138. }
  139. void Shader::setVector4f(const char* name, const glm::vec4& value, bool useShader)
  140. {
  141. if (useShader)
  142. this->use();
  143. glUniform4f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z, value.w);
  144. }
  145. void Shader::setMatrix4(const char* name, const glm::mat4& matrix, bool useShader)
  146. {
  147. if (useShader)
  148. this->use();
  149. glUniformMatrix4fv(glGetUniformLocation(this->ID, name), 1, false, glm::value_ptr(matrix));
  150. }