diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/libjin/graphics/shaders/je_shader.cpp | 78 | ||||
-rw-r--r-- | src/libjin/graphics/shaders/je_shader.h | 14 |
2 files changed, 57 insertions, 35 deletions
diff --git a/src/libjin/graphics/shaders/je_shader.cpp b/src/libjin/graphics/shaders/je_shader.cpp index 6235128..0eb3631 100644 --- a/src/libjin/graphics/shaders/je_shader.cpp +++ b/src/libjin/graphics/shaders/je_shader.cpp @@ -57,8 +57,11 @@ namespace JinEngine // const int DEFAULT_TEXTURE_UNIT = 0; + GLint Shader::mTextureUnit = DEFAULT_TEXTURE_UNIT; + GLint Shader::mAttributeIndex = 0; + Shader::Shader(const string& program) - : mCurrentTextureUnit(DEFAULT_TEXTURE_UNIT) + //: mCurrentTextureUnit(DEFAULT_TEXTURE_UNIT) { if (!compile(program)) { @@ -70,7 +73,7 @@ namespace JinEngine { if (gl.getShader() == this) gl.unuseShader(); - // delete shader program + // Delete shader program. glDeleteShader(mPID); } @@ -92,6 +95,8 @@ namespace JinEngine for(; mAttributeIndex > 0; --mAttributeIndex) glDisableVertexAttribArray(mAttributeIndex); + mTextureUnit = DEFAULT_TEXTURE_UNIT; + return *this; } @@ -142,34 +147,45 @@ namespace JinEngine return maxTextureUnits; } - GLint Shader::claimTextureUnit(const std::string& name) + GLint Shader::claimTextureUnit(/*const std::string& name*/) + { + //std::map<std::string, GLint>::iterator unit = mTextureUnits.find(name); + //if (unit != mTextureUnits.end()) + // return unit->second; + //static GLint MAX_TEXTURE_UNITS = getMaxTextureUnits(); + //if (++mCurrentTextureUnit >= MAX_TEXTURE_UNITS) + // return 0; + //mTextureUnits[name] = mCurrentTextureUnit; + //return mCurrentTextureUnit; + return mTextureUnit++; + } + + GLint Shader::getUniformLocation(const char* uniform) { - std::map<std::string, GLint>::iterator unit = mTextureUnits.find(name); - if (unit != mTextureUnits.end()) - return unit->second; - static GLint MAX_TEXTURE_UNITS = getMaxTextureUnits(); - if (++mCurrentTextureUnit >= MAX_TEXTURE_UNITS) - return 0; - mTextureUnits[name] = mCurrentTextureUnit; - return mCurrentTextureUnit; + map<std::string, GLint>::iterator it = mUniformsLocation.find(uniform); + if (it != mUniformsLocation.end()) + return it->second; + GLint loc = glGetUniformLocation(mPID, uniform); + mUniformsLocation.insert(pair<std::string, GLint>(uniform, loc)); + return loc; } - #define checkJSL() \ + #define check_jsl() \ if (gl.getShader() != this) \ return *this Shader & Shader::sendInt(const char* name, int value) { - checkJSL(); - int loc = glGetUniformLocation(mPID, name); + check_jsl(); + int loc = getUniformLocation(name); glUniform1i(loc, value); return *this; } Shader& Shader::sendFloat(const char* variable, float number) { - checkJSL(); - int loc = glGetUniformLocation(mPID, variable); + check_jsl(); + int loc = getUniformLocation(variable); glUniform1f(loc, number); return *this; } @@ -190,11 +206,11 @@ namespace JinEngine // Shader& Shader::sendTexture(const char* variable, const Texture* tex) { - checkJSL(); - GLint location = glGetUniformLocation(mPID, variable); + check_jsl(); + GLint location = getUniformLocation(variable); if (location == -1) return *this; - GLint unit = claimTextureUnit(variable); + GLint unit = claimTextureUnit(/*variable*/); if (unit == 0) { // TODO: 쳣 @@ -209,11 +225,11 @@ namespace JinEngine Shader& Shader::sendCanvas(const char* variable, const Canvas* canvas) { - checkJSL(); - GLint location = glGetUniformLocation(mPID, variable); + check_jsl(); + GLint location = getUniformLocation(variable); if (location == -1) return *this; - GLint unit = claimTextureUnit(variable); + GLint unit = claimTextureUnit(/*variable*/); if (unit == 0) { // TODO: 쳣 @@ -229,32 +245,32 @@ namespace JinEngine Shader& Shader::sendVec2(const char* name, float x, float y) { - checkJSL(); - int loc = glGetUniformLocation(mPID, name); + check_jsl(); + int loc = getUniformLocation(name); glUniform2f(loc, x, y); return *this; } Shader& Shader::sendVec3(const char* name, float x, float y, float z) { - checkJSL(); - int loc = glGetUniformLocation(mPID, name); + check_jsl(); + int loc = getUniformLocation(name); glUniform3f(loc, x, y, z); return *this; } Shader& Shader::sendVec4(const char* name, float x, float y, float z, float w) { - checkJSL(); - int loc = glGetUniformLocation(mPID, name); + check_jsl(); + int loc = getUniformLocation(name); glUniform4f(loc, x, y, z, w); return *this; } Shader& Shader::sendColor(const char* name, const Color* col) { - checkJSL(); - int loc = glGetUniformLocation(mPID, name); + check_jsl(); + int loc = getUniformLocation(name); glUniform4f(loc, col->r / 255.f, col->g / 255.f, @@ -266,7 +282,7 @@ namespace JinEngine Shader& Shader::sendMatrix4(const char* name, const Math::Matrix* mat4) { - int loc = glGetUniformLocation(mPID, name); + int loc = getUniformLocation(name); glUniformMatrix4fv(loc, 1, GL_FALSE, mat4->getElements()); return *this; } diff --git a/src/libjin/graphics/shaders/je_shader.h b/src/libjin/graphics/shaders/je_shader.h index a279a79..c3ca721 100644 --- a/src/libjin/graphics/shaders/je_shader.h +++ b/src/libjin/graphics/shaders/je_shader.h @@ -169,7 +169,9 @@ namespace JinEngine /// @param name Name of the texture uniform variable. /// @return Texture unit which texture variable be assigned. /// - GLint claimTextureUnit(const std::string& name); + GLint claimTextureUnit(/*const std::string& name*/); + + GLint getUniformLocation(const char* uniforms); /// /// Compile JSL program into GLSL source. @@ -179,10 +181,14 @@ namespace JinEngine /// bool compile(const std::string& program); + static GLint mTextureUnit; + static GLint mAttributeIndex; + GLuint mPID; - GLint mCurrentTextureUnit; - std::map<std::string, GLint> mTextureUnits; - GLint mAttributeIndex; + //GLint mCurrentTextureUnit; + //std::map<std::string, GLint> mTextureUnits; + + std::map<std::string, GLint> mUniformsLocation; }; |