From 0f07dbfb64dd490f2756fb973cd3e0c9093eb833 Mon Sep 17 00:00:00 2001 From: themancalledjakob Date: Sun, 26 Mar 2023 21:19:37 +0200 Subject: [PATCH] Layer has Atlas not atlasImage, etc --- src/Layer.cpp | 59 ++++++++++++++++++++++++++++++++++++---- src/Layer.h | 6 ++-- src/LayerComposition.cpp | 16 +++++++---- src/LayerComposition.h | 2 +- 4 files changed, 67 insertions(+), 16 deletions(-) diff --git a/src/Layer.cpp b/src/Layer.cpp index 0afba6b..6c3021c 100644 --- a/src/Layer.cpp +++ b/src/Layer.cpp @@ -1,6 +1,8 @@ #include "Layer.h" +#include "conversion.h" #include "ofAppRunner.h" #include "ofGraphics.h" +#include "ofUtils.h" namespace ofxVariableLab { @@ -14,25 +16,70 @@ MsdfLayer::~MsdfLayer(){ } void Layer::update(){ + if(isDirty){ + isDirty = false; + } } -void MsdfLayer::draw(float x, float y) const { +void MsdfLayer::draw(glm::vec3 position) const { + const ofImage & atlasImage = atlas->getAtlasImage(); ofDisableAlphaBlending(); ofEnableDepthTest(); shader->begin(); - shader->setUniformTexture("msdf", atlasImage->getTexture(), 0); + shader->setUniformTexture("msdf", atlasImage.getTexture(), 0); shader->setUniform4f("fontColor", ofFloatColor::white); float pixelRange = 2; - int atlas_w = atlasImage->getWidth(); - int atlas_h = atlasImage->getHeight(); + int atlas_w = atlasImage.getWidth(); + int atlas_h = atlasImage.getHeight(); int atlas_x = 0; int atlas_y = 0; glm::vec2 unitRange = glm::vec2(pixelRange) / glm::vec2(atlas_w, atlas_h); shader->setUniform2f("unitRange", unitRange); - //atlasImage->drawSubsection(0, 0, w, h, x, y, w, h); - atlasImage->draw(x, y); shader->end(); + + ofPushMatrix(); + //cout << "drawing letters (" << ofToString(propsBuffer.size()) << ")" << endl; + for(const char c : propsBuffer[0].text){ + ofxMsdfgen::GlyphGeometry gg = atlas->getGlyphGeometry(c); + int x, y, w, h; + gg.getBoxRect(x, y, w, h); + y = atlas_h - (y + h); + if(y < 0){ + // FIXME: make sure this does not happen + ofLogError("MsdfLayer::draw") << "y smaller 0, this is not good" << endl; + } + ofPushStyle(); + ofNoFill(); + ofSetColor(ofFloatColor(sin(ofGetElapsedTimeMicros() * 0.0001) * 0.5 + 0.5, 0, 0, 1)); + double pl, pb, pr, pt, + il, ib, ir, it; + gg.getQuadPlaneBounds(pl, pb, pr, pt); + gg.getQuadAtlasBounds(il, ib, ir, it); + + ofPushMatrix(); + float magic = ofMap(sin(ofGetElapsedTimeMicros() * 0.000001), -1, 1, 1, 1000); + ofTranslate(pr * magic, pt * magic, 0); + //if(ofGetFrameNum() % 30 == 0){ + //cout << "drawing letter " << c << endl + //<< "\tpl: " << ofToString(pl) + //<< "\tpb: " << ofToString(pb) + //<< "\tpr: " << ofToString(pr) + //<< "\tpt: " << ofToString(pt) + //<< endl; + //} + + ofFill(); + shader->begin(); + ofSetColor(ofFloatColor(1, 1, 1, 1)); + atlasImage.drawSubsection(0, 0, w, h, x, y, w, h); + shader->end(); + ofPopMatrix(); + ofPopStyle(); + ofTranslate(w, 0, 0); + } + ofPopMatrix(); + //atlasImage.draw(x, y); ofDisableDepthTest(); ofEnableAlphaBlending(); } diff --git a/src/Layer.h b/src/Layer.h index ab003e0..976482e 100644 --- a/src/Layer.h +++ b/src/Layer.h @@ -41,7 +41,7 @@ class Layer { }; void update(); - virtual void draw(float x = 0, float y = 0) const = 0; + virtual void draw(glm::vec3 position = glm::vec3(0, 0, 0)) const = 0; void setProps(const Props & props); //void setProps(float _x, //float _y, @@ -73,8 +73,8 @@ class MsdfLayer : public Layer { public: MsdfLayer(); ~MsdfLayer(); - void draw(float x = 0, float y = 0) const override; - shared_ptr atlasImage; + void draw(glm::vec3 position = glm::vec3(0, 0, 0)) const override; + shared_ptr atlas; }; } diff --git a/src/LayerComposition.cpp b/src/LayerComposition.cpp index 599f8fc..5384a9a 100644 --- a/src/LayerComposition.cpp +++ b/src/LayerComposition.cpp @@ -1,21 +1,23 @@ #include "LayerComposition.h" #include "ofUtils.h" +#include namespace ofxVariableLab { void LayerComposition::setup(){ ofxMsdfgen::AtlasSettings settings; - settings.characters = "ABCDEFGHIJKL"; + //settings.characters = "ABCDEFGHIJKL"; settings.scale = 64; //string fontName = "RobotoFlex.ttf"; //string fontPath = "data/fonts/" + fontName; //string fontPath = "data/celines-fonts/testing2VF.ttf"; string fontPath = "data/celines-fonts/Version-2-var.ttf"; //string fontPath = "data/celines-fonts/Cottagecore.ttf"; - atlas.setup(fontPath, settings); + atlas = make_shared (); + atlas->setup(fontPath, settings); - atlasImage = make_shared (atlas.getAtlasImage()); - glyphGeometries = atlas.getGlyphGeometries(); + atlasImage = make_shared (atlas->getAtlasImage()); + glyphGeometries = atlas->getGlyphGeometries(); msdfShader = make_shared (); #ifdef TARGET_EMSCRIPTEN msdfShader->load("ofxMsdfgen/shaders/unitRange/ES3/shader"); @@ -24,11 +26,13 @@ void LayerComposition::setup(){ #endif auto layer = make_unique (); - layer->atlasImage = atlasImage; + layer->atlas = atlas; layer->shader = msdfShader; - layer->atlasImage->update(); + + layer->setProps(Layer::Props()); layers.push_back(std::move(layer)); + } void LayerComposition::update(){ diff --git a/src/LayerComposition.h b/src/LayerComposition.h index d0f262a..a761837 100644 --- a/src/LayerComposition.h +++ b/src/LayerComposition.h @@ -18,7 +18,7 @@ class LayerComposition { #endif private: - ofxMsdfgen::Atlas atlas; + shared_ptr atlas; shared_ptr atlasImage; vector glyphGeometries; shared_ptr msdfShader;