From: Peter Schaefer Date: Thu, 4 Jun 2015 06:51:32 +0000 (+0200) Subject: split scaling and transformation X-Git-Url: https://git.leopard-lacewing.eu/?a=commitdiff_plain;h=216323d9bf6e0015d37e223ba5c7d121770437d1;p=cgue_weave.git split scaling and transformation --- diff --git a/Weave/Graphix/Model/IMesh.cpp b/Weave/Graphix/Model/IMesh.cpp index 4d07f26..3b30a6d 100644 --- a/Weave/Graphix/Model/IMesh.cpp +++ b/Weave/Graphix/Model/IMesh.cpp @@ -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); diff --git a/Weave/Graphix/Model/IMesh.h b/Weave/Graphix/Model/IMesh.h index 7afa124..c84aa87 100644 --- a/Weave/Graphix/Model/IMesh.h +++ b/Weave/Graphix/Model/IMesh.h @@ -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(); diff --git a/Weave/Graphix/Model/IMetaMesh.cpp b/Weave/Graphix/Model/IMetaMesh.cpp index 407654e..c90b1e2 100644 --- a/Weave/Graphix/Model/IMetaMesh.cpp +++ b/Weave/Graphix/Model/IMetaMesh.cpp @@ -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()); diff --git a/Weave/Graphix/Model/IMetaMesh.h b/Weave/Graphix/Model/IMetaMesh.h index 6bac683..84ab3b8 100644 --- a/Weave/Graphix/Model/IMetaMesh.h +++ b/Weave/Graphix/Model/IMetaMesh.h @@ -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; diff --git a/Weave/Graphix/Model/Model.cpp b/Weave/Graphix/Model/Model.cpp index 201c185..b9773b2 100644 --- a/Weave/Graphix/Model/Model.cpp +++ b/Weave/Graphix/Model/Model.cpp @@ -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; diff --git a/Weave/Graphix/Model/Model.h b/Weave/Graphix/Model/Model.h index 9802bf5..3b4ee4b 100644 --- a/Weave/Graphix/Model/Model.h +++ b/Weave/Graphix/Model/Model.h @@ -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();