Platformer in OpenGL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.3 KiB

5 years ago
  1. #pragma once
  2. #include <string>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <iostream>
  6. #include <glm.hpp>
  7. #include "opengl.h"
  8. class Shader {
  9. private:
  10. // Members
  11. GLuint m_programID;
  12. // Checks if compilation or linking failed and if so, print the error logs
  13. int checkCompileErrors(GLuint object, std::string type);
  14. public:
  15. Shader()
  16. {
  17. m_programID = 0;
  18. }
  19. ~Shader()
  20. {
  21. if (0 != m_programID)
  22. glDeleteShader(m_programID);
  23. }
  24. // Inits with two file paths
  25. // TODO: Remove this once a resource manager is done
  26. int initShader(const std::string vertexPath, const std::string fragmentPath, const std::string geometryPath = std::string());
  27. // Compiles the shader from given source code
  28. // Note: geometry source code is optional
  29. int compile(const char *vertexSource, const char *fragmentSource, const char *geometrySource = nullptr);
  30. // Return the program ID
  31. inline GLuint ID()
  32. {
  33. return m_programID;
  34. }
  35. // sets the current shader as active
  36. inline void use()
  37. {
  38. glUseProgram(this->ID());
  39. }
  40. // Utility functions
  41. /*inline void setFloat(const GLchar *name, GLfloat value)
  42. {
  43. glUniform1f(glGetUniformLocation(this->ID(), name), value);
  44. }
  45. inline void setInt(const GLchar *name, GLint value)
  46. {
  47. glUniform1i(glGetUniformLocation(this->ID(), name), value);
  48. }
  49. inline void setVector2f(const GLchar *name, GLfloat x, GLfloat y)
  50. {
  51. glUniform2f(glGetUniformLocation(this->ID(), name), x, y);
  52. }
  53. inline void setVector2f(const GLchar *name, const glm::vec2 &value)
  54. {
  55. glUniform2f(glGetUniformLocation(this->ID(), name), value.x, value.y);
  56. }
  57. inline void setVector3f(const GLchar *name, GLfloat x, GLfloat y, GLfloat z)
  58. {
  59. glUniform3f(glGetUniformLocation(this->ID(), name), x, y, z);
  60. }
  61. inline void setVector3f(const GLchar *name, const glm::vec3 &value)
  62. {
  63. glUniform3f(glGetUniformLocation(this->ID(), name), value.x, value.y, value.z);
  64. }
  65. inline void setVector4f(const GLchar *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
  66. {
  67. glUniform4f(glGetUniformLocation(this->ID(), name), x, y, z, w);
  68. }
  69. inline void setVector4f(const GLchar *name, const glm::vec4 &value)
  70. {
  71. glUniform4f(glGetUniformLocation(this->ID(), name), value.x, value.y, value.z, value.w);
  72. }
  73. inline void setMatrix4(const GLchar *name, const glm::mat4 &matrix)
  74. {
  75. glUniformMatrix4fv(glGetUniformLocation(this->ID(), name), 1, GL_FALSE, glm::value_ptr(matrix));
  76. }*/
  77. };