From d905c91f282d76c44882109275e07256c19a4927 Mon Sep 17 00:00:00 2001 From: Peter Schaefer Date: Tue, 19 May 2015 15:41:57 +0200 Subject: [PATCH] implemented SubBBox Check -> (one way) changed to size/center to Box Class --- Weave/GLM.cpp | 9 ++ Weave/GLM.h | 2 + Weave/Graphix/Model/BBox.cpp | 2 +- Weave/Graphix/Model/IMesh.cpp | 146 +++++++++++++++++++++++----- Weave/Graphix/Model/IMesh.h | 7 +- Weave/Graphix/Model/IMetaMesh.cpp | 42 ++++---- Weave/Graphix/Model/Model.cpp | 154 +++++++++++++++++++++--------- Weave/Graphix/Model/Model.h | 44 +++++++-- Weave/Graphix/Model/SkyBox.cpp | 5 - 9 files changed, 306 insertions(+), 105 deletions(-) diff --git a/Weave/GLM.cpp b/Weave/GLM.cpp index ef5ecf3..170257e 100644 --- a/Weave/GLM.cpp +++ b/Weave/GLM.cpp @@ -1,5 +1,7 @@ #include "GLM.h" +typedef unsigned int uint; + float VektorAbs(const vec3& vek) { return sqrt(vek.x*vek.x + vek.y*vek.y + vek.z*vek.z); @@ -96,3 +98,10 @@ void updateVec3MinMax(vec3& updMin, vec3& updMax, const vec3& inpMin, const vec3 updMax[j] = inpMax[j]; } } + +void scaleVec3(vec3& _vec, const vec3& _scale) +{ + for (uint i = 0; i < 3; ++i) + _vec[i] *= _scale[i]; +} + diff --git a/Weave/GLM.h b/Weave/GLM.h index 0330451..45c9f19 100644 --- a/Weave/GLM.h +++ b/Weave/GLM.h @@ -50,3 +50,5 @@ vec3 sortVec3(const vec3& vec); void updateVec3MinMax(vec3& updMin, vec3& updMax, const vec3& inpMin, const vec3& inpMax); +void scaleVec3(vec3& _vec, const vec3& _scale); + diff --git a/Weave/Graphix/Model/BBox.cpp b/Weave/Graphix/Model/BBox.cpp index 7ec7e53..25f9cf7 100644 --- a/Weave/Graphix/Model/BBox.cpp +++ b/Weave/Graphix/Model/BBox.cpp @@ -10,7 +10,7 @@ BBox::BBox() float vertex[] { -.5f, -.5f, -.5f, .5f, -.5f, -.5f, .5f, .5f, -.5f, -.5f, .5f, -.5f, -.5f, -.5f, .5f, .5f, -.5f, .5f, .5f, .5f, .5f, -.5f, .5f, .5f }; - updateBB(vec3(-.5f), vec3(.5f)); + setBB(vec3(-.5f), vec3(.5f)); //import("skybox.dae", numvertices, numfaces, vertex, uvs, normals, index, 0); diff --git a/Weave/Graphix/Model/IMesh.cpp b/Weave/Graphix/Model/IMesh.cpp index 83dc7f2..f930a88 100644 --- a/Weave/Graphix/Model/IMesh.cpp +++ b/Weave/Graphix/Model/IMesh.cpp @@ -23,7 +23,7 @@ typedef IMesh::Node Node; // Vergleiche Unterbaum //#define COMPARE_MTREE //draw BBox of MTREE deep LVL -1 means all lvl's -#define BBOX_MTREE 7 +#define BBOX_MTREE 10 // Vergleiche auch Triangles #define COMPARE_FACE @@ -36,13 +36,13 @@ IMesh::IMesh(const string& _modelpath, uint _mindex) : modelpath(_modelpath), ro import(_modelpath, numvertices, numfaces, vertex, uvs, normals, index, _mindex); #ifdef COMPARE_MTREE - root = buildMeshTree(modelMat, numvertices, numfaces, vertex, normals, index); + root = buildMeshTree(mat4(1.f), numvertices, numfaces, vertex, normals, index); vec3 min, max; root->getBBmm(min, max); - updateBB(min, max); + setBB(min, max); #else - updateBB(numvertices, vertex); + setBB(numvertices, vertex); #endif genBuffer(vertexBuffer, numvertices * 3 * sizeof(float), (void*)vertex); @@ -58,13 +58,13 @@ IMesh::IMesh(const aiMesh* _mesh, const mat4& _transformation) : modelMat(_trans import(_mesh, numvertices, numfaces, vertex, uvs, normals, index); #ifdef COMPARE_MTREE - root = buildMeshTree(modelMat, numvertices, numfaces, vertex, normals, index); + root = buildMeshTree(_transformation, numvertices, numfaces, vertex, normals, index); vec3 min, max; root->getBBmm(min, max); - updateBB(min, max); + setBB(min, max); #else - updateBB(numvertices, vertex,modelMat); + setBB(numvertices, vertex, _transformation); #endif genBuffer(vertexBuffer, numvertices * 3 * sizeof(float), (void*)vertex); @@ -135,10 +135,88 @@ void IMesh::drawBBox(const mat4& _modelMat, const vec4& _color) const // //} +Overlap IMesh::checkColNode2O(const Node* _node, const vec3& _scale, const mat4& _mOSMat, const Model* _model, const mat4& _modelMat) const +{ + vec3 pos; + vec3 size; + _node->getBBcs(pos, size); + + scaleVec3(pos, _scale); + scaleVec3(size, _scale); + + Overlap stepNode = _model->checkColO2SBox(_modelMat, pos, size,_mOSMat); + + //Falls _node nicht overlap + if (!stepNode) + return stepNode; + + //check sub _nodes + if (_node->getElement() == nullptr) + { + Overlap left = checkColNode2O(_node->getLNode(), _scale, _mOSMat, _model, _modelMat); + Overlap right = checkColNode2O(_node->getUNode(), _scale, _mOSMat, _model, _modelMat); + Overlap stepSub; + if (left) + stepSub.update(left); + if (right) + stepSub.update(right); + + return stepSub; + } + else + { + //TODO ELEMENT CHECK + return stepNode; + } + +} + +Overlap IMesh::checkColO2NodeBox(const Node* _node, const vec3& _scale, const mat4& _mOSMat, const vec3& _pos, const vec3& _size, const mat4& _modelMat) const +{ + //vec3 pos; + //vec3 size; + //_node->getBBcs(pos, size); + + //scaleVec3(pos, _scale); + //scaleVec3(size, _scale); + + //Overlap stepNode = checkColO2SBox(_modelMat, pos, size, _mOSMat); + + ////Falls _node nicht overlap + //if (!stepNode) + // return stepNode; + + ////check sub _nodes + //if (_node->getElement() == nullptr) + //{ + // Overlap left = checkColNode2O(_node->getLNode(), _scale, _mOSMat, _model, _modelMat); + // Overlap right = checkColNode2O(_node->getUNode(), _scale, _mOSMat, _model, _modelMat); + // Overlap stepSub; + // if (left) + // stepSub.update(left); + // if (right) + // stepSub.update(right); + + // return stepSub; + //} + //else + //{ + // //TODO ELEMENT CHECK + // return stepNode; + //} + return Overlap(); +} + Overlap IMesh::checkColS2O(const mat4& _mMat, const Model* _model, const mat4& _modelMat) const { - //Outer BBox + //Outer BBoxes +#ifdef COMPARE_MTREE + vec3 scale = getScale(_mMat); + Overlap firstBB = checkColNode2O(root, scale, removeScale(_mMat), _model, _modelMat); +#else Overlap firstBB = Model::checkColS2O(_mMat, _model, _modelMat); +#endif + //if (!firstBB) return firstBB; @@ -163,7 +241,13 @@ Overlap IMesh::checkColS2O(const mat4& _mMat, const Model* _model, const mat4& _ Overlap IMesh::checkColO2SBox(const mat4& _mMat, const vec3& _pos, const vec3& _size, const mat4& _modelMat) const { //Outer Mesh +//#ifdef COMPARE_MTREE +// vec3 scale = getScale(_mMat); +// Overlap firstBB = checkColO2NodeBox(root, scale, removeScale(_mMat), _pos, _size, _modelMat); +//#else Overlap firstBB = Model::checkColO2SBox(_mMat, _pos, _size, _modelMat); +//#endif + //if (!firstBB) return firstBB; @@ -187,20 +271,29 @@ IMesh::operator string() const return "IMesh"; } -void getPSfromNodeList(vec3& center, vec3& size, const list& TriList) +void getCSfromNodeList(vec3& _center, vec3& _size, const list& _TriList) { vec3 BBmin, BBmax; - TriList.front()->getBBmm(BBmin, BBmax); - for (auto k = TriList.cbegin(); k != TriList.cend(); ++k) + _TriList.front()->getBBmm(BBmin, BBmax); + for (auto k = _TriList.cbegin(); k != _TriList.cend(); ++k) { (*k)->getUBBmm(BBmin, BBmax); } - size = (BBmax - BBmin) / 2.f; - center = (BBmin + BBmax) / 2.f; + _size = (BBmax - BBmin) / 2.f; + _center = (BBmin + BBmax) / 2.f; +} + +void getMMfromNodeList(vec3& _min, vec3& _max, const list& _TriList) +{ + _TriList.front()->getBBmm(_min, _max); + for (auto k = _TriList.cbegin(); k != _TriList.cend(); ++k) + { + (*k)->getUBBmm(_min, _max); + } } -Node* IMesh::buildMeshTree(const list& _triList, vec3 center, vec3 size) +Node* IMesh::buildMeshTree(const list& _triList, vec3 _min, vec3 _max) { //catch if list is empty if (_triList.size() == 0) @@ -210,18 +303,19 @@ Node* IMesh::buildMeshTree(const list& _triList, vec3 center, vec3 size) return _triList.front(); //catch if list has only two elements => 2 LEAFS if (_triList.size() == 2) - return new Node(center, size, _triList.front(), _triList.back()); + return new Node(_min, _max, _triList.front(), _triList.back()); if (_triList.size() == 3) { - vec3 nC, nS; + vec3 nMi, nMa; list tmpList = _triList; Node* tmpNode = tmpList.front(); tmpList.pop_front(); - getPSfromNodeList(nC, nS, tmpList); + getMMfromNodeList(nMi, nMa, tmpList); - return new Node(center, size, tmpNode, buildMeshTree(tmpList, nC, nS)); + return new Node(_min, _max, tmpNode, buildMeshTree(tmpList, nMi, nMa)); } - + vec3 size = (_min - _max) / 2.f; + vec3 center = (_min + _max) / 2.f; //find longest dist vec3 sort = sortVec3(size); @@ -280,8 +374,8 @@ Node* IMesh::buildMeshTree(const list& _triList, vec3 center, vec3 size) dim = bestDim; - return new Node(center, size, buildMeshTree(lower[dim], (lMin[dim] + lMax[dim]) / 2.f, (lMax[dim] - lMin[dim]) / 2.f), - buildMeshTree(upper[dim], (uMin[dim] + uMax[dim]) / 2.f, (uMax[dim] - uMin[dim]) / 2.f)); + return new Node(_min, _max, buildMeshTree(lower[dim], lMin[dim], lMax[dim]), + buildMeshTree(upper[dim], uMin[dim], uMax[dim])); } Node* IMesh::buildMeshTree(const mat4& modelMat, const uint& numvertices, const uint& numfaces, const float* vertex, const float* normals, const uint* index) @@ -300,11 +394,11 @@ Node* IMesh::buildMeshTree(const mat4& modelMat, const uint& numvertices, const tmp_normal *= -1; node_list.push_back(new Node(new Triangle(tmp_vert, tmp_normal))); } - vec3 center, size; - getPSfromNodeList(center, size, node_list); + vec3 min, max; + getMMfromNodeList(min, max, node_list); - return buildMeshTree(node_list, center, size); + return buildMeshTree(node_list, min, max); } @@ -321,8 +415,8 @@ Node::Node(Triangle* tri) : element(tri), lNode(nullptr), uNode(nullptr), splitD //{ //} -Node::Node(const vec3& _center, const vec3& _size, Node* _lower, Node* _upper) - : element(nullptr), lNode(_lower), uNode(_upper), splitDim(-1), center(_center), size(_size) +Node::Node(const vec3& _min, const vec3& _max, Node* _lower, Node* _upper) + : element(nullptr), lNode(_lower), uNode(_upper), splitDim(-1), center((_min + _max) / 2.f), size((_max - _min) / 2.f), min(_min), max(_max) { } diff --git a/Weave/Graphix/Model/IMesh.h b/Weave/Graphix/Model/IMesh.h index bc29c51..6a3b320 100644 --- a/Weave/Graphix/Model/IMesh.h +++ b/Weave/Graphix/Model/IMesh.h @@ -32,17 +32,20 @@ public: Overlap checkColO2SBox(const mat4& mMat, const vec3& pos, const vec3& size, const mat4& modelMat) const override; Overlap checkColO2STriangle(const mat4& mMat, const vec3& pos, const mat3& directions) const override; + Overlap checkColNode2O(const Node* node, const vec3& scale, const mat4& mOSMat, const Model* model, const mat4& modelMat) const; + Overlap checkColO2NodeBox(const Node* node, const vec3& scale, const mat4& mOSMat, const vec3& pos, const vec3& size, const mat4& modelMat) const; + operator std::string() const override; static Node* buildMeshTree(const mat4& modelMat, const uint& numvertices, const uint& numfaces, const float* vertex, const float* normals, const uint* index); - static Node* buildMeshTree(const std::list& triList, vec3 center, vec3 size); + static Node* buildMeshTree(const std::list& triList, vec3 min, vec3 max); class Node { public: Node(Triangle* tri); //Node(Triangle* tri, const vec3& center, const vec3& size, const vec3& min, const vec3& max); - Node::Node(const vec3& center, const vec3& size, Node* lower, Node* upper); + Node::Node(const vec3& min, const vec3& max, Node* lower, Node* upper); ~Node(); /* sets center & size */ diff --git a/Weave/Graphix/Model/IMetaMesh.cpp b/Weave/Graphix/Model/IMetaMesh.cpp index 5388319..484cf03 100644 --- a/Weave/Graphix/Model/IMetaMesh.cpp +++ b/Weave/Graphix/Model/IMetaMesh.cpp @@ -25,36 +25,41 @@ IMetaMesh::IMetaMesh(const string& _modelpath) : modelpath(_modelpath) const aiNode* root = scene->mRootNode; - aiMatrix4x4* aimat; - mat4 tmpModelMat; - vec3 BBmin, BBmax, tmpMin, tmpMax; + if (scene->HasMeshes())// && scene->mNumMeshes > mindex) { - aimat = &(root->mChildren[0]->mTransformation); + IMesh* tmpIMesh; + aiMatrix4x4* aimat; + mat4 tmpModelMat; + //vec3 BBmin, BBmax, tmpMin, tmpMax; + + //aimat = &(root->mChildren[0]->mTransformation); - tmpModelMat = mat4(aimat->a1, aimat->c1, -aimat->b1, aimat->d1, aimat->a2, aimat->c2, -aimat->b2, aimat->d2, aimat->a3, aimat->c3, -aimat->b3, aimat->d3, aimat->a4, aimat->c4, -aimat->b4, aimat->d4); + //tmpModelMat = mat4(aimat->a1, aimat->c1, -aimat->b1, aimat->d1, aimat->a2, aimat->c2, -aimat->b2, aimat->d2, aimat->a3, aimat->c3, -aimat->b3, aimat->d3, aimat->a4, aimat->c4, -aimat->b4, aimat->d4); - IMesh* tmpIMesh = new IMesh(scene->mMeshes[0], tmpModelMat); - models.push_back(tmpIMesh); - tmpIMesh->getBBmm(BBmin, BBmax); + //tmpIMesh = new IMesh(scene->mMeshes[0], tmpModelMat); + //models.push_back(tmpIMesh); + //tmpIMesh->getBBmm(BBmin, BBmax); - for (uint i = 1; i < scene->mNumMeshes; i++) + for (uint i = 0; i < scene->mNumMeshes; i++) { aimat = &(root->mChildren[i]->mTransformation); tmpModelMat = mat4(aimat->a1, aimat->c1, -aimat->b1, aimat->d1, aimat->a2, aimat->c2, -aimat->b2, aimat->d2, aimat->a3, aimat->c3, -aimat->b3, aimat->d3, aimat->a4, aimat->c4, -aimat->b4, aimat->d4); tmpIMesh = new IMesh(scene->mMeshes[i], tmpModelMat); models.push_back(tmpIMesh); - tmpIMesh->getBBmm(tmpMin, tmpMax); - for (auto j = 0; j < 3; ++j) - { - if (tmpMin[j] < BBmin[j]) - BBmin[j] = tmpMin[j]; - if (tmpMax[j] > BBmax[j]) - BBmax[j] = tmpMax[j]; - } + updateBB(tmpIMesh->getBBox()); + //tmpIMesh->getBBmm(tmpMin, tmpMax); + //for (auto j = 0; j < 3; ++j) + //{ + // if (tmpMin[j] < BBmin[j]) + // BBmin[j] = tmpMin[j]; + // if (tmpMax[j] > BBmax[j]) + // BBmax[j] = tmpMax[j]; + //} } - updateBB(BBmin, BBmax); + + //setBB(BBmin, BBmax); } else { @@ -90,6 +95,7 @@ void IMetaMesh::drawModel(Shader* _shader, Texture* _texture, const mat4& _model void IMetaMesh::drawBBox(const mat4& _modelMat,const vec4& _color) const { + Model::drawBBox(_modelMat, vec4(0.f,.9f,.9f,1.f)); if (models.size() == 1) models.front()->drawBBox(_modelMat, _color); else diff --git a/Weave/Graphix/Model/Model.cpp b/Weave/Graphix/Model/Model.cpp index dec4283..965068c 100644 --- a/Weave/Graphix/Model/Model.cpp +++ b/Weave/Graphix/Model/Model.cpp @@ -18,6 +18,9 @@ #include "../../Events.h" +typedef Model::Triangle Triangle; +typedef Model::Box Box; + Model::Model() : numfaces(0), numvertices(0), @@ -108,30 +111,25 @@ void Model::drawModel(Shader* _shader, Texture* _texture, const mat4& _modelMat) void Model::drawBBox(const mat4& _modelMat, const vec4& _color) const { - vec3 BBsiz, BBpos; - getBBsp(BBsiz, BBpos); Graphix::getGlError(); Graphix::shader_BBox->useShader(); int tmp = Graphix::shader_BBox->getUniformLocation("uFragmentColor"); if (tmp >= 0) glUniform4fv(tmp, 1, value_ptr(_color)); Graphix::getGlError(); - getBBoxModel()->drawModel(Graphix::shader_BBox, NULL, _modelMat*translate(BBpos)*glm::scale(BBsiz*2.f)); + getBBoxModel()->drawModel(Graphix::shader_BBox, NULL, _modelMat*translate(box.center())*glm::scale(box.size()*2.f)); } -void Model::updateBB(const vec3& _min, const vec3& _max) +void Model::setBB(const vec3& _min, const vec3& _max) { - BBmin = _min; - BBmax = _max; - BBsize = (BBmax - BBmin) / 2.f; - BBposition = (BBmin + BBmax) / 2.f; + box = Box((_min + _max) * .5f, (_max - _min) * .5f); } -void Model::updateBB(const uint _numvertices, const float* _vertices) +void Model::setBB(const uint _numvertices, const float* _vertices) { - BBmin = vec3(_vertices[0], _vertices[1], _vertices[2]); - BBmax = BBmin; + vec3 BBmin = vec3(_vertices[0], _vertices[1], _vertices[2]); + vec3 BBmax = BBmin; for (uint i = 3; i < _numvertices * 3; ++i) { if (_vertices[i] < BBmin[i % 3]) @@ -140,14 +138,13 @@ void Model::updateBB(const uint _numvertices, const float* _vertices) BBmax[i % 3] = _vertices[i]; } - BBsize = (BBmax - BBmin) / 2.f; - BBposition = (BBmin + BBmax) / 2.f; + box = Box((BBmin + BBmax) * .5f, (BBmax - BBmin) * .5f); } -void Model::updateBB(const uint _numvertices, const float* _vertices, const mat4& _modelMat) +void Model::setBB(const uint _numvertices, const float* _vertices, const mat4& _modelMat) { - BBmin = (vec3)(_modelMat * vec4(_vertices[0], _vertices[1], _vertices[2], 1.f)); - BBmax = BBmin; + vec3 BBmin = (vec3)(_modelMat * vec4(_vertices[0], _vertices[1], _vertices[2], 1.f)); + vec3 BBmax = BBmin; vec3 tmpVert; for (uint j = 3; j < _numvertices * 3; j += 3) { @@ -161,23 +158,30 @@ void Model::updateBB(const uint _numvertices, const float* _vertices, const mat4 } } - BBsize = (BBmax - BBmin) / 2.f; - BBposition = (BBmin + BBmax) / 2.f; + box = Box((BBmin + BBmax) * .5f, (BBmax - BBmin) * .5f); } - -void Model::getBBmm(vec3& _min, vec3& _max) const +void Model::updateBB(const Box& _box) { - _min = BBmin; - _max = BBmax; + box = Box::merge(box, _box); } -void Model::getBBsp(vec3& _size, vec3& _position) const +//void Model::getBBmm(vec3& _min, vec3& _max) const +//{ +// _min = BBmin; +// _max = BBmax; +//} + +void Model::getBBcs(vec3& _center, vec3& _size) const { - _size = BBsize; - _position = BBposition; + _size = box.size(); + _center = box.center(); } +Box Model::getBBox() const +{ + return box; +} void Model::genBuffer(uint &buffer, uint size, void* value) { @@ -284,9 +288,10 @@ Overlap Model::isColliding(const Model* _modelThis, const Model* _modelOther, co Overlap Model::checkColS2O(const mat4& _mMat, const Model* _model, const mat4& _modelMat) const { vec3 scale = getScale(_mMat); - vec3 pos = BBposition; + vec3 pos = box.center(); + vec3 size = box.size(); + scaleVec3(pos, scale); - vec3 size = BBsize; scaleVec3(size, scale); Overlap stepBB = _model->checkColO2SBox(_modelMat, pos, size, removeScale(_mMat)); @@ -297,9 +302,10 @@ Overlap Model::checkColS2O(const mat4& _mMat, const Model* _model, const mat4& _ Overlap Model::checkColO2SBox(const mat4& _mMat, const vec3& _posB, const vec3& _sizeB, const mat4& _modelMatB) const { vec3 scale = getScale(_mMat); - vec3 posA = BBposition; + vec3 posA = box.center(); + vec3 sizeA = box.size(); + scaleVec3(posA, scale); - vec3 sizeA = BBsize; scaleVec3(sizeA, scale); mat4 mMat = removeScale(_mMat); @@ -328,8 +334,8 @@ Overlap Model::checkColO2SBox(const mat4& _mMat, const vec3& _posB, const vec3& Overlap Model::checkColO2STriangle(const mat4& _mMat, const vec3& _posB, const mat3& _directionsB) const { - vec3 posA = BBposition; - vec3 sizeA = BBsize; + vec3 posA = box.center(); + vec3 sizeA = box.size(); vec3 scale = getScale(_mMat); scaleVec3(posA, scale); @@ -430,13 +436,6 @@ float Model::directSize(const vec3& _size, const vec3& _direction) return ret; } -void Model::scaleVec3(vec3& _vec, const vec3& _scale) -{ - for (uint i = 0; i < 3; ++i) - _vec[i] *= _scale[i]; -} - - Model* Model::getBBoxModel() { if (BoundingBox == nullptr) @@ -507,7 +506,7 @@ float Model::getPDistHit(const vec3& _P, const vec3& _direction) const { vec3 posA, sizeA; - getBBsp(sizeA, posA); + getBBcs(posA, sizeA); //vec3 posAR = (vec3)(modelAR*vec4(posA, 1.f)); //vec3 posBR = (vec3)(modelBR*vec4(posB, 1.f)); @@ -589,15 +588,17 @@ Model* Model::SkyBoxModel = nullptr; std::unordered_map Model::IMetaModel; std::unordered_map Model::IMetaModel_count; -Model::Triangle::Triangle(const mat3& _vertex, const vec3& _normal) : vertex(_vertex), normal(normalize(_normal)) +// CLASS TRIANGLE + +Triangle::Triangle(const mat3& _vertex, const vec3& _normal) : vertex(_vertex), normal(normalize(_normal)) { } -Model::Triangle::~Triangle() +Triangle::~Triangle() { } -void Model::Triangle::getBBcs(vec3& center, vec3& size) const +void Triangle::getBBcs(vec3& center, vec3& size) const { vec3 BBmin = vertex[0]; vec3 BBmax = BBmin; @@ -607,14 +608,14 @@ void Model::Triangle::getBBcs(vec3& center, vec3& size) const center = (BBmin + BBmax) / 2.f; } -void Model::Triangle::getBBmm(vec3& min, vec3& max) const +void Triangle::getBBmm(vec3& min, vec3& max) const { min = vertex[0]; max = min; getUBBmm(min, max); } -void Model::Triangle::getUBBmm(vec3& min, vec3& max) const +void Triangle::getUBBmm(vec3& min, vec3& max) const { for (uint i = 0; i < 3; ++i) { @@ -628,7 +629,70 @@ void Model::Triangle::getUBBmm(vec3& min, vec3& max) const } } -vec3 Model::Triangle::getMean() const +vec3 Triangle::getMean() const { return (vertex[0] + vertex[1] + vertex[2]) / 3.f; +} + +//CLASS BOX + +Box::Box() : cen(0.f), siz(0.f) +{} + +Box::Box(const vec3& _center, const vec3& _size) : cen(_center), siz(_size) +{ +} + +Box::Box(const Box& _box) : cen(_box.cen), siz(_box.siz) +{ +} + +Box::~Box() +{ +} + +void Box::getBBcs(vec3& _center, vec3& _size) const +{ + _center = cen; + _size = siz; +} + +vec3 Box::center() const +{ + return cen; +} + +vec3 Box::size() const +{ + return siz; +} + +Box Box::merge(const Box& _boxA, const Box& _boxB) +{ + vec3 diff = _boxB.cen - _boxA.cen; + vec3 size; + vec3 center; + for (int i = 0; i<3; ++i) + { + if (abs(_boxA.siz[i] - _boxB.siz[i]) - abs(diff[i]) >= 0) + { + if (_boxA.siz[i] > _boxB.siz[i]) + { + size[i] = _boxA.siz[i]; + center[i] = _boxA.cen[i]; + } + else + { + size[i] = _boxB.siz[i]; + center[i] = _boxB.cen[i]; + } + } + else + { + size[i] = (_boxA.siz[i] + _boxB.siz[i] + abs(diff[i])) * .5f; + center[i] = _boxA.cen[i] - sign(diff[i]) * _boxA.siz[i] - size[i]; + } + + } + return Box(center,size); } \ No newline at end of file diff --git a/Weave/Graphix/Model/Model.h b/Weave/Graphix/Model/Model.h index a45e2bb..fa47587 100644 --- a/Weave/Graphix/Model/Model.h +++ b/Weave/Graphix/Model/Model.h @@ -16,6 +16,8 @@ typedef unsigned int uint; class Model { public: + class Triangle; + class Box; /* Binds Model to the Model*/ virtual void bindShader(Shader* shader); @@ -27,9 +29,11 @@ public: virtual void drawBBox(const mat4& modelMat, const vec4& color = vec4(0.9f, 0.f, 0.f, 1.f)) const; /* Get min&max from BBox */ - virtual void getBBmm(vec3& min, vec3& max) const; +// virtual void getBBmm(vec3& min, vec3& max) const; /* Get size&position from BBox */ - virtual void getBBsp(vec3& size, vec3& pos) const; + virtual void getBBcs(vec3& center, vec3& size) const; + + Box getBBox() const; /* calls Colliding to check if self collides with model*/ static Overlap isColliding(const Model* modelThis, const Model* modelOther, const mat4& modelMatThis, const mat4& modelMatOther); @@ -57,6 +61,7 @@ public: { public: Triangle(const mat3& vertex, const vec3& normal); + Triangle(const Triangle& tri) = default; ~Triangle(); /* sets center & size */ @@ -73,6 +78,26 @@ public: }; + class Box + { + public: + Box(); + Box(const Box& box); + Box(const vec3& center, const vec3& size); + ~Box(); + + //Box& operator=(const Box& box) const; + + void getBBcs(vec3& center, vec3& size) const; + + static Box merge(const Box& boxA, const Box& boxB); + vec3 center() const; + vec3 size() const; + protected: + vec3 cen; + vec3 siz; + }; + // Mesh Speichern? static bool import(const std::string& modelpath, uint& numvertices, uint& numfaces, float*& vertex, float*& uvs, float*& normals, uint*& index, uint mindex = 0); static bool import(const aiMesh* mesh, uint& numvertices, uint& numfaces, float*& vertex, float*& uvs, float*& normals, uint*& index); @@ -86,9 +111,11 @@ protected: std::unordered_map shader_map; - void updateBB(const vec3& min, const vec3& max); - void updateBB(const uint numvertices, const float* vertices); - void updateBB(const uint numvertices, const float* vertices, const mat4& modelMat); + void setBB(const vec3& min, const vec3& max); + void setBB(const uint numvertices, const float* vertices); + void setBB(const uint numvertices, const float* vertices, const mat4& modelMat); + + void updateBB(const Box& box); void genBuffer(uint &buffer, uint size, void* value); void bindBuffer(const uint &buffer, const uint index, const uint dim = 3); @@ -105,12 +132,13 @@ protected: static void checkCollideByAxis(float& overlap, float& inside, uint dim, const float* posA, const float* posB, const float* sizeA, const float* sizeB); - static void scaleVec3(vec3& vec, const vec3& scale); static vec3 rotateSize(const vec3& size, const mat4& modelMat); static float directSize(const vec3& size, const vec3& direction); private: - vec3 BBmin, BBmax; - vec3 BBsize, BBposition; +// vec3 BBmin, BBmax; +// vec3 BBsize, BBposition; + + Box box; }; \ No newline at end of file diff --git a/Weave/Graphix/Model/SkyBox.cpp b/Weave/Graphix/Model/SkyBox.cpp index 30d4ed5..bfb62eb 100644 --- a/Weave/Graphix/Model/SkyBox.cpp +++ b/Weave/Graphix/Model/SkyBox.cpp @@ -8,15 +8,10 @@ SkyBox::SkyBox() { - numvertices = 4; - numfaces = 2; float *vertex = nullptr, *normals = nullptr, *uvs = nullptr; uint *index = nullptr; - //float vertex[] { -1.f, - 1.f, 0.f, 1.f, - 1.f, 0.f, - 1.f, 1.f, 0.f, 1.f, 1.f, 0.f }; - //uint index[] { 1,3,2,0,1,2}; - import("SkyBox.dae", numvertices, numfaces, vertex, uvs, normals, index, 0); genBuffer(vertexBuffer, numvertices * 3 * sizeof(float), (void*)vertex); -- 2.47.3