From 3c83e6a76b9d2b5fc664ae9b818e3a511f17529d Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Tue, 30 Sep 2014 22:09:21 +0200 Subject: [PATCH] [DEV] add special system --- ege/ElementGame.h | 2 +- ege/resource/Mesh.cpp | 118 +++++++----------------- ege/resource/Mesh.h | 50 ++--------- ege/resource/tools/Face.cpp | 0 ege/resource/tools/Face.h | 52 +++++++++++ ege/resource/tools/FaceIndexing.cpp | 0 ege/resource/tools/FaceIndexing.h | 21 +++++ ege/resource/tools/icoSphere.cpp | 133 ++++++++++++++++++++++++++++ ege/resource/tools/icoSphere.h | 26 ++++++ ege/resource/tools/isoSphere.cpp | 27 ++++++ ege/resource/tools/isoSphere.h | 26 ++++++ ege/resource/tools/viewBox.cpp | 103 +++++++++++++++++++++ ege/resource/tools/viewBox.h | 26 ++++++ lutin_ege.py | 3 + 14 files changed, 453 insertions(+), 134 deletions(-) create mode 100644 ege/resource/tools/Face.cpp create mode 100644 ege/resource/tools/Face.h create mode 100644 ege/resource/tools/FaceIndexing.cpp create mode 100644 ege/resource/tools/FaceIndexing.h create mode 100644 ege/resource/tools/icoSphere.cpp create mode 100644 ege/resource/tools/icoSphere.h create mode 100644 ege/resource/tools/isoSphere.cpp create mode 100644 ege/resource/tools/isoSphere.h create mode 100644 ege/resource/tools/viewBox.cpp create mode 100644 ege/resource/tools/viewBox.h diff --git a/ege/ElementGame.h b/ege/ElementGame.h index f0ec6a7..ef8fd59 100644 --- a/ege/ElementGame.h +++ b/ege/ElementGame.h @@ -74,7 +74,7 @@ namespace ege { inline uint32_t getUID() const { return m_uID; }; - private: + protected: std::shared_ptr m_mesh; //!< Mesh of the Element (can be nullptr) btCollisionShape* m_shape; //!< shape of the element (set a copy here to have the debug display of it) public: diff --git a/ege/resource/Mesh.cpp b/ege/resource/Mesh.cpp index 90c4d1e..e049b69 100644 --- a/ege/resource/Mesh.cpp +++ b/ege/resource/Mesh.cpp @@ -10,6 +10,9 @@ #include #include #include +#include +#include +#include #undef __class__ #define __class__ "resource::Mesh" @@ -20,11 +23,11 @@ ege::resource::Mesh::Mesh() : m_pointerShape(nullptr), m_functionFreeShape(nullptr) { addObjectType("ege::resource::Mesh"); - } void ege::resource::Mesh::init(const std::string& _fileName, const std::string& _shaderName) { ewol::Resource::init(_fileName); + addObjectType("ege::resource::Mesh"); EGE_VERBOSE("Load a new mesh : '" << _fileName << "'"); // get the shader resource : m_GLPosition = 0; @@ -69,6 +72,7 @@ void ege::resource::Mesh::init(const std::string& _fileName, const std::string& } } + ege::resource::Mesh::~Mesh() { // remove dynamics dependencies : if (m_functionFreeShape!=nullptr) { @@ -77,6 +81,23 @@ ege::resource::Mesh::~Mesh() { } } +void ege::resource::Mesh::clean() { + for (auto &elem : m_physics) { + delete(elem); + } + m_physics.clear(); + for (int32_t iii=0; iii #include #include +#include +#include + #include // 3 "float" elements #define MESH_VBO_VERTICES (0) @@ -31,51 +34,6 @@ #define MESH_VBO_COLOR (4) namespace ege { - /** - * @not-in-doc - */ - class Face { - public: - int32_t m_vertex[3]; - int32_t m_uv[3]; - int32_t m_normal[3]; - public: - Face() {}; - Face(int32_t v1, int32_t t1, - int32_t v2, int32_t t2, - int32_t v3, int32_t t3) { - m_vertex[0] = v1; - m_vertex[1] = v2; - m_vertex[2] = v3; - m_uv[0] = t1; - m_uv[1] = t2; - m_uv[2] = t3; - m_normal[0] = -1; - m_normal[1] = -1; - m_normal[2] = -1; - }; - Face(int32_t v1, int32_t t1, int32_t n1, - int32_t v2, int32_t t2, int32_t n2, - int32_t v3, int32_t t3, int32_t n3) { - m_vertex[0] = v1; - m_vertex[1] = v2; - m_vertex[2] = v3; - m_uv[0] = t1; - m_uv[1] = t2; - m_uv[2] = t3; - m_normal[0] = n1; - m_normal[1] = n2; - m_normal[2] = n3; - }; - }; - /** - * @not-in-doc - */ - class FaceIndexing { - public: - std::vector m_faces; - std::vector m_index; - }; namespace resource { class Mesh : public ewol::Resource { public: @@ -109,6 +67,7 @@ namespace ege { etk::Hash m_listFaces; //!< List of all Face for the mesh etk::Hash m_materials; std::vector m_physics; //!< collision shape module ... (independent of bullet lib) + void clean(); protected: std::shared_ptr m_verticesVBO; protected: @@ -131,6 +90,7 @@ namespace ege { void calculateNormaleEdge(); public : void createViewBox(const std::string& _materialName,float _size=1.0); + void createIcoSphere(const std::string& _materialName,float _size=1.0); private: bool loadOBJ(const std::string& _fileName); bool loadEMF(const std::string& _fileName); diff --git a/ege/resource/tools/Face.cpp b/ege/resource/tools/Face.cpp new file mode 100644 index 0000000..e69de29 diff --git a/ege/resource/tools/Face.h b/ege/resource/tools/Face.h new file mode 100644 index 0000000..5bce07a --- /dev/null +++ b/ege/resource/tools/Face.h @@ -0,0 +1,52 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#ifndef __EGE_FACE_H__ +#define __EGE_FACE_H__ + +namespace ege { + /** + * @not-in-doc + */ + class Face { + public: + int32_t m_vertex[3]; + int32_t m_uv[3]; + int32_t m_normal[3]; + public: + Face() {}; + Face(int32_t v1, int32_t t1, + int32_t v2, int32_t t2, + int32_t v3, int32_t t3) { + m_vertex[0] = v1; + m_vertex[1] = v2; + m_vertex[2] = v3; + m_uv[0] = t1; + m_uv[1] = t2; + m_uv[2] = t3; + m_normal[0] = -1; + m_normal[1] = -1; + m_normal[2] = -1; + }; + Face(int32_t v1, int32_t t1, int32_t n1, + int32_t v2, int32_t t2, int32_t n2, + int32_t v3, int32_t t3, int32_t n3) { + m_vertex[0] = v1; + m_vertex[1] = v2; + m_vertex[2] = v3; + m_uv[0] = t1; + m_uv[1] = t2; + m_uv[2] = t3; + m_normal[0] = n1; + m_normal[1] = n2; + m_normal[2] = n3; + }; + }; +}; + +#endif diff --git a/ege/resource/tools/FaceIndexing.cpp b/ege/resource/tools/FaceIndexing.cpp new file mode 100644 index 0000000..e69de29 diff --git a/ege/resource/tools/FaceIndexing.h b/ege/resource/tools/FaceIndexing.h new file mode 100644 index 0000000..442a946 --- /dev/null +++ b/ege/resource/tools/FaceIndexing.h @@ -0,0 +1,21 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#ifndef __EGE_FACE_INDEXING_H__ +#define __EGE_FACE_INDEXING_H__ + +namespace ege { + class FaceIndexing { + public: + std::vector m_faces; + std::vector m_index; + }; +}; + +#endif + diff --git a/ege/resource/tools/icoSphere.cpp b/ege/resource/tools/icoSphere.cpp new file mode 100644 index 0000000..2cb5a86 --- /dev/null +++ b/ege/resource/tools/icoSphere.cpp @@ -0,0 +1,133 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#include +#include + +// return index of point in the middle of p1 and p2 +static int32_t getMiddlePoint(std::vector& _listVertex, int32_t _p1, int32_t _p2) { + vec3 middle = (_listVertex[_p1] + _listVertex[_p2])*0.5f; + // check if this point exist: + int32_t id=0; + for (auto &point : _listVertex) { + if (middle == point) { + return id; + } + id++; + } + _listVertex.push_back(middle.normalize()); + return _listVertex.size()-1; +} + +void ege::icoSphere::create(etk::Hash& _materials, etk::Hash& _listFaces, std::vector& _listVertex, std::vector& _listUV, + const std::string& _materialName, int32_t _recursionLevel) { + /* + 5 + o + /| + / | + / | + / | + 11 / | 9 + Z o--------/...---------0 1 + | y | 4o o....|......|--o + | / | |. 0 | | / + | / | .| . |/ + | / | . | . | + |/ | / | . .| + o-------x | / | . / | + o--/....|........../--o + 10 / | 7o / 8 + / . . / + / . . / + o---------------o + 2 | / 3 + | / + |/ + o + 6 + */ + // create 12 vertices of a icosahedron + double size = 1.0; + double ttt = (1.0 + sqrt(5.0)) / 2.0*size; + EGE_ERROR("podition : " << ttt); + _listVertex.push_back(vec3(-size, ttt, 0.0).normalize()); // 0 + _listVertex.push_back(vec3( size, ttt, 0.0).normalize()); // 1 + _listVertex.push_back(vec3(-size, -ttt, 0.0).normalize()); // 2 + _listVertex.push_back(vec3( size, -ttt, 0.0).normalize()); // 3 + + _listVertex.push_back(vec3( 0.0, -1, ttt).normalize()); // 4 + _listVertex.push_back(vec3( 0.0, 1, ttt).normalize()); // 5 + _listVertex.push_back(vec3( 0.0, -1, -ttt).normalize()); // 6 + _listVertex.push_back(vec3( 0.0, 1, -ttt).normalize()); // 7 + + _listVertex.push_back(vec3( ttt, 0.0, -size).normalize()); // 8 + _listVertex.push_back(vec3( ttt, 0.0, size).normalize()); // 9 + _listVertex.push_back(vec3(-ttt, 0.0, -size).normalize()); // 10 + _listVertex.push_back(vec3(-ttt, 0.0, size).normalize()); // 11 + + // _listUV ==> TODO : very bad code ... get from viewBox .... + _listUV.push_back(vec2(0.0, 0.0 )); // 0 + _listUV.push_back(vec2(0.0, 1.0 )); // 1 + _listUV.push_back(vec2(1.0, 0.0 )); // 2 + + for (auto &elem : _listVertex) { + EGE_INFO("plop : " << etk::to_string(elem)); + } + + if (_listFaces.exist(_materialName) == false) { + FaceIndexing empty; + _listFaces.add(_materialName, empty); + } + { + FaceIndexing& tmpElement = _listFaces[_materialName]; + // 5 faces around point 0 + tmpElement.m_faces.push_back(Face(0,0, 11,1, 5,2)); + tmpElement.m_faces.push_back(Face(0,0, 5,1, 1,2)); + tmpElement.m_faces.push_back(Face(0,0, 1,1, 7,2)); + tmpElement.m_faces.push_back(Face(0,0, 7,1, 10,2)); + tmpElement.m_faces.push_back(Face(0,0, 10,1, 11,2)); + + // 5 adjacent faces + tmpElement.m_faces.push_back(Face( 1,0, 5,1, 9,2)); + tmpElement.m_faces.push_back(Face( 5,0, 11,1, 4,2)); + tmpElement.m_faces.push_back(Face(11,0, 10,1, 2,2)); + tmpElement.m_faces.push_back(Face(10,0, 7,1, 6,2)); + tmpElement.m_faces.push_back(Face( 7,0, 1,1, 8,2)); + + // 5 faces around point 3 + tmpElement.m_faces.push_back(Face(3,0, 9,1, 4,2)); + tmpElement.m_faces.push_back(Face(3,0, 4,1, 2,2)); + tmpElement.m_faces.push_back(Face(3,0, 2,1, 6,2)); + tmpElement.m_faces.push_back(Face(3,0, 6,1, 8,2)); + tmpElement.m_faces.push_back(Face(3,0, 8,1, 9,2)); + + // 5 adjacent faces + tmpElement.m_faces.push_back(Face(4,0, 9,1, 5,2)); + tmpElement.m_faces.push_back(Face(2,0, 4,1, 11,2)); + tmpElement.m_faces.push_back(Face(6,0, 2,1, 10,2)); + tmpElement.m_faces.push_back(Face(8,0, 6,1, 7,2)); + tmpElement.m_faces.push_back(Face(9,0, 8,1, 1,2)); + // refine triangles + for (int i = 0; i < _recursionLevel; i++) { + std::vector listFaces; + for (auto &tri : tmpElement.m_faces) { + // replace triangle by 4 triangles + int32_t a = getMiddlePoint(_listVertex, tri.m_vertex[0], tri.m_vertex[1]); + int32_t b = getMiddlePoint(_listVertex, tri.m_vertex[1], tri.m_vertex[2]); + int32_t c = getMiddlePoint(_listVertex, tri.m_vertex[2], tri.m_vertex[0]); + listFaces.push_back(Face(tri.m_vertex[0],0, a,1, c,2)); + listFaces.push_back(Face(tri.m_vertex[1],0, b,1, a,2)); + listFaces.push_back(Face(tri.m_vertex[2],0, c,1, b,2)); + listFaces.push_back(Face(a,0, b,1, c,2)); + } + tmpElement.m_faces = listFaces; + } + } +} + diff --git a/ege/resource/tools/icoSphere.h b/ege/resource/tools/icoSphere.h new file mode 100644 index 0000000..0a0770f --- /dev/null +++ b/ege/resource/tools/icoSphere.h @@ -0,0 +1,26 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#ifndef __EGE_TOOLS_ICOSPHERE_H__ +#define __EGE_TOOLS_ICOSPHERE_H__ + +#include +#include +#include +#include + +namespace ege { + namespace icoSphere { + void create(etk::Hash& _materials, etk::Hash& _listFaces, std::vector& _listVertex, std::vector& _listUV, + const std::string& _materialName, int32_t _recursionLevel); + }; +}; + + +#endif + diff --git a/ege/resource/tools/isoSphere.cpp b/ege/resource/tools/isoSphere.cpp new file mode 100644 index 0000000..4496f69 --- /dev/null +++ b/ege/resource/tools/isoSphere.cpp @@ -0,0 +1,27 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#include +#include + +void ege::isoSphere::create(etk::Hash& _materials, etk::Hash& _listFaces, std::vector& _listVertex, std::vector& _listUV, + const std::string& _materialName, int32_t _recursionLevel) { + _recursionLevel = std::max(_recursionLevel, 3); + float size = 1.0f; + //Add top and buttom point: + _listVertex.push_back(vec3(0.0f, 0.0f, size)); // 0 (top) + _listVertex.push_back(vec3(0.0f, 0.0f, -size)); // 1 (buttom) + _listUV.push_back(vec2(0.5, 1.0 )); // 0 + _listUV.push_back(vec2(0.5, 0.0 )); // 1 + + for (int32_t iii=0; iii<_recursionLevel; iii++) { + + + } + +} diff --git a/ege/resource/tools/isoSphere.h b/ege/resource/tools/isoSphere.h new file mode 100644 index 0000000..8ed6a3a --- /dev/null +++ b/ege/resource/tools/isoSphere.h @@ -0,0 +1,26 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#ifndef __EGE_TOOLS_ISOSPHERE_H__ +#define __EGE_TOOLS_ISOSPHERE_H__ + +#include +#include +#include +#include + +namespace ege { + namespace isoSphere { + void create(etk::Hash& _materials, etk::Hash& _listFaces, std::vector& _listVertex, std::vector& _listUV, + const std::string& _materialName, int32_t _recursionLevel); + }; +}; + + +#endif + diff --git a/ege/resource/tools/viewBox.cpp b/ege/resource/tools/viewBox.cpp new file mode 100644 index 0000000..25587ff --- /dev/null +++ b/ege/resource/tools/viewBox.cpp @@ -0,0 +1,103 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#include +#include + + + +void ege::viewBox::create(etk::Hash& _materials, etk::Hash& _listFaces, std::vector& _listVertex, std::vector& _listUV, + const std::string& _materialName, float _size) { + // This is the direct generation basis on the .obj system + /* + 5 6 + o---------------------o + /. /| + / . / | + / . / | + / . / | + / . / | + 4 / . / | + o---------------------o | + | . |7 | + | . | | + | . | | + | . | | + | o..............|......o + | . 1 | / 2 + | . | / + | . | / + | . | / + | . | / + |. |/ + o---------------------o + 0 3 + */ + _listVertex.push_back(vec3( _size, -_size, -_size)); // 0 + _listVertex.push_back(vec3( _size, -_size, _size)); // 1 + _listVertex.push_back(vec3(-_size, -_size, _size)); // 2 + _listVertex.push_back(vec3(-_size, -_size, -_size)); // 3 + _listVertex.push_back(vec3( _size, _size, -_size)); // 4 + _listVertex.push_back(vec3( _size, _size, _size)); // 5 + _listVertex.push_back(vec3(-_size, _size, _size)); // 6 + _listVertex.push_back(vec3(-_size, _size, -_size)); // 7 + /* + o----------o----------o----------o + |8 |9 |10 |11 + | | | | + | | | | + | 0 | 1 | 2 | + | | | | + | | | | + | | | | + | | | | + o----------o----------o----------o + |4 |5 |6 |7 + | | | | + | | | | + | 3 | 4 | 5 | + | | | | + | | | | + | | | | + | | | | + o----------o----------o----------o + 0 1 2 3 + */ + _listUV.push_back(vec2(0.0 , 0.0 )); // 0 + _listUV.push_back(vec2(1.0/3.0, 0.0 )); // 1 + _listUV.push_back(vec2(2.0/3.0, 0.0 )); // 2 + _listUV.push_back(vec2(1.0 , 0.0 )); // 3 + _listUV.push_back(vec2(0.0 , 0.5 )); // 4 + _listUV.push_back(vec2(1.0/3.0, 0.5 )); // 5 + _listUV.push_back(vec2(2.0/3.0, 0.5 )); // 6 + _listUV.push_back(vec2(1.0 , 0.5 )); // 7 + _listUV.push_back(vec2(0.0 , 1.0 )); // 8 + _listUV.push_back(vec2(1.0/3.0, 1.0 )); // 9 + _listUV.push_back(vec2(2.0/3.0, 1.0 )); // 10 + _listUV.push_back(vec2(1.0 , 1.0 )); // 11 + + if (_listFaces.exist(_materialName) == false) { + FaceIndexing empty; + _listFaces.add(_materialName, empty); + } + { + FaceIndexing& tmpElement = _listFaces[_materialName]; + tmpElement.m_faces.push_back(Face(0,1, 1,5, 2,6)); // 4 + tmpElement.m_faces.push_back(Face(0,1, 2,6, 3,2)); // 4 + tmpElement.m_faces.push_back(Face(4,4, 0,0, 3,1)); // 3 + tmpElement.m_faces.push_back(Face(4,4, 3,1, 7,5)); // 3 + tmpElement.m_faces.push_back(Face(2,6, 6,10, 7,11)); // 2 + tmpElement.m_faces.push_back(Face(2,6, 7,11, 3,7)); // 2 + tmpElement.m_faces.push_back(Face(4,2, 7,3, 6,7)); // 5 + tmpElement.m_faces.push_back(Face(4,2, 6,7, 5,6)); // 5 + tmpElement.m_faces.push_back(Face(1,5, 5,9, 6,10)); // 1 + tmpElement.m_faces.push_back(Face(1,5, 6,10, 2,6)); // 1 + tmpElement.m_faces.push_back(Face(0,4, 4,8, 5,9)); // 0 + tmpElement.m_faces.push_back(Face(0,4, 5,9, 1,5)); // 0 + } +} diff --git a/ege/resource/tools/viewBox.h b/ege/resource/tools/viewBox.h new file mode 100644 index 0000000..903d681 --- /dev/null +++ b/ege/resource/tools/viewBox.h @@ -0,0 +1,26 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#ifndef __EGE_TOOLS_VIEWBOX_H__ +#define __EGE_TOOLS_VIEWBOX_H__ + +#include +#include +#include +#include + +namespace ege { + namespace viewBox { + void create(etk::Hash& _materials, etk::Hash& _listFaces, std::vector& _listVertex, std::vector& _listUV, + const std::string& _materialName, float _size); + }; +}; + + +#endif + diff --git a/lutin_ege.py b/lutin_ege.py index 55644fc..168032a 100644 --- a/lutin_ege.py +++ b/lutin_ege.py @@ -26,6 +26,9 @@ def create(target): 'ege/Environement.cpp', 'ege/resource/Mesh.cpp', 'ege/resource/ParticuleMesh.cpp', + 'ege/resource/tools/icoSphere.cpp', + 'ege/resource/tools/isoSphere.cpp', + 'ege/resource/tools/viewBox.cpp', 'ege/Light.cpp', 'ege/Material.cpp', 'ege/physicsShape/PhysicsShape.cpp',