From bdf3a56e6b7fb9483f2df83152cfa0ead3c8237a Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Mon, 10 Nov 2014 21:32:42 +0100 Subject: [PATCH] [DEV] add engine and separate element property --- ege/Environement.cpp | 111 ++++---- ege/Environement.h | 37 +-- ege/camera/View.cpp | 2 +- ege/elements/Element.cpp | 209 +++++++++++++++ ege/{ElementGame.h => elements/Element.h} | 125 ++------- ege/elements/ElementBase.cpp | 44 +++ ege/elements/ElementBase.h | 44 +++ .../ElementPhysic.cpp} | 252 ++++-------------- ege/elements/ElementPhysic.h | 170 ++++++++++++ ege/physics/Engine.cpp | 37 ++- ege/physics/Engine.h | 20 +- ege/resource/Mesh.cpp | 29 +- ege/widget/Scene.cpp | 6 +- ege/widget/Scene.h | 2 +- lutin_ege.py | 5 +- sample/MeshCreator/appl/Windows.cpp | 11 +- 16 files changed, 713 insertions(+), 391 deletions(-) create mode 100644 ege/elements/Element.cpp rename ege/{ElementGame.h => elements/Element.h} (70%) create mode 100644 ege/elements/ElementBase.cpp create mode 100644 ege/elements/ElementBase.h rename ege/{ElementGame.cpp => elements/ElementPhysic.cpp} (61%) create mode 100644 ege/elements/ElementPhysic.h diff --git a/ege/Environement.cpp b/ege/Environement.cpp index b07f992..d2281b1 100644 --- a/ege/Environement.cpp +++ b/ege/Environement.cpp @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include @@ -30,31 +30,31 @@ #define __class__ "Environement" -std::shared_ptr ege::Environement::getElementNearest(std::shared_ptr _sourceRequest, float& _distance) { +std::shared_ptr ege::Environement::getElementNearest(std::shared_ptr _sourceRequest, float& _distance) { if (_sourceRequest == nullptr) { return nullptr; } vec3 sourcePosition = _sourceRequest->getPosition(); - std::shared_ptr result = nullptr; - for (size_t iii=0; iii result = nullptr; + for (size_t iii=0; iiigetGroup() <= 0) { + if (m_listElement[iii]->getGroup() <= 0) { continue; } // check if they are in the same group: - if (m_listElementGame[iii]->getGroup() == _sourceRequest->getGroup()) { + if (m_listElement[iii]->getGroup() == _sourceRequest->getGroup()) { continue; } // check distance ... - vec3 destPosition = m_listElementGame[iii]->getPosition(); + vec3 destPosition = m_listElement[iii]->getPosition(); float distance = btDistance(sourcePosition, destPosition); //EGE_DEBUG("Distance : " << _distance << " >? " << distance << " id=" << iii); if (_distance>distance) { _distance = distance; - result = m_listElementGame[iii]; + result = m_listElement[iii]; } } return result; @@ -68,9 +68,9 @@ void ege::Environement::getElementNearest(const vec3& _sourcePosition, ege::Environement::ResultNearestElement result; result.dist = 99999999999.0f; result.element = nullptr; - for (size_t iii=0; iii ege::Environement::createElement(const std::string& _type, bool _autoAddElement, enum ege::property _property, void* _value) { +std::shared_ptr ege::Environement::createElement(const std::string& _type, bool _autoAddElement, enum ege::property _property, void* _value) { if (false == getHachTableCreating().exist(_type)) { EGE_ERROR("Request creating of an type that is not known '" << _type << "'"); return nullptr; @@ -150,7 +150,7 @@ std::shared_ptr ege::Environement::createElement(const std::st EGE_ERROR("nullptr pointer creator == > internal error... '" << _type << "'"); return nullptr; } - std::shared_ptr tmpElement = creatorPointer(std::dynamic_pointer_cast(shared_from_this())); + std::shared_ptr tmpElement = creatorPointer(std::dynamic_pointer_cast(shared_from_this())); if (tmpElement == nullptr) { EGE_ERROR("allocation error '" << _type << "'"); return nullptr; @@ -160,57 +160,57 @@ std::shared_ptr ege::Environement::createElement(const std::st return nullptr; } if (_autoAddElement == true) { - addElementGame(tmpElement); + addElement(tmpElement); } return tmpElement; } -std::shared_ptr ege::Environement::createElement(const std::string& _type, std::string& _description, bool _autoAddElement) { +std::shared_ptr ege::Environement::createElement(const std::string& _type, std::string& _description, bool _autoAddElement) { return createElement(_type, _autoAddElement, ege::typeString, static_cast(&_description)); } -std::shared_ptr ege::Environement::createElement(const std::string& _type, ejson::Value* _value, bool _autoAddElement) { +std::shared_ptr ege::Environement::createElement(const std::string& _type, ejson::Value* _value, bool _autoAddElement) { return createElement(_type, _autoAddElement, ege::typeJson, static_cast(_value)); } -std::shared_ptr ege::Environement::createElement(const std::string& _type, exml::Node* _node, bool _autoAddElement) { +std::shared_ptr ege::Environement::createElement(const std::string& _type, exml::Node* _node, bool _autoAddElement) { return createElement(_type, _autoAddElement, ege::typeXml, static_cast(_node)); } -void ege::Environement::addElementGame(std::shared_ptr _newElement) { +void ege::Environement::addElement(std::shared_ptr _newElement) { // prevent memory allocation and un allocation ... if (_newElement == nullptr) { return; } - for (size_t iii=0; iiidynamicEnable(); + for (size_t iii=0; iiidynamicEnable(); return; } } - m_listElementGame.push_back(_newElement); + m_listElement.push_back(_newElement); _newElement->dynamicEnable(); } -void ege::Environement::rmElementGame(std::shared_ptr _removeElement) { +void ege::Environement::rmElement(std::shared_ptr _removeElement) { if (_removeElement == nullptr) { return; } // inform the element that an element has been removed == > this permit to keep pointer on elements ... - for (size_t iii=0; iiielementIsRemoved(_removeElement); + for (size_t iii=0; iiielementIsRemoved(_removeElement); } } // ream remove on the element : - for (size_t iii=0; iiionDestroy(); - m_listElementGame[iii]->dynamicDisable(); - m_listElementGame[iii]->unInit(); - m_listElementGame[iii].reset(); + for (size_t iii=0; iiionDestroy(); + m_listElement[iii]->dynamicDisable(); + m_listElement[iii]->unInit(); + m_listElement[iii].reset(); } } } @@ -219,6 +219,7 @@ void ege::Environement::rmElementGame(std::shared_ptr _removeE void ege::Environement::getOrderedElementForDisplay(std::vector& _resultList, const vec3& _position, const vec3& _direction) { + // TODO : Set it back ... corrected... // remove all unneeded elements (old display...) _resultList.clear(); // basic element result @@ -226,27 +227,31 @@ void ege::Environement::getOrderedElementForDisplay(std::vectorgetPosition(); vec3 angleView = (destPosition - _position).normalized(); float dotResult=angleView.dot(_direction); //EGE_DEBUG("Dot position : " << destPosition << " == > dot=" << dotResult); + /* if (dotResult <= 0.85f) { // they are not in the camera angle view ... == > no need to process display continue; } + */ result.dist = btDistance(_position, destPosition); + /* if (result.dist>500.0f) { // The element is realy too far ... == > no need to display continue; } + */ // try to add the element at the best positions: size_t jjj; for (jjj=0; jjj<_resultList.size(); jjj++) { @@ -265,27 +270,26 @@ void ege::Environement::getOrderedElementForDisplay(std::vector this permit to keep pointer on elements ... - for (size_t iii=0; iiigetPosition(); + vec3 destPosition = m_listElement[iii]->getPosition(); float dist = btDistance(sourcePosition, destPosition); if (dist == 0 || dist>decreasePower) { continue; } float inpact = (decreasePower-dist)/decreasePower * power; - g_listElementGame[iii]->setFireOn(groupIdSource, type, -inpact, sourcePosition); + g_listElement[iii]->setFireOn(groupIdSource, type, -inpact, sourcePosition); */ } } ege::Environement::Environement() : signalPlayTimeChange(*this, "time-change"), - m_dynamicsWorld(), - m_listElementGame(), + m_listElement(), m_status(*this, "status", gameStart, "Satus of the activity of the Environement"), m_ratio(*this, "ratio", 1.0f, "game speed ratio"), m_particuleEngine(*this) { @@ -301,7 +305,7 @@ void ege::Environement::init() { } void ege::Environement::clear() { - m_listElementGame.clear(); + m_listElement.clear(); } @@ -330,18 +334,18 @@ void ege::Environement::periodicCall(const ewol::event::Time& _event) { } //EGE_DEBUG("stepSimulation (start)"); ///step the simulation - if (m_dynamicsWorld != nullptr) { - m_dynamicsWorld->stepSimulation(curentDelta); + if (m_physicEngine.getDynamicWorld() != nullptr) { + m_physicEngine.getDynamicWorld()->stepSimulation(curentDelta); //optional but useful: debug drawing - m_dynamicsWorld->debugDrawWorld(); + m_physicEngine.getDynamicWorld()->debugDrawWorld(); } m_particuleEngine.update(curentDelta); // remove all element that requested it ... { int32_t numberEnnemyKilled=0; int32_t victoryPoint=0; - auto it(m_listElementGame.begin()); - while (it != m_listElementGame.end()) { + auto it(m_listElement.begin()); + while (it != m_listElement.end()) { if(*it != nullptr) { if ((*it)->needToRemove() == true) { if ((*it)->getGroup() > 1) { @@ -349,7 +353,10 @@ void ege::Environement::periodicCall(const ewol::event::Time& _event) { victoryPoint++; } EGE_DEBUG("[" << (*it)->getUID() << "] element Removing ... " << (*it)->getType()); - rmElementGame((*it)); + rmElement((*it)); + it = m_listElement.begin(); + } else { + ++it; } } } diff --git a/ege/Environement.h b/ege/Environement.h index 6ad6e7b..67412f3 100644 --- a/ege/Environement.h +++ b/ege/Environement.h @@ -29,6 +29,7 @@ class btDynamicsWorld; #include #include #include +#include namespace ege { enum property { @@ -39,9 +40,9 @@ namespace ege { typeUser1, //!< user type 1 typeUser2 //!< User type 2 }; - class ElementGame; + class Element; class Environement; - typedef std::shared_ptr (*createElement_tf)(const std::shared_ptr& _env); + typedef std::shared_ptr (*createElement_tf)(const std::shared_ptr& _env); enum gameStatus { gameStart, @@ -84,7 +85,7 @@ namespace ege { m_positionSource(_pos) { }; public: - virtual void applyEvent(ege::ElementGame& _element) { }; + virtual void applyEvent(ege::Element& _element) { }; }; class Environement : public ewol::Object { @@ -92,8 +93,9 @@ namespace ege { // extern event ewol::Signal signalPlayTimeChange; private: - std::shared_ptr m_dynamicsWorld; //!< curent system world description - std::vector> m_listElementGame; //!< List of all element added in the Game + //std::shared_ptr m_dynamicsWorld; //!< curent system world description + ege::physics::Engine m_physicEngine; //!< EGE physic engine interface. + std::vector> m_listElement; //!< List of all element added in the Game protected: Environement(); void init(); @@ -168,16 +170,17 @@ namespace ege { * @return nullptr if an error occured OR the pointer on the element and it is already added on the system. * @note Pointer is return in case of setting properties on it... */ - std::shared_ptr createElement(const std::string& _type, bool _autoAddElement=true, enum ege::property _property=ege::typeNone, void* _value=nullptr); - std::shared_ptr createElement(const std::string& _type, std::string& _description, bool _autoAddElement=true); - std::shared_ptr createElement(const std::string& _type, ejson::Value* _value, bool _autoAddElement=true); - std::shared_ptr createElement(const std::string& _type, exml::Node* _node, bool _autoAddElement=true); + std::shared_ptr createElement(const std::string& _type, bool _autoAddElement=true, enum ege::property _property=ege::typeNone, void* _value=nullptr); + std::shared_ptr createElement(const std::string& _type, std::string& _description, bool _autoAddElement=true); + std::shared_ptr createElement(const std::string& _type, ejson::Value* _value, bool _autoAddElement=true); + std::shared_ptr createElement(const std::string& _type, exml::Node* _node, bool _autoAddElement=true); public: class ResultNearestElement { public: - std::shared_ptr element; + std::shared_ptr element; float dist; }; + #if 0 /** * @brief set the curent world * @param[in] _newWorld Pointer on the current world @@ -192,12 +195,16 @@ namespace ege { std::shared_ptr getDynamicWorld() { return m_dynamicsWorld; }; + #endif + ege::physics::Engine& getPhysicEngine() { + return m_physicEngine; + } /** * @breif get a reference on the curent list of element games * @return all element list */ - std::vector>& getElementGame() { - return m_listElementGame; + std::vector>& getElement() { + return m_listElement; }; /** * @brief get the nearest Element @@ -205,7 +212,7 @@ namespace ege { * @param[in] _distance Maximum distance search == > return the element distance * @return Pointer on the neares element OR nullptr */ - std::shared_ptr getElementNearest(std::shared_ptr _sourceRequest, float& _distance); + std::shared_ptr getElementNearest(std::shared_ptr _sourceRequest, float& _distance); void getElementNearest(const vec3& _sourcePosition, float _distanceMax, @@ -217,12 +224,12 @@ namespace ege { * @brief add an element on the list availlable. * @param[in] _newElement Element to add. */ - void addElementGame(std::shared_ptr _newElement); + void addElement(std::shared_ptr _newElement); /** * @brief remove an element on the list availlable. * @param[in] _removeElement Element to remove. */ - void rmElementGame(std::shared_ptr _removeElement); + void rmElement(std::shared_ptr _removeElement); /** * @brief get the element order from the nearest to the farest, and remove all element that are not in the camera angle and axes. * @param[in,out] _resultList List of the element ordered. diff --git a/ege/camera/View.cpp b/ege/camera/View.cpp index 7a0ebd0..551abd1 100644 --- a/ege/camera/View.cpp +++ b/ege/camera/View.cpp @@ -32,7 +32,7 @@ void ege::camera::View::update() { m_matrix.translate(vec3(0,0,-distance)); m_matrix.rotate(vec3(1,0,0), -M_PI*0.5f + psy); m_matrix.rotate(vec3(0,0,1), tetha); - m_matrix.translate(m_target); + m_matrix.translate(-m_target); EGE_DEBUG("Camera properties : distance=" << distance ); EGE_DEBUG(" psy=" << psy); diff --git a/ege/elements/Element.cpp b/ege/elements/Element.cpp new file mode 100644 index 0000000..2999140 --- /dev/null +++ b/ege/elements/Element.cpp @@ -0,0 +1,209 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#undef __class__ +#define __class__ "Element" + +const std::string& ege::Element::getType() const { + static const std::string nameType("----"); + return nameType; +} + + +ege::Element::Element(const std::shared_ptr& _env) : + m_env(_env), + m_uID(0), + m_mesh(), + m_life(100), + m_lifeMax(100), + m_group(0), + m_fixe(true), + m_radius(0) { + static uint32_t unique=0; + m_uID = unique; + EGE_DEBUG("Create element : uId=" << m_uID); + m_debugText.setFontSize(12); + unique++; +} + +ege::Element::~Element() { + EGE_DEBUG("Destroy element : uId=" << m_uID); +} + +bool ege::Element::loadMesh(const std::string& _meshFileName) { + std::shared_ptr tmpMesh = ege::resource::Mesh::create(_meshFileName); + if(nullptr == tmpMesh) { + EGE_ERROR("can not load the resources : " << _meshFileName); + return false; + } + return setMesh(tmpMesh); +} + +bool ege::Element::setMesh(const std::shared_ptr& _mesh) { + if (nullptr!=m_mesh) { + m_mesh.reset(); + } + m_mesh = _mesh; + // auto load the shape : + if (m_mesh == nullptr) { + return true; + } + return true; +} + + +float ege::Element::getLifeRatio() { + if (0 >= m_life) { + return 0; + } + return m_life/m_lifeMax; +} + +void ege::Element::setFireOn(int32_t _groupIdSource, int32_t _type, float _power, const vec3& _center) { + float previousLife = m_life; + m_life += _power; + m_life = std::avg(0.0f, m_life, m_lifeMax); + if (m_life <= 0) { + EGE_DEBUG("[" << getUID() << "] element is killed ..." << getType()); + } + if (m_life!=previousLife) { + onLifeChange(); + } +} + +const vec3& ege::Element::getPosition() { + // this is to prevent error like segmentation fault ... + static vec3 emptyPosition(-1000000,-1000000,-1000000); + return emptyPosition; +}; + +static void drawSphere(const std::shared_ptr& _draw, + btScalar _radius, + int _lats, + int _longs, + mat4& _transformationMatrix, + etk::Color& _tmpColor) { + int i, j; + std::vector EwolVertices; + for(i = 0; i <= _lats; i++) { + btScalar lat0 = SIMD_PI * (-btScalar(0.5) + (btScalar) (i - 1) / _lats); + btScalar z0 = _radius*sin(lat0); + btScalar zr0 = _radius*cos(lat0); + + btScalar lat1 = SIMD_PI * (-btScalar(0.5) + (btScalar) i / _lats); + btScalar z1 = _radius*sin(lat1); + btScalar zr1 = _radius*cos(lat1); + + //glBegin(GL_QUAD_STRIP); + for(j = 0; j < _longs; j++) { + btScalar lng = 2 * SIMD_PI * (btScalar) (j - 1) / _longs; + btScalar x = cos(lng); + btScalar y = sin(lng); + vec3 v1 = vec3(x * zr1, y * zr1, z1); + vec3 v4 = vec3(x * zr0, y * zr0, z0); + + lng = 2 * SIMD_PI * (btScalar) (j) / _longs; + x = cos(lng); + y = sin(lng); + vec3 v2 = vec3(x * zr1, y * zr1, z1); + vec3 v3 = vec3(x * zr0, y * zr0, z0); + + EwolVertices.push_back(v1); + EwolVertices.push_back(v2); + EwolVertices.push_back(v3); + + EwolVertices.push_back(v1); + EwolVertices.push_back(v3); + EwolVertices.push_back(v4); + } + } + _draw->draw(EwolVertices, _tmpColor, _transformationMatrix); +} + +const float lifeBorder = 0.1f; +const float lifeHeight = 0.3f; +const float lifeWidth = 2.0f; +const float lifeYPos = 1.7f; + +void ege::Element::drawLife(const std::shared_ptr& _draw, const ege::Camera& _camera) { + if (nullptr == _draw) { + return; + } + float ratio = getLifeRatio(); + if (ratio == 1.0f) { + return; + } + #if 0 + mat4 transformationMatrix = etk::matTranslate(getPosition()) + * etk::matRotate(vec3(0,0,1),_camera.getAngleZ()) + * etk::matRotate(vec3(1,0,0),(M_PI/2.0f-_camera.getAngleTeta())); + std::vector localVertices; + localVertices.push_back(vec3(-lifeWidth/2.0-lifeBorder,lifeYPos -lifeBorder,0)); + localVertices.push_back(vec3(-lifeWidth/2.0-lifeBorder,lifeYPos+lifeHeight+lifeBorder,0)); + localVertices.push_back(vec3( lifeWidth/2.0+lifeBorder,lifeYPos+lifeHeight+lifeBorder,0)); + localVertices.push_back(vec3(-lifeWidth/2.0-lifeBorder,lifeYPos -lifeBorder,0)); + localVertices.push_back(vec3( lifeWidth/2.0+lifeBorder,lifeYPos+lifeHeight+lifeBorder,0)); + localVertices.push_back(vec3( lifeWidth/2.0+lifeBorder,lifeYPos -lifeBorder,0)); + etk::Color myColor(0x0000FF99); + _draw->draw(localVertices, myColor, transformationMatrix, false, false); + localVertices.clear(); + /** Bounding box == > model shape **/ + localVertices.push_back(vec3(-lifeWidth/2.0 ,lifeYPos,0)); + localVertices.push_back(vec3(-lifeWidth/2.0 ,lifeYPos + lifeHeight,0)); + localVertices.push_back(vec3(-lifeWidth/2.0+lifeWidth*ratio,lifeYPos + lifeHeight,0)); + localVertices.push_back(vec3(-lifeWidth/2.0 ,lifeYPos,0)); + localVertices.push_back(vec3(-lifeWidth/2.0+lifeWidth*ratio,lifeYPos + lifeHeight,0)); + localVertices.push_back(vec3(-lifeWidth/2.0+lifeWidth*ratio,lifeYPos,0)); + myColor =0x00FF00FF; + if (ratio < 0.2f) { + myColor = 0xFF0000FF; + } else if (ratio < 0.4f) { + myColor = 0xDA7B00FF; + } + _draw->draw(localVertices, myColor, transformationMatrix, false, false); + #endif +} + +void ege::Element::drawDebug(const std::shared_ptr& _draw, const ege::Camera& _camera) { + m_debugText.clear(); + m_debugText.setColor(etk::Color<>(0x00, 0xFF, 0x00, 0xFF)); + m_debugText.setPos(vec3(-20,32,0)); + m_debugText.print(getType()); + m_debugText.setPos(vec3(-20,20,0)); + m_debugText.print("life=("+etk::to_string(getLifeRatio())); + //m_debugText.print(std::string("Axe=(")+std::string(m_tmpAxe.x())+std::string(",")+etk::UString(m_tmpAxe.y())+etk::UString(",")+etk::UString(m_tmpAxe.z())+etk::UString(")")); + /* + m_debugText.draw( etk::matTranslate(getPosition()) + * etk::matRotate(vec3(0,0,1),_camera.getAngleZ()) + * etk::matRotate(vec3(1,0,0),(M_PI/2.0f-_camera.getAngleTeta())) + * etk::matScale(vec3(0.05,0.05,0.05))); + */ +} + + + + diff --git a/ege/ElementGame.h b/ege/elements/Element.h similarity index 70% rename from ege/ElementGame.h rename to ege/elements/Element.h index 7d90c69..e1ceba6 100644 --- a/ege/ElementGame.h +++ b/ege/elements/Element.h @@ -6,8 +6,8 @@ * @license BSD v3 (see license file) */ -#ifndef __EGE_ELEMENT_GAME_H__ -#define __EGE_ELEMENT_GAME_H__ +#ifndef __EGE_ELEMENT_H__ +#define __EGE_ELEMENT_H__ #include #include @@ -29,24 +29,20 @@ #define ELEMENT_SCALE (1.0f/8.0f) namespace ege { - class ElementGame { - private: - static void FunctionFreeShape(void* _pointer); + class Element { protected: std::shared_ptr m_env; - protected: - btRigidBody* m_body; //!< all the element have a body == > otherwise it will be not manage with this system... public: /** * @brief Constructor (when constructer is called just add element that did not change. * The objest will be stored in a pool of element and keep a second time if needed == > redure memory allocation, * when needed, the system will call the init and un-init function... */ - ElementGame(const std::shared_ptr& _env); + Element(const std::shared_ptr& _env); /** * @brief Destructor */ - virtual ~ElementGame(); + virtual ~Element(); /** * @brief get the element Type description string. * @return A reference on the descriptive string. @@ -76,7 +72,6 @@ namespace ege { }; 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: /** * @brief Select a mesh with a specific name. @@ -84,21 +79,14 @@ namespace ege { * @note Automaticly load the shape if it is specify in the mesh file * @return true if no error occured */ - bool loadMesh(const std::string& _meshFileName); + virtual bool loadMesh(const std::string& _meshFileName); /** * @brief set the the Mesh properties. * @param[in] _mesh The mesh pointer. (nullptr to force the mesh remove ...) * @note : this remove the shape and the mesh properties. * @return true if no error occured */ - bool setMesh(const std::shared_ptr& _mesh); - /** - * @brief set the shape properties. - * @param[in] _shape The shape pointer. - * @note : this remove the shape properties. - * @return true if no error occured - */ - bool setShape(btCollisionShape* _shape); + virtual bool setMesh(const std::shared_ptr& _mesh); /** * @brief get a pointer on the Mesh file. * @return the mesh pointer. @@ -106,18 +94,6 @@ namespace ege { inline const std::shared_ptr& getMesh() { return m_mesh; }; - /** - * @brief get a pointer on the bullet collision shape. - * @return the collision pointer. - */ - inline btCollisionShape* getShape() { - return m_shape; - }; - private: - /** - * @brief remove the curent selected shape. - */ - void removeShape(); protected: float m_life; //!< Current life of the object float m_lifeMax; //!< Maximum possible life of the element @@ -150,7 +126,7 @@ namespace ege { */ virtual void setFireOn(int32_t _groupIdSource, int32_t _type, float _power, const vec3& _center=vec3(0,0,0)); /** - * @brief Call chan the element life change + * @brief Call when the element life change. */ virtual void onLifeChange() { }; protected: @@ -170,7 +146,7 @@ namespace ege { inline void setGroup(int32_t _newGroup) { m_group=_newGroup; }; - + public: /** * @brief Can be call tu opdate the list of the element displayed on the scren (example : no display of the hiden triangle) * @param[in] the camera properties @@ -181,11 +157,12 @@ namespace ege { * @brief draw the curent element (can have multiple display) * @param[in] pass Id of the current pass : [0..?] */ - virtual void draw(int32_t _pass=0); + virtual void draw(int32_t _pass=0) = 0; /** * @brief draw the current life of the element */ + // TODO : Remove this ... virtual void drawLife(const std::shared_ptr& _draw, const ege::Camera& _camera); protected: @@ -205,36 +182,21 @@ namespace ege { virtual vec3 getPositionTheoric() { return getPosition(); }; - /** - * @brief set the current Theoric position of the element - * @param[in] set the 3D position. - */ - virtual void setPositionTheoric(const vec3& _pos) { }; /** * @brief get the current position of the element * @return the 3D position. */ - const vec3& getPosition(); + virtual const vec3& getPosition(); /** * @brief set the current position of the element * @param[in] _pos set the 3D position. */ - void setPosition(const vec3& _pos); - /** - * @brief get the current speed of the element - * @return the 3D speed. - */ - const vec3& getSpeed(); - /** - * @brief get the current mass of the element - * @return the mass in kG. - */ - const float getInvMass(); + virtual void setPosition(const vec3& _pos) {}; /** * @brief Event arrive when an element has been remove from the system == > this permit to keep pointer of ennemy, and not search them every cycle ... * @param[in] _removedElement Pointer on the element removed. */ - virtual void elementIsRemoved(std::shared_ptr _removedElement) { }; + virtual void elementIsRemoved(std::shared_ptr _removedElement) { }; protected: bool m_fixe; //!< is a fixed element == > used for placement of every elements public: @@ -252,66 +214,21 @@ namespace ege { * @brief get the current space needed by the element in the workspace * @return The dimention needed. */ - inline float getRadius() - { + inline float getRadius() { return m_radius; }; - protected: - bool m_elementInPhysicsSystem; - public: + /** + * @brief, call when the element is removed (call only one time) + */ + virtual void onDestroy() {}; /** * @brief set the elment in the physique engine */ - void dynamicEnable(); + virtual void dynamicEnable() {}; /** * @brief remove this element from the physique engine */ - void dynamicDisable(); - private: - class localIA : public btActionInterface { - private: - ege::ElementGame& m_element; - public: - /** - * @brief Constructor - */ - localIA(ElementGame& _element) : - m_element(_element) { - - }; - /** - * @brief Destructor - */ - virtual ~localIA() { - - }; - public: // herited function - void debugDraw(btIDebugDraw* _debugDrawer) { - - }; - void updateAction(btCollisionWorld* _collisionWorld, btScalar _step) { - m_element.iaAction(_step); - }; - }; - localIA* m_IA; - public: - /** - * @brief enable periodic call Of this object for processing Artificial Intelligence - */ - void iaEnable(); - /** - * @brief disable periodic call Of this object for processing Artificial Intelligence - */ - void iaDisable(); - /** - * @brief periodic call for intelligence artificial. - * @param[in] step : step of time in s - */ - virtual void iaAction(float _step) { }; - /** - * @brief, call when the element is removed (call only one time - */ - virtual void onDestroy() {}; + virtual void dynamicDisable() {}; }; }; diff --git a/ege/elements/ElementBase.cpp b/ege/elements/ElementBase.cpp new file mode 100644 index 0000000..961ea74 --- /dev/null +++ b/ege/elements/ElementBase.cpp @@ -0,0 +1,44 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + + +#include +#include + +ege::ElementBase::ElementBase(const std::shared_ptr& _env) : + ege::Element(_env), + m_position(0,0,0) { + +} + +ege::ElementBase::~ElementBase() { + EGE_WARNING("Remove ... "); +} + +const std::string& ege::ElementBase::getType() const { + return ege::Element::getType(); +} + +void ege::ElementBase::draw(int32_t _pass) { + mat4 transformationMatrix(0); + transformationMatrix.identity(); + transformationMatrix.translate(m_position); + //transformationMatrix.transpose(); + m_mesh->draw(transformationMatrix); + EGE_VERBOSE("draw ... " << transformationMatrix); +} + +const vec3& ege::ElementBase::getPosition() { + return m_position; +} + +void ege::ElementBase::setPosition(const vec3& _pos) { + m_position = _pos; +} + + diff --git a/ege/elements/ElementBase.h b/ege/elements/ElementBase.h new file mode 100644 index 0000000..bdb2b3e --- /dev/null +++ b/ege/elements/ElementBase.h @@ -0,0 +1,44 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#ifndef __EGE_ELEMENT_BASE_H__ +#define __EGE_ELEMENT_BASE_H__ + +#include + + +namespace ege { + class ElementBase : public ege::Element { + public: + /** + * @brief Constructor (when constructer is called just add element that did not change. + * The objest will be stored in a pool of element and keep a second time if needed == > redure memory allocation, + * when needed, the system will call the init and un-init function... + */ + ElementBase(const std::shared_ptr& _env); + /** + * @brief Destructor + */ + virtual ~ElementBase(); + /** + * @brief get the element Type description string. + * @return A reference on the descriptive string. + */ + virtual const std::string& getType() const; + virtual void draw(int32_t _pass=0); + private: + vec3 m_position; + public: + virtual const vec3& getPosition(); + virtual void setPosition(const vec3& _pos); + }; +}; + +#endif + + diff --git a/ege/ElementGame.cpp b/ege/elements/ElementPhysic.cpp similarity index 61% rename from ege/ElementGame.cpp rename to ege/elements/ElementPhysic.cpp index c8e3b50..cf27e71 100644 --- a/ege/ElementGame.cpp +++ b/ege/elements/ElementPhysic.cpp @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include #include @@ -26,35 +26,24 @@ #include #undef __class__ -#define __class__ "ElementGame" +#define __class__ "ElementPhysic" -const std::string& ege::ElementGame::getType() const { +const std::string& ege::ElementPhysic::getType() const { static const std::string nameType("----"); return nameType; } -ege::ElementGame::ElementGame(const std::shared_ptr& _env) : - m_env(_env), +ege::ElementPhysic::ElementPhysic(const std::shared_ptr& _env) : + ege::Element(_env), m_body(nullptr), - m_uID(0), - m_mesh(), m_shape(nullptr), - m_life(100), - m_lifeMax(100), - m_group(0), - m_fixe(true), - m_radius(0), m_elementInPhysicsSystem(false), m_IA(nullptr) { - static uint32_t unique=0; - m_uID = unique; - EGE_DEBUG("Create element : uId=" << m_uID); - m_debugText.setFontSize(12); - unique++; + } -ege::ElementGame::~ElementGame() { +ege::ElementPhysic::~ElementPhysic() { // in every case remove IA iaDisable(); // same ... @@ -62,10 +51,35 @@ ege::ElementGame::~ElementGame() { removeShape(); delete m_body; m_body = nullptr; - EGE_DEBUG("Destroy element : uId=" << m_uID); } -void ege::ElementGame::removeShape() { +bool ege::ElementPhysic::setMesh(const std::shared_ptr& _mesh) { + if (nullptr!=m_mesh) { + removeShape(); + } + ege::Element::setMesh(_mesh); + // auto load the shape : + if (m_mesh == nullptr) { + return true; + } + if (m_mesh->getShape() != nullptr) { + m_shape = static_cast(m_mesh->getShape()); + return true; + } + m_mesh->setShape(ege::collision::createShape(m_mesh)); + m_mesh->setFreeShapeFunction(&FunctionFreeShape); + m_shape = static_cast(m_mesh->getShape()); + return true; +} + + +bool ege::ElementPhysic::setShape(btCollisionShape* _shape) { + removeShape(); + m_shape = _shape; + return true; +} + +void ege::ElementPhysic::removeShape() { // no shape if (nullptr == m_shape) { return; @@ -85,68 +99,14 @@ void ege::ElementGame::removeShape() { // otherwise : the shape is auto remove by the resources when no more needed ... } -void ege::ElementGame::FunctionFreeShape(void* _pointer) { +void ege::ElementPhysic::FunctionFreeShape(void* _pointer) { if (nullptr == _pointer) { return; } delete(static_cast(_pointer)); } -bool ege::ElementGame::loadMesh(const std::string& _meshFileName) { - std::shared_ptr tmpMesh = ege::resource::Mesh::create(_meshFileName); - if(nullptr == tmpMesh) { - EGE_ERROR("can not load the resources : " << _meshFileName); - return false; - } - return setMesh(tmpMesh); -} - -bool ege::ElementGame::setMesh(const std::shared_ptr& _mesh) { - if (nullptr!=m_mesh) { - removeShape(); - m_mesh.reset(); - } - m_mesh = _mesh; - // auto load the shape : - if (m_mesh == nullptr) { - return true; - } - if (m_mesh->getShape() != nullptr) { - m_shape = static_cast(m_mesh->getShape()); - return true; - } - m_mesh->setShape(ege::collision::createShape(m_mesh)); - m_mesh->setFreeShapeFunction(&FunctionFreeShape); - m_shape = static_cast(m_mesh->getShape()); - return true; -} - -bool ege::ElementGame::setShape(btCollisionShape* _shape) { - removeShape(); - m_shape = _shape; - return true; -} - -float ege::ElementGame::getLifeRatio() { - if (0 >= m_life) { - return 0; - } - return m_life/m_lifeMax; -} - -void ege::ElementGame::setFireOn(int32_t _groupIdSource, int32_t _type, float _power, const vec3& _center) { - float previousLife = m_life; - m_life += _power; - m_life = std::avg(0.0f, m_life, m_lifeMax); - if (m_life <= 0) { - EGE_DEBUG("[" << getUID() << "] element is killed ..." << getType()); - } - if (m_life!=previousLife) { - onLifeChange(); - } -} - -void ege::ElementGame::setPosition(const vec3& _pos) { +void ege::ElementPhysic::setPosition(const vec3& _pos) { if (nullptr!=m_body) { btTransform transformation = m_body->getCenterOfMassTransform(); transformation.setOrigin(_pos); @@ -154,17 +114,14 @@ void ege::ElementGame::setPosition(const vec3& _pos) { } } -const vec3& ege::ElementGame::getPosition() { - // this is to prevent error like segmentation fault ... - static vec3 emptyPosition(-1000000,-1000000,-1000000); +const vec3& ege::ElementPhysic::getPosition() { if (nullptr!=m_body) { return m_body->getCenterOfMassPosition(); } - return emptyPosition; + return ege::Element::getPosition(); }; -const vec3& ege::ElementGame::getSpeed() { - // this is to prevent error like segmentation fault ... +const vec3& ege::ElementPhysic::getSpeed() { static vec3 emptySpeed(0,0,0); if (nullptr!=m_body) { return m_body->getLinearVelocity(); @@ -172,100 +129,13 @@ const vec3& ege::ElementGame::getSpeed() { return emptySpeed; }; -const float ege::ElementGame::getInvMass() { +const float ege::ElementPhysic::getInvMass() { if (nullptr!=m_body) { return m_body->getInvMass(); } return 0.0000000001f; }; -static void drawSphere(const std::shared_ptr& _draw, - btScalar _radius, - int _lats, - int _longs, - mat4& _transformationMatrix, - etk::Color& _tmpColor) { - int i, j; - std::vector EwolVertices; - for(i = 0; i <= _lats; i++) { - btScalar lat0 = SIMD_PI * (-btScalar(0.5) + (btScalar) (i - 1) / _lats); - btScalar z0 = _radius*sin(lat0); - btScalar zr0 = _radius*cos(lat0); - - btScalar lat1 = SIMD_PI * (-btScalar(0.5) + (btScalar) i / _lats); - btScalar z1 = _radius*sin(lat1); - btScalar zr1 = _radius*cos(lat1); - - //glBegin(GL_QUAD_STRIP); - for(j = 0; j < _longs; j++) { - btScalar lng = 2 * SIMD_PI * (btScalar) (j - 1) / _longs; - btScalar x = cos(lng); - btScalar y = sin(lng); - vec3 v1 = vec3(x * zr1, y * zr1, z1); - vec3 v4 = vec3(x * zr0, y * zr0, z0); - - lng = 2 * SIMD_PI * (btScalar) (j) / _longs; - x = cos(lng); - y = sin(lng); - vec3 v2 = vec3(x * zr1, y * zr1, z1); - vec3 v3 = vec3(x * zr0, y * zr0, z0); - - EwolVertices.push_back(v1); - EwolVertices.push_back(v2); - EwolVertices.push_back(v3); - - EwolVertices.push_back(v1); - EwolVertices.push_back(v3); - EwolVertices.push_back(v4); - } - } - _draw->draw(EwolVertices, _tmpColor, _transformationMatrix); -} - -const float lifeBorder = 0.1f; -const float lifeHeight = 0.3f; -const float lifeWidth = 2.0f; -const float lifeYPos = 1.7f; - -void ege::ElementGame::drawLife(const std::shared_ptr& _draw, const ege::Camera& _camera) { - if (nullptr == _draw) { - return; - } - float ratio = getLifeRatio(); - if (ratio == 1.0f) { - return; - } - #if 0 - mat4 transformationMatrix = etk::matTranslate(getPosition()) - * etk::matRotate(vec3(0,0,1),_camera.getAngleZ()) - * etk::matRotate(vec3(1,0,0),(M_PI/2.0f-_camera.getAngleTeta())); - std::vector localVertices; - localVertices.push_back(vec3(-lifeWidth/2.0-lifeBorder,lifeYPos -lifeBorder,0)); - localVertices.push_back(vec3(-lifeWidth/2.0-lifeBorder,lifeYPos+lifeHeight+lifeBorder,0)); - localVertices.push_back(vec3( lifeWidth/2.0+lifeBorder,lifeYPos+lifeHeight+lifeBorder,0)); - localVertices.push_back(vec3(-lifeWidth/2.0-lifeBorder,lifeYPos -lifeBorder,0)); - localVertices.push_back(vec3( lifeWidth/2.0+lifeBorder,lifeYPos+lifeHeight+lifeBorder,0)); - localVertices.push_back(vec3( lifeWidth/2.0+lifeBorder,lifeYPos -lifeBorder,0)); - etk::Color myColor(0x0000FF99); - _draw->draw(localVertices, myColor, transformationMatrix, false, false); - localVertices.clear(); - /** Bounding box == > model shape **/ - localVertices.push_back(vec3(-lifeWidth/2.0 ,lifeYPos,0)); - localVertices.push_back(vec3(-lifeWidth/2.0 ,lifeYPos + lifeHeight,0)); - localVertices.push_back(vec3(-lifeWidth/2.0+lifeWidth*ratio,lifeYPos + lifeHeight,0)); - localVertices.push_back(vec3(-lifeWidth/2.0 ,lifeYPos,0)); - localVertices.push_back(vec3(-lifeWidth/2.0+lifeWidth*ratio,lifeYPos + lifeHeight,0)); - localVertices.push_back(vec3(-lifeWidth/2.0+lifeWidth*ratio,lifeYPos,0)); - myColor =0x00FF00FF; - if (ratio < 0.2f) { - myColor = 0xFF0000FF; - } else if (ratio < 0.4f) { - myColor = 0xDA7B00FF; - } - _draw->draw(localVertices, myColor, transformationMatrix, false, false); - #endif -} - static void drawShape(const btCollisionShape* _shape, const std::shared_ptr& _draw, mat4 _transformationMatrix, @@ -284,7 +154,7 @@ static void drawShape(const btCollisionShape* _shape, //EGE_DEBUG(" draw (01): SPHERE_SHAPE_PROXYTYPE"); const btSphereShape* sphereShape = static_cast(_shape); float radius = sphereShape->getMargin();//radius doesn't include the margin, so draw with margin - drawSphere(_draw, radius, 10, 10, _transformationMatrix, tmpColor); + // TODO : drawSphere(_draw, radius, 10, 10, _transformationMatrix, tmpColor); break; } case BOX_SHAPE_PROXYTYPE: { @@ -434,14 +304,8 @@ static void drawShape(const btCollisionShape* _shape, } } -void ege::ElementGame::drawDebug(const std::shared_ptr& _draw, const ege::Camera& _camera) { - m_debugText.clear(); - m_debugText.setColor(etk::Color<>(0x00, 0xFF, 0x00, 0xFF)); - m_debugText.setPos(vec3(-20,32,0)); - m_debugText.print(getType()); - m_debugText.setPos(vec3(-20,20,0)); - m_debugText.print("life=("+etk::to_string(getLifeRatio())); - //m_debugText.print(std::string("Axe=(")+std::string(m_tmpAxe.x())+std::string(",")+etk::UString(m_tmpAxe.y())+etk::UString(",")+etk::UString(m_tmpAxe.z())+etk::UString(")")); +void ege::ElementPhysic::drawDebug(const std::shared_ptr& _draw, const ege::Camera& _camera) { + ege::Element::drawDebug(_draw, _camera); btScalar mmm[16]; btDefaultMotionState* myMotionState = (btDefaultMotionState*)m_body->getMotionState(); myMotionState->m_graphicsWorldTrans.getOpenGLMatrix(mmm); @@ -452,15 +316,9 @@ void ege::ElementGame::drawDebug(const std::shared_ptr EwolVertices; drawShape(m_shape, _draw, transformationMatrix, EwolVertices); - /* - m_debugText.draw( etk::matTranslate(getPosition()) - * etk::matRotate(vec3(0,0,1),_camera.getAngleZ()) - * etk::matRotate(vec3(1,0,0),(M_PI/2.0f-_camera.getAngleTeta())) - * etk::matScale(vec3(0.05,0.05,0.05))); - */ } -void ege::ElementGame::draw(int32_t _pass) { +void ege::ElementPhysic::draw(int32_t _pass) { if (false == m_elementInPhysicsSystem) { return; } @@ -479,35 +337,35 @@ void ege::ElementGame::draw(int32_t _pass) { } } -void ege::ElementGame::dynamicEnable() { +void ege::ElementPhysic::dynamicEnable() { if (true == m_elementInPhysicsSystem) { return; } if(nullptr!=m_body) { - m_env->getDynamicWorld()->addRigidBody(m_body); + m_env->getPhysicEngine().getDynamicWorld()->addRigidBody(m_body); } if(nullptr!=m_IA) { - m_env->getDynamicWorld()->addAction(m_IA); + m_env->getPhysicEngine().getDynamicWorld()->addAction(m_IA); } m_elementInPhysicsSystem = true; } -void ege::ElementGame::dynamicDisable() { +void ege::ElementPhysic::dynamicDisable() { if (false == m_elementInPhysicsSystem) { return; } if(nullptr!=m_IA) { - m_env->getDynamicWorld()->removeAction(m_IA); + m_env->getPhysicEngine().getDynamicWorld()->removeAction(m_IA); } if(nullptr!=m_body) { // Unlink element from the engine - m_env->getDynamicWorld()->removeRigidBody(m_body); - m_env->getDynamicWorld()->removeCollisionObject(m_body); + m_env->getPhysicEngine().getDynamicWorld()->removeRigidBody(m_body); + m_env->getPhysicEngine().getDynamicWorld()->removeCollisionObject(m_body); } m_elementInPhysicsSystem = false; } -void ege::ElementGame::iaEnable() { +void ege::ElementPhysic::iaEnable() { if (nullptr != m_IA) { // IA already started ... return; @@ -518,17 +376,17 @@ void ege::ElementGame::iaEnable() { return; } if (true == m_elementInPhysicsSystem) { - m_env->getDynamicWorld()->addAction(m_IA); + m_env->getPhysicEngine().getDynamicWorld()->addAction(m_IA); } } -void ege::ElementGame::iaDisable() { +void ege::ElementPhysic::iaDisable() { if (nullptr == m_IA) { // IA already stopped ... return; } if (true == m_elementInPhysicsSystem) { - m_env->getDynamicWorld()->removeAction(m_IA); + m_env->getPhysicEngine().getDynamicWorld()->removeAction(m_IA); } // remove IA : delete(m_IA); diff --git a/ege/elements/ElementPhysic.h b/ege/elements/ElementPhysic.h new file mode 100644 index 0000000..1c83004 --- /dev/null +++ b/ege/elements/ElementPhysic.h @@ -0,0 +1,170 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#ifndef __EGE_ELEMENT_PHYSIC_H__ +#define __EGE_ELEMENT_PHYSIC_H__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define INDEX_RIGHT_AXIS (0) +#define INDEX_FORWARD_AXIS (1) +#define INDEX_UP_AXIS (2) + +#define ELEMENT_SCALE (1.0f/8.0f) + +namespace ege { + class ElementPhysic : public ege::Element { + private: + static void FunctionFreeShape(void* _pointer); + protected: + btRigidBody* m_body; //!< all the element have a body == > otherwise it will be not manage with this system... + public: + /** + * @brief Constructor (when constructer is called just add element that did not change. + * The objest will be stored in a pool of element and keep a second time if needed == > redure memory allocation, + * when needed, the system will call the init and un-init function... + */ + ElementPhysic(const std::shared_ptr& _env); + /** + * @brief Destructor + */ + virtual ~ElementPhysic(); + /** + * @brief get the element Type description string. + * @return A reference on the descriptive string. + */ + virtual const std::string& getType() const; + protected: + btCollisionShape* m_shape; //!< shape of the element (set a copy here to have the debug display of it) + public: + /** + * @brief set the shape properties. + * @param[in] _shape The shape pointer. + * @note : this remove the shape properties. + * @return true if no error occured + */ + bool setShape(btCollisionShape* _shape); + /** + * @brief get a pointer on the bullet collision shape. + * @return the collision pointer. + */ + inline btCollisionShape* getShape() { + return m_shape; + }; + private: + /** + * @brief remove the curent selected shape. + */ + void removeShape(); + public: + virtual bool setMesh(const std::shared_ptr& _mesh); + /** + * @brief draw the curent element (can have multiple display) + * @param[in] pass Id of the current pass : [0..?] + */ + virtual void draw(int32_t _pass=0); + + /** + * @brief draw the current life of the element + */ + // virtual void drawLife(const std::shared_ptr& _draw, const ege::Camera& _camera); + + /** + * @brief get the theoric position. Sometimes, the element has move due to an explosion or something else, then its real position in not the one that woult it be at the end ... + * @return the theoric position + */ + virtual vec3 getPositionTheoric() { + return getPosition(); + }; + /** + * @brief set the current Theoric position of the element + * @param[in] set the 3D position. + */ + virtual void setPositionTheoric(const vec3& _pos) { }; + /** + * @brief get the current speed of the element + * @return the 3D speed. + */ + const vec3& getSpeed(); + /** + * @brief get the current mass of the element + * @return the mass in kG. + */ + const float getInvMass(); + protected: + bool m_elementInPhysicsSystem; + public: + virtual void dynamicEnable(); + virtual void dynamicDisable(); + private: + class localIA : public btActionInterface { + private: + ege::ElementPhysic& m_element; + public: + /** + * @brief Constructor + */ + localIA(ElementPhysic& _element) : + m_element(_element) { + + }; + /** + * @brief Destructor + */ + virtual ~localIA() { + + }; + public: // herited function + void debugDraw(btIDebugDraw* _debugDrawer) { + + }; + void updateAction(btCollisionWorld* _collisionWorld, btScalar _step) { + m_element.iaAction(_step); + }; + }; + localIA* m_IA; + public: + /** + * @brief enable periodic call Of this object for processing Artificial Intelligence + */ + void iaEnable(); + /** + * @brief disable periodic call Of this object for processing Artificial Intelligence + */ + void iaDisable(); + /** + * @brief periodic call for intelligence artificial. + * @param[in] step : step of time in s + */ + virtual void iaAction(float _step) { }; + /** + * @brief, call when the element is removed (call only one time + */ + virtual void onDestroy() {}; + virtual const vec3& getPosition(); + virtual void setPosition(const vec3& _pos); + virtual void drawDebug(const std::shared_ptr& _draw, const ege::Camera& _camera); + }; +}; + +#endif + + diff --git a/ege/physics/Engine.cpp b/ege/physics/Engine.cpp index b88cf3c..adc1e98 100644 --- a/ege/physics/Engine.cpp +++ b/ege/physics/Engine.cpp @@ -9,6 +9,21 @@ #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + ege::physics::Engine::Engine() { setBulletConfig(); @@ -24,43 +39,43 @@ ege::physics::Engine::~Engine() { */ } -void ege::physics::Engine::setBulletConfig(std::make_shared(btDefaultCollisionConfiguration> _collisionConfiguration, - std::make_shared(btCollisionDispatcher> _dispatcher, - std::make_shared(btBroadphaseInterface> _broadphase, - std::make_shared(btConstraintSolver> _solver, - std::make_shared(btDynamicsWorld> _dynamicsWorld) { +void ege::physics::Engine::setBulletConfig(std::shared_ptr _collisionConfiguration, + std::shared_ptr _dispatcher, + std::shared_ptr _broadphase, + std::shared_ptr _solver, + std::shared_ptr _dynamicsWorld) { if (_collisionConfiguration != nullptr) { m_collisionConfiguration = _collisionConfiguration; } else { - m_collisionConfiguration = std::make_shared(btDefaultCollisionConfiguration()); + m_collisionConfiguration = std::make_shared(); } ///use the default collision dispatcher. if (_dispatcher != nullptr) { m_dispatcher = _dispatcher; } else { - m_dispatcher = std::make_shared(btCollisionDispatcher(m_collisionConfiguration)); + m_dispatcher = std::make_shared(m_collisionConfiguration.get()); } if (_broadphase != nullptr) { m_broadphase = _broadphase; } else { - m_broadphase = std::make_shared(btDbvtBroadphase()); + m_broadphase = std::make_shared(); } ///the default constraint solver. if (_solver != nullptr) { m_solver = _solver; } else { - m_solver = std::make_shared(btSequentialImpulseConstraintSolver()); + m_solver = std::make_shared(); } if (_dynamicsWorld != nullptr) { m_dynamicsWorld = _dynamicsWorld; } else { - m_dynamicsWorld = std::make_shared(btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration)); + m_dynamicsWorld = std::make_shared(m_dispatcher.get(),m_broadphase.get(),m_solver.get(),m_collisionConfiguration.get()); // By default we set no gravity m_dynamicsWorld->setGravity(btVector3(0,0,0)); } - m_env.setDynamicWorld(m_dynamicsWorld); + //m_env.setDynamicWorld(m_dynamicsWorld); } \ No newline at end of file diff --git a/ege/physics/Engine.h b/ege/physics/Engine.h index 8776ccb..f416556 100644 --- a/ege/physics/Engine.h +++ b/ege/physics/Engine.h @@ -14,11 +14,10 @@ #include #include #include -#include +#include #include #include #include -#include #include class btBroadphaseInterface; @@ -32,13 +31,14 @@ class btDynamicsWorld; #include class btVector3; +//#include namespace ege { namespace physics { class Engine { private: ///this is the most important class - std::shared_ptr m_collisionConfiguration; std::shared_ptr m_dispatcher; std::shared_ptr m_broadphase; std::shared_ptr m_solver; @@ -51,6 +51,20 @@ namespace ege { std::shared_ptr _broadphase=nullptr, std::shared_ptr _solver=nullptr, std::shared_ptr _dynamicsWorld=nullptr); + /** + * @brief set the curent world + * @param[in] _newWorld Pointer on the current world + */ + void setDynamicWorld(const std::shared_ptr& _newWorld) { + m_dynamicsWorld=_newWorld; + }; + /** + * @brief get the curent world + * @return pointer on the current world + */ + std::shared_ptr getDynamicWorld() { + return m_dynamicsWorld; + }; }; }; }; diff --git a/ege/resource/Mesh.cpp b/ege/resource/Mesh.cpp index 05695d9..14ec1f0 100644 --- a/ege/resource/Mesh.cpp +++ b/ege/resource/Mesh.cpp @@ -258,19 +258,44 @@ void ege::resource::Mesh::generateVBO() { // when no normal detected == > auto generate Face normal .... calculateNormaleFace(m_listFaces.getKeys()[0]); } + + // generate element in 2 pass : // - create new index dependeng a vertex is a unique componenet of position, texture, normal // - the index list generation (can be dynamic ... (TODO later) for (int32_t kkk=0; kkkgetRenderMode()) { + case ewol::openGL::renderTriangle: + case ewol::openGL::renderTriangleStrip: + case ewol::openGL::renderTriangleFan: + nbIndicInFace = 3; + break; + case ewol::openGL::renderLine: + case ewol::openGL::renderLineStrip: + case ewol::openGL::renderLineLoop: + nbIndicInFace = 2; + break; + case ewol::openGL::renderPoint: + nbIndicInFace = 1; + break; + case ewol::openGL::renderQuad: + case ewol::openGL::renderQuadStrip: + nbIndicInFace = 4; + break; + case ewol::openGL::renderPolygon: + nbIndicInFace = 3; + break; + } #ifdef TRY_MINIMAL_VBO int64_t tmpppppp=0; #endif FaceIndexing& tmpFaceList = m_listFaces.getValue(kkk); for (size_t iii=0; iiigetRenderMode(); if ( tmpRenderMode != ewol::openGL::renderTriangle - && tmpRenderMode != ewol::openGL::renderLineStrip + && tmpRenderMode != ewol::openGL::renderTriangleStrip && tmpRenderMode != ewol::openGL::renderTriangleFan) { EGE_ERROR("try to add Line in a mesh material section that not support Line"); return; diff --git a/ege/widget/Scene.cpp b/ege/widget/Scene.cpp index d3afcd5..ab5c35d 100644 --- a/ege/widget/Scene.cpp +++ b/ege/widget/Scene.cpp @@ -83,11 +83,11 @@ void ege::widget::Scene::onDraw() { std::shared_ptr camera = m_env->getCamera(m_cameraName); //EGE_DEBUG("Draw (start)"); mat4 tmpMatrix; - std::shared_ptr world = m_env->getDynamicWorld(); + std::shared_ptr world = m_env->getPhysicEngine().getDynamicWorld(); if (world != nullptr) { m_env->getOrderedElementForDisplay(m_displayElementOrdered, camera->getEye(), camera->getViewVector()); - //EGE_DEBUG("DRAW : " << m_displayElementOrdered.size() << " elements"); + EGE_DEBUG("DRAW : " << m_displayElementOrdered.size() << "/" << m_env->getElement().size() << " elements"); // TODO : remove this == > no more needed ==> checked in the generate the list of the element ordered for (size_t iii=0; iiidraw(pass); } } + } else { + EGE_WARNING("No Dynamic world ..."); } if (camera != nullptr) { m_env->getParticuleEngine().draw(*camera); diff --git a/ege/widget/Scene.h b/ege/widget/Scene.h index d68ec4f..219bd92 100644 --- a/ege/widget/Scene.h +++ b/ege/widget/Scene.h @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include class btBroadphaseInterface; diff --git a/lutin_ege.py b/lutin_ege.py index 6ac691a..e08c66c 100644 --- a/lutin_ege.py +++ b/lutin_ege.py @@ -19,7 +19,10 @@ def create(target): 'ege/camera/View.cpp', 'ege/camera/FPS.cpp', 'ege/CollisionShapeCreator.cpp', - 'ege/ElementGame.cpp', + 'ege/physics/Engine.cpp', + 'ege/elements/Element.cpp', + 'ege/elements/ElementBase.cpp', + 'ege/elements/ElementPhysic.cpp', 'ege/Particule.cpp', 'ege/ParticuleEngine.cpp', 'ege/ParticuleSimple.cpp', diff --git a/sample/MeshCreator/appl/Windows.cpp b/sample/MeshCreator/appl/Windows.cpp index 4af00cd..5e8b0be 100644 --- a/sample/MeshCreator/appl/Windows.cpp +++ b/sample/MeshCreator/appl/Windows.cpp @@ -14,6 +14,8 @@ #include #include #include +#include +#include #undef __class__ #define __class__ "Windows" @@ -137,7 +139,12 @@ void appl::Windows::init() { myMesh->addMaterial("basics", material); myMesh->createIcoSphere("basics", 16, 3); myMesh->generateVBO(); - m_env->addStaticMeshToDraw(myMesh); + std::shared_ptr element = std::make_shared(m_env); + //std::shared_ptr element = std::make_shared(m_env); + element->setPosition(vec3(50,0,0)); + element->setMesh(myMesh); + m_env->addElement(element); + //m_env->addStaticMeshToDraw(myMesh); } } } @@ -146,7 +153,7 @@ void appl::Windows::init() { void appl::Windows::onCallbackPeriodicUpdateCamera(const ewol::event::Time& _event) { static float offset = 0; offset += 0.01; - m_camera->setEye(vec3(100*std::sin(offset),100*std::cos(offset),40)); + m_camera->setEye(vec3(100*std::sin(offset),100*std::cos(offset),40)+vec3(50,0,0)); }