diff --git a/Src/Graphics/New3D/GLSLShader.cpp b/Src/Graphics/New3D/GLSLShader.cpp index 4b27e1f..98d945f 100644 --- a/Src/Graphics/New3D/GLSLShader.cpp +++ b/Src/Graphics/New3D/GLSLShader.cpp @@ -1,28 +1,41 @@ #include "GLSLShader.h" #include -GLSLShader::GLSLShader() { - - m_vShader = 0; - m_fShader = 0; - m_program = 0; - - for (auto &i : uniformLoc) { - i = 0; - } - - for (auto &i : attribLoc) { - i = 0; - } +GLSLShader::GLSLShader() +{ + Reset(); } -GLSLShader::~GLSLShader() { - +GLSLShader::~GLSLShader() +{ UnloadShaders(); } -bool GLSLShader::LoadShaders(const char *vertexShader, const char *fragmentShader) { +GLSLShader::GLSLShader(GLSLShader&& other) noexcept +{ + *this = std::move(other); +} +GLSLShader& GLSLShader::operator=(GLSLShader&& other) noexcept +{ + if (this != &other) { + m_program = other.m_program; + m_vShader = other.m_vShader; + m_fShader = other.m_fShader; + uniformLocMap = other.uniformLocMap; + attribLocMap = other.attribLocMap; + + std::copy(std::begin(other.uniformLoc), std::end(other.uniformLoc), std::begin(uniformLoc)); + std::copy(std::begin(other.attribLoc), std::end(other.attribLoc), std::begin(attribLoc)); + + other.Reset(); + } + + return *this; +} + +bool GLSLShader::LoadShaders(const char* vertexShader, const char* fragmentShader) +{ m_program = glCreateProgram(); m_vShader = glCreateShader(GL_VERTEX_SHADER); m_fShader = glCreateShader(GL_FRAGMENT_SHADER); @@ -58,52 +71,80 @@ void GLSLShader::UnloadShaders() m_program = 0; } -void GLSLShader::EnableShader() { - +void GLSLShader::EnableShader() +{ glUseProgram(m_program); } -void GLSLShader::DisableShader() { - +void GLSLShader::DisableShader() +{ glUseProgram(0); } -void GLSLShader::PrintShaderInfoLog(GLuint obj) { +void GLSLShader::Reset() +{ + m_vShader = 0; + m_fShader = 0; + m_program = 0; + for (auto& i : uniformLoc) { + i = -1; + } + + for (auto& i : attribLoc) { + i = -1; + } + + uniformLocMap.clear(); + attribLocMap.clear(); +} + +void GLSLShader::PrintShaderInfoLog(GLuint obj) +{ int infologLength = 0; int charsWritten = 0; - + glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &infologLength); if (infologLength > 0) { - char *infoLog = new char[infologLength]; + char* infoLog = new char[infologLength]; glGetShaderInfoLog(obj, infologLength, &charsWritten, infoLog); printf("%s\n", infoLog); delete[] infoLog; } } -void GLSLShader::PrintProgramInfoLog(GLuint obj) { - +void GLSLShader::PrintProgramInfoLog(GLuint obj) +{ int infologLength = 0; int charsWritten = 0; glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &infologLength); if (infologLength > 0) { - char *infoLog = new char[infologLength]; + char* infoLog = new char[infologLength]; glGetProgramInfoLog(obj, infologLength, &charsWritten, infoLog); printf("%s\n", infoLog); delete[] infoLog; } } -int GLSLShader::GetUniformLocation(const char *str) +int GLSLShader::GetUniformLocation(const char* str) { return glGetUniformLocation(m_program, str); } -int GLSLShader::GetAttributeLocation(const char *str) +void GLSLShader::GetUniformLocationMap(const char* str) +{ + uniformLocMap[str] = GetUniformLocation(str); +} + +int GLSLShader::GetAttributeLocation(const char* str) { return glGetAttribLocation(m_program, str); } + +void GLSLShader::GetAttributeLocationMap(const char* str) +{ + attribLocMap[str] = GetAttributeLocation(str); +} diff --git a/Src/Graphics/New3D/GLSLShader.h b/Src/Graphics/New3D/GLSLShader.h index 7b07a55..1d32de6 100644 --- a/Src/Graphics/New3D/GLSLShader.h +++ b/Src/Graphics/New3D/GLSLShader.h @@ -2,27 +2,52 @@ #define _GLSLSHADER_H_ #include +#include class GLSLShader { public: GLSLShader(); - ~GLSLShader(); + ~GLSLShader(); - bool LoadShaders(const char *vertexShader, const char *fragmentShader); + GLSLShader(GLSLShader&& other) noexcept; + GLSLShader(const GLSLShader&) = delete; + GLSLShader& operator=(const GLSLShader&) = delete; + GLSLShader& operator=(GLSLShader&& other) noexcept; + + bool LoadShaders(const char* vertexShader, const char* fragmentShader); void UnloadShaders(); void EnableShader(); void DisableShader(); - int GetUniformLocation(const char *str); - int GetAttributeLocation(const char *str); + int GetUniformLocation(const char* str); + void GetUniformLocationMap(const char* str); + int GetAttributeLocation(const char* str); + void GetAttributeLocationMap(const char* str); int uniformLoc[64]; int attribLoc[16]; private: + struct cmp_str + { + bool operator()(char const* a, char const* b) const + { + return std::strcmp(a, b) < 0; + } + }; + +public: + + std::map uniformLocMap; + std::map attribLocMap; + +private: + + void Reset(); + void PrintShaderInfoLog(GLuint obj); void PrintProgramInfoLog(GLuint obj); @@ -31,4 +56,5 @@ private: GLuint m_fShader; }; + #endif \ No newline at end of file