From 9fb437ed21ce6a9c5fe2146b6f43f8cd1003b8a0 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Tue, 25 Nov 2014 21:25:24 +0100 Subject: [PATCH] [DEV] better rayscale and add callback on debug display --- ege/Ray.cpp | 23 ++++++++++++++++++++++- ege/Ray.h | 6 ++++++ ege/physics/Engine.h | 5 +++++ ege/widget/Scene.cpp | 9 ++++++++- ege/widget/Scene.h | 3 +++ sample/RayTest/appl/Windows.cpp | 33 +++++++++++++++------------------ sample/RayTest/appl/Windows.h | 2 ++ 7 files changed, 61 insertions(+), 20 deletions(-) diff --git a/ege/Ray.cpp b/ege/Ray.cpp index 37d2d63..f516784 100644 --- a/ege/Ray.cpp +++ b/ege/Ray.cpp @@ -9,6 +9,9 @@ #include #include +#include +#include + #undef __class__ #define __class__ "Ray" @@ -36,4 +39,22 @@ std::ostream& ege::operator <<(std::ostream& _os, const ege::Ray& _obj) { _os << _obj.getDirection(); _os << "}"; return _os; -} \ No newline at end of file +} + +void ege::Ray::testRay(ege::physics::Engine& _engine) { + vec3 start = m_origin; + vec3 stop = m_origin+m_direction*1000.0f; + // Start and End are vectors + btCollisionWorld::ClosestRayResultCallback rayCallback(start, stop); + EGE_ERROR("Process Raycast :"); + // Perform raycast + _engine.getDynamicWorld()->rayTest(start, stop, rayCallback); + if(rayCallback.hasHit()) { + vec3 end = rayCallback.m_hitPointWorld; + vec3 normal = rayCallback.m_hitNormalWorld; + EGE_ERROR(" hit at point=" << end << " normal=" << normal); + } else { + EGE_ERROR(" No Hit"); + } +} + diff --git a/ege/Ray.h b/ege/Ray.h index 4e84f27..200052b 100644 --- a/ege/Ray.h +++ b/ege/Ray.h @@ -10,6 +10,10 @@ #define __EGE_RAY_H__ #include +namespace ege { + class Ray; +}; +#include namespace ege { class Ray { @@ -61,6 +65,8 @@ namespace ege { * @param[in] _direction The ray's direction. */ void set(const vec3& _origin, const vec3& _direction); + public: + void testRay(ege::physics::Engine& _engine); }; std::ostream& operator <<(std::ostream& _os, const ege::Ray& _obj); }; diff --git a/ege/physics/Engine.h b/ege/physics/Engine.h index f416556..7499d72 100644 --- a/ege/physics/Engine.h +++ b/ege/physics/Engine.h @@ -9,6 +9,11 @@ #ifndef __EGE_PHYSICS_ENGINE_H__ #define __EGE_PHYSICS_ENGINE_H__ +namespace ege { + namespace physics { + class Engine; + }; +}; #include #include #include diff --git a/ege/widget/Scene.cpp b/ege/widget/Scene.cpp index 0339d3f..3294c66 100644 --- a/ege/widget/Scene.cpp +++ b/ege/widget/Scene.cpp @@ -29,9 +29,15 @@ #undef __class__ #define __class__ "Scene" +namespace etk { + template<> std::string to_string >(const std::shared_ptr& _value) { + return "{{ERROR}}"; + } +}; ege::widget::Scene::Scene() : - m_cameraName("default") { + signalDisplayDebug(*this, "drawDebug", "Call to draw debug after all elements"), + m_cameraName("default") { addObjectType("ege::widget::Scene"); } @@ -114,6 +120,7 @@ void ege::widget::Scene::onDraw() { for (int32_t iii=m_displayElementOrdered.size()-1; iii >= 0; iii--) { m_displayElementOrdered[iii].element->drawDebug(m_debugDrawProperty, camera); } + signalDisplayDebug.emit(m_debugDrawProperty); } else { EGE_WARNING("No Dynamic world ..."); } diff --git a/ege/widget/Scene.h b/ege/widget/Scene.h index 0c3b942..fdbb2b0 100644 --- a/ege/widget/Scene.h +++ b/ege/widget/Scene.h @@ -32,6 +32,7 @@ class btDynamicsWorld; #include class btVector3; #include +#include namespace ege { namespace widget { @@ -39,6 +40,8 @@ namespace ege { protected: std::shared_ptr m_env; std::shared_ptr m_debugDrawProperty; + public: + ewol::Signal/*, std::shared_ptr*/> signalDisplayDebug; protected: /** * @brief Constructor of the widget classes diff --git a/sample/RayTest/appl/Windows.cpp b/sample/RayTest/appl/Windows.cpp index 1736b57..3e60430 100644 --- a/sample/RayTest/appl/Windows.cpp +++ b/sample/RayTest/appl/Windows.cpp @@ -82,6 +82,7 @@ void appl::Windows::init() { tmpWidget->setFill(bvec2(true,true)); tmpWidget->setCamera("basic"); setSubWidget(tmpWidget); + tmpWidget->signalDisplayDebug.bind(shared_from_this(), &appl::Windows::onCallbackDisplayDebug); } std::shared_ptr myMesh; // Create an external box : @@ -151,26 +152,22 @@ bool appl::Windows::onEventInput(const ewol::event::Input& _event) { vec2 pos = relativePosition(_event.getPos()); ege::Ray ray = m_camera->getRayFromScreenPosition(pos, m_size); APPL_INFO("pos=" << pos << " ray = " << ray); + ray.testRay(m_env->getPhysicEngine()); return true; } return false; } -/* -btCollisionWorld::ClosestRayResultCallback RayCallback( - btVector3(out_origin.x, out_origin.y, out_origin.z), - btVector3(out_direction.x, out_direction.y, out_direction.z) -); -dynamicsWorld->rayTest( - btVector3(out_origin.x, out_origin.y, out_origin.z), - btVector3(out_direction.x, out_direction.y, out_direction.z), - RayCallback -); - -if(RayCallback.hasHit()) { - std::ostringstream oss; - oss << "mesh " << (int)RayCallback.m_collisionObject->getUserPointer(); - message = oss.str(); -}else{ - message = "background"; + +void appl::Windows::onCallbackDisplayDebug(const std::shared_ptr& _obj) { + EWOL_INFO("draw user debug"); + etk::Color tmpColor(0.0, 1.0, 0.0, 0.3); + std::vector vertices; + vertices.push_back(vec3(100,0,0)); + vertices.push_back(vec3(100,100,0)); + vertices.push_back(vec3(100,100,100)); + mat4 mat; + mat.identity(); + mat.translate(vec3(0,0,0)); + _obj->draw(vertices, tmpColor, mat); } -*/ \ No newline at end of file + diff --git a/sample/RayTest/appl/Windows.h b/sample/RayTest/appl/Windows.h index 5f85db7..04385b8 100644 --- a/sample/RayTest/appl/Windows.h +++ b/sample/RayTest/appl/Windows.h @@ -12,6 +12,7 @@ #include #include #include +#include namespace appl { class Windows : public ewol::widget::Windows { @@ -27,6 +28,7 @@ namespace appl { private: void onCallbackPeriodicUpdateCamera(const ewol::event::Time& _event); bool onEventInput(const ewol::event::Input& _event); + void onCallbackDisplayDebug(const std::shared_ptr& _obj); }; };