]> git.leopard-lacewing.eu Git - cgue_weave.git/commitdiff
saving loaded meshes
authorPeter Schaefer <schaeferpm@gmail.com>
Thu, 7 May 2015 09:34:34 +0000 (11:34 +0200)
committerPeter Schaefer <schaeferpm@gmail.com>
Thu, 7 May 2015 09:34:34 +0000 (11:34 +0200)
collision on SubBBox works :)
new class Overlap (overlap, inside and vec and shortest direction)
Overlap (shortest direction doesnt work)

Weave/Game.cpp
Weave/Graphix/Model/IMesh.cpp
Weave/Graphix/Model/IMesh.h
Weave/Graphix/Model/IMetaMesh.cpp
Weave/Graphix/Model/IMetaMesh.h
Weave/Graphix/Model/Model.cpp
Weave/Graphix/Model/Model.h
Weave/Scene/SceneObject.cpp

index 19ec862228e14e028bb86a41a0acfa57cb55d420..dd53c20607c7239c3d91286102ad412ef4874505 100644 (file)
@@ -66,8 +66,7 @@ Game::Game() : playing(true)
        //current_world->addObject(new SceneObject(shader1, glm::mat4(1.0f), "Player.dae", "model_player.png"));
        //current_world->addObject(new SceneObject(shader1, translate(vec3(-3.f, .4f, 0.f))*scale(vec3(3.f)), "cow/cow.dae", "model_cow_2D.jpg"));
        current_world->addObject(new SceneObject(shader1, translate(vec3(-3.f, .4f, 0.f))*scale(vec3(.32f)), vec4(3.0f, 0.5f, 0.4f, 1.5f), "duck.dae", "model_duck_2D.png"));
-
-       current_world->addObject(new SceneObject(shader1, translate(vec3(-3.f, .4f, 0.f))*scale(vec3(.32f)), vec4(3.0f, 3.f, 0.4f, 1.5f), "duck.dae", "model_cow_2D.jpg"));
+       current_world->addObject(new SceneObject(shader1, translate(vec3(-5.f, .4f, 0.f))*scale(vec3(.32f)), vec4(3.0f, 3.f, 0.4f, 1.5f), "duck.dae", "model_cow_2D.jpg"));
 
        current_world->addObject(new EventBox(translate(vec3(3.f, .4f, 0.f)),EB_LOSTZONE));
        current_world->addObject(new EventBox(translate(vec3(3.f, .4f, -15.f)), EB_WINZONE));
