diff --git a/src/MsdfAtlasLayerCombo.cpp b/src/MsdfAtlasLayerCombo.cpp index 3326474..f46f286 100644 --- a/src/MsdfAtlasLayerCombo.cpp +++ b/src/MsdfAtlasLayerCombo.cpp @@ -1,6 +1,10 @@ #include "MsdfAtlasLayerCombo.h" #include "AtlasLayerCombo.h" +#include "MsdfLayer.h" #include "Utils.h" +#include "ofFileUtils.h" +#include +#include namespace ofxVariableLab { @@ -20,7 +24,11 @@ void MsdfAtlasLayerCombo::setup(const ComboIdentifier & layerIdentifier, msdfShader = make_shared (); #ifdef TARGET_EMSCRIPTEN - msdfShader->load("ofxMsdfgen/shaders/mix/ES3/shader"); + if(ofFile("shaders/ES3/msdf/shader.frag").exists()){ + msdfShader->load("shaders/ES3/msdf/shader"); + }else{ + msdfShader->load("ofxMsdfgen/shaders/mix/ES3/shader"); + } #else msdfShader->load("ofxMsdfgen/shaders/mix/GL3/shader"); #endif @@ -35,6 +43,28 @@ void MsdfAtlasLayerCombo::update(){ atlas->generate(useCache, saveToCache); isDirty = false; } + if(!uboIsSetup){ + setupUbo(); + } +} + +void MsdfAtlasLayerCombo::setupUbo(){ + msdfShader->begin(); + GLint prog = 0; + glGetIntegerv(GL_CURRENT_PROGRAM, &prog); + msdfShader->end(); + + glGenBuffers(1, &uboName); + uboIndex = glGetUniformBlockIndex(prog, "ubo"); + glBindBufferBase(GL_UNIFORM_BUFFER, 0, uboName); + MsdfLayer::UboParameters up; + glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::vec4) * 8, NULL, GL_DYNAMIC_DRAW); + glUniformBlockBinding(prog, uboIndex, 0); + for(const auto & layer : layers){ + layer->uboName = uboName; + layer->uboIndex = uboIndex; + } + uboIsSetup = true; } void MsdfAtlasLayerCombo::careForChild(shared_ptr layer){ @@ -44,6 +74,8 @@ void MsdfAtlasLayerCombo::careForChild(shared_ptr layer){ msdfLayer->setDirtyDirty(true); msdfLayer->setAtlas(this->atlas); msdfLayer->setShader(msdfShader); + msdfLayer->uboName = uboName; + msdfLayer->uboIndex = uboIndex; msdfLayer->scaleFactor = scaleFactor; auto & as = atlas->settings; for(const char c : layer->getProps().text){ diff --git a/src/MsdfAtlasLayerCombo.h b/src/MsdfAtlasLayerCombo.h index 4aac291..c0a52ed 100644 --- a/src/MsdfAtlasLayerCombo.h +++ b/src/MsdfAtlasLayerCombo.h @@ -34,5 +34,10 @@ class MsdfAtlasLayerCombo : public AtlasLayerCombo { ComboIdentifier identifier; bool isDirty = false; float scaleFactor = 1.0; + + GLuint uboName; + GLint uboIndex; + void setupUbo(); + bool uboIsSetup = false; }; } diff --git a/src/MsdfLayer.cpp b/src/MsdfLayer.cpp index b72fab6..e0b3d73 100644 --- a/src/MsdfLayer.cpp +++ b/src/MsdfLayer.cpp @@ -136,6 +136,7 @@ void MsdfLayer::draw(glm::vec3 position){ //cout << "translation_a: " << ofToString(translation_a) << endl; //cout << "scale_a: " << ofToString(scale_a) << endl; //} + // shader->begin(); shader->setUniform4f("fontColor", ofFloatColor::yellow); shader->setUniform2f("translation_a", translation_a); @@ -143,6 +144,7 @@ void MsdfLayer::draw(glm::vec3 position){ shader->setUniform2f("translation_b", translation_b); shader->setUniform2f("scale_b", scale_b); shader->setUniform1f("msdf_mix", mix); + atlasImage.draw(0, 0, w, h); shader->end(); ofPopMatrix(); @@ -176,7 +178,7 @@ void MsdfLayer::drawCharacter(const char character, shader->begin(); shader->setUniformTexture("msdf", atlasImage.getTexture(), 0); - shader->setUniform2f("unitRange", unitRange); + //shader->setUniform2f("unitRange", unitRange); shader->end(); ofPushMatrix(); @@ -244,12 +246,15 @@ void MsdfLayer::drawCharacter(const char character, glm::vec2 scale_b = glm::vec2(float(w_b) / float(atlas_w), float(h_b) / float(atlas_h)); shader->begin(); - shader->setUniform4f("fontColor", color); - shader->setUniform2f("translation_a", translation_a); - shader->setUniform2f("scale_a", scale_a); - shader->setUniform2f("translation_b", translation_b); - shader->setUniform2f("scale_b", scale_b); - shader->setUniform1f("msdf_mix", mix); + uboParameters.unitRange = unitRange; + uboParameters.fontColor = color; + uboParameters.translation_a = translation_a; + uboParameters.translation_b = translation_b; + uboParameters.scale_a = scale_a; + uboParameters.scale_b = scale_b; + uboParameters.mix = mix; + glBindBufferBase(GL_UNIFORM_BUFFER, 0, uboName); + glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(UboParameters), &uboParameters); ofRotateDeg(rotation, 0, 0, 1); atlasImage.draw(0, 0, w * scale, h * scale); shader->end(); diff --git a/src/MsdfLayer.h b/src/MsdfLayer.h index fd849bb..4e03125 100644 --- a/src/MsdfLayer.h +++ b/src/MsdfLayer.h @@ -11,6 +11,16 @@ namespace ofxVariableLab { class MsdfLayer : public Layer { public: + struct UboParameters { + glm::vec4 fontColor; + glm::vec4 bgColor; + glm::vec2 unitRange; + glm::vec2 translation_a; + glm::vec2 scale_a; + glm::vec2 translation_b; + glm::vec2 scale_b; + float mix; + }; void setup(const LayerSettings & settings = LayerSettings()) override; void update() override; void draw(glm::vec3 position = glm::vec3(0, 0, 0)) override; @@ -53,6 +63,9 @@ class MsdfLayer : public Layer { string id = ""; VFlipState vFlip = V_FLIP_UNKNOWN; float scaleFactor = 1.0; + GLuint uboName; + GLint uboIndex; + UboParameters uboParameters; private: Props lastProps;