From ed1cc7a0926970b9cfc11d5c37d0b8c385fc677f Mon Sep 17 00:00:00 2001 From: Lynix Date: Sat, 3 Jan 2015 22:39:51 +0100 Subject: [PATCH] Added viewer-relative directions Former-commit-id: 9d80744bd227689426b76df7e3c273de13424041 --- include/Nazara/Graphics/AbstractViewer.hpp | 3 + include/Nazara/Graphics/Camera.hpp | 3 + include/Nazara/Graphics/Scene.hpp | 6 ++ include/Nazara/Graphics/SceneNode.hpp | 6 ++ include/Nazara/Graphics/View.hpp | 3 + include/Nazara/Utility/Node.hpp | 14 ++-- src/Nazara/Graphics/Camera.cpp | 15 ++++ src/Nazara/Graphics/Scene.cpp | 86 +++++++++++++++++++++- src/Nazara/Graphics/SceneNode.cpp | 79 ++++++++++++++++++++ src/Nazara/Graphics/View.cpp | 15 ++++ src/Nazara/Utility/Node.cpp | 2 +- 11 files changed, 223 insertions(+), 9 deletions(-) diff --git a/include/Nazara/Graphics/AbstractViewer.hpp b/include/Nazara/Graphics/AbstractViewer.hpp index 7e0306689..d6c3d5f3f 100644 --- a/include/Nazara/Graphics/AbstractViewer.hpp +++ b/include/Nazara/Graphics/AbstractViewer.hpp @@ -28,6 +28,9 @@ class NAZARA_API NzAbstractViewer virtual NzVector3f GetEyePosition() const = 0; virtual NzVector3f GetForward() const = 0; virtual const NzFrustumf& GetFrustum() const = 0; + virtual NzVector3f GetGlobalForward() const = 0; + virtual NzVector3f GetGlobalRight() const = 0; + virtual NzVector3f GetGlobalUp() const = 0; virtual const NzMatrix4f& GetProjectionMatrix() const = 0; virtual const NzRenderTarget* GetTarget() const = 0; virtual const NzMatrix4f& GetViewMatrix() const = 0; diff --git a/include/Nazara/Graphics/Camera.hpp b/include/Nazara/Graphics/Camera.hpp index 534f48df7..3cdb0eb41 100644 --- a/include/Nazara/Graphics/Camera.hpp +++ b/include/Nazara/Graphics/Camera.hpp @@ -32,6 +32,9 @@ class NAZARA_API NzCamera : public NzAbstractViewer, public NzNode, NzRenderTarg NzVector3f GetForward() const; float GetFOV() const; const NzFrustumf& GetFrustum() const; + NzVector3f GetGlobalForward() const; + NzVector3f GetGlobalRight() const; + NzVector3f GetGlobalUp() const; const NzMatrix4f& GetProjectionMatrix() const; const NzRenderTarget* GetTarget() const; const NzRectf& GetTargetRegion() const; diff --git a/include/Nazara/Graphics/Scene.hpp b/include/Nazara/Graphics/Scene.hpp index d286c4c19..8a9b1b89c 100644 --- a/include/Nazara/Graphics/Scene.hpp +++ b/include/Nazara/Graphics/Scene.hpp @@ -41,9 +41,15 @@ class NAZARA_API NzScene NzColor GetAmbientColor() const; NzAbstractBackground* GetBackground() const; + NzVector3f GetBackward() const; + NzVector3f GetDown() const; + NzVector3f GetForward() const; + NzVector3f GetLeft() const; NzAbstractRenderTechnique* GetRenderTechnique() const; + NzVector3f GetRight() const; NzSceneNode& GetRoot() const; NzAbstractViewer* GetViewer() const; + NzVector3f GetUp() const; float GetUpdateTime() const; unsigned int GetUpdatePerSecond() const; diff --git a/include/Nazara/Graphics/SceneNode.hpp b/include/Nazara/Graphics/SceneNode.hpp index 09aa1fab3..e5d70d10c 100644 --- a/include/Nazara/Graphics/SceneNode.hpp +++ b/include/Nazara/Graphics/SceneNode.hpp @@ -27,10 +27,16 @@ class NAZARA_API NzSceneNode : public NzNode void EnableDrawing(bool drawingEnabled); + NzVector3f GetBackward() const; virtual const NzBoundingVolumef& GetBoundingVolume() const = 0; + NzVector3f GetDown() const; + NzVector3f GetForward() const; + NzVector3f GetLeft() const; nzNodeType GetNodeType() const final; + NzVector3f GetRight() const; NzScene* GetScene() const; virtual nzSceneNodeType GetSceneNodeType() const = 0; + NzVector3f GetUp() const; virtual bool IsDrawable() const = 0; bool IsDrawingEnabled() const; diff --git a/include/Nazara/Graphics/View.hpp b/include/Nazara/Graphics/View.hpp index bc048aeb6..e9d0de8da 100644 --- a/include/Nazara/Graphics/View.hpp +++ b/include/Nazara/Graphics/View.hpp @@ -32,6 +32,9 @@ class NAZARA_API NzView : public NzAbstractViewer, public NzNode, NzRenderTarget NzVector3f GetEyePosition() const; NzVector3f GetForward() const; const NzFrustumf& GetFrustum() const; + NzVector3f GetGlobalForward() const; + NzVector3f GetGlobalRight() const; + NzVector3f GetGlobalUp() const; const NzMatrix4f& GetProjectionMatrix() const; const NzVector2f& GetSize() const; const NzRenderTarget* GetTarget() const; diff --git a/include/Nazara/Utility/Node.hpp b/include/Nazara/Utility/Node.hpp index 4536739a7..11f9d1f15 100644 --- a/include/Nazara/Utility/Node.hpp +++ b/include/Nazara/Utility/Node.hpp @@ -24,26 +24,26 @@ class NAZARA_API NzNode void EnsureDerivedUpdate() const; void EnsureTransformMatrixUpdate() const; - NzVector3f GetBackward() const; + virtual NzVector3f GetBackward() const; const std::vector& GetChilds() const; - NzVector3f GetDown() const; - NzVector3f GetForward() const; + virtual NzVector3f GetDown() const; + virtual NzVector3f GetForward() const; bool GetInheritPosition() const; bool GetInheritRotation() const; bool GetInheritScale() const; NzVector3f GetInitialPosition() const; NzQuaternionf GetInitialRotation() const; NzVector3f GetInitialScale() const; - NzVector3f GetLeft() const; + virtual NzVector3f GetLeft() const; const NzString& GetName() const; virtual nzNodeType GetNodeType() const; const NzNode* GetParent() const; NzVector3f GetPosition(nzCoordSys coordSys = nzCoordSys_Global) const; - NzVector3f GetRight() const; + virtual NzVector3f GetRight() const; NzQuaternionf GetRotation(nzCoordSys coordSys = nzCoordSys_Global) const; NzVector3f GetScale(nzCoordSys coordSys = nzCoordSys_Global) const; const NzMatrix4f& GetTransformMatrix() const; - NzVector3f GetUp() const; + virtual NzVector3f GetUp() const; bool HasChilds() const; @@ -95,7 +95,7 @@ class NAZARA_API NzNode virtual void InvalidateNode(); virtual void OnParenting(const NzNode* parent); void RemoveChild(NzNode* node) const; - void UpdateDerived() const; + virtual void UpdateDerived() const; virtual void UpdateTransformMatrix() const; mutable std::vector m_childs; diff --git a/src/Nazara/Graphics/Camera.cpp b/src/Nazara/Graphics/Camera.cpp index a0eb272a6..570b1f289 100644 --- a/src/Nazara/Graphics/Camera.cpp +++ b/src/Nazara/Graphics/Camera.cpp @@ -80,6 +80,21 @@ const NzFrustumf& NzCamera::GetFrustum() const return m_frustum; } +NzVector3f NzCamera::GetGlobalForward() const +{ + return NzVector3f::Forward(); +} + +NzVector3f NzCamera::GetGlobalRight() const +{ + return NzVector3f::Right(); +} + +NzVector3f NzCamera::GetGlobalUp() const +{ + return NzVector3f::Up(); +} + const NzMatrix4f& NzCamera::GetProjectionMatrix() const { if (!m_projectionMatrixUpdated) diff --git a/src/Nazara/Graphics/Scene.cpp b/src/Nazara/Graphics/Scene.cpp index 2c9f286a8..e66a99f03 100644 --- a/src/Nazara/Graphics/Scene.cpp +++ b/src/Nazara/Graphics/Scene.cpp @@ -134,6 +134,58 @@ NzAbstractBackground* NzScene::GetBackground() const return m_impl->background.get(); } +NzVector3f NzScene::GetBackward() const +{ + #if NAZARA_GRAPHICS_SAFE + if (!m_impl->viewer) + { + NazaraError("No viewer"); + return NzVector3f::Backward(); + } + #endif + + return -m_impl->viewer->GetGlobalForward(); +} + +NzVector3f NzScene::GetDown() const +{ + #if NAZARA_GRAPHICS_SAFE + if (!m_impl->viewer) + { + NazaraError("No viewer"); + return NzVector3f::Down(); + } + #endif + + return -m_impl->viewer->GetGlobalUp(); +} + +NzVector3f NzScene::GetForward() const +{ + #if NAZARA_GRAPHICS_SAFE + if (!m_impl->viewer) + { + NazaraError("No viewer"); + return NzVector3f::Forward(); + } + #endif + + return m_impl->viewer->GetGlobalForward(); +} + +NzVector3f NzScene::GetLeft() const +{ + #if NAZARA_GRAPHICS_SAFE + if (!m_impl->viewer) + { + NazaraError("No viewer"); + return NzVector3f::Left(); + } + #endif + + return -m_impl->viewer->GetGlobalRight(); +} + NzAbstractRenderTechnique* NzScene::GetRenderTechnique() const { if (!m_impl->renderTechnique) @@ -142,6 +194,19 @@ NzAbstractRenderTechnique* NzScene::GetRenderTechnique() const return m_impl->renderTechnique.get(); } +NzVector3f NzScene::GetRight() const +{ + #if NAZARA_GRAPHICS_SAFE + if (!m_impl->viewer) + { + NazaraError("No viewer"); + return NzVector3f::Right(); + } + #endif + + return m_impl->viewer->GetGlobalRight(); +} + NzSceneNode& NzScene::GetRoot() const { return m_impl->root; @@ -152,6 +217,19 @@ NzAbstractViewer* NzScene::GetViewer() const return m_impl->viewer; } +NzVector3f NzScene::GetUp() const +{ + #if NAZARA_GRAPHICS_SAFE + if (!m_impl->viewer) + { + NazaraError("No viewer"); + return NzVector3f::Up(); + } + #endif + + return m_impl->viewer->GetGlobalUp(); +} + float NzScene::GetUpdateTime() const { return m_impl->updateTime; @@ -197,7 +275,13 @@ void NzScene::SetRenderTechnique(NzAbstractRenderTechnique* renderTechnique) void NzScene::SetViewer(NzAbstractViewer* viewer) { - m_impl->viewer = viewer; + if (m_impl->viewer != viewer) + { + m_impl->viewer = viewer; + + // Invalidation de tous les nodes de la scène (utile pour la régénération des sommets dépendant du viewer) + m_impl->root.InvalidateNode(); + } } void NzScene::SetViewer(NzAbstractViewer& viewer) diff --git a/src/Nazara/Graphics/SceneNode.cpp b/src/Nazara/Graphics/SceneNode.cpp index fbd88b866..fcaf02743 100644 --- a/src/Nazara/Graphics/SceneNode.cpp +++ b/src/Nazara/Graphics/SceneNode.cpp @@ -31,16 +31,94 @@ void NzSceneNode::EnableDrawing(bool drawingEnabled) m_drawingEnabled = drawingEnabled; } +NzVector3f NzSceneNode::GetBackward() const +{ + if (m_scene) + { + if (!m_derivedUpdated) + UpdateDerived(); + + return m_derivedRotation * m_scene->GetBackward(); + } + else + return NzNode::GetBackward(); +} + +NzVector3f NzSceneNode::GetDown() const +{ + if (m_scene) + { + if (!m_derivedUpdated) + UpdateDerived(); + + return m_derivedRotation * m_scene->GetDown(); + } + else + return NzNode::GetDown(); +} + +NzVector3f NzSceneNode::GetForward() const +{ + if (m_scene) + { + if (!m_derivedUpdated) + UpdateDerived(); + + return m_derivedRotation * m_scene->GetForward(); + } + else + return NzNode::GetForward(); +} + +NzVector3f NzSceneNode::GetLeft() const +{ + if (m_scene) + { + if (!m_derivedUpdated) + UpdateDerived(); + + return m_derivedRotation * m_scene->GetLeft(); + } + else + return NzNode::GetLeft(); +} + nzNodeType NzSceneNode::GetNodeType() const { return nzNodeType_Scene; } +NzVector3f NzSceneNode::GetRight() const +{ + if (m_scene) + { + if (!m_derivedUpdated) + UpdateDerived(); + + return m_derivedRotation * m_scene->GetRight(); + } + else + return NzNode::GetRight(); +} + NzScene* NzSceneNode::GetScene() const { return m_scene; } +NzVector3f NzSceneNode::GetUp() const +{ + if (m_scene) + { + if (!m_derivedUpdated) + UpdateDerived(); + + return m_derivedRotation * m_scene->GetUp(); + } + else + return NzNode::GetUp(); +} + bool NzSceneNode::IsDrawingEnabled() const { return m_drawingEnabled; @@ -82,6 +160,7 @@ void NzSceneNode::OnParenting(const NzNode* parent) { if (parent) { + ///FIXME: Remonter jusqu'au premier parent de type SceneNode plutôt que de s'arrêter au premier venu if (parent->GetNodeType() == nzNodeType_Scene) SetScene(static_cast(parent)->m_scene); } diff --git a/src/Nazara/Graphics/View.cpp b/src/Nazara/Graphics/View.cpp index 44d00294c..045b22a22 100644 --- a/src/Nazara/Graphics/View.cpp +++ b/src/Nazara/Graphics/View.cpp @@ -80,6 +80,21 @@ const NzFrustumf& NzView::GetFrustum() const return m_frustum; } +NzVector3f NzView::GetGlobalForward() const +{ + return NzVector3f::UnitZ(); +} + +NzVector3f NzView::GetGlobalRight() const +{ + return NzVector3f::UnitX(); +} + +NzVector3f NzView::GetGlobalUp() const +{ + return -NzVector3f::UnitY(); +} + const NzMatrix4f& NzView::GetProjectionMatrix() const { if (!m_projectionMatrixUpdated) diff --git a/src/Nazara/Utility/Node.cpp b/src/Nazara/Utility/Node.cpp index 459f6d74f..6361ea3de 100644 --- a/src/Nazara/Utility/Node.cpp +++ b/src/Nazara/Utility/Node.cpp @@ -685,7 +685,7 @@ void NzNode::UpdateDerived() const m_derivedRotation.Normalize(); } else - m_derivedRotation = m_initialRotation * m_rotation; ///FIXME: Besoin d'une normalisation ? + m_derivedRotation = m_initialRotation * m_rotation; m_derivedScale = m_initialScale * m_scale; if (m_inheritScale)