index 4a6b3e558e32121ec092e3a6b012485284ba8cdb..de003673f270d59025219f417f0fad99f118dabe 100644 (file)
@@ -16,9 +16,6 @@ using std::string;
 IMesh::IMesh(const string& _modelpath, uint _mindex) : modelpath(_modelpath)
 {
 
-       float *vertex = nullptr, *normals = nullptr, *uvs = nullptr;
-       uint *index = nullptr;
-
        import(_modelpath, numvertices, numfaces, vertex, uvs, normals, index, _mindex);
 
        updateBB(numvertices,vertex);
@@ -28,16 +25,11 @@ IMesh::IMesh(const string& _modelpath, uint _mindex) : modelpath(_modelpath)
        genBuffer(uvBuffer, numvertices * 2 * sizeof(float), (void*)uvs);
        genBuffer(indexBuffer, numfaces * 3 * sizeof(uint), (void*)index);
 
-       delete vertex, normals, uvs, index;
-
 }
 
 IMesh::IMesh(const aiMesh* _mesh, const mat4& _transformation) : modelMat(_transformation), modelpath("IMesh")
 {
 
-       float *vertex = nullptr, *normals = nullptr, *uvs = nullptr;
-       uint *index = nullptr;
-
        import(_mesh, numvertices, numfaces, vertex, uvs, normals, index);
 
        updateBB(numvertices,vertex,modelMat);
@@ -47,13 +39,13 @@ IMesh::IMesh(const aiMesh* _mesh, const mat4& _transformation) : modelMat(_trans
        genBuffer(uvBuffer, numvertices * 2 * sizeof(float), (void*)uvs);
        genBuffer(indexBuffer, numfaces * 3 * sizeof(uint), (void*)index);
 
-       delete vertex, normals, uvs, index;
-
 }
 
 
 IMesh::~IMesh()
 {
+       delete vertex, normals, uvs, index;
+
        glDeleteBuffers(1, &vertexBuffer);
        glDeleteBuffers(1, &indexBuffer);
        glDeleteBuffers(1, &normalBuffer);
index 14e73539dc115c5ffdb961ffdaafbbb1482ce2d3..43451d448358741d2060f99a56f3370270308490 100644 (file)
@@ -33,6 +33,9 @@ protected:
        std::string modelpath;
        mat4 modelMat;
 
+       float *vertex = nullptr, *normals = nullptr, *uvs = nullptr;
+       uint *index = nullptr;
+
        // Mesh Speichern?
        bool import(const std::string& modelpath, uint& numvertices, uint& numfaces, float*& vertex, float*& uvs, float*& normals, uint*& index , uint mindex = 0) const;
        bool import(const aiMesh* mesh, uint& numvertices, uint& numfaces, float*& vertex, float*& uvs, float*& normals, uint*& index) const;
index 7bd695878fe6bd3184adaa82735e8da991fddbb8..155502a4cc5e5d32fca5b8a3aff995a9ccbb60f5 100644 (file)
@@ -99,26 +99,70 @@ void IMetaMesh::drawBBox(const mat4& _modelMat,const vec4& _color) const
 
 }
 
+Overlap IMetaMesh::checkColS2O(const mat4& _mMat, const Model* _model, const mat4& _modelMat) const
+{
+       //Outer BBox
+       Overlap firstBB = Model::checkColS2O(_mMat, _model, _modelMat);
+       if (!firstBB)
+               return firstBB;
+
+       //Inner BBox
+       Overlap tmp,secondSubBB;
+       for (auto i = models.begin(); i != models.end(); ++i)
+       {
+               tmp = (*i)->checkColS2O(_mMat, _model, _modelMat);
+               if (tmp)
+               {
+                       secondSubBB.update(tmp);
+               }
+                       
+       }
+       //if (!secondSubBB)
+               return secondSubBB;
+       
+}
 
-void IMetaMesh::checkColliding(float& _overlap, float& _inside, const Model* _model, const mat4& _modelMatThis, const mat4& _modelMatOther) const
+Overlap IMetaMesh::checkColO2SBox(const mat4& _mMat, const vec3& _pos, const vec3& _size, const mat4& _modelMat) const
 {
-       Model::checkColliding(_overlap, _inside, _model, _modelMatThis, _modelMatOther);
+       //Outer BBox
+       Overlap firstBB = Model::checkColO2SBox(_mMat, _pos, _size, _modelMat);
+       if (!firstBB)
+               return firstBB;
 
-       if (min(_overlap, _inside) >= 0)
+       //Inner BBox
+       Overlap tmp, secondSubBB;
+       for (auto i = models.begin(); i != models.end(); ++i)
        {
-               _overlap = -1; _inside = -1;
-               float subOverlap = -1, subInside = -1;
-               for (auto i = models.begin(); i != models.end(); ++i)
+               tmp = (*i)->checkColO2SBox(_mMat, _pos, _size, _modelMat);
+               if (tmp)
                {
-                       _model->checkColliding(subOverlap, subInside, *i, _modelMatThis, _modelMatOther);
-                       if (subOverlap > _overlap)
-                               _overlap = subOverlap;
-                       if (subInside > _inside)
-                               _inside = subInside;
+                       secondSubBB.update(tmp);
                }
+
        }
+       //if (!secondSubBB)
+               return secondSubBB;
 }
 
+//void checkColliding(float& _overlap, float& _inside, const Model* _model, const mat4& _modelMatThis, const mat4& _modelMatOther) const
+//{
+//     Model::checkColliding(_overlap, _inside, _model, _modelMatThis, _modelMatOther);
+//
+//     if (min(_overlap, _inside) >= 0)
+//     {
+//             _overlap = -1; _inside = -1;
+//             float subOverlap = -1, subInside = -1;
+//             for (auto i = models.begin(); i != models.end(); ++i)
+//             {
+//                     _model->checkColliding(subOverlap, subInside, *i, _modelMatThis, _modelMatOther);
+//                     if (subOverlap > _overlap)
+//                             _overlap = subOverlap;
+//                     if (subInside > _inside)
+//                             _inside = subInside;
+//             }
+//     }
+//}
+
 IMetaMesh::operator string() const
 {
        return modelpath;
index 13f763fbd82fe8ecadb0ba5dbd2c8a1ecf77fc78..60aafa683a1d91b799ce137eb6da9d550b9b6bb4 100644 (file)
@@ -22,8 +22,11 @@ public:
 
        void drawBBox(const mat4& modelMat, const vec4& color = vec4(0.9f, 0.f, 0.f, 1.f)) const override;
 
+       Overlap checkColS2O(const mat4& mMat, const Model* model, const mat4& modelMat) const override;
+       Overlap checkColO2SBox(const mat4& mMat, const vec3& pos, const vec3& size, const mat4& modelMat) const override;
+
        /* calls Colliding to check if self collides with model*/
-       void checkColliding(float& overlap, float& inside, const Model* model, const mat4& modelMatThis, const mat4& modelMatOther) const;
+//     void checkColliding(float& overlap, float& inside, const Model* model, const mat4& modelMatThis, const mat4& modelMatOther) const;
 
        operator std::string() const override;
 
index 941bc8fbe9d4000de444c5f9229202558a4a8d7d..8f29e3e161387805931412ebb1514dfc538c6610 100644 (file)
@@ -237,40 +237,86 @@ void Model::useModelMat(const mat4& _model, Shader* _shader) const
                glUniformMatrix4fv(tmp, 1, GL_FALSE, value_ptr(_model));
 }
 
-void Model::isColliding(float& _overlap, float& _inside, const Model* _modelThis, const Model* _modelOther, const mat4& _modelMatThis, const mat4& _modelMatOther)
+Overlap Model::isColliding(const Model* _modelThis, const Model* _modelOther, const mat4& _modelMatThis, const mat4& _modelMatOther)
 {
-       vec3 posA, sizeA, posB, sizeB;
-       float overlap=INFINITY, inside=INFINITY;
+       return _modelThis->checkColS2O(_modelMatThis, _modelOther, _modelMatOther);
 
-       _modelThis->getBBsp(sizeA, posA);
-       _modelOther->getBBsp(sizeB, posB);
+       //vec3 posA, sizeA, posB, sizeB;
+       //float overlap, inside;
 
-       mat4 modelAR = glm::inverse(removeScale(_modelMatOther))*_modelMatThis;
-       mat4 modelBR = glm::inverse(removeScale(_modelMatThis))*_modelMatOther;
+       //_modelThis->getBBsp(sizeA, posA);
+       //_modelOther->getBBsp(sizeB, posB);
 
-       vec3 posAR = (vec3)(modelAR*vec4(posA, 1.f));
-       vec3 posBR = (vec3)(modelBR*vec4(posB, 1.f));
+       //mat4 modelAR = glm::inverse(removeScale(_modelMatOther))*_modelMatThis;
+       //mat4 modelBR = glm::inverse(removeScale(_modelMatThis))*_modelMatOther;
 
-       vec3 sizeAR = rotateSize(sizeA, modelAR);
-       vec3 sizeBR = rotateSize(sizeB, modelBR);
+       //vec3 posAR = (vec3)(modelAR*vec4(posA, 1.f));
+       //vec3 posBR = (vec3)(modelBR*vec4(posB, 1.f));
 
-       vec3 scaleA = getScale(_modelMatThis);
-       vec3 scaleB = getScale(_modelMatOther);
+       //vec3 sizeAR = rotateSize(sizeA, modelAR);
+       //vec3 sizeBR = rotateSize(sizeB, modelBR);
 
-       scaleVec3(posA, scaleA);
-       scaleVec3(posB, scaleB);
+       //vec3 scaleA = getScale(_modelMatThis);
+       //vec3 scaleB = getScale(_modelMatOther);
 
-       scaleVec3(sizeA, scaleA);
-       scaleVec3(sizeB, scaleB);
+       //scaleVec3(posA, scaleA);
+       //scaleVec3(posB, scaleB);
 
-       checkCollideByAxis(_overlap, _inside, 3, value_ptr(posAR), value_ptr(posB), value_ptr(sizeAR), value_ptr(sizeB));
-       if (_overlap >= 0)
+       //scaleVec3(sizeA, scaleA);
+       //scaleVec3(sizeB, scaleB);
+
+       //checkCollideByAxis(overlap,inside, 3, value_ptr(posAR), value_ptr(posB), value_ptr(sizeAR), value_ptr(sizeB));
+       //Overlap ret(overlap, inside);
+       //if (ret.overlap() >= 0)
+       //{
+       //      checkCollideByAxis(overlap, inside, 3, value_ptr(posA), value_ptr(posBR), value_ptr(sizeA), value_ptr(sizeBR));
+       //      ret.update(overlap, inside);
+       //}
+       //return ret;
+}
+
+Overlap Model::checkColS2O(const mat4& _mMat, const Model* _model, const mat4& _modelMat) const
+{
+       vec3 scale = getScale(_mMat);
+       vec3 pos = BBposition;
+       scaleVec3(pos, scale);
+       vec3 size = BBsize;
+       scaleVec3(size, scale);
+
+       Overlap stepBB = _model->checkColO2SBox(_modelMat, pos, size, removeScale(_mMat));
+
+       return stepBB;
+}
+
+Overlap Model::checkColO2SBox(const mat4& _mMat, const vec3& _posB, const vec3& _sizeB, const mat4& _modelMat) const
+{
+       vec3 scale = getScale(_mMat);
+       vec3 posA = BBposition;
+       scaleVec3(posA, scale);
+       vec3 sizeA = BBsize;
+       scaleVec3(sizeA, scale);
+
+       mat4 mMat = removeScale(_mMat);
+
+       mat4 modelAR = glm::inverse(_modelMat)*mMat;
+       mat4 modelBR = glm::inverse(mMat)*_modelMat;
+
+       vec3 posAR = (vec3)(modelAR*vec4(posA, 1.f));
+       vec3 posBR = (vec3)(modelBR*vec4(_posB, 1.f));
+
+       vec3 sizeAR = rotateSize(sizeA, modelAR);
+       vec3 sizeBR = rotateSize(_sizeB, modelBR);
+
+       float overlap, inside;
+       checkCollideByAxis(overlap, inside, 3, value_ptr(posAR), value_ptr(_posB), value_ptr(sizeAR), value_ptr(_sizeB));
+       Overlap ret(overlap, inside);
+       if (ret.overlap() >= 0)
        {
                checkCollideByAxis(overlap, inside, 3, value_ptr(posA), value_ptr(posBR), value_ptr(sizeA), value_ptr(sizeBR));
-               _overlap = min(overlap, _overlap);
-               _inside = min(overlap, _inside);
+               ret.update(overlap, inside);
        }
 
+       return ret;
 }
 
 void Model::checkColliding(float& _overlap, float& _inside, const Model* _model, const mat4& _modelMatThis, const mat4& _modelMatOther) const
@@ -431,4 +477,88 @@ float Model::getPDistHit(const vec3& _P, const vec3& _direction) const
 Model* Model::BoundingBox = nullptr;
 Model* Model::SkyBoxModel = nullptr;
 std::unordered_map<string, Model*> Model::IMetaModel;
-std::unordered_map<string, uint> Model::IMetaModel_count;
\ No newline at end of file
+std::unordered_map<string, uint> Model::IMetaModel_count;
+
+
+
+Overlap::Overlap() : unset(true)
+{
+}
+
+Overlap::Overlap(float _over, float _in) : in(_in), over(_over), unset(false)
+{
+}
+
+Overlap::~Overlap()
+{
+}
+
+float Overlap::overlap() const
+{
+       if (unset)
+               return -1;
+       else
+               return over;
+}
+
+float Overlap::inside() const
+{
+       if (unset)
+               return -1;
+       else
+               return in;
+}
+
+Overlap::operator bool() const
+{
+       if (unset)
+               return false;
+       else
+               return over>=0;
+}
+
+void Overlap::update(float _over, float _in)
+{
+       if (unset)
+       {
+               over = _over;
+               in = _in;
+               unset = true;
+               return;
+       }
+
+       // select closest distance
+       if (over > _over)
+       {
+               over = _over;
+       }
+
+       //sync inside
+       in = min(in, _in);
+}
+
+void Overlap::update(const Overlap& _other)
+{
+       if (unset)
+       {
+               *this = _other;
+               return;
+       }
+
+       // select closest distance
+       if (over > _other.over)
+       {
+               over = _other.over;
+       }
+
+       //sync inside
+       in = min(in, _other.in);
+}
+
+Overlap Overlap::merge(const Overlap& _first, const Overlap& _second)
+{
+       // select closest distance
+       Overlap ret = _first;
+       ret.update(_second);
+       return ret;
+}
\ No newline at end of file
index e18bc9e91b180f9bb5bcb2d92cc952719f7a164b..3793e0f130a94b4f25089edf38ad830891499ae8 100644 (file)
@@ -6,6 +6,7 @@
 
 class Shader;
 class Texture;
+class Overlap;
 
 typedef unsigned int uint;
 
@@ -30,8 +31,14 @@ public:
        virtual void getBBsp(vec3& size, vec3& pos) const;
 
        /* calls Colliding to check if self collides with model*/
-       static void isColliding(float& overlap, float& inside, const Model* modelThis, const Model* modelOther, const mat4& modelMatThis, const mat4& modelMatOther);
-       void checkColliding(float& overlap, float& inside, const Model* model, const mat4& modelMatThis, const mat4& modelMatOther) const;
+       static Overlap isColliding(const Model* modelThis, const Model* modelOther, const mat4& modelMatThis, const mat4& modelMatOther);
+       
+
+       /* test self against other*/
+       virtual Overlap checkColS2O(const mat4& mMat, const Model* model, const mat4& modelMat) const;
+       virtual Overlap checkColO2SBox(const mat4& mMat, const vec3& pos, const vec3& size, const mat4& modelMat) const;
+       //virtual void checkColO2STriangle() const;
+       virtual void checkColliding(float& overlap, float& inside, const Model* model, const mat4& modelMatThis, const mat4& modelMatOther) const;
 
        static Model* getBBoxModel();
        static Model* getSkyBoxModel();
@@ -78,3 +85,27 @@ private:
 
 };
 
