wuyize 2023-02-10 14:57:00 +08:00
commit 23f14cb764
3 changed files with 21 additions and 8 deletions

View File

@ -4,8 +4,8 @@ GLuint BvhTree::getBvhNodeNum() {
return tot; return tot;
} }
void BvhTree::buildBvhTree(BvhTreeData initBound[], int len) { void BvhTree::buildBvhTree(BvhTreeData initBound[], int len, GLuint transformParam) {
tot = 0; tot = 0; transform = transformParam;
root = subBvhTree(initBound, 0, len-1); 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 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++) { for (int i = l + 1; i <= r; i++) {
res = Union(res, initBound[i].bound); res = Union(res, initBound[i].boundWithRotation());
} }
return res; return res;
} }
@ -30,7 +30,7 @@ BvhPtr BvhTree::subBvhTree(BvhTreeData initBound[], int l, int r) {
if (l > r) return NULL; if (l > r) return NULL;
BvhPtr p(new BvhNode()); BvhPtr p(new BvhNode());
p->lab = tot++; p->lab = tot++;
p->bound = calcBound(initBound, l, r); //p->bound = calcBound(initBound, l, r);
if (l == r) { if (l == r) {
p->isLeaf = true; p->isLeaf = true;
BvhPtr lp(new BvhNode()); BvhPtr lp(new BvhNode());
@ -39,6 +39,7 @@ BvhPtr BvhTree::subBvhTree(BvhTreeData initBound[], int l, int r) {
BvhPtr rp(new BvhNode()); BvhPtr rp(new BvhNode());
rp->lab = initBound[r].rightSon; rp->lab = initBound[r].rightSon;
p->child[1] = rp; p->child[1] = rp;
p->bound = calcBound(initBound, l, r);
return p; return p;
} }
int dim = p->maximumBound(); int dim = p->maximumBound();
@ -50,6 +51,7 @@ BvhPtr BvhTree::subBvhTree(BvhTreeData initBound[], int l, int r) {
int mid = (l + r) >> 1; int mid = (l + r) >> 1;
p->child[0] = subBvhTree(initBound, l, mid); p->child[0] = subBvhTree(initBound, l, mid);
p->child[1] = subBvhTree(initBound, mid+1, r); p->child[1] = subBvhTree(initBound, mid+1, r);
p->bound = Union(p->child[0]->bound, p->child[1]->bound);
return p; return p;
} }

View File

@ -20,6 +20,17 @@ namespace Renderer
bound.x(), bound.y(), bound.z(), bound.w(), bound.x(), bound.y(), bound.z(), bound.w(),
leftSon, rightSon); 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) BvhTreeData(QVector4D bound, GLuint leftSon, GLuint rightSon)
: bound(bound), leftSon(leftSon), rightSon(rightSon) {} : bound(bound), leftSon(leftSon), rightSon(rightSon) {}
BvhTreeData() BvhTreeData()
@ -67,7 +78,7 @@ namespace Renderer
{ {
private: private:
GLuint tot; GLuint tot, transform;
BvhPtr root; BvhPtr root;
static QVector4D calcBound(BvhTreeData initBound[], int l, int r); static QVector4D calcBound(BvhTreeData initBound[], int l, int r);
static QVector4D Union(QVector4D a, QVector4D b); static QVector4D Union(QVector4D a, QVector4D b);
@ -79,7 +90,7 @@ namespace Renderer
root = NULL; root = NULL;
} }
// 根据底层包围盒生成bvh树 // 根据底层包围盒生成bvh树
void buildBvhTree(BvhTreeData initBound[], int len); void buildBvhTree(BvhTreeData initBound[], int len, GLuint transformParam = 0);
// 获得 Bvh rootBvh部分的 children 数组,包围盒数组 vector 传输 // 获得 Bvh rootBvh部分的 children 数组,包围盒数组 vector 传输
void getBvhArray(std::vector<GLuint>& children, std::vector<QVector4D>& bounds); void getBvhArray(std::vector<GLuint>& children, std::vector<QVector4D>& bounds);
// 获得 BvhTree 中节点总数 // 获得 BvhTree 中节点总数

View File

@ -100,7 +100,7 @@ GLuint encodeZIndexRotation(GLuint zIndex, float rotation)
BvhTreeData Painting::encodeElementLeaf(ElementWithTransform e) BvhTreeData Painting::encodeElementLeaf(ElementWithTransform e)
{ {
QVector4D bound(e.transform.bound.x, e.transform.bound.y, e.transform.bound.z, e.transform.bound.w); 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); + (e.transform.flip.x << 16) + (e.transform.flip.y << 17) + (e.transform.zIndex << 18);
return BvhTreeData(bound, elementPool[e.element], rightSon); return BvhTreeData(bound, elementPool[e.element], rightSon);
} }