diff options
Diffstat (limited to 'src/libjin/render')
-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 |
5 files changed, 101 insertions, 30 deletions
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); }; |