From 9a397b16998705c56f56ab1f26664dd6ee0df3f8 Mon Sep 17 00:00:00 2001 From: Peter Schaefer Date: Tue, 16 Jun 2015 16:13:34 +0200 Subject: [PATCH] Bullet Shapes looking correct --- Weave/Graphix/Model/IMesh.cpp | 42 +++++++++++++------------------ Weave/Graphix/Model/IMesh.h | 2 +- Weave/Graphix/Model/IMetaMesh.cpp | 6 ++--- Weave/Graphix/Model/IMetaMesh.h | 2 +- Weave/Graphix/Model/Model.cpp | 4 +-- Weave/Graphix/Model/Model.h | 2 +- Weave/Scene/Level.cpp | 2 +- 7 files changed, 27 insertions(+), 33 deletions(-) diff --git a/Weave/Graphix/Model/IMesh.cpp b/Weave/Graphix/Model/IMesh.cpp index c512cde..8cf696e 100644 --- a/Weave/Graphix/Model/IMesh.cpp +++ b/Weave/Graphix/Model/IMesh.cpp @@ -39,33 +39,27 @@ using std::array; // //} -IMesh::IMesh(const aiMesh* _mesh, const vec3& _scale) : modelpath("IMesh") +IMesh::IMesh(const aiMesh* _mesh, const vec3& _scale, bool _isConvex) : modelpath("IMesh") { import(_mesh, numvertices, numfaces, vertex, uvs, normals, index, _scale); - btConvexHullShape* tmp_shape = new btConvexHullShape(); - btTriangleMesh* tmp_mesh = new btTriangleMesh(); - - for (uint i = 0; i < numfaces * 3; i += 3) - { - tmp_mesh->addTriangle( - btVector3(vertex[index[i]], vertex[index[i] + 1], vertex[index[i] + 2]), - btVector3(vertex[index[i + 1]], vertex[index[i + 1] + 1], vertex[index[i + 1] + 2]), - btVector3(vertex[index[i + 2]], vertex[index[i + 2] + 1], vertex[index[i + 2] + 2]), - false); - - } - - //Message::info(std::to_string(tmp_mesh->getNumTriangles())); - - for (uint i = 0; i < numvertices*3; i += 3) - tmp_shape->addPoint(btVector3(vertex[i], vertex[i + 1], vertex[i + 2])); - - //tmp_shape->recalcLocalAabb(); - //bt_collision_shape = new btBvhTriangleMeshShape(tmp_mesh,true); - bt_collision_shape = new btConvexTriangleMeshShape(tmp_mesh,true); - //bt_collision_shape = tmp_shape; - + btTriangleIndexVertexArray* meshInterface = new btTriangleIndexVertexArray(); + btIndexedMesh part; + part.m_vertexBase = (const unsigned char*)vertex; + part.m_vertexStride = sizeof(float) * 3; + part.m_numVertices = numvertices; + part.m_triangleIndexBase = (const unsigned char*)index; + part.m_triangleIndexStride = sizeof(uint) * 3; + part.m_numTriangles = numfaces; + part.m_indexType = PHY_INTEGER; + + meshInterface->addIndexedMesh(part, PHY_INTEGER); + + if (_isConvex) + bt_collision_shape = new btConvexTriangleMeshShape(meshInterface, true); + else + bt_collision_shape = new btBvhTriangleMeshShape(meshInterface, true); + 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/IMesh.h b/Weave/Graphix/Model/IMesh.h index ce4f10a..573b0f4 100644 --- a/Weave/Graphix/Model/IMesh.h +++ b/Weave/Graphix/Model/IMesh.h @@ -16,7 +16,7 @@ public: class Node; // IMesh(const std::string& modelpath, uint index=0); - IMesh(const aiMesh* mesh, const vec3& scale = vec3(1.f)); + IMesh(const aiMesh* mesh, const vec3& scale = vec3(1.f), bool isConvex=true); virtual ~IMesh(); diff --git a/Weave/Graphix/Model/IMetaMesh.cpp b/Weave/Graphix/Model/IMetaMesh.cpp index 8c7c9de..ee47474 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, const vec3& _scale) : modelpath(_modelpath) +IMetaMesh::IMetaMesh(const string& _modelpath, const vec3& _scale, bool _isConvex) : modelpath(_modelpath) { Assimp::Importer importer; @@ -41,7 +41,7 @@ IMetaMesh::IMetaMesh(const string& _modelpath, const vec3& _scale) : 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); tmpModelMatClean = removeScale(tmpModelMat); tmpModelMatBT.setFromOpenGLMatrix(value_ptr(tmpModelMatClean)); - tmpIMesh = new IMesh(scene->mMeshes[0], _scale* getScale(tmpModelMat)); + tmpIMesh = new IMesh(scene->mMeshes[0], _scale* getScale(tmpModelMat),_isConvex); models.push_back(std::pair(tmpIMesh,tmpModelMatClean)); tmp_shape->addChildShape(tmpModelMatBT,*tmpIMesh); @@ -52,7 +52,7 @@ IMetaMesh::IMetaMesh(const string& _modelpath, const vec3& _scale) : 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); tmpModelMatClean = removeScale(tmpModelMat); tmpModelMatBT.setFromOpenGLMatrix(value_ptr(tmpModelMatClean)); - tmpIMesh = new IMesh(scene->mMeshes[i], _scale* getScale(tmpModelMat)); + tmpIMesh = new IMesh(scene->mMeshes[i], _scale* getScale(tmpModelMat), _isConvex); models.push_back(std::pair(tmpIMesh, tmpModelMatClean)); tmp_shape->addChildShape(tmpModelMatBT, *tmpIMesh); diff --git a/Weave/Graphix/Model/IMetaMesh.h b/Weave/Graphix/Model/IMetaMesh.h index 2422aeb..50a2b1f 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, const vec3& scale = vec3(1.f)); + IMetaMesh(const std::string& modelpath, const vec3& scale = vec3(1.f), bool isConvex = true); ~IMetaMesh(); void bindShader(Shader* shader) override; diff --git a/Weave/Graphix/Model/Model.cpp b/Weave/Graphix/Model/Model.cpp index dae550c..c1244e0 100644 --- a/Weave/Graphix/Model/Model.cpp +++ b/Weave/Graphix/Model/Model.cpp @@ -253,12 +253,12 @@ Model* Model::getSkyBoxModel() return SkyBoxModel; } -Model* Model::newIMetaModel(const string& _modelpath) +Model* Model::newIMetaModel(const string& _modelpath, bool _isConvex) { Model*& ptr = IMetaModel[_modelpath]; if (ptr == nullptr) { - ptr = new IMetaMesh(_modelpath); + ptr = new IMetaMesh(_modelpath,vec3(1.f),_isConvex); //ptr->bindShader(Graphix::shader_BBox); } ++IMetaModel_count[_modelpath]; diff --git a/Weave/Graphix/Model/Model.h b/Weave/Graphix/Model/Model.h index 356c478..499d2a1 100644 --- a/Weave/Graphix/Model/Model.h +++ b/Weave/Graphix/Model/Model.h @@ -37,7 +37,7 @@ public: static Model* getBBoxModel(); static Model* getSkyBoxModel(); - static Model* newIMetaModel(const std::string& modelpath); + static Model* newIMetaModel(const std::string& modelpath,bool isConvex=true); static void deleteBBoxModel(); static void deleteSkyBoxModel(); diff --git a/Weave/Scene/Level.cpp b/Weave/Scene/Level.cpp index 03f67a2..e58d2a0 100644 --- a/Weave/Scene/Level.cpp +++ b/Weave/Scene/Level.cpp @@ -7,7 +7,7 @@ #include "../GLM.h" -Level::Level(Shader* _shader, std::string _modelpath, std::string _texturepath) : SceneObject(_shader, mat4(1.f), vec4(1.f), _modelpath, _texturepath) +Level::Level(Shader* _shader, std::string _modelpath, std::string _texturepath) : SceneObject(_shader, mat4(1.f), vec4(1.f), Model::newIMetaModel(_modelpath,false), _texturepath) { collide_group = COL_LEVEL; collide_with = COL_MARVIN | COL_ENEMY; -- 2.47.3