]> git.leopard-lacewing.eu Git - cgue_weave.git/commitdiff
BBox works for MetaMeshes
authorPeter Schaefer <schaeferpm@gmail.com>
Thu, 23 Apr 2015 17:23:37 +0000 (19:23 +0200)
committerPeter Schaefer <schaeferpm@gmail.com>
Thu, 23 Apr 2015 17:23:37 +0000 (19:23 +0200)
Weave/Graphix/Model/IMesh.cpp
Weave/Graphix/Model/IMetaMesh.cpp
Weave/Graphix/Model/IMetaMesh.h
Weave/Graphix/Model/Model.cpp
Weave/Graphix/Model/Model.h

index bcb2bfe13d341a59a6e1a4f5a11f8acbe4092525..14ef3b76adaeac2118a671f68654dcd092d73440 100644 (file)
@@ -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);
index d094020bff4bb7a0df9d941a3eb312760ab6772a..69d7043413672aa2857cfe0a74b8408b1f574a75 100644 (file)
@@ -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);
+//     }
+//
+//}
+
index bb3bbdd90814fc940610596c241545507a98edf7..bc68bb6a4b0ec1ccae235d2fdbbef73d281c9b2f 100644 (file)
@@ -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<IMesh*> models;
 };
index e7addf9a760eacdae2bc91d7b1a972cb25dfa8ab..6219431cc0cd50edba96d1e2187ce6e4cd159939 100644 (file)
@@ -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);
index 6b901c4fba99a407c2da07058c416afd98b87596..574a34736e824e30c9fc2a0e9e0680d0b8c260b6 100644 (file)
@@ -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<uint, uint> 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;
+       
 
 };