From: Peter Schaefer Date: Thu, 23 Apr 2015 17:23:37 +0000 (+0200) Subject: BBox works for MetaMeshes X-Git-Url: https://git.leopard-lacewing.eu/?a=commitdiff_plain;h=5c84d76e34a3f48ccef95134c09f4c984002ca57;p=cgue_weave.git BBox works for MetaMeshes --- diff --git a/Weave/Graphix/Model/IMesh.cpp b/Weave/Graphix/Model/IMesh.cpp index bcb2bfe..14ef3b7 100644 --- a/Weave/Graphix/Model/IMesh.cpp +++ b/Weave/Graphix/Model/IMesh.cpp @@ -21,6 +21,8 @@ IMesh::IMesh(const string& _modelpath, uint _mindex) import(_modelpath, numvertices, numfaces, vertex, uvs, normals, index, _mindex); + updateBB(vertex); + genBuffer(vertexBuffer, numvertices * 3 * sizeof(float), (void*)vertex); genBuffer(normalBuffer, numvertices * 3 * sizeof(float), (void*)normals); genBuffer(uvBuffer, numvertices * 2 * sizeof(float), (void*)uvs); @@ -38,6 +40,8 @@ IMesh::IMesh(const aiMesh* _mesh) import(_mesh, numvertices, numfaces, vertex, uvs, normals, index); + updateBB(vertex); + genBuffer(vertexBuffer, numvertices * 3 * sizeof(float), (void*)vertex); genBuffer(normalBuffer, numvertices * 3 * sizeof(float), (void*)normals); genBuffer(uvBuffer, numvertices * 2 * sizeof(float), (void*)uvs); diff --git a/Weave/Graphix/Model/IMetaMesh.cpp b/Weave/Graphix/Model/IMetaMesh.cpp index d094020..69d7043 100644 --- a/Weave/Graphix/Model/IMetaMesh.cpp +++ b/Weave/Graphix/Model/IMetaMesh.cpp @@ -20,13 +20,28 @@ IMetaMesh::IMetaMesh(const string& _modelpath) return; } + IMesh* tmpIMesh = nullptr; + vec3 tmpMin, tmpMax; if (scene->HasMeshes())// && scene->mNumMeshes > mindex) { - for (unsigned int i = 0; i < scene->mNumMeshes; i++) + tmpIMesh = new IMesh(scene->mMeshes[0]); + models.push_back(tmpIMesh); + tmpIMesh->getBBmm(BBmin, BBmax); + + for (auto i = 1; i < scene->mNumMeshes; i++) { - models.push_back(new IMesh(scene->mMeshes[i])); + tmpIMesh = new IMesh(scene->mMeshes[i]); + 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(BBmin, BBmax); } else { @@ -61,3 +76,13 @@ void IMetaMesh::drawModel(Shader* _shader, Texture* _texture, const mat4& _model } +//void IMetaMesh::drawBBox(Shader* _shader, const mat4 & _modelMat) const +//{ +// Model::drawBBox(_shader, _modelMat); +// for (auto i = models.begin(); i != models.end(); ++i) +// { +// (*i)->drawBBox(_shader, _modelMat); +// } +// +//} + diff --git a/Weave/Graphix/Model/IMetaMesh.h b/Weave/Graphix/Model/IMetaMesh.h index bb3bbdd..bc68bb6 100644 --- a/Weave/Graphix/Model/IMetaMesh.h +++ b/Weave/Graphix/Model/IMetaMesh.h @@ -22,6 +22,8 @@ public: void drawModel(Shader* shader, Texture* texture, const mat4& modelMat) const; + //void drawBBox(Shader* shader, const mat4 & modelMat) const; + protected: std::list models; }; diff --git a/Weave/Graphix/Model/Model.cpp b/Weave/Graphix/Model/Model.cpp index e7addf9..6219431 100644 --- a/Weave/Graphix/Model/Model.cpp +++ b/Weave/Graphix/Model/Model.cpp @@ -114,7 +114,7 @@ void Model::drawBBox(Shader* _shader,const mat4& _modelMat) const } BoundingBox->bindShader(_shader); - BoundingBox->drawModel(_shader, (Texture*)nullptr, _modelMat); + BoundingBox->drawModel(_shader, (Texture*)nullptr, _modelMat*translate(BBposition)*scale(BBsize)); } void Model::updateBB(const vec3& _min, const vec3& _max) @@ -123,6 +123,37 @@ void Model::updateBB(const vec3& _min, const vec3& _max) BBposition = (_min + _max) / 2.f; } +void Model::updateBB(const float* _vertices) +{ + BBmin = vec3(_vertices[0], _vertices[1], _vertices[2]); + BBmax = BBmin; + for (auto i = 3; i < numvertices * 3; ++i) + { + if (_vertices[i] < BBmin[i % 3]) + BBmin[i % 3] = _vertices[i]; + if (_vertices[i] > BBmax[i % 3]) + BBmax[i % 3] = _vertices[i]; + } + + BBsize = BBmax - BBmin; + BBposition = (BBmin + BBmax) / 2.f; +} + + +void Model::getBBmm(vec3& _min, vec3& _max) const +{ + _min = BBmin; + _max = BBmax; +} + +void Model::getBBsp(vec3& _size, vec3& _position) const +{ + _size = BBsize; + _position = BBposition; +} + + + void Model::genBuffer(uint &buffer, uint size, void* value) { glGenBuffers(1, &buffer); diff --git a/Weave/Graphix/Model/Model.h b/Weave/Graphix/Model/Model.h index 6b901c4..574a347 100644 --- a/Weave/Graphix/Model/Model.h +++ b/Weave/Graphix/Model/Model.h @@ -23,6 +23,9 @@ public: /* Draws a BoundingBox around the Model */ virtual void drawBBox(Shader* shader,const mat4 & modelMat) const; + void getBBmm(vec3& min, vec3& max) const; + void getBBsp(vec3& size, vec3& pos) const; + protected: uint numvertices, numfaces; uint vertexBuffer, indexBuffer, normalBuffer, uvBuffer; @@ -30,6 +33,7 @@ protected: std::unordered_map shader_map; void updateBB(const vec3& min, const vec3& max); + void updateBB(const float* vertices); void genBuffer(uint &buffer, uint size, void* value); void bindBuffer(const uint &buffer, const uint index, const uint dim = 3); @@ -39,12 +43,16 @@ protected: virtual void useModelMat(const mat4& model, Shader* shader) const; virtual void drawModel() const; + static Model* BoundingBox; static Shader* BBoxShader; static bool exBBox; + vec3 BBmin, BBmax; + private: vec3 BBsize, BBposition; + };