diff options
author | chai <chaifix@163.com> | 2019-01-02 00:11:22 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-01-02 00:11:22 +0800 |
commit | 3222d2665183cecc1b1d47e7b7472da698406ddb (patch) | |
tree | c6fe68272af359270411d9d84afff94ed314c4bf | |
parent | 435a1f782415b50c187bac8dea01ffbcd6856034 (diff) |
*misc
-rw-r--r-- | bin/jin.exe | bin | 572928 -> 572416 bytes | |||
-rw-r--r-- | samples/post-processing/main.lua | 2 | ||||
-rw-r--r-- | src/libjin/graphics/shaders/je_shader.cpp | 78 | ||||
-rw-r--r-- | src/libjin/graphics/shaders/je_shader.h | 14 |
4 files changed, 58 insertions, 36 deletions
diff --git a/bin/jin.exe b/bin/jin.exe Binary files differindex ece88fa..dd2f048 100644 --- a/bin/jin.exe +++ b/bin/jin.exe diff --git a/samples/post-processing/main.lua b/samples/post-processing/main.lua index 8577432..bc464c5 100644 --- a/samples/post-processing/main.lua +++ b/samples/post-processing/main.lua @@ -126,7 +126,7 @@ function jin.core.onLoad() #FRAGMENT_SHADER Color frag(Color col, Texture tex, Vertex v) { - col.a *= v.uv.x; + col.a *= sin(jin_Time.x); return col; } #END_FRAGMENT_SHADER 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; }; |