From ff4e7ac001ef8a84ef1086c99a5b19fdb5610fce Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Wed, 28 Jun 2017 21:41:31 +0200 Subject: [PATCH] [DEV] manage convex shape --- ege/Entity.cpp | 16 +++++------ ege/physics/Component.cpp | 44 ++++++++++++++--------------- ege/physics/shape/Concave.hpp | 8 ++++-- ege/resource/Mesh.cpp | 27 ++++++++++++------ ege/resource/tools/FaceIndexing.hpp | 2 +- 5 files changed, 56 insertions(+), 41 deletions(-) diff --git a/ege/Entity.cpp b/ege/Entity.cpp index 785ee80..ecdf489 100644 --- a/ege/Entity.cpp +++ b/ege/Entity.cpp @@ -42,7 +42,7 @@ void ege::Entity::addComponent(const ememory::SharedPtr& _ref) { ememory::SharedPtr componentRemoved; int32_t findId = -1; // check if not exist - for (int32_t iii=0; iii& _ref) { } // try to add in an empty slot if (findId == -1) { - for (int32_t iii=0; iii& _ref) { findId = m_component.size(); m_component.push_back(_ref); } - for (int32_t iii=0; iii& _ref) { } // notify new component of all previously added component: componentRemoved = _ref; - for (int32_t iii=0; iii& _ref) { } int32_t findId = -1; // check if not exist - for (int32_t iii=0; iii& _ref) { EGE_ERROR("try to remove an unexisting component"); return; } - for (int32_t iii=0; iii componentRemoved; // check if not exist - for (int32_t iii=0; iii can not cast in Concave"); continue; } - - /* - float* vertices = (float*)&tmpElement->getVertex()[0]; - int* indices = (int*)&tmpElement->getIndices()[0]; - - int32_t nbVertices = tmpElement->getVertex().size(); - int32_t nbTriangles = tmpElement->getIndices().size()/3; - */ + #if 0 static const std::vector vertices = {vec3(-100.0f,-100.0f,-50.0f),vec3(100.0f,-100.0f,-50.0f),vec3(100.0f,100.0f,-50.0f)}; - static const std::vector indices = {1,2,3}; + static const std::vector indices = {0,1,2}; ephysics::TriangleVertexArray* triangleArray = new ephysics::TriangleVertexArray(vertices, indices); + #else + ephysics::TriangleVertexArray* triangleArray = new ephysics::TriangleVertexArray(tmpElement->getVertex(), tmpElement->getIndices()); + #endif // Now that we have a TriangleVertexArray, we need to create a TriangleMesh and add the TriangleVertexArray into it as a subpart. // Once this is done, we can create the actual ConcaveMeshShape and add it to the body we want to simulate as in the following example: ephysics::TriangleMesh* triangleMesh = new ephysics::TriangleMesh(); @@ -221,7 +217,7 @@ void ege::physics::Component::generate() { // TODO : Manage memory leak ... ephysics::ConcaveShape* shape = new ephysics::ConcaveMeshShape(triangleMesh); // The ephysic use Y as UP ==> ege use Z as UP - //etk::Quaternion orientation = it->getOrientation() * ephysics::Quaternion(-0.707107, 0, 0, 0.707107); + etk::Quaternion orientation = it->getOrientation() * etk::Quaternion(-0.707107, 0, 0, 0.707107); etk::Transform3D transform(it->getOrigin(), it->getOrientation()); ephysics::ProxyShape* proxyShape = m_rigidBody->addCollisionShape(shape, transform, it->getMass()); proxyShape->setUserData(this); @@ -490,6 +486,22 @@ void ege::physics::Component::drawShape(ememory::SharedPtrdrawSphere(tmpElement->getRadius(), 10, 10, transformationMatrixLocal, tmpColor); break; } + case ege::physics::Shape::type::concave: { + EGE_DEBUG(" concave"); + const ege::physics::shape::Concave* tmpElement = it->toConcave(); + if (tmpElement == nullptr) { + EGE_ERROR(" concave ==> can not cast in convexHull"); + continue; + } + etk::Transform3D transformLocal(it->getOrigin(), it->getOrientation()); + transformLocal.getOpenGLMatrix(mmm); + mat4 transformationMatrixLocal(mmm); + transformationMatrixLocal.transpose(); + transformationMatrixLocal = transformationMatrix * transformationMatrixLocal; + + _draw->drawTriangles(tmpElement->getVertex(), tmpElement->getIndices(), transformationMatrixLocal, tmpColor); + break; + } case ege::physics::Shape::type::convexHull: { EGE_DEBUG(" convexHull"); const ege::physics::shape::ConvexHull* tmpElement = it->toConvexHull(); @@ -497,18 +509,6 @@ void ege::physics::Component::drawShape(ememory::SharedPtr can not cast in convexHull"); continue; } - /* - btConvexHullShape* tmpShape = new btConvexHullShape(&(tmpElement->getPointList()[0].x()), tmpElement->getPointList().size()); - if (tmpShape != nullptr) { - if (outputShape == nullptr) { - return tmpShape; - } else { - vec4 qqq = tmpElement->getQuaternion(); - const btTransform localTransform(btQuaternion(qqq.x(),qqq.y(),qqq.z(),qqq.w()), tmpElement->getOrigin()); - outputShape->addChildShape(localTransform, tmpShape); - } - } - */ break; } default : diff --git a/ege/physics/shape/Concave.hpp b/ege/physics/shape/Concave.hpp index c5917a8..87217b8 100644 --- a/ege/physics/shape/Concave.hpp +++ b/ege/physics/shape/Concave.hpp @@ -33,7 +33,7 @@ namespace ege { } private: std::vector m_listVertex; - std::vector m_indices; // TODO : do it better: this is dependent of ephysics + std::vector m_indices; public: void clear() { m_listVertex.clear(); @@ -49,6 +49,10 @@ namespace ege { return; } */ + if (_index.size()%3 != 0 ) { + EGE_ERROR("wrong number of faces : " << _index.size() << " ==> not a multiple of 3"); + return; + } for (auto &it: _index) { m_indices.push_back(it); } @@ -56,7 +60,7 @@ namespace ege { const std::vector& getVertex() const { return m_listVertex; } - const std::vector& getIndices() const { + const std::vector& getIndices() const { return m_indices; } }; diff --git a/ege/resource/Mesh.cpp b/ege/resource/Mesh.cpp index 030822e..2721e5b 100644 --- a/ege/resource/Mesh.cpp +++ b/ege/resource/Mesh.cpp @@ -159,7 +159,7 @@ void ege::resource::Mesh::draw(mat4& _positionMatrix, int32_t nbElementDrawTheoric = 0; int32_t nbElementDraw = 0; #endif - for (int32_t kkk=0; kkkgetRenderMode()) { case gale::openGL::renderMode::triangle: case gale::openGL::renderMode::triangleStrip: @@ -296,7 +296,7 @@ void ege::resource::Mesh::drawNormal(mat4& _positionMatrix, center += position; } center /= float(nbIndicInFace); - int32_t index = tmpFaceList.m_faces[iii].m_normal[0]; + size_t index = tmpFaceList.m_faces[iii].m_normal[0]; if (index >= m_listFacesNormal.size()) { EGE_ERROR("not enougth normal in the buffer ... " << index << " >= " << m_listFacesNormal.size()); return; @@ -401,7 +401,7 @@ void ege::resource::Mesh::generateVBO() { // generate element in 2 pass: // - create new index depending on a vertex is a unique component of position, texture, normal // - the index list generation (can be dynamic ... (TODO later)) - for (int32_t kkk=0; kkk>& ege::resource::Mesh: return m_physics; } tmpElement->clear(); + //EGE_INFO(" add vertices : " << m_listVertex); tmpElement->setListOfVertex(m_listVertex); - for (int32_t kkk=0; kkkaddTriangle(m_listFaces.getValue(kkk).m_index); + for (size_t kkk=0; kkk index; + for (auto &it : m_listFaces.getValue(kkk).m_faces) { + index.push_back(it.m_vertex[0]); + index.push_back(it.m_vertex[1]); + index.push_back(it.m_vertex[2]); + } + //EGE_INFO(" add triangle : " << m_listFaces.getValue(kkk).m_index); + + //tmpElement->addTriangle(m_listFaces.getValue(kkk).m_index); + tmpElement->addTriangle(index); } + //EGE_CRITICAL("kjlkj"); // Can have only one concave element in a mesh ... - return m_physics; + //return m_physics; } } return m_physics; diff --git a/ege/resource/tools/FaceIndexing.hpp b/ege/resource/tools/FaceIndexing.hpp index ba8a2a5..dcd05ee 100644 --- a/ege/resource/tools/FaceIndexing.hpp +++ b/ege/resource/tools/FaceIndexing.hpp @@ -9,7 +9,7 @@ namespace ege { class FaceIndexing { public: std::vector m_faces; - std::vector m_index; + std::vector m_index; //!< index of the vertex in the VBO not in the Mesh.m_listVertex }; }