+class Overlap
+{
+public:
+       Overlap();
+       Overlap(float over, float in);
+       ~Overlap();
+
+       float overlap() const;
+       float inside() const;
+
+       operator bool() const;
+
+       static Overlap merge(const Overlap& first, const Overlap& second);
+       void update(float over, float in);
+       void update(const Overlap& other);
+
+protected:
+       bool unset;
+       float over;
+       float in;
+
+       vec3 center;
+       vec3 normal;
+};
\ No newline at end of file
index 8caf26fbb07c4580f880a4225a5e057f84ef80d9..7ffd15619f66840cf92a5f24224eaac3e6d6f491 100644 (file)
@@ -147,11 +147,11 @@ void SceneObject::checkCollision(SceneObject* _first, SceneObject* _second)
        if (_first->collision_ignore || _second->collision_ignore)
                return;
 
-       float isOverlaping, isInside;
-       Model::isColliding(isOverlaping, isInside, _first->model,_second->model, _first->modelMat, _second->modelMat);
+
+       Overlap over = Model::isColliding(_first->model,_second->model, _first->modelMat, _second->modelMat);
        /* overlapping 1 yes, 0 touch, -1 no*/
        /* inside 1 yes, 0 touch, -1 no*/
-       if (isOverlaping >= 0)
+       if (over.overlap() >= 0)
        {
                _first->collides(_second);
                _second->collides(_first);