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 9655bb8..e78a81a 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); }