From 106db1462cbe84162b1914280928f0982719416b Mon Sep 17 00:00:00 2001 From: themancalledjakob Date: Mon, 26 Jun 2023 08:43:45 +0200 Subject: [PATCH] multiple nodes for collect/drawing --- src/MsdfLayer.cpp | 118 +++++++++++++++++++++------------------------- src/MsdfLayer.h | 2 +- 2 files changed, 55 insertions(+), 65 deletions(-) diff --git a/src/MsdfLayer.cpp b/src/MsdfLayer.cpp index 517caef..8651ca9 100644 --- a/src/MsdfLayer.cpp +++ b/src/MsdfLayer.cpp @@ -314,15 +314,13 @@ void MsdfLayer::setBoundingBox(const Layer::BoundingBox & boundingBox){ this->boundingBox = boundingBox; } bool MsdfLayer::collectCharacter(const char character, - const ofNode & node, + vector & nodes, const ofCamera & camera, glm::vec4 color, ofxVariableLab::FontVariation fontVariation){ OFX_PROFILER_FUNCTION() const ofImage & atlasImage = atlas->getAtlasImage(); - ofNode n = node; - n.setScale(n.getScale() * scaleFactor); int atlas_w = atlasImage.getWidth(); int atlas_h = atlasImage.getHeight(); @@ -355,72 +353,64 @@ bool MsdfLayer::collectCharacter(const char character, float w = glm::mix(float(w_a), float(w_b), mix); float h = glm::mix(float(h_a), float(h_b), mix); - glm::vec4 p0 = n.getGlobalTransformMatrix() * glm::vec4(0, 0, 0, 1); - glm::vec4 p1 = n.getGlobalTransformMatrix() * glm::vec4(0, h, 0, 1); - glm::vec4 p2 = n.getGlobalTransformMatrix() * glm::vec4(w, h, 0, 1); - glm::vec4 p3 = n.getGlobalTransformMatrix() * glm::vec4(w, 0, 0, 1); + glm::vec2 translation_a = glm::vec2(float(x_a) / float(atlas_w), + float(y_a) / float(atlas_h)); + glm::vec2 scale_a = glm::vec2(float(w_a) / float(atlas_w), + float(h_a) / float(atlas_h)); + glm::vec2 translation_b = glm::vec2(float(x_b) / float(atlas_w), + float(y_b) / float(atlas_h)); + glm::vec2 scale_b = glm::vec2(float(w_b) / float(atlas_w), + float(h_b) / float(atlas_h)); - if(isInside(camera, p0) - || isInside(camera, p1) - || isInside(camera, p2) - || isInside(camera, p3)){ - OFX_PROFILER_SCOPE("prepare vertices / indices"); + glm::vec2 uv0_a = translation_a; + glm::vec2 uv0_b = translation_b; + glm::vec2 uv1_a = glm::vec2(0, scale_a.y) + translation_a; + glm::vec2 uv1_b = glm::vec2(0, scale_b.y) + translation_b; + glm::vec2 uv2_a = scale_a + translation_a; + glm::vec2 uv2_b = scale_b + translation_b; + glm::vec2 uv3_a = glm::vec2(scale_a.x, 0) + translation_a; + glm::vec2 uv3_b = glm::vec2(scale_b.x, 0) + translation_b; - glm::vec2 translation_a = glm::vec2(float(x_a) / float(atlas_w), - float(y_a) / float(atlas_h)); - glm::vec2 scale_a = glm::vec2(float(w_a) / float(atlas_w), - float(h_a) / float(atlas_h)); - glm::vec2 translation_b = glm::vec2(float(x_b) / float(atlas_w), - float(y_b) / float(atlas_h)); - glm::vec2 scale_b = glm::vec2(float(w_b) / float(atlas_w), - float(h_b) / float(atlas_h)); + for(int i = 0; i < nodes.size(); i++){ + ofNode & node = nodes[i]; + glm::vec4 p0 = node.getGlobalTransformMatrix() * glm::vec4(0, 0, 0, 1); + glm::vec4 p1 = node.getGlobalTransformMatrix() * glm::vec4(0, h, 0, 1); + glm::vec4 p2 = node.getGlobalTransformMatrix() * glm::vec4(w, h, 0, 1); + glm::vec4 p3 = node.getGlobalTransformMatrix() * glm::vec4(w, 0, 0, 1); - glm::vec2 uv0_a = translation_a; - glm::vec2 uv0_b = translation_b; - glm::vec2 uv1_a = glm::vec2(0, scale_a.y) + translation_a; - glm::vec2 uv1_b = glm::vec2(0, scale_b.y) + translation_b; - glm::vec2 uv2_a = scale_a + translation_a; - glm::vec2 uv2_b = scale_b + translation_b; - glm::vec2 uv3_a = glm::vec2(scale_a.x, 0) + translation_a; - glm::vec2 uv3_b = glm::vec2(scale_b.x, 0) + translation_b; - for(int i = 0; i < 1; i++){ - float distance = 200; - int32_t base = static_cast (vertices.size()); - vertices.push_back(BufferVertex{ - {p0.x, p0.y + i * distance, p0.z, p0.w}, - {uv0_a.x, uv0_a.y}, - {uv0_b.x, uv0_b.y}, - {color.r, color.g, color.b, color.a}, - mix - }); - vertices.push_back(BufferVertex{ - {p1.x, p1.y + i * distance, p1.z, p1.w}, - {uv1_a.x, uv1_a.y}, - {uv1_b.x, uv1_b.y}, - {color.r, color.g, color.b, color.a}, - mix - }); - vertices.push_back(BufferVertex{ - {p2.x, p2.y + i * distance, p2.z, p2.w}, - {uv2_a.x, uv2_a.y}, - {uv2_b.x, uv2_b.y}, - {color.r, color.g, color.b, color.a}, - mix - }); - vertices.push_back(BufferVertex{ - {p3.x, p3.y + i * distance, p3.z, p3.w}, - {uv3_a.x, uv3_a.y}, - {uv3_b.x, uv3_b.y}, - {color.r, color.g, color.b, color.a}, - mix - }); - indices.insert(indices.end(), {base, base + 1, base + 2, base + 2, base + 3, base}); - } + int32_t base = static_cast (vertices.size()); + vertices.push_back(BufferVertex{ + {p0.x, p0.y, p0.z, p0.w}, + {uv0_a.x, uv0_a.y}, + {uv0_b.x, uv0_b.y}, + {color.r, color.g, color.b, color.a}, + mix + }); + vertices.push_back(BufferVertex{ + {p1.x, p1.y, p1.z, p1.w}, + {uv1_a.x, uv1_a.y}, + {uv1_b.x, uv1_b.y}, + {color.r, color.g, color.b, color.a}, + mix + }); + vertices.push_back(BufferVertex{ + {p2.x, p2.y, p2.z, p2.w}, + {uv2_a.x, uv2_a.y}, + {uv2_b.x, uv2_b.y}, + {color.r, color.g, color.b, color.a}, + mix + }); + vertices.push_back(BufferVertex{ + {p3.x, p3.y, p3.z, p3.w}, + {uv3_a.x, uv3_a.y}, + {uv3_b.x, uv3_b.y}, + {color.r, color.g, color.b, color.a}, + mix + }); + indices.insert(indices.end(), {base, base + 1, base + 2, base + 2, base + 3, base}); nElements++; - return true; - }else{ - return false; } + return true; } void MsdfLayer::setAtlas(shared_ptr _atlas){ atlas = _atlas; diff --git a/src/MsdfLayer.h b/src/MsdfLayer.h index 197c70a..4482a87 100644 --- a/src/MsdfLayer.h +++ b/src/MsdfLayer.h @@ -57,7 +57,7 @@ class MsdfLayer : public Layer { const Layer::BoundingBox & getBoundingBox() override; void setBoundingBox(const Layer::BoundingBox & boundingBox) override; bool collectCharacter(const char character, - const ofNode & node, + vector & nodes, const ofCamera & camera, glm::vec4 color = glm::vec4(1, 1, 1, 1), ofxVariableLab::FontVariation fontVariation = ofxVariableLab::FontVariation());