prepare to connect with theatrejs

This commit is contained in:
jrkb 2023-04-14 16:55:15 +02:00
parent 095580d851
commit bd81be30df
9 changed files with 125 additions and 40 deletions

View file

@ -64,12 +64,34 @@ void GPUFontAtlasLayerCombo::update(){
//font->prepareGlyphsForText(mainText, true); //font->prepareGlyphsForText(mainText, true);
//} //}
//} //}
for(const auto & layer : layers){
if(layer->isDirtyDirty()){
isDirty = true;
break;
}
}
if(isDirty){ if(isDirty){
//ofxGPUFont::tryUpdateMainFont(library, //ofxGPUFont::tryUpdateMainFont(library,
//this->identifier.fontPath, //this->identifier.fontPath,
//mainText, //mainText,
//font, //font,
//this->settings.gpuTextureOffset); //this->settings.gpuTextureOffset);
// update the text
// this doesn't happen very often, so let's just
// iterate over all layers and calculate fresh
std::string text = "";
totalCharacters = 0;
for(const auto & layer : layers){
std::string layerText = layer->getProps().text;
totalCharacters += layerText.length();
text += layerText;
layer->setDirtyDirty(false); // technically not clean yet
// but this is synchronous
// and will happen, so we can avoid
// another loop
}
removeDuplicateCharacters(text);
mainText = text;
font->prepareGlyphsForText(mainText, true); font->prepareGlyphsForText(mainText, true);
isDirty = false; isDirty = false;
} }
@ -130,6 +152,7 @@ void GPUFontAtlasLayerCombo::setVFlip(VFlipState vFlipState){
const vector <shared_ptr <GPUFontLayer> > & GPUFontAtlasLayerCombo::getLayers(){ const vector <shared_ptr <GPUFontLayer> > & GPUFontAtlasLayerCombo::getLayers(){
return layers; return layers;
} }
bool lalala = false;
void GPUFontAtlasLayerCombo::draw(){ void GPUFontAtlasLayerCombo::draw(){
GLuint location; GLuint location;
@ -207,7 +230,14 @@ void GPUFontAtlasLayerCombo::draw(){
layer->getProps().text, layer->getProps().text,
vertices, indices, true, vertices, indices, true,
layer->getProps().fontSize_px); layer->getProps().fontSize_px);
if(!lalala){
nlohmann::json json;
auto props = layer->getProps();
props.to_json(json, props);
cout << "prooops:" << json.dump(2);
}
} }
lalala = true;
vertices.resize(totalCharacters * 4); vertices.resize(totalCharacters * 4);
indices.resize(totalCharacters * 6); indices.resize(totalCharacters * 6);
//cx = 0.5f * (bb.minX + bb.maxX); //cx = 0.5f * (bb.minX + bb.maxX);

View file

@ -10,15 +10,12 @@ namespace ofxVariableLab {
void GPUFontLayer::setup(const LayerSettings & settings){ void GPUFontLayer::setup(const LayerSettings & settings){
this->settings = settings; this->settings = settings;
if(id == ""){ if(id == ""){
id = ofToString(Layer::n_layers); id = "layer-" + ofToString(Layer::n_layers);
n_layers++; n_layers++;
} }
} }
void GPUFontLayer::update(){ void GPUFontLayer::update(){
if(isDirty){
isDirty = false;
}
} }
void GPUFontLayer::draw(glm::vec3 position){ void GPUFontLayer::draw(glm::vec3 position){
@ -37,22 +34,27 @@ void GPUFontLayer::setVFlip(const VFlipState vFlip){
this->vFlip = vFlip; this->vFlip = vFlip;
} }
//void GPUFontLayer::setAtlas(shared_ptr <ofxGPUFont::Atlas> _atlas){ bool GPUFontLayer::isDirtyDirty() const {
//atlas = _atlas; return isDirty;
//// TODO: this does not seem proper }
//propsBuffer[0].fontSize_px = atlas->settings.scale;
//} void GPUFontLayer::setDirtyDirty(bool dirtyDirty){
//shared_ptr <ofxGPUFont::Atlas> GPUFontLayer::getAtlas() const { isDirty = dirtyDirty;
//return atlas; }
//}
void GPUFontLayer::setProps(const Props & props){ void GPUFontLayer::setProps(const Props & props){
// TODO: for now only check text,
// but we should of course check also variations
if(propsBuffer.size() == 0 || props.text != propsBuffer[0].text){
setDirtyDirty(true);
}
while(propsBuffer.size() > max(0, int(settings.maxBufferSize - 1))){ while(propsBuffer.size() > max(0, int(settings.maxBufferSize - 1))){
propsBuffer.pop_back(); propsBuffer.pop_back();
} }
propsBuffer.push_front(props); propsBuffer.push_front(props);
} }
const Layer::Props & GPUFontLayer::getProps() const { const Layer::Props & GPUFontLayer::getProps() const {
return propsBuffer[0]; return propsBuffer[0]; // gets newest
} }
void GPUFontLayer::clearPropsBuffer(){ void GPUFontLayer::clearPropsBuffer(){
propsBuffer.clear(); propsBuffer.clear();

View file

@ -34,6 +34,8 @@ class GPUFontLayer : public Layer {
const Type & getType() const override; const Type & getType() const override;
void setVFlip(const VFlipState vFlip) override; void setVFlip(const VFlipState vFlip) override;
bool isDirtyDirty() const override;
void setDirtyDirty(bool dirtyDirty) override;
//void setAtlas(shared_ptr <ofxGPUFont::Atlas> _atlas); //void setAtlas(shared_ptr <ofxGPUFont::Atlas> _atlas);
//shared_ptr <ofxGPUFont::Atlas> getAtlas() const; //shared_ptr <ofxGPUFont::Atlas> getAtlas() const;

View file

@ -11,6 +11,8 @@
namespace ofxVariableLab { namespace ofxVariableLab {
// TODO: proper ID management and generation
// what do we have this fancy random_id for?
using LayerID = string; using LayerID = string;
struct LayerSettings { struct LayerSettings {
@ -25,10 +27,10 @@ class Layer {
GPUFONT GPUFONT
}; };
struct Props { struct Props {
float x = 0; float x = 200;
float y = 0; float y = 200;
float rotation = 0; float rotation = 0;
float fontSize_px = 24; float fontSize_px = 42;
float color[4]; float color[4];
bool mirror_x = false; bool mirror_x = false;
float mirror_x_distance = 0; float mirror_x_distance = 0;
@ -36,7 +38,20 @@ class Layer {
float mirror_y_distance = 0; float mirror_y_distance = 0;
float letterDelay = 0; float letterDelay = 0;
string text = "abc"; string text = "abc";
string fontPath; string fontPath = "data/celines-fonts/Version-2-var.ttf";
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Props,
x,
y,
rotation,
fontSize_px,
color,
mirror_x,
mirror_x_distance,
mirror_y,
mirror_y_distance,
letterDelay,
text,
fontPath)
}; };
virtual void setup(const LayerSettings & settings = LayerSettings()) = 0; virtual void setup(const LayerSettings & settings = LayerSettings()) = 0;
@ -57,6 +72,8 @@ class Layer {
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;
virtual void setVFlip(const VFlipState vFlip) = 0; virtual void setVFlip(const VFlipState vFlip) = 0;
virtual bool isDirtyDirty() const = 0;
virtual void setDirtyDirty(bool dirtyDirty) = 0;
static int n_layers; static int n_layers;
}; };
} }

View file

@ -20,10 +20,27 @@ void LayerComposition::update(){
atlasLayerCombo.second->update(); atlasLayerCombo.second->update();
} }
} }
LayerID LayerComposition::addLayer(const Layer::Props & props,
LayerID layerID){
ComboIdentifier identifier;
identifier.fontPath = props.fontPath;
identifier.type = Layer::GPUFONT;
string text = props.text;
std::vector <FontVariation> variations = {
{"Weight", 100.0},
{"Weight", 700.0}
};
return addLayer(identifier,
text,
variations,
layerID);
}
LayerID 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,
LayerID layerID){
auto props = Layer::Props(); auto props = Layer::Props();
props.text = text; props.text = text;
props.fontPath = identifier.fontPath; props.fontPath = identifier.fontPath;
@ -34,8 +51,8 @@ LayerID LayerComposition::addLayer(const ComboIdentifier & identifier,
LayerID LayerComposition::addLayer(const ComboIdentifier & identifier, LayerID LayerComposition::addLayer(const ComboIdentifier & identifier,
const Layer::Props & props, const Layer::Props & props,
const std::vector <FontVariation> & variations){ const std::vector <FontVariation> & variations,
string layerID = ""; LayerID layerID){
switch(identifier.type){ switch(identifier.type){
case Layer::GPUFONT: { case Layer::GPUFONT: {
shared_ptr <GPUFontAtlasLayerCombo> combo; shared_ptr <GPUFontAtlasLayerCombo> combo;
@ -55,9 +72,16 @@ LayerID LayerComposition::addLayer(const ComboIdentifier & identifier,
} }
auto layer = make_shared <GPUFontLayer>(); auto layer = make_shared <GPUFontLayer>();
layer->vFlip = vFlipState;
layer->setProps(props); layer->setProps(props);
layer->setup();
if(layerID == ""){
layerID = layer->getId();
cout << "yess layerID is now " << layerID << "!" << endl;
}else{
layer->setId(layerID);
}
combo->careForChild(layer); combo->careForChild(layer);
layerID = layer->getId();
layers[layerID] = layer; layers[layerID] = layer;
break; break;
@ -72,6 +96,11 @@ LayerID LayerComposition::addLayer(const ComboIdentifier & identifier,
layer->vFlip = vFlipState; layer->vFlip = vFlipState;
layer->setProps(props); layer->setProps(props);
layer->setup(); layer->setup();
if(layerID == ""){
layerID = layer->getId();
}else{
layer->setId(layerID);
}
std::vector <ofxMsdfgen::FontVariation> msdfVariations; std::vector <ofxMsdfgen::FontVariation> msdfVariations;
for(const auto & v : variations){ for(const auto & v : variations){
msdfVariations.push_back({v.name, v.value}); msdfVariations.push_back({v.name, v.value});

View file

@ -15,12 +15,16 @@ class LayerComposition {
void setup(); void setup();
void update(); void update();
void draw() const; void draw() const;
LayerID addLayer(const Layer::Props & props,
LayerID layerID = "");
LayerID 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,
LayerID layerID = "");
LayerID addLayer(const ComboIdentifier & identifier, LayerID addLayer(const ComboIdentifier & identifier,
const Layer::Props & props, const Layer::Props & props,
const std::vector <FontVariation> & variations); const std::vector <FontVariation> & variations,
LayerID layerID = "");
void removeLayer(const LayerID & id); // TODO: make bool, to catch nonexisting void removeLayer(const LayerID & id); // TODO: make bool, to catch nonexisting
shared_ptr <Layer> getLayer(const LayerID & layerID); shared_ptr <Layer> getLayer(const LayerID & layerID);
const unordered_map <ComboIdentifier, shared_ptr <AtlasLayerCombo> > & getAtlasLayerCombos() const; const unordered_map <ComboIdentifier, shared_ptr <AtlasLayerCombo> > & getAtlasLayerCombos() const;

View file

@ -10,15 +10,12 @@ namespace ofxVariableLab {
void MsdfLayer::setup(const LayerSettings & settings){ void MsdfLayer::setup(const LayerSettings & settings){
this->settings = settings; this->settings = settings;
if(id == ""){ if(id == ""){
id = ofToString(Layer::n_layers); id = "layer-" + ofToString(Layer::n_layers);
n_layers++; n_layers++;
} }
} }
void MsdfLayer::update(){ void MsdfLayer::update(){
if(isDirty){
isDirty = false;
}
} }
void MsdfLayer::draw(glm::vec3 position){ void MsdfLayer::draw(glm::vec3 position){
@ -277,7 +274,12 @@ const Layer::Type & MsdfLayer::getType() const {
void MsdfLayer::setVFlip(const VFlipState vFlip){ void MsdfLayer::setVFlip(const VFlipState vFlip){
this->vFlip = vFlip; this->vFlip = vFlip;
} }
bool MsdfLayer::isDirtyDirty() const {
return isDirty;
}
void MsdfLayer::setDirtyDirty(bool dirtyDirty){
isDirty = dirtyDirty;
}
void MsdfLayer::setAtlas(shared_ptr <ofxMsdfgen::Atlas> _atlas){ void MsdfLayer::setAtlas(shared_ptr <ofxMsdfgen::Atlas> _atlas){
atlas = _atlas; atlas = _atlas;
// TODO: this does not seem proper // TODO: this does not seem proper
@ -287,6 +289,11 @@ shared_ptr <ofxMsdfgen::Atlas> MsdfLayer::getAtlas() const {
return atlas; return atlas;
} }
void MsdfLayer::setProps(const Props & props){ void MsdfLayer::setProps(const Props & props){
// TODO: for now only check text,
// but we should of course check also variations
if(propsBuffer.size() == 0 || props.text != propsBuffer[0].text){
setDirtyDirty(true);
}
while(propsBuffer.size() > max(0, int(settings.maxBufferSize - 1))){ while(propsBuffer.size() > max(0, int(settings.maxBufferSize - 1))){
propsBuffer.pop_back(); propsBuffer.pop_back();
} }

View file

@ -28,6 +28,8 @@ class MsdfLayer : public Layer {
shared_ptr <ofShader> getShader() const override; shared_ptr <ofShader> getShader() const override;
const Type & getType() const override; const Type & getType() const override;
void setVFlip(const VFlipState vFlip) override; void setVFlip(const VFlipState vFlip) override;
bool isDirtyDirty() const override;
void setDirtyDirty(bool dirtyDirty) override;
void setAtlas(shared_ptr <ofxMsdfgen::Atlas> _atlas); void setAtlas(shared_ptr <ofxMsdfgen::Atlas> _atlas);
shared_ptr <ofxMsdfgen::Atlas> getAtlas() const; shared_ptr <ofxMsdfgen::Atlas> getAtlas() const;

View file

@ -4,6 +4,9 @@
#include <string> #include <string>
#include <set> #include <set>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_MULTIPLE_MASTERS_H
namespace ofxVariableLab { namespace ofxVariableLab {
@ -24,18 +27,6 @@ inline void hash_combine(std::size_t & seed, const T & v){
std::hash <T> hasher; std::hash <T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
} }
//template <class ForwardIt, class UnaryPredicate>
//ForwardIt remove_if(ForwardIt first, ForwardIt last, UnaryPredicate p){
//first = std::find_if(first, last, p);
//if(first != last){
//for(ForwardIt i = first; ++i != last;){
//if(!p(*i)){
//*first++ = std::move(*i);
//}
//}
//}
//return first;
//}
inline void removeDuplicateCharacters(std::string & str){ inline void removeDuplicateCharacters(std::string & str){
std::set <char> chars; std::set <char> chars;
@ -79,4 +70,5 @@ void getVFlip(const VFlipBehaviour & vFlipBehaviour,
const glm::mat4 & orientationMatrix, const glm::mat4 & orientationMatrix,
VFlipState & vFlip); VFlipState & vFlip);
} }