68 lines
1.5 KiB
C++
68 lines
1.5 KiB
C++
#pragma once
|
||
|
||
#include <QOpenGLTexture>
|
||
#include <QOpenGLContext>
|
||
#include <QTextCodec>
|
||
#include <algorithm>
|
||
#include <QVector4D>
|
||
#include <iostream>
|
||
#include <memory>
|
||
#include <vector>
|
||
#include <cmath>
|
||
#define bvhPtr std::tr1::shared_ptr<BvhNode>
|
||
|
||
// 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 getLeftSon(int k) {
|
||
if (child[0] == NULL) {
|
||
return INT_MAX;
|
||
}
|
||
return child[0].get()->lab;
|
||
}
|
||
GLuint getRightSon(int k) {
|
||
if (child[1] == NULL) {
|
||
return 0;
|
||
}
|
||
return child[1].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<GLuint>& children, std::vector<QVector4D>& bounds);
|
||
// 获得 Bvh (rootBvh部分)的 children 数组,包围盒数组 vector 传输
|
||
void getBvhArray(std::vector<GLuint>& children, std::vector<QVector4D>& bounds);
|
||
// 获得 BvhTree 中节点总数
|
||
GLuint getBvhNodeNum();
|
||
};
|
||
|