Layer has Atlas not atlasImage, etc

This commit is contained in:
jrkb 2023-03-26 21:19:37 +02:00
parent 7c3b8767e8
commit 0f07dbfb64
4 changed files with 67 additions and 16 deletions

View file

@ -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();
}

View file

@ -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 <ofImage> atlasImage;
void draw(glm::vec3 position = glm::vec3(0, 0, 0)) const override;
shared_ptr <ofxMsdfgen::Atlas> atlas;
};
}

View file

@ -1,21 +1,23 @@
#include "LayerComposition.h"
#include "ofUtils.h"
#include <memory>
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 <ofxMsdfgen::Atlas>();
atlas->setup(fontPath, settings);
atlasImage = make_shared <ofImage>(atlas.getAtlasImage());
glyphGeometries = atlas.getGlyphGeometries();
atlasImage = make_shared <ofImage>(atlas->getAtlasImage());
glyphGeometries = atlas->getGlyphGeometries();
msdfShader = make_shared <ofShader>();
#ifdef TARGET_EMSCRIPTEN
msdfShader->load("ofxMsdfgen/shaders/unitRange/ES3/shader");
@ -24,11 +26,13 @@ void LayerComposition::setup(){
#endif
auto layer = make_unique <ofxVariableLab::MsdfLayer>();
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(){

View file

@ -18,7 +18,7 @@ class LayerComposition {
#endif
private:
ofxMsdfgen::Atlas atlas;
shared_ptr <ofxMsdfgen::Atlas> atlas;
shared_ptr <ofImage> atlasImage;
vector <ofxMsdfgen::GlyphGeometry> glyphGeometries;
shared_ptr <ofShader> msdfShader;