multiple nodes for collect/drawing

This commit is contained in:
jrkb 2023-06-26 08:43:45 +02:00
parent 01740f7635
commit 106db1462c
2 changed files with 55 additions and 65 deletions

View file

@ -314,15 +314,13 @@ void MsdfLayer::setBoundingBox(const Layer::BoundingBox & boundingBox){
this->boundingBox = boundingBox; this->boundingBox = boundingBox;
} }
bool MsdfLayer::collectCharacter(const char character, bool MsdfLayer::collectCharacter(const char character,
const ofNode & node, vector <ofNode> & nodes,
const ofCamera & camera, const ofCamera & camera,
glm::vec4 color, glm::vec4 color,
ofxVariableLab::FontVariation fontVariation){ ofxVariableLab::FontVariation fontVariation){
OFX_PROFILER_FUNCTION() OFX_PROFILER_FUNCTION()
const ofImage & atlasImage = atlas->getAtlasImage(); const ofImage & atlasImage = atlas->getAtlasImage();
ofNode n = node;
n.setScale(n.getScale() * scaleFactor);
int atlas_w = atlasImage.getWidth(); int atlas_w = atlasImage.getWidth();
int atlas_h = atlasImage.getHeight(); 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 w = glm::mix(float(w_a), float(w_b), mix);
float h = glm::mix(float(h_a), float(h_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::vec2 translation_a = glm::vec2(float(x_a) / float(atlas_w),
glm::vec4 p1 = n.getGlobalTransformMatrix() * glm::vec4(0, h, 0, 1); float(y_a) / float(atlas_h));
glm::vec4 p2 = n.getGlobalTransformMatrix() * glm::vec4(w, h, 0, 1); glm::vec2 scale_a = glm::vec2(float(w_a) / float(atlas_w),
glm::vec4 p3 = n.getGlobalTransformMatrix() * glm::vec4(w, 0, 0, 1); 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) glm::vec2 uv0_a = translation_a;
|| isInside(camera, p1) glm::vec2 uv0_b = translation_b;
|| isInside(camera, p2) glm::vec2 uv1_a = glm::vec2(0, scale_a.y) + translation_a;
|| isInside(camera, p3)){ glm::vec2 uv1_b = glm::vec2(0, scale_b.y) + translation_b;
OFX_PROFILER_SCOPE("prepare vertices / indices"); 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), for(int i = 0; i < nodes.size(); i++){
float(y_a) / float(atlas_h)); ofNode & node = nodes[i];
glm::vec2 scale_a = glm::vec2(float(w_a) / float(atlas_w), glm::vec4 p0 = node.getGlobalTransformMatrix() * glm::vec4(0, 0, 0, 1);
float(h_a) / float(atlas_h)); glm::vec4 p1 = node.getGlobalTransformMatrix() * glm::vec4(0, h, 0, 1);
glm::vec2 translation_b = glm::vec2(float(x_b) / float(atlas_w), glm::vec4 p2 = node.getGlobalTransformMatrix() * glm::vec4(w, h, 0, 1);
float(y_b) / float(atlas_h)); glm::vec4 p3 = node.getGlobalTransformMatrix() * glm::vec4(w, 0, 0, 1);
glm::vec2 scale_b = glm::vec2(float(w_b) / float(atlas_w),
float(h_b) / float(atlas_h));
glm::vec2 uv0_a = translation_a; int32_t base = static_cast <int32_t>(vertices.size());
glm::vec2 uv0_b = translation_b; vertices.push_back(BufferVertex{
glm::vec2 uv1_a = glm::vec2(0, scale_a.y) + translation_a; {p0.x, p0.y, p0.z, p0.w},
glm::vec2 uv1_b = glm::vec2(0, scale_b.y) + translation_b; {uv0_a.x, uv0_a.y},
glm::vec2 uv2_a = scale_a + translation_a; {uv0_b.x, uv0_b.y},
glm::vec2 uv2_b = scale_b + translation_b; {color.r, color.g, color.b, color.a},
glm::vec2 uv3_a = glm::vec2(scale_a.x, 0) + translation_a; mix
glm::vec2 uv3_b = glm::vec2(scale_b.x, 0) + translation_b; });
for(int i = 0; i < 1; i++){ vertices.push_back(BufferVertex{
float distance = 200; {p1.x, p1.y, p1.z, p1.w},
int32_t base = static_cast <int32_t>(vertices.size()); {uv1_a.x, uv1_a.y},
vertices.push_back(BufferVertex{ {uv1_b.x, uv1_b.y},
{p0.x, p0.y + i * distance, p0.z, p0.w}, {color.r, color.g, color.b, color.a},
{uv0_a.x, uv0_a.y}, mix
{uv0_b.x, uv0_b.y}, });
{color.r, color.g, color.b, color.a}, vertices.push_back(BufferVertex{
mix {p2.x, p2.y, p2.z, p2.w},
}); {uv2_a.x, uv2_a.y},
vertices.push_back(BufferVertex{ {uv2_b.x, uv2_b.y},
{p1.x, p1.y + i * distance, p1.z, p1.w}, {color.r, color.g, color.b, color.a},
{uv1_a.x, uv1_a.y}, mix
{uv1_b.x, uv1_b.y}, });
{color.r, color.g, color.b, color.a}, vertices.push_back(BufferVertex{
mix {p3.x, p3.y, p3.z, p3.w},
}); {uv3_a.x, uv3_a.y},
vertices.push_back(BufferVertex{ {uv3_b.x, uv3_b.y},
{p2.x, p2.y + i * distance, p2.z, p2.w}, {color.r, color.g, color.b, color.a},
{uv2_a.x, uv2_a.y}, mix
{uv2_b.x, uv2_b.y}, });
{color.r, color.g, color.b, color.a}, indices.insert(indices.end(), {base, base + 1, base + 2, base + 2, base + 3, base});
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});
}
nElements++; nElements++;
return true;
}else{
return false;
} }
return true;
} }
void MsdfLayer::setAtlas(shared_ptr <ofxMsdfgen::Atlas> _atlas){ void MsdfLayer::setAtlas(shared_ptr <ofxMsdfgen::Atlas> _atlas){
atlas = _atlas; atlas = _atlas;

View file

@ -57,7 +57,7 @@ class MsdfLayer : public Layer {
const Layer::BoundingBox & getBoundingBox() override; const Layer::BoundingBox & getBoundingBox() override;
void setBoundingBox(const Layer::BoundingBox & boundingBox) override; void setBoundingBox(const Layer::BoundingBox & boundingBox) override;
bool collectCharacter(const char character, bool collectCharacter(const char character,
const ofNode & node, vector <ofNode> & nodes,
const ofCamera & camera, const ofCamera & camera,
glm::vec4 color = glm::vec4(1, 1, 1, 1), glm::vec4 color = glm::vec4(1, 1, 1, 1),
ofxVariableLab::FontVariation fontVariation = ofxVariableLab::FontVariation()); ofxVariableLab::FontVariation fontVariation = ofxVariableLab::FontVariation());