From f118627dbcc4bf23faf0acb07d0941d0c33cf968 Mon Sep 17 00:00:00 2001 From: themancalledjakob Date: Fri, 7 Apr 2023 16:17:07 +0200 Subject: [PATCH] introduce naive layerID --- src/Layer.cpp | 3 +++ src/Layer.h | 25 ++++++++++++++++--------- src/LayerComposition.cpp | 26 ++++++++++++++++++++------ src/LayerComposition.h | 11 +++++++---- src/MsdfLayer.cpp | 37 +++++++++++++++++++++++++++---------- src/MsdfLayer.h | 16 +++++++++------- 6 files changed, 82 insertions(+), 36 deletions(-) diff --git a/src/Layer.cpp b/src/Layer.cpp index b256135..1682481 100644 --- a/src/Layer.cpp +++ b/src/Layer.cpp @@ -1,3 +1,6 @@ #include "Layer.h" // Layer is virtual +namespace ofxVariableLab { +int Layer::n_layers = 0; +} diff --git a/src/Layer.h b/src/Layer.h index 94cee08..41a32cf 100644 --- a/src/Layer.h +++ b/src/Layer.h @@ -11,6 +11,14 @@ #include namespace ofxVariableLab { + +using LayerID = string; + +struct LayerSettings { + uint32_t maxBufferSize = 100; + VFlipBehaviour vFlipBehaviour = V_FLIP_ONCE_AUTO; +}; + class Layer { public: enum Type { @@ -29,26 +37,25 @@ class Layer { float mirror_y_distance = 0; float letterDelay = 0; string text = "abcdefghijklmnoprstuvqxyzABUIWERJSD"; - }; - struct Settings { - uint32_t maxBufferSize = 100; - VFlipBehaviour vFlipBehaviour = V_FLIP_ONCE_AUTO; + string fontPath; }; - virtual void setup(const Settings & settings) = 0; + virtual void setup(const LayerSettings & settings = LayerSettings()) = 0; virtual void update() = 0; virtual void draw(glm::vec3 position = glm::vec3(0, 0, 0)) = 0; virtual void drawCharacter(const char character, glm::vec3 position = glm::vec3(0, 0, 0), - ofxMsdfgen::FontVariation fontVariation = ofxMsdfgen::FontVariation()) = 0; + glm::vec4 color = glm::vec4(1, 1, 1, 1), + float rotation = 0, + ofxVariableLab::FontVariation fontVariation = ofxVariableLab::FontVariation()) = 0; virtual void setProps(const Props & props) = 0; virtual const Props & getProps() const = 0; virtual void clearPropsBuffer() = 0; - virtual void setId(const string & id) = 0; - virtual const string & getId() = 0; + virtual void setId(const LayerID & id) = 0; + virtual const LayerID & getId() = 0; virtual void setShader(shared_ptr _shader) = 0; virtual shared_ptr getShader() const = 0; virtual const Type & getType() const = 0; + static int n_layers; }; - } diff --git a/src/LayerComposition.cpp b/src/LayerComposition.cpp index 119df1c..1a01373 100644 --- a/src/LayerComposition.cpp +++ b/src/LayerComposition.cpp @@ -58,6 +58,7 @@ void MsdfAtlasLayerCombo::careForChild(shared_ptr layer){ isDirty = true; } } + layers.push_back(msdfLayer); } const ComboIdentifier & MsdfAtlasLayerCombo::getIdentifier() const { @@ -73,9 +74,10 @@ void LayerComposition::update(){ } } -void LayerComposition::addLayer(const ComboIdentifier & identifier, - const string & text, - const std::vector & variations){ +LayerID LayerComposition::addLayer(const ComboIdentifier & identifier, + const string & text, + const std::vector & variations){ + string layerID = ""; if(atlasLayerCombos.count(identifier) <= 0){ switch(identifier.type){ case Layer::GPUFONT: { @@ -92,8 +94,11 @@ void LayerComposition::addLayer(const ComboIdentifier & identifier, auto layer = make_shared (); auto props = Layer::Props(); props.text = text; + props.fontPath = identifier.fontPath; layer->setProps(props); + layer->setup(); std::vector msdfVariations; + cout << "yeah yeah" << layerID << endl; for(const auto & v : variations){ msdfVariations.push_back({v.name, v.value}); } @@ -101,17 +106,26 @@ void LayerComposition::addLayer(const ComboIdentifier & identifier, // so we know it's dirty combo->atlas->addVariations(msdfVariations); combo->careForChild(layer); - layers.push_back(layer); + layers[layer->getId()] = layer; atlasLayerCombos[identifier] = std::move(combo); - break; + layerID = layer->getId(); + cout << "yeah yeah and the id is then:" << layerID << endl; } } + }else{ + cout << "whaaat we didnt find anyone like this" << layerID << endl; } + cout << "LayerComposition::addLayer returns " << layerID << endl; + return layerID; +} + +shared_ptr LayerComposition::getLayer(const LayerID & layerID){ + return layers[layerID]; } void LayerComposition::draw() const { for(const auto & layer_it : layers){ - layer_it->draw(glm::vec3(0, 0, 0)); + layer_it.second->draw(glm::vec3(0, 0, 0)); } } diff --git a/src/LayerComposition.h b/src/LayerComposition.h index 9fb9644..cdd003b 100644 --- a/src/LayerComposition.h +++ b/src/LayerComposition.h @@ -70,9 +70,11 @@ class MsdfAtlasLayerCombo : public AtlasLayerCombo { void update() override; void careForChild(shared_ptr layer) override; const ComboIdentifier & getIdentifier() const override; + shared_ptr getLayer(); shared_ptr atlas; private: + vector > layers; vector glyphGeometries; shared_ptr msdfShader; ComboIdentifier identifier; @@ -84,13 +86,14 @@ class LayerComposition { void setup(); void update(); void draw() const; - void addLayer(const ComboIdentifier & identifier, - const string & text, - const std::vector & variations); + LayerID addLayer(const ComboIdentifier & identifier, + const string & text, + const std::vector & variations); + shared_ptr getLayer(const LayerID & layerID); private: - vector > layers; + unordered_map > layers; unordered_map > atlasLayerCombos; //unordered_map > layers; }; diff --git a/src/MsdfLayer.cpp b/src/MsdfLayer.cpp index 749584d..d2973c0 100644 --- a/src/MsdfLayer.cpp +++ b/src/MsdfLayer.cpp @@ -1,4 +1,5 @@ #include "MsdfLayer.h" +#include "Atlas.h" #include "Utils.h" #include "fwd.hpp" #include "ofGraphics.h" @@ -6,8 +7,12 @@ namespace ofxVariableLab { -void MsdfLayer::setup(const Settings & settings){ +void MsdfLayer::setup(const LayerSettings & settings){ this->settings = settings; + if(id == ""){ + id = ofToString(Layer::n_layers); + n_layers++; + } } void MsdfLayer::update(){ @@ -157,7 +162,9 @@ void MsdfLayer::draw(glm::vec3 position){ } void MsdfLayer::drawCharacter(const char character, glm::vec3 position, - ofxMsdfgen::FontVariation fontVariation){ + glm::vec4 color, + float rotation, + ofxVariableLab::FontVariation fontVariation){ const ofImage & atlasImage = atlas->getAtlasImage(); ofDisableAlphaBlending(); ofEnableDepthTest(); @@ -168,21 +175,24 @@ void MsdfLayer::drawCharacter(const char character, shader->begin(); shader->setUniformTexture("msdf", atlasImage.getTexture(), 0); - shader->setUniform4f("fontColor", ofFloatColor::white); shader->setUniform2f("unitRange", unitRange); shader->end(); ofPushMatrix(); ofTranslate(position); ofPushStyle(); - ofSetColor(ofColor::pink); float mix = -1; ofxMsdfgen::GlyphGeometry gg_a; ofxMsdfgen::GlyphGeometry gg_b; + ofxMsdfgen::FontVariation fv{ + fontVariation.name, + fontVariation.value + }; + bool success = atlas->getGlyphGeometryPair(character, - fontVariation, + fv, gg_a, gg_b, mix); @@ -215,8 +225,14 @@ void MsdfLayer::drawCharacter(const char character, ofPushMatrix(); float magic = atlas->settings.scale; ofSetColor(ofFloatColor(1, 1, 1, 1)); - ofTranslate(pl * magic, 0, 0); - ofTranslate(0, -1 * pt * magic, 0); + getVFlip(settings.vFlipBehaviour, + ofGetCurrentOrientationMatrix(), + vFlip); + if(vFlip == V_FLIP_OFF){ + ofTranslate(pl * magic, (pt * magic) + (-1 * h), 0); + }else{ + ofTranslate(pl * magic, -1 * pt * magic, 0); + } glm::vec2 translation_a = glm::vec2(float(x_a) / float(atlas_w), float(y_a) / float(atlas_h)); @@ -227,12 +243,13 @@ 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", ofFloatColor::yellow); + 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); + //ofRotateDeg(rotation, 0, 0, 1); atlasImage.draw(0, 0, w, h); shader->end(); ofPopMatrix(); @@ -274,10 +291,10 @@ const Layer::Props & MsdfLayer::getProps() const { void MsdfLayer::clearPropsBuffer(){ propsBuffer.clear(); } -void MsdfLayer::setId(const string & id){ +void MsdfLayer::setId(const LayerID & id){ this->id = id; } -const string & MsdfLayer::getId(){ +const LayerID & MsdfLayer::getId(){ return id; } } diff --git a/src/MsdfLayer.h b/src/MsdfLayer.h index b48ebcb..8075012 100644 --- a/src/MsdfLayer.h +++ b/src/MsdfLayer.h @@ -10,17 +10,19 @@ namespace ofxVariableLab { class MsdfLayer : public Layer { public: - void setup(const Settings & settings); + void setup(const LayerSettings & settings = LayerSettings()) override; void update() override; void draw(glm::vec3 position = glm::vec3(0, 0, 0)) override; void drawCharacter(const char character, glm::vec3 position = glm::vec3(0, 0, 0), - ofxMsdfgen::FontVariation fontVariation = ofxMsdfgen::FontVariation()) override; + glm::vec4 color = glm::vec4(1, 1, 1, 1), + float rotation = 0, + ofxVariableLab::FontVariation fontVariation = ofxVariableLab::FontVariation()) override; void setProps(const Props & props) override; const Props & getProps() const override; void clearPropsBuffer() override; - void setId(const string & id) override; - const string & getId() override; + void setId(const LayerID & id) override; + const LayerID & getId() override; void setShader(shared_ptr _shader) override; shared_ptr getShader() const override; const Type & getType() const override; @@ -30,13 +32,13 @@ class MsdfLayer : public Layer { shared_ptr atlas; - Settings settings; + LayerSettings settings; std::deque propsBuffer; shared_ptr shader; /// \brief are props updated but not drawn yet bool isDirty = true; - /// \brief hashed id - string id; + /// \brief hashed id, or just counting up + string id = ""; VFlipState vFlip = V_FLIP_UNKNOWN; private: