]> git.leopard-lacewing.eu Git - cgue_weave.git/commitdiff
split scaling and transformation
authorPeter Schaefer <schaeferpm@gmail.com>
Thu, 4 Jun 2015 06:51:32 +0000 (08:51 +0200)
committerPeter Schaefer <schaeferpm@gmail.com>
Thu, 4 Jun 2015 06:51:32 +0000 (08:51 +0200)
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

index 4d07f26e854699ac4346856d50599e22ca530e8e..3b30a6d1f48bae9ceef421dd6a9e1896634dec9c 100644 (file)
@@ -32,50 +32,60 @@ typedef IMesh::Node Node;
 // Fordere mindestens prozentuelles gleichgewicht .1 = 10%
 #define HEAP_SCALE_BOUND .1f
 
-IMesh::IMesh(const string& _modelpath, uint _mindex) : modelpath(_modelpath), root(nullptr)
+//IMesh::IMesh(const string& _modelpath, uint _mindex) : modelpath(_modelpath), root(nullptr)
+//{
+//
+//     import(_modelpath, numvertices, numfaces, vertex, uvs, normals, index, _mindex);
+//
+//     btConvexHullShape* tmp_shape = new btConvexHullShape();
+//
+//     for (uint i = 0; i < numvertices; i += 3)
+//             tmp_shape->addPoint(btVector3(vertex[i], vertex[i + 1], vertex[i + 2]));
+//
+//     bt_collision_shape = tmp_shape;
+//
+//#ifdef COMPARE_MTREE
+//     root = buildMeshTree(mat4(1.f), numvertices, numfaces, vertex, normals, index);
+//
+//     vec3 min, max;
+//     setBB(*root);
+//#else
+//     setBB(numvertices, vertex);
+//#endif
+//
+//     genBuffer(vertexBuffer, numvertices * 3 * sizeof(float), (void*)vertex);
+//     genBuffer(normalBuffer, numvertices * 3 * sizeof(float), (void*)normals);
+//     genBuffer(uvBuffer, numvertices * 2 * sizeof(float), (void*)uvs);
+//     genBuffer(indexBuffer, numfaces * 3 * sizeof(uint), (void*)index);
+//
+//}
+
+IMesh::IMesh(const aiMesh* _mesh, const mat4& _transformation, const vec3& _scale) : modelMat(removeScale(_transformation)), modelpath("IMesh"), root(nullptr)
 {
 
-       import(_modelpath, numvertices, numfaces, vertex, uvs, normals, index, _mindex);
+       import(_mesh, numvertices, numfaces, vertex, uvs, normals, index, _scale * getScale(_transformation));
 
        btConvexHullShape* tmp_shape = new btConvexHullShape();
 
-       for (int i = 0; i < numvertices; i += 3)
+       for (uint i = 0; i < numvertices; i += 3)
                tmp_shape->addPoint(btVector3(vertex[i], vertex[i + 1], vertex[i + 2]));
 
+       //tmp_shape->recalcLocalAabb();
        bt_collision_shape = tmp_shape;
 
-#ifdef COMPARE_MTREE
-       root = buildMeshTree(mat4(1.f), numvertices, numfaces, vertex, normals, index);
-
-       vec3 min, max;
-       setBB(*root);
-#else
-       setBB(numvertices, vertex);
-#endif
-
-       genBuffer(vertexBuffer, numvertices * 3 * sizeof(float), (void*)vertex);
-       genBuffer(normalBuffer, numvertices * 3 * sizeof(float), (void*)normals);
-       genBuffer(uvBuffer, numvertices * 2 * sizeof(float), (void*)uvs);
-       genBuffer(indexBuffer, numfaces * 3 * sizeof(uint), (void*)index);
-
-}
-
-IMesh::IMesh(const aiMesh* _mesh, const mat4& _transformation) : modelMat(_transformation), modelpath("IMesh"), root(nullptr)
-{
-
-       import(_mesh, numvertices, numfaces, vertex, uvs, normals, index);
-
-       btConvexHullShape* tmp_shape = new btConvexHullShape();
+       btVector3 minB,maxB;
+       btTransform t;
 
-       for (int i = 0; i < numvertices; i += 3)
-               tmp_shape->addPoint(btVector3(vertex[i], vertex[i + 1], vertex[i + 2]));
+       tmp_shape->getAabb(t, minB, maxB);
 
-       bt_collision_shape = tmp_shape;
+       minB = t(minB);
+       maxB = t(maxB);
 
-       btVector3 min,max;
-       btTransform t;
+       vec3 min = (vec3)(modelMat * vec4(minB.getX(), minB.getY(), minB.getZ(),0.f));
+       vec3 max = (vec3)(modelMat * vec4(maxB.getX(), maxB.getY(), maxB.getZ(), 0.f));
 
-       bt_collision_shape->getAabb(t,min,max);
+       vec3 size = (max - min) * .5f;
+       vec3 center = (max + min) * .5f;
 
 #ifdef COMPARE_MTREE
        root = buildMeshTree(_transformation, numvertices, numfaces, vertex, normals, index);
index 7afa12453edd32ec6d17160ecdc3dd46e6393920..c84aa870c93abfe52bdade03cd2efdf8f9c31807 100644 (file)
@@ -15,8 +15,8 @@ class IMesh : public Model
 public:
        class Node;
 
-       IMesh(const std::string& modelpath, uint index=0);
-       IMesh(const aiMesh* mesh, const mat4& transformation);
+//     IMesh(const std::string& modelpath, uint index=0);
+       IMesh(const aiMesh* mesh, const mat4& transformation, const vec3& scale = vec3(1.f));
 
        virtual ~IMesh();
 
index 407654eb27a389ffd25882c5c91dfc7896eda986..c90b1e2138744f43460582a3cf090c1b4478b14e 100644 (file)
@@ -12,7 +12,7 @@
 
 using std::string;
 
-IMetaMesh::IMetaMesh(const string& _modelpath) : modelpath(_modelpath)
+IMetaMesh::IMetaMesh(const string& _modelpath, const vec3& _scale) : modelpath(_modelpath)
 {
        Assimp::Importer importer;
 
@@ -39,7 +39,7 @@ IMetaMesh::IMetaMesh(const string& _modelpath) : modelpath(_modelpath)
 
                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[0], tmpModelMat);
+               tmpIMesh = new IMesh(scene->mMeshes[0], tmpModelMat, _scale);
                models.push_back(tmpIMesh);
                setBB(tmpIMesh->getBBox());
 
@@ -51,7 +51,7 @@ IMetaMesh::IMetaMesh(const string& _modelpath) : modelpath(_modelpath)
                        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);
+                       tmpIMesh = new IMesh(scene->mMeshes[i], tmpModelMat,_scale);
                        models.push_back(tmpIMesh);
                        updateBB(tmpIMesh->getBBox());
 
index 6bac683006d75f074c9cb736f3795810436e4da5..84ab3b8d65ec145b4aaaa83d79f34ee1243aa184 100644 (file)
@@ -14,7 +14,7 @@ class Texture;
 class IMetaMesh : public Model
 {
 public:
-       IMetaMesh(const std::string& modelpath);
+       IMetaMesh(const std::string& modelpath, const vec3& scale = vec3(1.f));
        ~IMetaMesh();
 
        void bindShader(Shader* shader) override;
index 201c1850d99de4a37d95da72eb20cccaf7890785..b9773b2aa54a8c5f3ca24c5756f3f38116467855 100644 (file)
@@ -121,6 +121,14 @@ void Model::drawBBox(const mat4& _modelMat, const vec4& _color) const
        if (tmp >= 0)
                glUniform4fv(tmp, 1, value_ptr(_color));
        Graphix::getGlError();
+
+       btVector3 min, max;
+       btTransform trans;
+       bt_collision_shape->getAabb(trans, min, max);
+
+       btVector3 size = (max - min) * .5f;
+       btVector3 center = trans((max + min) * .5f);
+
        getBBoxModel()->drawModel(Graphix::shader_BBox, NULL, _modelMat*translate(box.center())*glm::scale(box.size()*2.f));
 }
 
@@ -477,7 +485,7 @@ bool Model::import(const string& _modelpath, uint& numvertices, uint& numfaces,
        return import(scene->mMeshes[_mindex], numvertices, numfaces, vertex, uvs, normals, index);
 }
 
-bool Model::import(const aiMesh* mesh, uint& numvertices, uint& numfaces, float*& vertex, float*& uvs, float*& normals, uint*& index)
+bool Model::import(const aiMesh* mesh, uint& numvertices, uint& numfaces, float*& vertex, float*& uvs, float*& normals, uint*& index, const vec3& _scale)
 {
 
        numvertices = mesh->mNumVertices;
@@ -492,9 +500,9 @@ bool Model::import(const aiMesh* mesh, uint& numvertices, uint& numfaces, float*
        //load vertices from Mesh
        for (uint i = 0; i < numvertices; i++)
        {
-               vertex[3 * i] = mesh->mVertices[i].x;
-               vertex[3 * i + 1] = mesh->mVertices[i].y;
-               vertex[3 * i + 2] = mesh->mVertices[i].z;
+               vertex[3 * i] = _scale.x *  mesh->mVertices[i].x;
+               vertex[3 * i + 1] = _scale.y * mesh->mVertices[i].y;
+               vertex[3 * i + 2] = _scale.z * mesh->mVertices[i].z;
        }
 
        //load UVs from Mesh
@@ -515,9 +523,9 @@ bool Model::import(const aiMesh* mesh, uint& numvertices, uint& numfaces, float*
        //load normals from Mesh
        for (uint i = 0; i < numvertices; i++)
        {
-               normals[3 * i] = mesh->mNormals[i].x;
-               normals[3 * i + 1] = mesh->mNormals[i].y;
-               normals[3 * i + 2] = mesh->mNormals[i].z;
+               normals[3 * i] = mesh->mNormals[i].x / _scale.x;
+               normals[3 * i + 1] = mesh->mNormals[i].y / _scale.y;
+               normals[3 * i + 2] = mesh->mNormals[i].z / _scale.z;
        }
 
        return true;
index 9802bf57c62422f1cc4eb03ba0a5ea5b9ef6387a..3b4ee4bae0e7467a0b1551af6e17c5a4c338da35 100644 (file)
@@ -130,7 +130,7 @@ public:
 
        // 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);
+       static bool import(const aiMesh* mesh, uint& numvertices, uint& numfaces, float*& vertex, float*& uvs, float*& normals, uint*& index, const vec3& scale = vec3(1.f));
 
 protected:
        Model();