From e1dc0e4065875cb6a814085f251830252cb97339 Mon Sep 17 00:00:00 2001 From: "yang.yongquan" <3395816735@qq.com> Date: Thu, 9 Feb 2023 23:30:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0BvhTree=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=88=E6=97=8B=E8=BD=AC=E5=90=8E=E5=8C=85=E5=9B=B4=E7=9B=92?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Renderer/Painting/BvhTree.cpp | 12 +++++++----- .../src/Renderer/Painting/BvhTree.h | 15 +++++++++++++-- .../src/Renderer/Painting/Painting.cpp | 2 +- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/ArchitectureColoredPainting/src/Renderer/Painting/BvhTree.cpp b/ArchitectureColoredPainting/src/Renderer/Painting/BvhTree.cpp index f69c3f0..e5760d6 100644 --- a/ArchitectureColoredPainting/src/Renderer/Painting/BvhTree.cpp +++ b/ArchitectureColoredPainting/src/Renderer/Painting/BvhTree.cpp @@ -4,8 +4,8 @@ GLuint BvhTree::getBvhNodeNum() { return tot; } -void BvhTree::buildBvhTree(BvhTreeData initBound[], int len) { - tot = 0; +void BvhTree::buildBvhTree(BvhTreeData initBound[], int len, GLuint transformParam) { + tot = 0; transform = transformParam; root = subBvhTree(initBound, 0, len-1); } @@ -19,9 +19,9 @@ QVector4D BvhTree::Union(QVector4D a, QVector4D b) { } QVector4D BvhTree::calcBound(BvhTreeData initBound[], int l, int r) { - QVector4D res = initBound[l].bound; + QVector4D res = initBound[l].boundWithRotation(); for (int i = l + 1; i <= r; i++) { - res = Union(res, initBound[i].bound); + res = Union(res, initBound[i].boundWithRotation()); } return res; } @@ -30,7 +30,7 @@ BvhPtr BvhTree::subBvhTree(BvhTreeData initBound[], int l, int r) { if (l > r) return NULL; BvhPtr p(new BvhNode()); p->lab = tot++; - p->bound = calcBound(initBound, l, r); + //p->bound = calcBound(initBound, l, r); if (l == r) { p->isLeaf = true; BvhPtr lp(new BvhNode()); @@ -39,6 +39,7 @@ BvhPtr BvhTree::subBvhTree(BvhTreeData initBound[], int l, int r) { BvhPtr rp(new BvhNode()); rp->lab = initBound[r].rightSon; p->child[1] = rp; + p->bound = calcBound(initBound, l, r); return p; } int dim = p->maximumBound(); @@ -50,6 +51,7 @@ BvhPtr BvhTree::subBvhTree(BvhTreeData initBound[], int l, int r) { int mid = (l + r) >> 1; p->child[0] = subBvhTree(initBound, l, mid); p->child[1] = subBvhTree(initBound, mid+1, r); + p->bound = Union(p->child[0]->bound, p->child[1]->bound); return p; } diff --git a/ArchitectureColoredPainting/src/Renderer/Painting/BvhTree.h b/ArchitectureColoredPainting/src/Renderer/Painting/BvhTree.h index 0dfc795..2748327 100644 --- a/ArchitectureColoredPainting/src/Renderer/Painting/BvhTree.h +++ b/ArchitectureColoredPainting/src/Renderer/Painting/BvhTree.h @@ -20,6 +20,17 @@ namespace Renderer bound.x(), bound.y(), bound.z(), bound.w(), leftSon, rightSon); } + QVector4D boundWithRotation() { + double angle = (rightSon & ((1 << 16) - 1)) * acos(-1); + double px = bound.x() * cos(angle) + bound.y() * sin(angle), py = bound.y() * cos(angle) - bound.x() * sin(angle); + double qx = bound.z() * cos(angle) + bound.w() * sin(angle), qy = bound.w() * cos(angle) - bound.z() * sin(angle); + return QVector4D( + std::min(px, qx), + std::min(py, qy), + std::max(px, qx), + std::max(py, qy) + ); + } BvhTreeData(QVector4D bound, GLuint leftSon, GLuint rightSon) : bound(bound), leftSon(leftSon), rightSon(rightSon) {} BvhTreeData() @@ -67,7 +78,7 @@ namespace Renderer { private: - GLuint tot; + GLuint tot, transform; BvhPtr root; static QVector4D calcBound(BvhTreeData initBound[], int l, int r); static QVector4D Union(QVector4D a, QVector4D b); @@ -79,7 +90,7 @@ namespace Renderer root = NULL; } // 根据底层包围盒生成bvh树 - void buildBvhTree(BvhTreeData initBound[], int len); + void buildBvhTree(BvhTreeData initBound[], int len, GLuint transformParam = 0); // 获得 Bvh (rootBvh部分)的 children 数组,包围盒数组 vector 传输 void getBvhArray(std::vector& children, std::vector& bounds); // 获得 BvhTree 中节点总数 diff --git a/ArchitectureColoredPainting/src/Renderer/Painting/Painting.cpp b/ArchitectureColoredPainting/src/Renderer/Painting/Painting.cpp index f1b8337..66f7510 100644 --- a/ArchitectureColoredPainting/src/Renderer/Painting/Painting.cpp +++ b/ArchitectureColoredPainting/src/Renderer/Painting/Painting.cpp @@ -100,7 +100,7 @@ GLuint encodeZIndexRotation(GLuint zIndex, float rotation) BvhTreeData Painting::encodeElementLeaf(ElementWithTransform e) { QVector4D bound(e.transform.bound.x, e.transform.bound.y, e.transform.bound.z, e.transform.bound.w); - GLuint rightSon = GLuint(glm::mod(e.transform.rotation, 360.f) * (1 << 16)) + GLuint rightSon = GLuint(glm::mod(e.transform.rotation, 360.f)/360.f * (1 << 16)) + (e.transform.flip.x << 16) + (e.transform.flip.y << 17) + (e.transform.zIndex << 18); return BvhTreeData(bound, elementPool[e.element], rightSon); }