diff options
author | chai <chaifix@163.com> | 2018-05-18 08:27:45 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-05-18 08:27:45 +0800 |
commit | 91a592da979827b1735901388dba8712e6e3ecf3 (patch) | |
tree | 5b34330fc7ab0027557f4a4dfb1417fbe9acfb44 | |
parent | d66b07724d71321545e80a8e12736be0e9a5d24a (diff) |
修改JSL
-rw-r--r-- | bin/jin.exe | bin | 1391616 -> 1391616 bytes | |||
-rw-r--r-- | src/3rdparty/luax/luax.h | 1 | ||||
-rw-r--r-- | src/libjin/render/canvas.h | 2 | ||||
-rw-r--r-- | src/libjin/render/drawable.cpp | 10 | ||||
-rw-r--r-- | src/libjin/render/drawable.h | 22 | ||||
-rw-r--r-- | src/libjin/render/jsl.cpp | 84 | ||||
-rw-r--r-- | src/libjin/render/jsl.h | 13 | ||||
-rw-r--r-- | src/script/graphics/luaopen_JSL.cpp | 65 |
8 files changed, 159 insertions, 38 deletions
diff --git a/bin/jin.exe b/bin/jin.exe Binary files differindex 66fe30b..92cc968 100644 --- a/bin/jin.exe +++ b/bin/jin.exe diff --git a/src/3rdparty/luax/luax.h b/src/3rdparty/luax/luax.h index eeb0ea4..3816430 100644 --- a/src/3rdparty/luax/luax.h +++ b/src/3rdparty/luax/luax.h @@ -55,6 +55,7 @@ */ #define luax_checktype luaL_checkudata #define luax_checknumber luaL_checknumber +#define luax_checkinteger luaL_checkinteger #define luax_checkstring luaL_checkstring /** diff --git a/src/libjin/render/canvas.h b/src/libjin/render/canvas.h index 4600f8b..6f81ed6 100644 --- a/src/libjin/render/canvas.h +++ b/src/libjin/render/canvas.h @@ -28,6 +28,6 @@ namespace render static GLint cur; }; } -}// jin +} #endif diff --git a/src/libjin/render/drawable.cpp b/src/libjin/render/drawable.cpp index 7acd3bf..1a4cc43 100644 --- a/src/libjin/render/drawable.cpp +++ b/src/libjin/render/drawable.cpp @@ -47,16 +47,6 @@ namespace render ancy = y; } - int Drawable::getWidth() - { - return width; - } - - int Drawable::getHeight() - { - return height; - } - void Drawable::draw(int x, int y, float sx, float sy, float r) { // Must set textCoord and vertCoord before renderring diff --git a/src/libjin/render/drawable.h b/src/libjin/render/drawable.h index 5eaf705..baf6c97 100644 --- a/src/libjin/render/drawable.h +++ b/src/libjin/render/drawable.h @@ -12,18 +12,26 @@ namespace render Drawable(); virtual ~Drawable(); - /* pseudo constructor*/ void init(int w = 0, int h = 0); - /* set anchor of texture, (0, 0) by default */ void setAnchor(int x, int y); void draw(int x, int y, float sx, float sy, float r); - - int getWidth(); - int getHeight(); - - inline GLuint getTexture() const { return texture; }; + + inline int Drawable::getWidth() const + { + return width; + } + + inline int Drawable::getHeight() const + { + return height; + } + + inline GLuint getTexture() const + { + return texture; + }; protected: #define DRAWABLE_V_SIZE 8 diff --git a/src/libjin/render/jsl.cpp b/src/libjin/render/jsl.cpp index 1935edc..56cbc31 100644 --- a/src/libjin/render/jsl.cpp +++ b/src/libjin/render/jsl.cpp @@ -10,6 +10,7 @@ namespace render "#define number float \n" "#define Image sampler2D \n" "#define Texel texture2D \n" + "#define Color vec4 \n" "#define extern uniform \n" "uniform Image _tex0_; \n" "%s \n" @@ -17,12 +18,15 @@ namespace render "gl_FragColor = effect(gl_Color, _tex0_, gl_TexCoord[0].xy, gl_FragCoord.xy);\n" "}\0"; - shared GLint JSLProgram::_current_texture_unit; - shared GLint JSLProgram::_max_texture_units; + shared GLint JSLProgram::current_texture_unit = 0; + shared GLint JSLProgram::max_texture_units = 0; + + shared JSLProgram* JSLProgram::current_JSL_program = nullptr; void JSLProgram::init(const char* program) { - glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &_max_texture_units); + if(max_texture_units == 0) + glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_texture_units); char* fs = (char*)alloca(strlen(program) + strlen(base_f)); sprintf(fs, base_f, program); @@ -37,41 +41,101 @@ namespace render shared GLint JSLProgram::getTextureUnit(const std::string& name) { - if (++_current_texture_unit >= _max_texture_units) + if (++current_texture_unit >= max_texture_units) return 0; - return _current_texture_unit; + return current_texture_unit; } void JSLProgram::use() { glUseProgram(pid); - _current_texture_unit = 0; + JSLProgram::current_JSL_program = this; + JSLProgram::current_texture_unit = 0; } shared void JSLProgram::unuse() { glUseProgram(0); + JSLProgram::current_JSL_program = nullptr; + JSLProgram::current_texture_unit = 0; } +#define checkJSL() if (current_JSL_program != this) return + void JSLProgram::sendFloat(const char* variable, float number) { + checkJSL(); + int loc = glGetUniformLocation(pid, variable); glUniform1f(loc, number); } - void JSLProgram::sendImage(const char* variable, const Image* image) + void JSLProgram::sendImage(const char* variable, const Image* image) { - GLint texture_unit = getTextureUnit(variable); + checkJSL(); + + GLint texture_unit = JSLProgram::getTextureUnit(variable); GLint location = glGetUniformLocation(pid, variable); glUniform1i(location, texture_unit); glActiveTexture(GL_TEXTURE0 + texture_unit); - glBindTexture(GL_TEXTURE_2D, image->getTexture()); // guarantee it gets bound + glBindTexture(GL_TEXTURE_2D, image->getTexture()); - // reset texture unit glActiveTexture(GL_TEXTURE0); } + + void JSLProgram::sendCanvas(const char* variable, const Canvas* canvas) + { + checkJSL(); + + GLint texture_unit = getTextureUnit(variable); + + GLint location = glGetUniformLocation(pid, variable); + glUniform1i(location, texture_unit); + + glActiveTexture(GL_TEXTURE0 + texture_unit); + glBindTexture(GL_TEXTURE_2D, canvas->getTexture()); + + glActiveTexture(GL_TEXTURE0); + } + + void JSLProgram::sendVec2(const char* name, float x, float y) + { + checkJSL(); + + int loc = glGetUniformLocation(pid, name); + glUniform2f(loc, x, y); + } + + void JSLProgram::sendVec3(const char* name, float x, float y, float z) + { + checkJSL(); + + int loc = glGetUniformLocation(pid, name); + glUniform3f(loc, x, y, z); + } + + void JSLProgram::sendVec4(const char* name, float x, float y, float z, float w) + { + checkJSL(); + + int loc = glGetUniformLocation(pid, name); + glUniform4f(loc, x, y, z, w); + } + void JSLProgram::sendColor(const char* name, const color* col) + { + checkJSL(); + + int loc = glGetUniformLocation(pid, name); + glUniform4f(loc, + col->rgba.r / 255.f, + col->rgba.g / 255.f, + col->rgba.b / 255.f, + col->rgba.a / 255.f + ); + } + } } diff --git a/src/libjin/render/jsl.h b/src/libjin/render/jsl.h index 4e0a6f0..80ef4dc 100644 --- a/src/libjin/render/jsl.h +++ b/src/libjin/render/jsl.h @@ -3,7 +3,9 @@ #include <string> #include <map> +#include "color.h" #include "image.h" +#include "canvas.h" #include "3rdparty/GLee/GLee.h" namespace jin @@ -23,15 +25,22 @@ namespace render void sendFloat(const char* name, float number); void sendImage(const char* name, const Image* image); + void sendVec2(const char* name, float x, float y); + void sendVec3(const char* name, float x, float y, float z); + void sendVec4(const char* name, float x, float y, float z, float w); + void sendCanvas(const char* name, const Canvas* canvas); + void sendColor(const char* name, const color* col); private: JSLProgram(); GLuint pid; + + static JSLProgram* current_JSL_program; - static GLint _current_texture_unit; - static GLint _max_texture_units; + static GLint current_texture_unit; + static GLint max_texture_units; static GLint getTextureUnit(const std::string& name); }; diff --git a/src/script/graphics/luaopen_JSL.cpp b/src/script/graphics/luaopen_JSL.cpp index 218a973..b5ba125 100644 --- a/src/script/graphics/luaopen_JSL.cpp +++ b/src/script/graphics/luaopen_JSL.cpp @@ -17,17 +17,26 @@ namespace lua static enum VARIABLE_TYPE { INVALID = 0, - NUMBER , - IMAGE , - TEXEL + + NUMBER, + IMAGE, + CANVAS, + VEC2, + VEC3, + VEC4, + COLOR, }; static VARIABLE_TYPE strtotype(const char* str) { std::string s = std::string(str); - if (s == "number") return NUMBER; - else if (s == "Image") return IMAGE; - else if (s == "Texel") return TEXEL; + if (s == "number") return NUMBER; + else if (s == "Image") return IMAGE; + else if (s == "Canvas") return CANVAS; + else if (s == "vec2") return VEC2; + else if (s == "vec3") return VEC3; + else if (s == "vec4") return VEC4; + else if (s == "Color") return COLOR; else return INVALID; } @@ -58,8 +67,48 @@ namespace lua jsl->sendImage(variable, img); break; } - case TEXEL: - break; + case CANVAS: + { + Canvas* canvas = (Canvas*)luax_checktype(L, 4, TYPE_IMAGE); + jsl->sendCanvas(variable, canvas); + break; + } + case VEC2: + { + float x = luax_checknumber(L, 4); + float y = luax_checknumber(L, 5); + jsl->sendVec2(variable, x, y); + break; + } + case VEC3: + { + float x = luax_checknumber(L, 4); + float y = luax_checknumber(L, 5); + float z = luax_checknumber(L, 6); + jsl->sendVec3(variable, x, y, z); + break; + } + case VEC4: + { + float x = luax_checknumber(L, 4); + float y = luax_checknumber(L, 5); + float z = luax_checknumber(L, 6); + float w = luax_checknumber(L, 7); + jsl->sendVec4(variable, x, y, z, w); + break; + } + case COLOR: + { + color col; + col.rgba.r = luax_checkinteger(L, 4); + col.rgba.g = luax_checkinteger(L, 5); + col.rgba.b = luax_checkinteger(L, 6); + col.rgba.a = luax_checkinteger(L, 7); + jsl->sendColor(variable, &col); + break; + } + default: + return 0; } } return 1; |