introduce naive layerID

This commit is contained in:
jrkb 2023-04-07 16:17:07 +02:00
parent ad95ad0fe9
commit f118627dbc
6 changed files with 82 additions and 36 deletions

View file

@ -1,3 +1,6 @@
#include "Layer.h" #include "Layer.h"
// Layer is virtual // Layer is virtual
namespace ofxVariableLab {
int Layer::n_layers = 0;
}

View file

@ -11,6 +11,14 @@
#include <memory> #include <memory>
namespace ofxVariableLab { namespace ofxVariableLab {
using LayerID = string;
struct LayerSettings {
uint32_t maxBufferSize = 100;
VFlipBehaviour vFlipBehaviour = V_FLIP_ONCE_AUTO;
};
class Layer { class Layer {
public: public:
enum Type { enum Type {
@ -29,26 +37,25 @@ class Layer {
float mirror_y_distance = 0; float mirror_y_distance = 0;
float letterDelay = 0; float letterDelay = 0;
string text = "abcdefghijklmnoprstuvqxyzABUIWERJSD"; string text = "abcdefghijklmnoprstuvqxyzABUIWERJSD";
}; string fontPath;
struct Settings {
uint32_t maxBufferSize = 100;
VFlipBehaviour vFlipBehaviour = V_FLIP_ONCE_AUTO;
}; };
virtual void setup(const Settings & settings) = 0; virtual void setup(const LayerSettings & settings = LayerSettings()) = 0;
virtual void update() = 0; virtual void update() = 0;
virtual void draw(glm::vec3 position = glm::vec3(0, 0, 0)) = 0; virtual void draw(glm::vec3 position = glm::vec3(0, 0, 0)) = 0;
virtual void drawCharacter(const char character, virtual void drawCharacter(const char character,
glm::vec3 position = glm::vec3(0, 0, 0), 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 void setProps(const Props & props) = 0;
virtual const Props & getProps() const = 0; virtual const Props & getProps() const = 0;
virtual void clearPropsBuffer() = 0; virtual void clearPropsBuffer() = 0;
virtual void setId(const string & id) = 0; virtual void setId(const LayerID & id) = 0;
virtual const string & getId() = 0; virtual const LayerID & getId() = 0;
virtual void setShader(shared_ptr <ofShader> _shader) = 0; virtual void setShader(shared_ptr <ofShader> _shader) = 0;
virtual shared_ptr <ofShader> getShader() const = 0; virtual shared_ptr <ofShader> getShader() const = 0;
virtual const Type & getType() const = 0; virtual const Type & getType() const = 0;
static int n_layers;
}; };
} }

View file

@ -58,6 +58,7 @@ void MsdfAtlasLayerCombo::careForChild(shared_ptr <Layer> layer){
isDirty = true; isDirty = true;
} }
} }
layers.push_back(msdfLayer);
} }
const ComboIdentifier & MsdfAtlasLayerCombo::getIdentifier() const { const ComboIdentifier & MsdfAtlasLayerCombo::getIdentifier() const {
@ -73,9 +74,10 @@ void LayerComposition::update(){
} }
} }
void LayerComposition::addLayer(const ComboIdentifier & identifier, LayerID LayerComposition::addLayer(const ComboIdentifier & identifier,
const string & text, const string & text,
const std::vector <FontVariation> & variations){ const std::vector <FontVariation> & variations){
string layerID = "";
if(atlasLayerCombos.count(identifier) <= 0){ if(atlasLayerCombos.count(identifier) <= 0){
switch(identifier.type){ switch(identifier.type){
case Layer::GPUFONT: { case Layer::GPUFONT: {
@ -92,8 +94,11 @@ void LayerComposition::addLayer(const ComboIdentifier & identifier,
auto layer = make_shared <MsdfLayer>(); auto layer = make_shared <MsdfLayer>();
auto props = Layer::Props(); auto props = Layer::Props();
props.text = text; props.text = text;
props.fontPath = identifier.fontPath;
layer->setProps(props); layer->setProps(props);
layer->setup();
std::vector <ofxMsdfgen::FontVariation> msdfVariations; std::vector <ofxMsdfgen::FontVariation> msdfVariations;
cout << "yeah yeah" << layerID << endl;
for(const auto & v : variations){ for(const auto & v : variations){
msdfVariations.push_back({v.name, v.value}); msdfVariations.push_back({v.name, v.value});
} }
@ -101,17 +106,26 @@ void LayerComposition::addLayer(const ComboIdentifier & identifier,
// so we know it's dirty // so we know it's dirty
combo->atlas->addVariations(msdfVariations); combo->atlas->addVariations(msdfVariations);
combo->careForChild(layer); combo->careForChild(layer);
layers.push_back(layer); layers[layer->getId()] = layer;
atlasLayerCombos[identifier] = std::move(combo); 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 <Layer> LayerComposition::getLayer(const LayerID & layerID){
return layers[layerID];
} }
void LayerComposition::draw() const { void LayerComposition::draw() const {
for(const auto & layer_it : layers){ for(const auto & layer_it : layers){
layer_it->draw(glm::vec3(0, 0, 0)); layer_it.second->draw(glm::vec3(0, 0, 0));
} }
} }

View file

@ -70,9 +70,11 @@ class MsdfAtlasLayerCombo : public AtlasLayerCombo {
void update() override; void update() override;
void careForChild(shared_ptr <Layer> layer) override; void careForChild(shared_ptr <Layer> layer) override;
const ComboIdentifier & getIdentifier() const override; const ComboIdentifier & getIdentifier() const override;
shared_ptr <Layer> getLayer();
shared_ptr <ofxMsdfgen::Atlas> atlas; shared_ptr <ofxMsdfgen::Atlas> atlas;
private: private:
vector <shared_ptr <MsdfLayer> > layers;
vector <ofxMsdfgen::GlyphGeometry> glyphGeometries; vector <ofxMsdfgen::GlyphGeometry> glyphGeometries;
shared_ptr <ofShader> msdfShader; shared_ptr <ofShader> msdfShader;
ComboIdentifier identifier; ComboIdentifier identifier;
@ -84,13 +86,14 @@ class LayerComposition {
void setup(); void setup();
void update(); void update();
void draw() const; void draw() const;
void addLayer(const ComboIdentifier & identifier, LayerID addLayer(const ComboIdentifier & identifier,
const string & text, const string & text,
const std::vector <FontVariation> & variations); const std::vector <FontVariation> & variations);
shared_ptr <ofxVariableLab::Layer> getLayer(const LayerID & layerID);
private: private:
vector <shared_ptr <ofxVariableLab::Layer> > layers; unordered_map <LayerID, shared_ptr <ofxVariableLab::Layer> > layers;
unordered_map <ComboIdentifier, shared_ptr <AtlasLayerCombo> > atlasLayerCombos; unordered_map <ComboIdentifier, shared_ptr <AtlasLayerCombo> > atlasLayerCombos;
//unordered_map <LayerIdentifier, shared_ptr <ofxVariableLab::Layer> > layers; //unordered_map <LayerIdentifier, shared_ptr <ofxVariableLab::Layer> > layers;
}; };

View file

@ -1,4 +1,5 @@
#include "MsdfLayer.h" #include "MsdfLayer.h"
#include "Atlas.h"
#include "Utils.h" #include "Utils.h"
#include "fwd.hpp" #include "fwd.hpp"
#include "ofGraphics.h" #include "ofGraphics.h"
@ -6,8 +7,12 @@
namespace ofxVariableLab { namespace ofxVariableLab {
void MsdfLayer::setup(const Settings & settings){ void MsdfLayer::setup(const LayerSettings & settings){
this->settings = settings; this->settings = settings;
if(id == ""){
id = ofToString(Layer::n_layers);
n_layers++;
}
} }
void MsdfLayer::update(){ void MsdfLayer::update(){
@ -157,7 +162,9 @@ void MsdfLayer::draw(glm::vec3 position){
} }
void MsdfLayer::drawCharacter(const char character, void MsdfLayer::drawCharacter(const char character,
glm::vec3 position, glm::vec3 position,
ofxMsdfgen::FontVariation fontVariation){ glm::vec4 color,
float rotation,
ofxVariableLab::FontVariation fontVariation){
const ofImage & atlasImage = atlas->getAtlasImage(); const ofImage & atlasImage = atlas->getAtlasImage();
ofDisableAlphaBlending(); ofDisableAlphaBlending();
ofEnableDepthTest(); ofEnableDepthTest();
@ -168,21 +175,24 @@ void MsdfLayer::drawCharacter(const char character,
shader->begin(); shader->begin();
shader->setUniformTexture("msdf", atlasImage.getTexture(), 0); shader->setUniformTexture("msdf", atlasImage.getTexture(), 0);
shader->setUniform4f("fontColor", ofFloatColor::white);
shader->setUniform2f("unitRange", unitRange); shader->setUniform2f("unitRange", unitRange);
shader->end(); shader->end();
ofPushMatrix(); ofPushMatrix();
ofTranslate(position); ofTranslate(position);
ofPushStyle(); ofPushStyle();
ofSetColor(ofColor::pink);
float mix = -1; float mix = -1;
ofxMsdfgen::GlyphGeometry gg_a; ofxMsdfgen::GlyphGeometry gg_a;
ofxMsdfgen::GlyphGeometry gg_b; ofxMsdfgen::GlyphGeometry gg_b;
ofxMsdfgen::FontVariation fv{
fontVariation.name,
fontVariation.value
};
bool success = atlas->getGlyphGeometryPair(character, bool success = atlas->getGlyphGeometryPair(character,
fontVariation, fv,
gg_a, gg_a,
gg_b, gg_b,
mix); mix);
@ -215,8 +225,14 @@ void MsdfLayer::drawCharacter(const char character,
ofPushMatrix(); ofPushMatrix();
float magic = atlas->settings.scale; float magic = atlas->settings.scale;
ofSetColor(ofFloatColor(1, 1, 1, 1)); ofSetColor(ofFloatColor(1, 1, 1, 1));
ofTranslate(pl * magic, 0, 0); getVFlip(settings.vFlipBehaviour,
ofTranslate(0, -1 * pt * magic, 0); 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), glm::vec2 translation_a = glm::vec2(float(x_a) / float(atlas_w),
float(y_a) / float(atlas_h)); 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), glm::vec2 scale_b = glm::vec2(float(w_b) / float(atlas_w),
float(h_b) / float(atlas_h)); float(h_b) / float(atlas_h));
shader->begin(); shader->begin();
shader->setUniform4f("fontColor", ofFloatColor::yellow); shader->setUniform4f("fontColor", color);
shader->setUniform2f("translation_a", translation_a); shader->setUniform2f("translation_a", translation_a);
shader->setUniform2f("scale_a", scale_a); shader->setUniform2f("scale_a", scale_a);
shader->setUniform2f("translation_b", translation_b); shader->setUniform2f("translation_b", translation_b);
shader->setUniform2f("scale_b", scale_b); shader->setUniform2f("scale_b", scale_b);
shader->setUniform1f("msdf_mix", mix); shader->setUniform1f("msdf_mix", mix);
//ofRotateDeg(rotation, 0, 0, 1);
atlasImage.draw(0, 0, w, h); atlasImage.draw(0, 0, w, h);
shader->end(); shader->end();
ofPopMatrix(); ofPopMatrix();
@ -274,10 +291,10 @@ const Layer::Props & MsdfLayer::getProps() const {
void MsdfLayer::clearPropsBuffer(){ void MsdfLayer::clearPropsBuffer(){
propsBuffer.clear(); propsBuffer.clear();
} }
void MsdfLayer::setId(const string & id){ void MsdfLayer::setId(const LayerID & id){
this->id = id; this->id = id;
} }
const string & MsdfLayer::getId(){ const LayerID & MsdfLayer::getId(){
return id; return id;
} }
} }

View file

@ -10,17 +10,19 @@ namespace ofxVariableLab {
class MsdfLayer : public Layer { class MsdfLayer : public Layer {
public: public:
void setup(const Settings & settings); void setup(const LayerSettings & settings = LayerSettings()) override;
void update() override; void update() override;
void draw(glm::vec3 position = glm::vec3(0, 0, 0)) override; void draw(glm::vec3 position = glm::vec3(0, 0, 0)) override;
void drawCharacter(const char character, void drawCharacter(const char character,
glm::vec3 position = glm::vec3(0, 0, 0), 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; void setProps(const Props & props) override;
const Props & getProps() const override; const Props & getProps() const override;
void clearPropsBuffer() override; void clearPropsBuffer() override;
void setId(const string & id) override; void setId(const LayerID & id) override;
const string & getId() override; const LayerID & getId() override;
void setShader(shared_ptr <ofShader> _shader) override; void setShader(shared_ptr <ofShader> _shader) override;
shared_ptr <ofShader> getShader() const override; shared_ptr <ofShader> getShader() const override;
const Type & getType() const override; const Type & getType() const override;
@ -30,13 +32,13 @@ class MsdfLayer : public Layer {
shared_ptr <ofxMsdfgen::Atlas> atlas; shared_ptr <ofxMsdfgen::Atlas> atlas;
Settings settings; LayerSettings settings;
std::deque <Props> propsBuffer; std::deque <Props> propsBuffer;
shared_ptr <ofShader> shader; shared_ptr <ofShader> shader;
/// \brief are props updated but not drawn yet /// \brief are props updated but not drawn yet
bool isDirty = true; bool isDirty = true;
/// \brief hashed id /// \brief hashed id, or just counting up
string id; string id = "";
VFlipState vFlip = V_FLIP_UNKNOWN; VFlipState vFlip = V_FLIP_UNKNOWN;
private: private: