Auto-merging ArchitectureColoredPainting/Model.cpp

dev-VirtualTexture
wuyize 2022-08-07 21:31:15 +08:00
commit 5db300520b
7 changed files with 132 additions and 11 deletions

View File

@ -85,6 +85,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="BvhTree.cpp" />
<ClCompile Include="Camera.cpp" />
<ClCompile Include="Mesh.cpp" />
<ClCompile Include="Model.cpp" />
@ -110,6 +111,7 @@
<None Include="Shaders\shader.vert" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="BvhTree.h" />
<ClInclude Include="Camera.h" />
<ClInclude Include="Drawable.h" />
<ClInclude Include="Mesh.h" />

View File

@ -58,6 +58,9 @@
<ClCompile Include="PaintingMesh.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="BvhTree.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<QtMoc Include="RendererWidget.h">
@ -106,5 +109,8 @@
<ClInclude Include="Drawable.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="BvhTree.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -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<GLuint>& children, std::vector<QVector4D>& 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<GLuint>& children, std::vector<QVector4D>& bounds) {
children.push_back(tot);
children.push_back(0);
traverseBvhTree(root, children, bounds);
}

View File

@ -0,0 +1,60 @@
#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 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<GLuint>& children, std::vector<QVector4D>& bounds);
// 获得 Bvh rootBvh部分的 children 数组,包围盒数组 vector 传输
void getBvhArray(std::vector<GLuint>& children, std::vector<QVector4D>& bounds);
GLuint getBvhNodeNum();
};

View File

@ -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)
{

View File

@ -33,5 +33,4 @@ private:
//加载材质纹理
QVector<Texture*> loadMaterialTextures(aiMaterial* mat, aiTextureType type, QString typeName);
};
};

View File

@ -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: