From b0a9ec3ae19c6abc91775b3db8de033403c1027f Mon Sep 17 00:00:00 2001 From: "yang.yongquan" <3395816735@qq.com> Date: Sun, 7 Aug 2022 18:46:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=99=A4=E4=BA=86=E5=BA=95?= =?UTF-8?q?=E5=B1=82=E5=8C=85=E5=9B=B4=E7=9B=92=E7=9A=84BvhTree=E7=94=9F?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ArchitectureColoredPainting.vcxproj | 2 + ...rchitectureColoredPainting.vcxproj.filters | 6 ++ ArchitectureColoredPainting/BvhTree.cpp | 63 +++++++++++++++++++ ArchitectureColoredPainting/BvhTree.h | 60 ++++++++++++++++++ ArchitectureColoredPainting/Model.cpp | 1 - ArchitectureColoredPainting/Model.h | 3 +- ArchitectureColoredPainting/PaintingMesh.h | 8 --- 7 files changed, 132 insertions(+), 11 deletions(-) create mode 100644 ArchitectureColoredPainting/BvhTree.cpp create mode 100644 ArchitectureColoredPainting/BvhTree.h diff --git a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj index b04115d..e12aa09 100644 --- a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj +++ b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj @@ -85,6 +85,7 @@ + @@ -110,6 +111,7 @@ + diff --git a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters index 2cc8f3c..ac95887 100644 --- a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters +++ b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters @@ -58,6 +58,9 @@ Source Files + + Source Files + @@ -106,5 +109,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/ArchitectureColoredPainting/BvhTree.cpp b/ArchitectureColoredPainting/BvhTree.cpp new file mode 100644 index 0000000..13ad178 --- /dev/null +++ b/ArchitectureColoredPainting/BvhTree.cpp @@ -0,0 +1,63 @@ +#include "BvhTree.h" + +GLuint BvhTree::getBvhNodeNum() { + return tot; +} + +void BvhTree::buildBvhTree(QVector4D initBound[], int len) { + tot = 0; + root = subBvhTree(initBound, 0, len-1); +} + +QVector4D BvhTree::Union(QVector4D a, QVector4D b) { + return QVector4D( + std::min(a.x(), b.x()), + std::min(a.y(), b.y()), + std::max(a.z(), b.z()), + std::max(a.w(), b.w()) + ); +} + +QVector4D BvhTree::calcBound(QVector4D initBound[], int l, int r) { + QVector4D res = initBound[l]; + for (int i = l + 1; i < r; i++) { + res = Union(res, initBound[i]); + } + return res; +} + +bvhPtr BvhTree::subBvhTree(QVector4D initBound[], int l, int r) { + if (l > r) return NULL; + bvhPtr p(new BvhNode()); + p.get()->lab = tot++; + p.get()->bound = calcBound(initBound, l, r); + if (l == r) return p; + int dim = p.get()->maximumBound(); + if (dim == 0) { + std::sort(initBound + l, initBound + r, BvhNode::x_compare); + } else { + std::sort(initBound + l, initBound + r, BvhNode::y_compare); + } + int mid = (l + r) >> 1; + p.get()->child[0] = subBvhTree(initBound, l, mid); + p.get()->child[1] = subBvhTree(initBound, mid+1, r); + return p; +} + +void BvhTree::traverseBvhTree(bvhPtr now, std::vector& children, std::vector& bounds) { + if (now == NULL) return ; + if(now.get()->getSon(0) || now.get()->getSon(1)) { + children.push_back(now.get()->getSon(0)); + children.push_back(now.get()->getSon(1)); + } + bounds.push_back(now.get()->bound); + traverseBvhTree(now.get()->child[0], children, bounds); + traverseBvhTree(now.get()->child[1], children, bounds); + +} + +void BvhTree::getBvhArray(std::vector& children, std::vector& bounds) { + children.push_back(tot); + children.push_back(0); + traverseBvhTree(root, children, bounds); +} \ No newline at end of file diff --git a/ArchitectureColoredPainting/BvhTree.h b/ArchitectureColoredPainting/BvhTree.h new file mode 100644 index 0000000..1ccc0c6 --- /dev/null +++ b/ArchitectureColoredPainting/BvhTree.h @@ -0,0 +1,60 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define bvhPtr std::tr1::shared_ptr + +// BvhTree 节点 +struct BvhNode { + GLuint lab; + QVector4D bound; + bvhPtr child[2]; + static bool x_compare(QVector4D a, QVector4D b) { + return a.x() < b.x(); + } + static bool y_compare(QVector4D a, QVector4D b) { + return a.y() < b.y(); + } + GLuint getSon(int k) { + if (k >= 2) return 0; + if (child[k] == NULL) + return 0; + return child[k].get()->lab; + } + BvhNode() { + child[0] = child[1] = NULL; + } + int maximumBound() { + return (std::fabs(bound.x() - bound.w()) < std::fabs(bound.y() - bound.z())); + } + ~BvhNode() {} +}; + +class BvhTree +{ +private: + GLuint tot; + bvhPtr root; + static QVector4D calcBound(QVector4D initBound[], int l, int r); + static QVector4D Union(QVector4D a, QVector4D b); +public: + BvhTree() { + tot = 0; + root = NULL; + } + // 根据底层包围盒生成bvh树 + void buildBvhTree(QVector4D initBound[], int len); + bvhPtr subBvhTree(QVector4D initBound[], int l, int r); + void traverseBvhTree(bvhPtr now, std::vector& children, std::vector& bounds); + // 获得 Bvh (rootBvh部分)的 children 数组,包围盒数组 vector 传输 + void getBvhArray(std::vector& children, std::vector& bounds); + GLuint getBvhNodeNum(); +}; + diff --git a/ArchitectureColoredPainting/Model.cpp b/ArchitectureColoredPainting/Model.cpp index 192b49b..9cd537b 100644 --- a/ArchitectureColoredPainting/Model.cpp +++ b/ArchitectureColoredPainting/Model.cpp @@ -79,7 +79,6 @@ Model* Model::createModel(QString path, QOpenGLContext* context, QOpenGLShaderPr return new Model(path, context, shaderProgram); } - void Model::processNode(aiNode* node, const aiScene* scene, aiMatrix4x4 mat4) { diff --git a/ArchitectureColoredPainting/Model.h b/ArchitectureColoredPainting/Model.h index 6914019..3206614 100644 --- a/ArchitectureColoredPainting/Model.h +++ b/ArchitectureColoredPainting/Model.h @@ -33,5 +33,4 @@ private: //加载材质纹理 QVector loadMaterialTextures(aiMaterial* mat, aiTextureType type, QString typeName); -}; - +}; \ No newline at end of file diff --git a/ArchitectureColoredPainting/PaintingMesh.h b/ArchitectureColoredPainting/PaintingMesh.h index 9d4b869..e5eebd2 100644 --- a/ArchitectureColoredPainting/PaintingMesh.h +++ b/ArchitectureColoredPainting/PaintingMesh.h @@ -24,14 +24,6 @@ struct PaintingVertex QVector3D Bitangent; }; -struct BvhNode { - GLuint leftChild; - GLuint rightChild; - GLuint padding[2];//与显存对齐 - QVector4D bound; - BvhNode(GLuint leftChild, GLuint rightChild, QVector4D bound) :leftChild(leftChild), rightChild(rightChild), bound(bound) {} -}; - class PaintingMesh : public Drawable { public: