Improved DrawVisitor to accept list of nodes

This commit is contained in:
Bruno
2021-01-24 17:16:28 +01:00
parent edeec9568e
commit b17136d23a
2 changed files with 25 additions and 13 deletions

View File

@@ -1,3 +1,4 @@
#include <algorithm>
#include <glm/gtc/matrix_transform.hpp>
@@ -8,14 +9,21 @@
DrawVisitor::DrawVisitor(Node *nodetodraw, glm::mat4 projection, bool force): force_(force)
{
target_ = nodetodraw;
targets_.push_back(nodetodraw);
modelview_ = glm::identity<glm::mat4>();
projection_ = projection;
done_ = false;
num_duplicat_ = 1;
transform_duplicat_ = glm::identity<glm::mat4>();
}
DrawVisitor::DrawVisitor(std::vector<Node *> nodestodraw, glm::mat4 projection, bool force): force_(force)
{
targets_ = nodestodraw;
modelview_ = glm::identity<glm::mat4>();
projection_ = projection;
num_duplicat_ = 1;
transform_duplicat_ = glm::identity<glm::mat4>();
}
void DrawVisitor::loop(int num, glm::mat4 transform)
{
@@ -30,22 +38,25 @@ void DrawVisitor::visit(Node &n)
if (force_)
n.visible_ = true;
// draw the target
if ( target_ && n.id() == target_->id()) {
// find the node with this id
std::vector<Node *>::iterator it = std::find_if(targets_.begin(), targets_.end(), hasId(n.id()));
// found this node in the list of targets: draw it
if (it != targets_.end()) {
targets_.erase(it);
for (int i = 0; i < num_duplicat_; ++i) {
// draw multiple copies if requested
n.draw(modelview_, projection_);
modelview_ *= transform_duplicat_;
}
done_ = true;
}
// restore visibility
n.visible_ = v;
if (done_) return;
if (targets_.empty()) return;
// update transform
modelview_ *= n.transform_;
@@ -55,11 +66,11 @@ void DrawVisitor::visit(Node &n)
void DrawVisitor::visit(Group &n)
{
// no need to traverse deeper if this node was drawn already
if (done_) return;
if (targets_.empty()) return;
// traverse children
glm::mat4 mv = modelview_;
for (NodeSet::iterator node = n.begin(); !done_ && node != n.end(); node++) {
for (NodeSet::iterator node = n.begin(); !targets_.empty() && node != n.end(); node++) {
if ( (*node)->visible_ || force_)
(*node)->accept(*this);
modelview_ = mv;
@@ -68,7 +79,6 @@ void DrawVisitor::visit(Group &n)
void DrawVisitor::visit(Scene &n)
{
done_ = false;
modelview_ = glm::identity<glm::mat4>();
n.root()->accept(*this);
}
@@ -76,7 +86,7 @@ void DrawVisitor::visit(Scene &n)
void DrawVisitor::visit(Switch &n)
{
// no need to traverse deeper if this node was drawn already
if (done_) return;
if (targets_.empty()) return;
// traverse acive child
glm::mat4 mv = modelview_;