diff --git a/src/GPUFontAtlasLayerCombo.cpp b/src/GPUFontAtlasLayerCombo.cpp index 0abf29e..b8d6665 100644 --- a/src/GPUFontAtlasLayerCombo.cpp +++ b/src/GPUFontAtlasLayerCombo.cpp @@ -6,6 +6,7 @@ #include "ofGraphics.h" #include "ofRectangle.h" #include "ofUtils.h" +#include "quaternion.hpp" namespace ofxVariableLab { @@ -224,80 +225,15 @@ void GPUFontAtlasLayerCombo::draw(){ font->collectBoundingBoxes(layer->getVariationText(), bb, bbs, advanceY, true, layer->getProps().fontSize_px); - ofNode momNode; - ofNode node; - node.setParent(momNode); glm::vec4 transformOrigin; - switch(layer->getProps().transformOrigin){ - default: - case Layer::TransformOrigin::TOP_LEFT: { - node.move(glm::vec3(0, - ascender, - 0)); - momNode.rotateDeg(layer->getProps().rotation, glm::vec3(0, 0, 1)); - transformOrigin = glm::vec4(layer->getProps().x, - layer->getProps().y - ascender, - 0, 1); - momNode.move(transformOrigin); - break; - } - - case Layer::TransformOrigin::TOP_RIGHT: { - float moveX = bb.p1.x - bb.p0.x; - node.move(glm::vec3(-moveX, - ascender, - 0)); - momNode.rotateDeg(layer->getProps().rotation, glm::vec3(0, 0, 1)); - transformOrigin = glm::vec4(layer->getProps().x + moveX, - layer->getProps().y - ascender, - 0, 1); - momNode.move(transformOrigin); - break; - } - - case Layer::TransformOrigin::CENTER: { - float moveX = (bb.p2.x - bb.p0.x) * 0.5 + bb.p0.x; - float moveY = (bb.p2.y - bb.p0.y) * 0.5 - bb.p2.y; - node.move(glm::vec3(-moveX, - moveY, - 0)); - momNode.rotateDeg(layer->getProps().rotation, glm::vec3(0, 0, 1)); - transformOrigin = glm::vec4(layer->getProps().x + moveX, - layer->getProps().y - moveY, - 0, 1); - momNode.move(transformOrigin); - break; - } - - case Layer::TransformOrigin::BOTTOM_LEFT: { - float moveY = ascender - advanceY; - node.move(glm::vec3(0, - moveY, - 0)); - momNode.rotateDeg(layer->getProps().rotation, glm::vec3(0, 0, 1)); - transformOrigin = glm::vec4(layer->getProps().x, - layer->getProps().y - moveY, - 0, 1); - momNode.move(transformOrigin); - break; - } - - case Layer::TransformOrigin::BOTTOM_RIGHT: { - float moveY = ascender - (advanceY); - float moveX = (bb.p2.x - bb.p0.x); - node.move(glm::vec3(-moveX, - moveY, - 0)); - momNode.rotateDeg(layer->getProps().rotation, glm::vec3(0, 0, 1)); - transformOrigin = glm::vec4(layer->getProps().x + moveX, - layer->getProps().y - moveY, - 0, 1); - momNode.move(transformOrigin); - break; - } - - } - bb.multiply(node.getGlobalTransformMatrix()); + getAndApplyTransformOrigin(transformOrigin, + layer->getOuterNode(), + layer->getInnerNode(), + bb, + ascender, + advanceY, + layer->getProps()); + bb.multiply(layer->getInnerNode().getGlobalTransformMatrix()); ofVboMesh mesh; mesh.addVertices({bb.p0, bb.p1, bb.p2, bb.p3, glm::vec4(layer->getProps().x, layer->getProps().y, 0, 1), @@ -313,7 +249,7 @@ void GPUFontAtlasLayerCombo::draw(){ }); outerBoundingBoxes.push_back(mesh); - font->collectVerticesAndIndices(node, + font->collectVerticesAndIndices(layer->getInnerNode(), bbs, vertices, indices, @@ -350,4 +286,74 @@ void GPUFontAtlasLayerCombo::draw(){ , 20, 20); } +void GPUFontAtlasLayerCombo::getAndApplyTransformOrigin(glm::vec4 & transformOrigin, + ofNode & momNode, + ofNode & node, + const ofxGPUFont::Font::BoundingBox & bb, + const float ascender, + const float advanceY, + const Layer::Props & props){ + switch(props.transformOrigin){ + default: + case Layer::TransformOrigin::TOP_LEFT: { + node.setPosition(glm::vec3(0, + ascender, + 0)); + transformOrigin = glm::vec4(props.x, + props.y - ascender, + 0, 1); + break; + } + + case Layer::TransformOrigin::TOP_RIGHT: { + float moveX = bb.p1.x - bb.p0.x; + node.setPosition(glm::vec3(-moveX, + ascender, + 0)); + transformOrigin = glm::vec4(props.x + moveX, + props.y - ascender, + 0, 1); + break; + } + + case Layer::TransformOrigin::CENTER: { + float moveX = (bb.p2.x - bb.p0.x) * 0.5 + bb.p0.x; + float moveY = (bb.p2.y - bb.p0.y) * 0.5 - bb.p2.y; + node.setPosition(glm::vec3(-moveX, + moveY, + 0)); + transformOrigin = glm::vec4(props.x + moveX, + props.y - moveY, + 0, 1); + break; + } + + case Layer::TransformOrigin::BOTTOM_LEFT: { + float moveY = ascender - advanceY; + node.setPosition(glm::vec3(0, + moveY, + 0)); + transformOrigin = glm::vec4(props.x, + props.y - moveY, + 0, 1); + break; + } + + case Layer::TransformOrigin::BOTTOM_RIGHT: { + float moveY = ascender - (advanceY); + float moveX = (bb.p2.x - bb.p0.x); + node.setPosition(glm::vec3(-moveX, + moveY, + 0)); + transformOrigin = glm::vec4(props.x + moveX, + props.y - moveY, + 0, 1); + break; + } + } + momNode.setOrientation(glm::quat(1, 0, 0, 0)); + momNode.rotateDeg(props.rotation, glm::vec3(0, 0, 1)); + momNode.setPosition(transformOrigin); +} + } diff --git a/src/GPUFontAtlasLayerCombo.h b/src/GPUFontAtlasLayerCombo.h index 16b6d36..71b37ac 100644 --- a/src/GPUFontAtlasLayerCombo.h +++ b/src/GPUFontAtlasLayerCombo.h @@ -10,6 +10,7 @@ #endif namespace ofxVariableLab { + struct GPUFontAtlasLayerComboSettings : public AtlasLayerComboSettings { int gpuTextureOffset = 0; }; @@ -55,6 +56,13 @@ class GPUFontAtlasLayerCombo : public AtlasLayerCombo { return glm::lookAt(glm::vec3(0, 0, distance), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0)) * glm::mat4(rotation) * translation; } }; + void getAndApplyTransformOrigin(glm::vec4 & transformOrigin, + ofNode & momNode, + ofNode & node, + const ofxGPUFont::Font::BoundingBox & bb, + const float ascender, + const float advanceY, + const Layer::Props & props); public: void setup(const ComboIdentifier & identifier, AtlasLayerComboSettings settings = GPUFontAtlasLayerComboSettings()) override; diff --git a/src/GPUFontLayer.cpp b/src/GPUFontLayer.cpp index cadb983..8dda300 100644 --- a/src/GPUFontLayer.cpp +++ b/src/GPUFontLayer.cpp @@ -15,6 +15,7 @@ void GPUFontLayer::setup(const LayerSettings & settings){ id = "layer-" + ofToString(Layer::n_layers); n_layers++; } + innerNode.setParent(outerNode); } void GPUFontLayer::update(){ @@ -99,6 +100,12 @@ const Layer::Props & GPUFontLayer::getProps() const { const glm::vec4 & GPUFontLayer::getColor() const { return color; } +ofNode & GPUFontLayer::getOuterNode(){ + return outerNode; +} +ofNode & GPUFontLayer::getInnerNode(){ + return innerNode; +} void GPUFontLayer::clearPropsBuffer(){ propsBuffer.clear(); } diff --git a/src/GPUFontLayer.h b/src/GPUFontLayer.h index 8ca88a7..3739f7f 100644 --- a/src/GPUFontLayer.h +++ b/src/GPUFontLayer.h @@ -43,6 +43,8 @@ class GPUFontLayer : public Layer { void isWithNewMom() override; const ComboIdentifier & getMomsComboIdentifier() const override; void setMomsComboIdentifier(const ComboIdentifier & identifier) override; + ofNode & getOuterNode() override; + ofNode & getInnerNode() override; const vector & getVariationText(); @@ -65,5 +67,7 @@ class GPUFontLayer : public Layer { bool notHappyWithMom = false; LayerType type = GPUFONT; glm::vec4 color; + ofNode outerNode; + ofNode innerNode; }; } diff --git a/src/Layer.h b/src/Layer.h index 638fda3..12fb1df 100644 --- a/src/Layer.h +++ b/src/Layer.h @@ -81,6 +81,8 @@ class Layer { virtual void isWithNewMom() = 0; virtual const ComboIdentifier & getMomsComboIdentifier() const = 0; virtual void setMomsComboIdentifier(const ComboIdentifier & identifier) = 0; + virtual ofNode & getOuterNode() = 0; + virtual ofNode & getInnerNode() = 0; static int n_layers; }; } diff --git a/src/MsdfLayer.cpp b/src/MsdfLayer.cpp index 0c8c272..ea6b863 100644 --- a/src/MsdfLayer.cpp +++ b/src/MsdfLayer.cpp @@ -13,6 +13,7 @@ void MsdfLayer::setup(const LayerSettings & settings){ id = "layer-" + ofToString(Layer::n_layers); n_layers++; } + innerNode.setParent(outerNode); } void MsdfLayer::update(){ @@ -298,6 +299,12 @@ const ComboIdentifier & MsdfLayer::getMomsComboIdentifier() const { void MsdfLayer::setMomsComboIdentifier(const ComboIdentifier & identifier){ momsComboIdentifier = identifier; } +ofNode & MsdfLayer::getOuterNode(){ + return outerNode; +} +ofNode & MsdfLayer::getInnerNode(){ + return innerNode; +} void MsdfLayer::setAtlas(shared_ptr _atlas){ atlas = _atlas; // TODO: this does not seem proper diff --git a/src/MsdfLayer.h b/src/MsdfLayer.h index 012e9f9..a25a29e 100644 --- a/src/MsdfLayer.h +++ b/src/MsdfLayer.h @@ -36,6 +36,8 @@ class MsdfLayer : public Layer { void isWithNewMom() override; const ComboIdentifier & getMomsComboIdentifier() const override; void setMomsComboIdentifier(const ComboIdentifier & identifier) override; + ofNode & getOuterNode() override; + ofNode & getInnerNode() override; void setAtlas(shared_ptr _atlas); shared_ptr getAtlas() const; @@ -56,5 +58,7 @@ class MsdfLayer : public Layer { ComboIdentifier momsComboIdentifier; LayerType type = MSDFGEN; glm::vec4 color; + ofNode outerNode; + ofNode innerNode; }; }