diff --git a/SDK/include/NDK/Components/CameraComponent.inl b/SDK/include/NDK/Components/CameraComponent.inl index 37f13d9c4..6a6eb3084 100644 --- a/SDK/include/NDK/Components/CameraComponent.inl +++ b/SDK/include/NDK/Components/CameraComponent.inl @@ -3,6 +3,7 @@ // For conditions of distribution and use, see copyright notice in Prerequesites.hpp #include +#include namespace Ndk { @@ -130,7 +131,7 @@ namespace Ndk inline void CameraComponent::SetFOV(float fov) { - NazaraAssert(fov != 0.f, "FOV must be different from zero"); + NazaraAssert(!NzNumberEquals(fov, 0.f), "FOV must be different from zero"); m_fov = fov; InvalidateProjectionMatrix(); diff --git a/SDK/include/NDK/Systems/RenderSystem.hpp b/SDK/include/NDK/Systems/RenderSystem.hpp index 9fc8eac0b..91f63f8ed 100644 --- a/SDK/include/NDK/Systems/RenderSystem.hpp +++ b/SDK/include/NDK/Systems/RenderSystem.hpp @@ -7,6 +7,7 @@ #ifndef NDK_SYSTEMS_RENDERSYSTEM_HPP #define NDK_SYSTEMS_RENDERSYSTEM_HPP +#include #include #include #include @@ -26,6 +27,10 @@ namespace Ndk inline RenderSystem(const RenderSystem& renderSystem); ~RenderSystem() = default; + inline const NzBackgroundRef& GetDefaultBackground() const; + + inline void SetDefaultBackground(NzBackgroundRef background); + static SystemIndex systemIndex; private: @@ -37,6 +42,7 @@ namespace Ndk EntityList m_cameras; EntityList m_drawables; EntityList m_lights; + NzBackgroundRef m_background; NzDepthRenderTechnique m_shadowTechnique; NzForwardRenderTechnique m_renderTechnique; NzRenderTexture m_shadowRT; diff --git a/SDK/include/NDK/Systems/RenderSystem.inl b/SDK/include/NDK/Systems/RenderSystem.inl index 065fc714d..ac4db251f 100644 --- a/SDK/include/NDK/Systems/RenderSystem.inl +++ b/SDK/include/NDK/Systems/RenderSystem.inl @@ -8,4 +8,14 @@ namespace Ndk System(renderSystem) { } + + inline const NzBackgroundRef& RenderSystem::GetDefaultBackground() const + { + return m_background; + } + + inline void RenderSystem::SetDefaultBackground(NzBackgroundRef background) + { + m_background = std::move(background); + } } diff --git a/SDK/src/NDK/Systems/RenderSystem.cpp b/SDK/src/NDK/Systems/RenderSystem.cpp index e13dda40f..dfeb04928 100644 --- a/SDK/src/NDK/Systems/RenderSystem.cpp +++ b/SDK/src/NDK/Systems/RenderSystem.cpp @@ -15,6 +15,7 @@ namespace Ndk { RenderSystem::RenderSystem() { + SetDefaultBackground(NzColorBackground::New()); SetUpdateRate(0.f); } @@ -27,6 +28,8 @@ namespace Ndk void RenderSystem::OnEntityValidation(Entity* entity, bool justAdded) { + NazaraUnused(justAdded); + if (entity->HasComponent() && entity->HasComponent()) { m_cameras.Insert(entity); @@ -51,6 +54,8 @@ namespace Ndk void RenderSystem::OnUpdate(float elapsedTime) { + NazaraUnused(elapsedTime); + UpdateShadowMaps(); for (const Ndk::EntityHandle& camera : m_cameras) @@ -77,11 +82,9 @@ namespace Ndk lightComponent.AddToRenderQueue(renderQueue, lightNode.GetTransformMatrix()); } - NzColorBackground background; - NzSceneData sceneData; sceneData.ambientColor = NzColor(25, 25, 25); - sceneData.background = &background; + sceneData.background = m_background; sceneData.viewer = &camComponent; m_renderTechnique.Draw(sceneData); diff --git a/include/Nazara/Audio.hpp b/include/Nazara/Audio.hpp index b3c3ead17..d0fd17d6f 100644 --- a/include/Nazara/Audio.hpp +++ b/include/Nazara/Audio.hpp @@ -1,4 +1,4 @@ -// This file was automatically generated on 19 Mar 2015 at 13:11:33 +// This file was automatically generated on 24 Jun 2015 at 13:55:50 /* Nazara Engine - Audio module diff --git a/include/Nazara/Core.hpp b/include/Nazara/Core.hpp index 5db5c3007..cabf45cc2 100644 --- a/include/Nazara/Core.hpp +++ b/include/Nazara/Core.hpp @@ -1,4 +1,4 @@ -// This file was automatically generated on 19 Mar 2015 at 13:11:33 +// This file was automatically generated on 24 Jun 2015 at 13:55:50 /* Nazara Engine - Core module @@ -75,6 +75,7 @@ #include #include #include +#include #include #include #include diff --git a/include/Nazara/Core/MemoryManager.hpp b/include/Nazara/Core/MemoryManager.hpp index 23e9982d9..fb17539e9 100644 --- a/include/Nazara/Core/MemoryManager.hpp +++ b/include/Nazara/Core/MemoryManager.hpp @@ -16,6 +16,7 @@ class NAZARA_CORE_API NzMemoryManager public: static void* Allocate(std::size_t size, bool multi = false, const char* file = nullptr, unsigned int line = 0); + static void EnableAllocationFilling(bool allocationFilling); static void EnableAllocationLogging(bool logAllocations); static void Free(void* pointer, bool multi = false); @@ -24,6 +25,7 @@ class NAZARA_CORE_API NzMemoryManager static std::size_t GetAllocatedSize(); static unsigned int GetAllocationCount(); + static bool IsAllocationFillingEnabled(); static bool IsAllocationLoggingEnabled(); static void NextFree(const char* file, unsigned int line); diff --git a/include/Nazara/Graphics.hpp b/include/Nazara/Graphics.hpp index 11b65456b..5b3c94d39 100644 --- a/include/Nazara/Graphics.hpp +++ b/include/Nazara/Graphics.hpp @@ -1,4 +1,4 @@ -// This file was automatically generated on 19 Mar 2015 at 13:11:33 +// This file was automatically generated on 24 Jun 2015 at 13:55:50 /* Nazara Engine - Graphics module @@ -33,7 +33,6 @@ #include #include #include -#include #include #include #include @@ -53,6 +52,7 @@ #include #include #include +#include #include #include #include @@ -64,16 +64,14 @@ #include #include #include +#include #include -#include -#include -#include +#include #include #include #include #include #include #include -#include #endif // NAZARA_GLOBAL_GRAPHICS_HPP diff --git a/include/Nazara/Graphics/AbstractBackground.hpp b/include/Nazara/Graphics/AbstractBackground.hpp index 9bccde7c7..b557ee871 100644 --- a/include/Nazara/Graphics/AbstractBackground.hpp +++ b/include/Nazara/Graphics/AbstractBackground.hpp @@ -8,12 +8,19 @@ #define NAZARA_ABSTRACTBACKGROUND_HPP #include +#include +#include #include #include +class NzAbstractBackground; class NzAbstractViewer; -class NAZARA_GRAPHICS_API NzAbstractBackground +using NzBackgroundConstRef = NzObjectRef; +using NzBackgroundLibrary = NzObjectLibrary; +using NzBackgroundRef = NzObjectRef; + +class NAZARA_GRAPHICS_API NzAbstractBackground : public NzRefCounted { public: NzAbstractBackground() = default; @@ -22,6 +29,9 @@ class NAZARA_GRAPHICS_API NzAbstractBackground virtual void Draw(const NzAbstractViewer* viewer) const = 0; virtual nzBackgroundType GetBackgroundType() const = 0; + + private: + static NzBackgroundLibrary::LibraryMap s_library; }; #endif // NAZARA_ABSTRACTBACKGROUND_HPP diff --git a/include/Nazara/Graphics/Camera.hpp b/include/Nazara/Graphics/Camera.hpp deleted file mode 100644 index de474bc00..000000000 --- a/include/Nazara/Graphics/Camera.hpp +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Engine - Graphics module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#pragma once - -#ifndef NAZARA_CAMERA_HPP -#define NAZARA_CAMERA_HPP - -#include -#include -#include -#include -#include -#include -#include -#include - -class NAZARA_GRAPHICS_API NzCamera : public NzAbstractViewer, public NzNode -{ - public: - NzCamera(); - ~NzCamera() = default; - - void EnsureFrustumUpdate() const; - void EnsureProjectionMatrixUpdate() const; - void EnsureViewMatrixUpdate() const; - void EnsureViewportUpdate() const; - - float GetAspectRatio() const; - NzVector3f GetEyePosition() const; - 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; - const NzMatrix4f& GetViewMatrix() const; - const NzRecti& GetViewport() const; - float GetZFar() const; - float GetZNear() const; - - void SetFOV(float fov); - void SetTarget(const NzRenderTarget* renderTarget); - void SetTarget(const NzRenderTarget& renderTarget); - void SetTargetRegion(const NzRectf& region); - void SetViewport(const NzRecti& viewport); - void SetZFar(float zFar); - void SetZNear(float zNear); - - private: - void ApplyView() const override; - void InvalidateNode() override; - - void OnRenderTargetRelease(const NzRenderTarget* renderTarget); - void OnRenderTargetSizeChange(const NzRenderTarget* renderTarget); - - void UpdateFrustum() const; - void UpdateProjectionMatrix() const; - void UpdateViewMatrix() const; - void UpdateViewport() const; - - NazaraSlot(NzRenderTarget, OnRenderTargetRelease, m_targetReleaseSlot); - NazaraSlot(NzRenderTarget, OnRenderTargetSizeChange, m_targetResizeSlot); - - mutable NzFrustumf m_frustum; - mutable NzMatrix4f m_projectionMatrix; - mutable NzMatrix4f m_viewMatrix; - NzRectf m_targetRegion; - mutable NzRecti m_viewport; - const NzRenderTarget* m_target; - mutable bool m_frustumUpdated; - mutable bool m_projectionMatrixUpdated; - mutable bool m_viewMatrixUpdated; - mutable bool m_viewportUpdated; - mutable float m_aspectRatio; - float m_fov; - float m_zFar; - float m_zNear; -}; - -#endif // NAZARA_CAMERA_HPP diff --git a/include/Nazara/Graphics/ColorBackground.hpp b/include/Nazara/Graphics/ColorBackground.hpp index aa00fd154..c769ba15d 100644 --- a/include/Nazara/Graphics/ColorBackground.hpp +++ b/include/Nazara/Graphics/ColorBackground.hpp @@ -12,6 +12,11 @@ #include #include +class NzColorBackground; + +using NzColorBackgroundConstRef = NzObjectRef; +using NzColorBackgroundRef = NzObjectRef; + class NAZARA_GRAPHICS_API NzColorBackground : public NzAbstractBackground { public: @@ -24,6 +29,8 @@ class NAZARA_GRAPHICS_API NzColorBackground : public NzAbstractBackground void SetColor(const NzColor& color); + template static NzColorBackgroundRef New(Args&&... args); + private: NzColor m_color; NzUberShaderConstRef m_uberShader; @@ -32,4 +39,6 @@ class NAZARA_GRAPHICS_API NzColorBackground : public NzAbstractBackground int m_vertexDepthUniform; }; +#include + #endif // NAZARA_COLORBACKGROUND_HPP diff --git a/include/Nazara/Graphics/ColorBackground.inl b/include/Nazara/Graphics/ColorBackground.inl new file mode 100644 index 000000000..c082703f0 --- /dev/null +++ b/include/Nazara/Graphics/ColorBackground.inl @@ -0,0 +1,17 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Engine - Graphics module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include + +template +NzColorBackgroundRef NzColorBackground::New(Args&&... args) +{ + std::unique_ptr object(new NzColorBackground(std::forward(args)...)); + object->SetPersistent(false); + + return object.release(); +} + +#include diff --git a/include/Nazara/Graphics/ForwardRenderTechnique.inl b/include/Nazara/Graphics/ForwardRenderTechnique.inl index 888c0d0fa..8d4d8736b 100644 --- a/include/Nazara/Graphics/ForwardRenderTechnique.inl +++ b/include/Nazara/Graphics/ForwardRenderTechnique.inl @@ -52,6 +52,9 @@ inline void NzForwardRenderTechnique::SendLightUniforms(const NzShader* shader, inline float NzForwardRenderTechnique::ComputeDirectionalLightScore(const NzSpheref& object, const NzAbstractRenderQueue::DirectionalLight& light) { + NazaraUnused(object); + NazaraUnused(light); + ///TODO: Compute a score depending on the light luminosity return 0.f; } @@ -70,6 +73,9 @@ inline float NzForwardRenderTechnique::ComputeSpotLightScore(const NzSpheref& ob inline bool NzForwardRenderTechnique::IsDirectionalLightSuitable(const NzSpheref& object, const NzAbstractRenderQueue::DirectionalLight& light) { + NazaraUnused(object); + NazaraUnused(light); + // Directional light are always suitables return true; } diff --git a/include/Nazara/Graphics/InstancedRenderable.inl b/include/Nazara/Graphics/InstancedRenderable.inl index be49fa49c..6023e684c 100644 --- a/include/Nazara/Graphics/InstancedRenderable.inl +++ b/include/Nazara/Graphics/InstancedRenderable.inl @@ -3,6 +3,7 @@ // For conditions of distribution and use, see copyright notice in Config.hpp inline NzInstancedRenderable::NzInstancedRenderable(const NzInstancedRenderable& renderable) : +NzRefCounted(), m_boundingVolume(renderable.m_boundingVolume), m_boundingVolumeUpdated(renderable.m_boundingVolumeUpdated) { diff --git a/include/Nazara/Graphics/Model.inl b/include/Nazara/Graphics/Model.inl index 007ad23d0..b45c6296f 100644 --- a/include/Nazara/Graphics/Model.inl +++ b/include/Nazara/Graphics/Model.inl @@ -3,7 +3,7 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include -#include +#include template NzModelRef NzModel::New(Args&&... args) @@ -14,4 +14,4 @@ NzModelRef NzModel::New(Args&&... args) return object.release(); } -#include +#include diff --git a/include/Nazara/Graphics/ParticleSystem.hpp b/include/Nazara/Graphics/ParticleSystem.hpp index 36f5969c6..73a53addf 100644 --- a/include/Nazara/Graphics/ParticleSystem.hpp +++ b/include/Nazara/Graphics/ParticleSystem.hpp @@ -8,33 +8,32 @@ #define NAZARA_PARTICLESYSTEM_HPP #include -#include #include #include #include #include #include -#include +#include #include #include #include #include #include -class NAZARA_GRAPHICS_API NzParticleSystem : public NzSceneNode, NzUpdatable +class NAZARA_GRAPHICS_API NzParticleSystem : public NzRenderable { public: NzParticleSystem(unsigned int maxParticleCount, nzParticleLayout layout); - NzParticleSystem(unsigned int maxParticleCount, const NzParticleDeclaration* declaration); + NzParticleSystem(unsigned int maxParticleCount, NzParticleDeclarationConstRef declaration); NzParticleSystem(const NzParticleSystem& emitter); ~NzParticleSystem(); - void AddController(NzParticleController* controller); + void AddController(NzParticleControllerRef controller); void AddEmitter(NzParticleEmitter* emitter); - void AddGenerator(NzParticleGenerator* generator); - void AddToRenderQueue(NzAbstractRenderQueue* renderQueue) const; + void AddGenerator(NzParticleGeneratorRef generator); + void AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const NzMatrix4f& transformMatrix) const; - void ApplyControllers(NzParticleMapper& mapper, unsigned int particleCount, float elapsedTime, float& stepAccumulator); + void ApplyControllers(NzParticleMapper& mapper, unsigned int particleCount, float elapsedTime); void* CreateParticle(); void* CreateParticles(unsigned int count); @@ -44,14 +43,12 @@ class NAZARA_GRAPHICS_API NzParticleSystem : public NzSceneNode, NzUpdatable void* GenerateParticle(); void* GenerateParticles(unsigned int count); - const NzParticleDeclaration* GetDeclaration() const; + const NzParticleDeclarationConstRef& GetDeclaration() const; float GetFixedStepSize() const; unsigned int GetMaxParticleCount() const; unsigned int GetParticleCount() const; unsigned int GetParticleSize() const; - nzSceneNodeType GetSceneNodeType() const override; - bool IsDrawable() const; bool IsFixedStepEnabled() const; void KillParticle(unsigned int index); @@ -64,14 +61,14 @@ class NAZARA_GRAPHICS_API NzParticleSystem : public NzSceneNode, NzUpdatable void SetFixedStepSize(float stepSize); void SetRenderer(NzParticleRenderer* renderer); + void Update(float elapsedTime); + void UpdateBoundingVolume(const NzMatrix4f& transformMatrix) override; + NzParticleSystem& operator=(const NzParticleSystem& emitter); private: void MakeBoundingVolume() const override; - void Register() override; void ResizeBuffer(); - void Unregister() override; - void Update() override; std::set> m_dyingParticles; mutable std::vector m_buffer; diff --git a/include/Nazara/Graphics/Renderable.hpp b/include/Nazara/Graphics/Renderable.hpp index 7226db4ae..5e2794bc3 100644 --- a/include/Nazara/Graphics/Renderable.hpp +++ b/include/Nazara/Graphics/Renderable.hpp @@ -27,7 +27,7 @@ class NAZARA_GRAPHICS_API NzRenderable inline void EnsureBoundingVolumeUpdated() const; virtual void AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const NzMatrix4f& transformMatrix) const = 0; - virtual bool Cull(const NzFrustumf& frustum, const NzMatrix4f& transformMatrix) const = 0; + virtual bool Cull(const NzFrustumf& frustum, const NzMatrix4f& transformMatrix) const; virtual const NzBoundingVolumef& GetBoundingVolume() const; virtual void UpdateBoundingVolume(const NzMatrix4f& transformMatrix); diff --git a/include/Nazara/Graphics/Scene.hpp b/include/Nazara/Graphics/Scene.hpp deleted file mode 100644 index b1088fa44..000000000 --- a/include/Nazara/Graphics/Scene.hpp +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Engine - Graphics module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#pragma once - -#ifndef NAZARA_SCENE_HPP -#define NAZARA_SCENE_HPP - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -class NzAbstractRenderQueue; -class NzAbstractViewer; -class NzNode; -class NzSceneNode; - -class NAZARA_GRAPHICS_API NzScene -{ - friend NzSceneNode; - - public: - NzScene(); - ~NzScene() = default; - - void AddToVisibilityList(NzUpdatable* object); - - template T* CreateNode(Args&&... args); - template T* CreateNode(const NzString& name, Args&&... args); - template T* CreateNode(const NzString& name, const NzString& templateNodeName); - - void Clear(); - - void Cull(); - void Draw(); - - void EnableBackground(bool enable); - - NzSceneNode* FindNode(const NzString& name); - const NzSceneNode* FindNode(const NzString& name) const; - template T* FindNodeAs(const NzString& name); - template const T* FindNodeAs(const NzString& name) const; - - 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 NzSceneNode& GetRoot() const; - NzAbstractViewer* GetViewer() const; - NzVector3f GetUp() const; - float GetUpdateTime() const; - unsigned int GetUpdatePerSecond() const; - - bool IsBackgroundEnabled() const; - - void RegisterForUpdate(NzUpdatable* object); - - void RemoveNode(NzSceneNode* node); - void RemoveNode(const NzString& name); - - void RenderFrame(); - - void SetAmbientColor(const NzColor& color); - void SetBackground(NzAbstractBackground* background); - void SetRenderTechnique(NzAbstractRenderTechnique* renderTechnique); - void SetViewer(NzAbstractViewer* viewer); - void SetViewer(NzAbstractViewer& viewer); - void SetUpdatePerSecond(unsigned int updatePerSecond); - - void UnregisterForUpdate(NzUpdatable* object); - - void Update(); - void UpdateVisible(); - - operator const NzSceneNode&() const; - - private: - bool ChangeNodeName(NzSceneNode* node, const NzString& newName); - bool RegisterSceneNode(const NzString& name, NzSceneNode* node); - void RecursiveFrustumCull(NzAbstractRenderQueue* renderQueue, const NzFrustumf& frustum, NzNode* node); - - mutable std::unique_ptr m_background; - mutable std::unique_ptr m_renderTechnique; - std::unordered_map m_nodeMap; - std::vector> m_nodes; - std::vector m_updateList; - std::vector m_visibleUpdateList; - NzClock m_updateClock; - NzColor m_ambientColor; - NzSceneRoot m_root; - NzAbstractViewer* m_viewer; - bool m_backgroundEnabled; - bool m_update; - float m_frameTime; - float m_updateTime; - mutable int m_renderTechniqueRanking; - unsigned int m_updatePerSecond; -}; - -#include - -#endif // NAZARA_SCENE_HPP diff --git a/include/Nazara/Graphics/Scene.inl b/include/Nazara/Graphics/Scene.inl deleted file mode 100644 index 2231d1364..000000000 --- a/include/Nazara/Graphics/Scene.inl +++ /dev/null @@ -1,139 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Engine - Graphics module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#include -#include - -///TODO: Déplacer vers SceneNode et exposer - -// Pour être sûr que ce code soit à jour -static_assert(nzSceneNodeType_Max == 6, "Please update the code below"); - -class NzLight; -class NzModel; -class NzParticleEmitter; -class NzSceneRoot; -class NzSprite; -class NzTextSprite; - -template -constexpr nzSceneNodeType NzImplGetType() -{ - return nzSceneNodeType_User; -} - -template<> -inline constexpr nzSceneNodeType NzImplGetType() -{ - return nzSceneNodeType_Light; -} - -template<> -inline constexpr nzSceneNodeType NzImplGetType() -{ - return nzSceneNodeType_Model; -} - -template<> -inline constexpr nzSceneNodeType NzImplGetType() -{ - return nzSceneNodeType_ParticleEmitter; -} - -template<> -inline constexpr nzSceneNodeType NzImplGetType() -{ - return nzSceneNodeType_Root; -} - -template<> -inline constexpr nzSceneNodeType NzImplGetType() -{ - return nzSceneNodeType_Sprite; -} - -template<> -inline constexpr nzSceneNodeType NzImplGetType() -{ - return nzSceneNodeType_TextSprite; -} - - -template -T* NzScene::CreateNode(Args&&... args) -{ - std::unique_ptr node(new T(std::forward(args)...)); - if (!RegisterSceneNode(NzString(), node.get())) - return nullptr; - - return node.release(); -} - -template -T* NzScene::CreateNode(const NzString& name, Args&&... args) -{ - std::unique_ptr node(new T(std::forward(args)...)); - if (!RegisterSceneNode(name, node.get())) - return nullptr; - - return node.release(); -} - -template -T* NzScene::CreateNode(const NzString& name, const NzString& templateNodeName) -{ - auto it = m_nodeMap.find(templateNodeName); - if (it == m_nodeMap.end()) - { - NazaraError("Node \"" + templateNodeName + "\" is not registred"); - return nullptr; - } - - NzSceneNode* sceneNode = it->second; - if (sceneNode->GetSceneNodeType() != NzImplGetType()) - { - NazaraError("Scene node type of T (" + NzString::Number(NzImplGetType()) + ") doesn't match template scene node type (" + NzString::Number(sceneNode->GetSceneNodeType()) + ")"); - return nullptr; - } - - std::unique_ptr node(static_cast(sceneNode)->Copy()); - if (!RegisterSceneNode(name, node.get())) - return nullptr; - - return node.release(); -} - -template -T* NzScene::FindNodeAs(const NzString& name) -{ - NzSceneNode* sceneNode = FindNode(name); - if (!sceneNode) - return nullptr; - - if (sceneNode->GetSceneNodeType() != NzImplGetType()) - { - NazaraError("Scene node type of T (" + NzString::Number(NzImplGetType()) + ") doesn't match \"" + name + "\" scene node type (" + NzString::Number(sceneNode->GetSceneNodeType()) + ")"); - return nullptr; - } - - return static_cast(sceneNode); -} - -template -const T* NzScene::FindNodeAs(const NzString& name) const -{ - const NzSceneNode* sceneNode = FindNode(name); - if (!sceneNode) - return nullptr; - - if (sceneNode->GetSceneNodeType() != NzImplGetType()) - { - NazaraError("Scene node type of T (" + NzString::Number(NzImplGetType()) + ") doesn't match \"" + name + "\" scene node type (" + NzString::Number(sceneNode->GetSceneNodeType()) + ")"); - return nullptr; - } - - return static_cast(sceneNode); -} - -#include diff --git a/include/Nazara/Graphics/SceneNode.hpp b/include/Nazara/Graphics/SceneNode.hpp deleted file mode 100644 index 80768ebb0..000000000 --- a/include/Nazara/Graphics/SceneNode.hpp +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Engine - Graphics module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#pragma once - -#ifndef NAZARA_SCENENODE_HPP -#define NAZARA_SCENENODE_HPP - -#include -#include -#include -#include -#include -#include - -class NzAbstractRenderQueue; -class NzScene; - -class NAZARA_GRAPHICS_API NzSceneNode : public NzNode -{ - friend NzScene; - - public: - NzSceneNode(); - NzSceneNode(const NzSceneNode& sceneNode); - NzSceneNode(NzSceneNode&& sceneNode) = delete; - virtual ~NzSceneNode(); - - virtual void AddToRenderQueue(NzAbstractRenderQueue* renderQueue) const = 0; - - // Idiôme "virtual constructor" - virtual NzSceneNode* Clone() const = 0; - virtual NzSceneNode* Create() const = 0; - - void EnableDrawing(bool drawingEnabled); - - NzVector3f GetBackward() const; - virtual const NzBoundingVolumef& GetBoundingVolume() const; - NzVector3f GetDown() const; - NzVector3f GetForward() const; - NzVector3f GetLeft() const; - const NzString& GetName() const; - nzNodeType GetNodeType() const final; - NzVector3f GetRight() const; - NzScene* GetScene() const; - virtual nzSceneNodeType GetSceneNodeType() const = 0; - NzVector3f GetUp() const; - - void InvalidateAABB(); - - virtual bool IsDrawable() const = 0; - bool IsDrawingEnabled() const; - bool IsVisible() const; - - void Remove(); - - bool SetName(const NzString& name); - - NzSceneNode& operator=(const NzSceneNode& sceneNode); - NzSceneNode& operator=(NzSceneNode&& sceneNode) = delete; - - protected: - virtual bool FrustumCull(const NzFrustumf& frustum) const; - virtual void InvalidateNode() override; - virtual void MakeBoundingVolume() const = 0; - virtual void OnParenting(const NzNode* parent) override; - virtual void OnVisibilityChange(bool visibility); - void RecursiveSetScene(NzScene* scene, NzNode* node); - virtual void Register(); - void SetNameInternal(const NzString& name); - void SetScene(NzScene* scene); - virtual void Unregister(); - virtual void Update(); - virtual void UpdateBoundingVolume() const; - - mutable NzBoundingVolumef m_boundingVolume; - NzScene* m_scene; - mutable bool m_boundingVolumeUpdated; - bool m_drawingEnabled; - bool m_visible; - - private: - void UpdateVisibility(const NzFrustumf& frustum); - - NzString m_name; -}; - -#endif // NAZARA_SCENENODE_HPP diff --git a/include/Nazara/Graphics/SceneRoot.hpp b/include/Nazara/Graphics/SceneRoot.hpp deleted file mode 100644 index 7a0465d60..000000000 --- a/include/Nazara/Graphics/SceneRoot.hpp +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Engine - Graphics module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#pragma once - -#ifndef NAZARA_SCENEROOT_HPP -#define NAZARA_SCENEROOT_HPP - -#include -#include - -class NzScene; - -class NAZARA_GRAPHICS_API NzSceneRoot : public NzSceneNode -{ - friend NzScene; - - public: - void AddToRenderQueue(NzAbstractRenderQueue* renderQueue) const override; - - nzSceneNodeType GetSceneNodeType() const override; - - bool IsDrawable() const; - - private: - NzSceneRoot(NzScene* scene); - virtual ~NzSceneRoot(); - - NzSceneRoot* Clone() const; - NzSceneRoot* Create() const; - - void MakeBoundingVolume() const override; - void Register(); - void Unregister(); -}; - -#endif // NAZARA_SCENEROOT_HPP diff --git a/include/Nazara/Graphics/SkyboxBackground.hpp b/include/Nazara/Graphics/SkyboxBackground.hpp index 29a170c79..08a9984ee 100644 --- a/include/Nazara/Graphics/SkyboxBackground.hpp +++ b/include/Nazara/Graphics/SkyboxBackground.hpp @@ -15,28 +15,39 @@ #include #include +class NzSkyboxBackground; + +using NzSkyboxBackgroundConstRef = NzObjectRef; +using NzSkyboxBackgroundRef = NzObjectRef; + class NAZARA_GRAPHICS_API NzSkyboxBackground : public NzAbstractBackground { + friend class NzGraphics; + public: - NzSkyboxBackground(); - NzSkyboxBackground(NzTexture* cubemapTexture); - ~NzSkyboxBackground(); + NzSkyboxBackground(NzTextureRef cubemapTexture = NzTextureRef()); + ~NzSkyboxBackground() = default; void Draw(const NzAbstractViewer* viewer) const; nzBackgroundType GetBackgroundType() const; - NzTexture* GetTexture() const; - const NzTextureSampler& GetTextureSampler(); + inline const NzTextureRef& GetTexture() const; + inline NzTextureSampler& GetTextureSampler(); + inline const NzTextureSampler& GetTextureSampler() const; - void SetTexture(NzTexture* cubemapTexture); - void SetTextureSampler(const NzTextureSampler& sampler); + inline void SetTexture(NzTextureRef cubemapTexture); + inline void SetTextureSampler(const NzTextureSampler& sampler); + + template static NzSkyboxBackgroundRef New(Args&&... args); private: + static bool Initialize(); + static void Uninitialize(); + NzTextureRef m_texture; NzTextureSampler m_sampler; - NzIndexBufferRef m_indexBuffer; - NzShaderRef m_shader; - NzVertexBufferRef m_vertexBuffer; }; +#include + #endif // NAZARA_SKYBOXBACKGROUND_HPP diff --git a/include/Nazara/Graphics/SkyboxBackground.inl b/include/Nazara/Graphics/SkyboxBackground.inl new file mode 100644 index 000000000..f8105bd30 --- /dev/null +++ b/include/Nazara/Graphics/SkyboxBackground.inl @@ -0,0 +1,45 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Engine - Graphics module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include + +inline const NzTextureRef& NzSkyboxBackground::GetTexture() const +{ + return m_texture; +} + +inline NzTextureSampler& NzSkyboxBackground::GetTextureSampler() +{ + return m_sampler; +} + +inline const NzTextureSampler& NzSkyboxBackground::GetTextureSampler() const +{ + return m_sampler; +} + +inline void NzSkyboxBackground::SetTexture(NzTextureRef cubemapTexture) +{ + NazaraAssert(!cubemapTexture || cubemapTexture->IsValid(), "Invalid texture"); + NazaraAssert(!cubemapTexture || cubemapTexture->IsCubemap(), "Texture must be a cubemap"); + + m_texture = std::move(cubemapTexture); +} + +void NzSkyboxBackground::SetTextureSampler(const NzTextureSampler& sampler) +{ + m_sampler = sampler; +} + +template +NzSkyboxBackgroundRef NzSkyboxBackground::New(Args&&... args) +{ + std::unique_ptr object(new NzSkyboxBackground(std::forward(args)...)); + object->SetPersistent(false); + + return object.release(); +} + +#include diff --git a/include/Nazara/Graphics/Sprite.hpp b/include/Nazara/Graphics/Sprite.hpp index babb30546..756cb4ba8 100644 --- a/include/Nazara/Graphics/Sprite.hpp +++ b/include/Nazara/Graphics/Sprite.hpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include diff --git a/include/Nazara/Graphics/TextureBackground.hpp b/include/Nazara/Graphics/TextureBackground.hpp index 78e25e2fb..0bd8804bb 100644 --- a/include/Nazara/Graphics/TextureBackground.hpp +++ b/include/Nazara/Graphics/TextureBackground.hpp @@ -12,18 +12,24 @@ #include #include +class NzTextureBackground; + +using NzTextureBackgroundConstRef = NzObjectRef; +using NzTextureBackgroundRef = NzObjectRef; + class NAZARA_GRAPHICS_API NzTextureBackground : public NzAbstractBackground { public: - NzTextureBackground(); - NzTextureBackground(NzTexture* texture); + NzTextureBackground(NzTextureRef texture = NzTextureRef()); void Draw(const NzAbstractViewer* viewer) const; nzBackgroundType GetBackgroundType() const; - NzTexture* GetTexture() const; + inline const NzTextureRef& GetTexture() const; - void SetTexture(NzTexture* texture); + inline void SetTexture(NzTextureRef texture); + + template static NzTextureBackgroundRef New(Args&&... args); private: NzTextureRef m_texture; @@ -34,4 +40,6 @@ class NAZARA_GRAPHICS_API NzTextureBackground : public NzAbstractBackground int m_vertexDepthUniform; }; +#include + #endif // NAZARA_TEXTUREBACKGROUND_HPP diff --git a/include/Nazara/Graphics/TextureBackground.inl b/include/Nazara/Graphics/TextureBackground.inl new file mode 100644 index 000000000..7016b4c9a --- /dev/null +++ b/include/Nazara/Graphics/TextureBackground.inl @@ -0,0 +1,29 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Engine - Graphics module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include + +inline const NzTextureRef& NzTextureBackground::GetTexture() const +{ + return m_texture; +} + +inline void NzTextureBackground::SetTexture(NzTextureRef texture) +{ + NazaraAssert(!texture || texture->IsValid(), "Invalid texture"); + + m_texture = std::move(texture); +} + +template +NzTextureBackgroundRef NzTextureBackground::New(Args&&... args) +{ + std::unique_ptr object(new NzTextureBackground(std::forward(args)...)); + object->SetPersistent(false); + + return object.release(); +} + +#include diff --git a/include/Nazara/Graphics/View.hpp b/include/Nazara/Graphics/View.hpp deleted file mode 100644 index 0bbf02929..000000000 --- a/include/Nazara/Graphics/View.hpp +++ /dev/null @@ -1,97 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Engine - Graphics module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#pragma once - -#ifndef NAZARA_VIEW_HPP -#define NAZARA_VIEW_HPP - -#include -#include -#include -#include -#include -#include -#include -#include - -class NAZARA_GRAPHICS_API NzView : public NzAbstractViewer, public NzNode -{ - public: - NzView(); - NzView(const NzVector2f& size); - ~NzView() = default; - - void EnsureFrustumUpdate() const; - void EnsureProjectionMatrixUpdate() const; - void EnsureViewMatrixUpdate() const; - void EnsureViewportUpdate() const; - - float GetAspectRatio() const; - NzVector3f GetEyePosition() const; - NzVector3f GetForward() const; - const NzFrustumf& GetFrustum() const; - NzVector3f GetGlobalForward() const; - NzVector3f GetGlobalRight() const; - NzVector3f GetGlobalUp() const; - const NzMatrix4f& GetInvViewProjMatrix() const; - const NzMatrix4f& GetProjectionMatrix() const; - const NzVector2f& GetSize() const; - const NzRenderTarget* GetTarget() const; - const NzRectf& GetTargetRegion() const; - const NzMatrix4f& GetViewMatrix() const; - const NzMatrix4f& GetViewProjMatrix() const; - const NzRecti& GetViewport() const; - float GetZFar() const; - float GetZNear() const; - - NzVector2f MapPixelToWorld(const NzVector2i& pixel); - NzVector2i MapWorldToPixel(const NzVector2f& coords); - - void SetSize(const NzVector2f& size); - void SetSize(float width, float height); - void SetTarget(const NzRenderTarget* renderTarget); - void SetTarget(const NzRenderTarget& renderTarget); - void SetTargetRegion(const NzRectf& region); - void SetViewport(const NzRecti& viewport); - void SetZFar(float zFar); - void SetZNear(float zNear); - - private: - void ApplyView() const override; - void InvalidateNode() override; - - void OnRenderTargetRelease(const NzRenderTarget* renderTarget); - void OnRenderTargetSizeChange(const NzRenderTarget* renderTarget); - - void UpdateFrustum() const; - void UpdateInvViewProjMatrix() const; - void UpdateProjectionMatrix() const; - void UpdateViewMatrix() const; - void UpdateViewProjMatrix() const; - void UpdateViewport() const; - - NazaraSlot(NzRenderTarget, OnRenderTargetRelease, m_targetReleaseSlot); - NazaraSlot(NzRenderTarget, OnRenderTargetSizeChange, m_targetResizeSlot); - - mutable NzFrustumf m_frustum; - mutable NzMatrix4f m_invViewProjMatrix; - mutable NzMatrix4f m_projectionMatrix; - mutable NzMatrix4f m_viewMatrix; - mutable NzMatrix4f m_viewProjMatrix; - NzRectf m_targetRegion; - mutable NzRecti m_viewport; - NzVector2f m_size; - const NzRenderTarget* m_target; - mutable bool m_frustumUpdated; - mutable bool m_invViewProjMatrixUpdated; - mutable bool m_projectionMatrixUpdated; - mutable bool m_viewMatrixUpdated; - mutable bool m_viewProjMatrixUpdated; - mutable bool m_viewportUpdated; - float m_zFar; - float m_zNear; -}; - -#endif // NAZARA_VIEW_HPP diff --git a/include/Nazara/Lua.hpp b/include/Nazara/Lua.hpp index d0b306b5d..f73541cb8 100644 --- a/include/Nazara/Lua.hpp +++ b/include/Nazara/Lua.hpp @@ -1,4 +1,4 @@ -// This file was automatically generated on 19 Mar 2015 at 13:11:33 +// This file was automatically generated on 24 Jun 2015 at 13:55:50 /* Nazara Engine - Lua scripting module diff --git a/include/Nazara/Math.hpp b/include/Nazara/Math.hpp index 84fd959c9..fd0239a61 100644 --- a/include/Nazara/Math.hpp +++ b/include/Nazara/Math.hpp @@ -1,4 +1,4 @@ -// This file was automatically generated on 19 Mar 2015 at 13:11:33 +// This file was automatically generated on 24 Jun 2015 at 13:55:50 /* Nazara Engine - Mathematics module diff --git a/include/Nazara/Noise.hpp b/include/Nazara/Noise.hpp index 3d332cef1..9ab591763 100644 --- a/include/Nazara/Noise.hpp +++ b/include/Nazara/Noise.hpp @@ -1,4 +1,4 @@ -// This file was automatically generated on 19 Mar 2015 at 13:11:33 +// This file was automatically generated on 24 Jun 2015 at 13:55:50 /* Nazara Engine - Noise module diff --git a/include/Nazara/Physics.hpp b/include/Nazara/Physics.hpp index a9a16890b..a4fcfc768 100644 --- a/include/Nazara/Physics.hpp +++ b/include/Nazara/Physics.hpp @@ -1,4 +1,4 @@ -// This file was automatically generated on 19 Mar 2015 at 13:11:33 +// This file was automatically generated on 24 Jun 2015 at 13:55:50 /* Nazara Engine - Physics module diff --git a/include/Nazara/Renderer.hpp b/include/Nazara/Renderer.hpp index 80ea8fa6c..b57d4c5a5 100644 --- a/include/Nazara/Renderer.hpp +++ b/include/Nazara/Renderer.hpp @@ -1,4 +1,4 @@ -// This file was automatically generated on 19 Mar 2015 at 13:11:33 +// This file was automatically generated on 24 Jun 2015 at 13:55:50 /* Nazara Engine - Renderer module diff --git a/include/Nazara/Utility.hpp b/include/Nazara/Utility.hpp index 5ef7cad32..156ca6389 100644 --- a/include/Nazara/Utility.hpp +++ b/include/Nazara/Utility.hpp @@ -1,4 +1,4 @@ -// This file was automatically generated on 19 Mar 2015 at 13:11:33 +// This file was automatically generated on 24 Jun 2015 at 13:55:50 /* Nazara Engine - Utility module diff --git a/src/Nazara/Core/MemoryManager.cpp b/src/Nazara/Core/MemoryManager.cpp index 9820d24b6..353f2c5bb 100644 --- a/src/Nazara/Core/MemoryManager.cpp +++ b/src/Nazara/Core/MemoryManager.cpp @@ -32,6 +32,7 @@ namespace unsigned int magic; }; + bool s_allocationFilling = true; bool s_allocationLogging = false; bool s_initialized = false; const char* s_logFileName = "NazaraMemory.log"; @@ -114,6 +115,12 @@ void* NzMemoryManager::Allocate(std::size_t size, bool multi, const char* file, s_allocatedSize += size; s_allocationCount++; + if (s_allocationFilling) + { + nzUInt8* data = reinterpret_cast(ptr) + sizeof(Block); + std::memset(data, 0xFF, size); + } + if (s_allocationLogging) { char timeStr[23]; @@ -138,6 +145,11 @@ void* NzMemoryManager::Allocate(std::size_t size, bool multi, const char* file, return reinterpret_cast(ptr) + sizeof(Block); } +void NzMemoryManager::EnableAllocationFilling(bool allocationFilling) +{ + s_allocationFilling = allocationFilling; +} + void NzMemoryManager::EnableAllocationLogging(bool logAllocations) { s_allocationLogging = logAllocations; @@ -195,6 +207,12 @@ void NzMemoryManager::Free(void* pointer, bool multi) s_allocatedBlock--; s_allocatedSize -= ptr->size; + if (s_allocationFilling) + { + nzUInt8* data = reinterpret_cast(ptr) + sizeof(Block); + std::memset(data, 0xFF, ptr->size); + } + std::free(ptr); s_nextFreeFile = nullptr; @@ -222,6 +240,11 @@ unsigned int NzMemoryManager::GetAllocationCount() return s_allocationCount; } +bool NzMemoryManager::IsAllocationFillingEnabled() +{ + return s_allocationFilling; +} + bool NzMemoryManager::IsAllocationLoggingEnabled() { return s_allocationLogging; diff --git a/src/Nazara/Graphics/AbstractBackground.cpp b/src/Nazara/Graphics/AbstractBackground.cpp index 5792b448f..510663c39 100644 --- a/src/Nazara/Graphics/AbstractBackground.cpp +++ b/src/Nazara/Graphics/AbstractBackground.cpp @@ -6,3 +6,5 @@ #include NzAbstractBackground::~NzAbstractBackground() = default; + +NzBackgroundLibrary::LibraryMap NzAbstractBackground::s_library; diff --git a/src/Nazara/Graphics/AbstractRenderQueue.cpp b/src/Nazara/Graphics/AbstractRenderQueue.cpp index 5b24d0946..df86300f0 100644 --- a/src/Nazara/Graphics/AbstractRenderQueue.cpp +++ b/src/Nazara/Graphics/AbstractRenderQueue.cpp @@ -24,6 +24,8 @@ void NzAbstractRenderQueue::AddSpotLight(const SpotLight& light) void NzAbstractRenderQueue::Clear(bool fully) { + NazaraUnused(fully); + directionalLights.clear(); pointLights.clear(); spotLights.clear(); diff --git a/src/Nazara/Graphics/Camera.cpp b/src/Nazara/Graphics/Camera.cpp deleted file mode 100644 index 7fc915e3f..000000000 --- a/src/Nazara/Graphics/Camera.cpp +++ /dev/null @@ -1,354 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Engine - Graphics module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#include -#include -#include -#include -#include - -NzCamera::NzCamera() : -m_targetRegion(0.f, 0.f, 1.f, 1.f), -m_target(nullptr), -m_frustumUpdated(false), -m_projectionMatrixUpdated(false), -m_viewMatrixUpdated(false), -m_viewportUpdated(false), -m_aspectRatio(0.f), -m_fov(70.f), -m_zFar(100.f), -m_zNear(1.f) -{ -} - -void NzCamera::EnsureFrustumUpdate() const -{ - if (!m_frustumUpdated) - UpdateFrustum(); -} - -void NzCamera::EnsureProjectionMatrixUpdate() const -{ - if (!m_projectionMatrixUpdated) - UpdateProjectionMatrix(); -} - -void NzCamera::EnsureViewMatrixUpdate() const -{ - if (!m_viewMatrixUpdated) - UpdateViewMatrix(); -} - -void NzCamera::EnsureViewportUpdate() const -{ - if (!m_viewportUpdated) - UpdateViewport(); -} - -float NzCamera::GetAspectRatio() const -{ - return m_aspectRatio; -} - -NzVector3f NzCamera::GetEyePosition() const -{ - return GetPosition(nzCoordSys_Global); -} - -NzVector3f NzCamera::GetForward() const -{ - return NzNode::GetForward(); -} - -float NzCamera::GetFOV() const -{ - return m_fov; -} - -const NzFrustumf& NzCamera::GetFrustum() const -{ - if (!m_frustumUpdated) - UpdateFrustum(); - - 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) - UpdateProjectionMatrix(); - - return m_projectionMatrix; -} - -const NzRenderTarget* NzCamera::GetTarget() const -{ - return m_target; -} - -const NzRectf& NzCamera::GetTargetRegion() const -{ - return m_targetRegion; -} - -const NzMatrix4f& NzCamera::GetViewMatrix() const -{ - if (!m_viewMatrixUpdated) - UpdateViewMatrix(); - - return m_viewMatrix; -} - -const NzRecti& NzCamera::GetViewport() const -{ - #if NAZARA_GRAPHICS_SAFE - if (!m_target) - { - NazaraError("Camera has no render target"); - return m_viewport; - } - #endif - - if (!m_viewportUpdated) - UpdateViewport(); - - return m_viewport; -} - -float NzCamera::GetZFar() const -{ - return m_zFar; -} - -float NzCamera::GetZNear() const -{ - return m_zNear; -} - -void NzCamera::SetFOV(float fov) -{ - #if NAZARA_GRAPHICS_SAFE - if (NzNumberEquals(fov, 0.f)) - { - NazaraError("FOV must be different from zero"); - return; - } - #endif - - m_fov = fov; - - m_frustumUpdated = false; - m_projectionMatrixUpdated = false; -} - -void NzCamera::SetTarget(const NzRenderTarget* renderTarget) -{ - m_target = renderTarget; - - if (m_target) - { - m_targetReleaseSlot.Connect(m_target->OnRenderTargetRelease, this, &NzCamera::OnRenderTargetRelease); - m_targetResizeSlot.Connect(m_target->OnRenderTargetSizeChange, this, &NzCamera::OnRenderTargetSizeChange); - } - else - { - m_targetReleaseSlot.Disconnect(); - m_targetResizeSlot.Disconnect(); - } - - m_frustumUpdated = false; - m_projectionMatrixUpdated = false; - m_viewportUpdated = false; -} - -void NzCamera::SetTarget(const NzRenderTarget& renderTarget) -{ - SetTarget(&renderTarget); -} - -void NzCamera::SetTargetRegion(const NzRectf& region) -{ - m_targetRegion = region; - - m_frustumUpdated = false; - m_projectionMatrixUpdated = false; - m_viewportUpdated = false; -} - -void NzCamera::SetViewport(const NzRecti& viewport) -{ - #if NAZARA_GRAPHICS_SAFE - if (!m_target) - { - NazaraError("Camera has no render target"); - return; - } - #endif - - // On calcule la région nécessaire pour produire ce viewport avec la taille actuelle de la cible - float invWidth = 1.f/m_target->GetWidth(); - float invHeight = 1.f/m_target->GetHeight(); - - SetTargetRegion(NzRectf(invWidth * viewport.x, invHeight * viewport.y, invWidth * viewport.width, invHeight * viewport.height)); -} - -void NzCamera::SetZFar(float zFar) -{ - m_zFar = zFar; - - m_frustumUpdated = false; - m_projectionMatrixUpdated = false; -} - -void NzCamera::SetZNear(float zNear) -{ - #if NAZARA_GRAPHICS_SAFE - if (zNear < 0.f || NzNumberEquals(zNear, 0.f)) - { - NazaraError("ZNear shall be a strictly positive number"); - return; - } - #endif - - m_zNear = zNear; - - m_frustumUpdated = false; - m_projectionMatrixUpdated = false; -} - -void NzCamera::ApplyView() const -{ - #if NAZARA_GRAPHICS_SAFE - if (!m_target) - { - NazaraError("Camera has no render target"); - return; - } - #endif - - if (!m_projectionMatrixUpdated) - UpdateProjectionMatrix(); - - if (!m_viewMatrixUpdated) - UpdateViewMatrix(); - - if (!m_viewportUpdated) - UpdateViewport(); - - NzRenderer::SetMatrix(nzMatrixType_Projection, m_projectionMatrix); - NzRenderer::SetMatrix(nzMatrixType_View, m_viewMatrix); - NzRenderer::SetTarget(m_target); - NzRenderer::SetViewport(m_viewport); -} - -void NzCamera::InvalidateNode() -{ - NzNode::InvalidateNode(); - - // Le frustum et la view matrix dépendent des paramètres du node, invalidons-les - m_frustumUpdated = false; - m_viewMatrixUpdated = false; -} - -void NzCamera::OnRenderTargetRelease(const NzRenderTarget* renderTarget) -{ - if (renderTarget == m_target) - m_target = nullptr; - else - NazaraInternalError("Not listening to " + NzString::Pointer(renderTarget)); -} - -void NzCamera::OnRenderTargetSizeChange(const NzRenderTarget* renderTarget) -{ - if (renderTarget == m_target) - { - m_frustumUpdated = false; - m_projectionMatrixUpdated = false; - m_viewportUpdated = false; - } - else - NazaraInternalError("Not listening to " + NzString::Pointer(renderTarget)); -} - -void NzCamera::UpdateFrustum() const -{ - if (!m_projectionMatrixUpdated) - UpdateProjectionMatrix(); - - if (!m_viewMatrixUpdated) - UpdateViewMatrix(); - - m_frustum.Extract(m_viewMatrix, m_projectionMatrix); - m_frustumUpdated = true; -} - -void NzCamera::UpdateProjectionMatrix() const -{ - #if NAZARA_GRAPHICS_SAFE - // Il n'y a pas grand chose à faire d'autre qu'un avertissement à ce stade - if (m_zNear >= m_zFar) - NazaraWarning("ZNear is greater or equal to ZFar (" + NzString::Number(m_zNear) + " >= " + NzString::Number(m_zFar) + ")."); - #endif - - if (!m_viewportUpdated) - UpdateViewport(); // Peut affecter l'aspect ratio - - m_projectionMatrix.MakePerspective(m_fov, m_aspectRatio, m_zNear, m_zFar); - m_projectionMatrixUpdated = true; -} - -void NzCamera::UpdateViewMatrix() const -{ - if (!m_derivedUpdated) - UpdateDerived(); - - m_viewMatrix.MakeViewMatrix(m_derivedPosition, m_derivedRotation); - m_viewMatrixUpdated = true; -} - -void NzCamera::UpdateViewport() const -{ - #if NAZARA_GRAPHICS_SAFE - if (!m_target) - { - NazaraError("Camera has no render target"); - return; - } - #endif - - unsigned int width = m_target->GetWidth(); - unsigned int height = std::max(m_target->GetHeight(), 1U); - - float vWidth = width * m_targetRegion.width; - float vHeight = height * m_targetRegion.height; - float aspectRatio = vWidth/vHeight; - - if (!NzNumberEquals(m_aspectRatio, aspectRatio, 0.001f)) - { - m_aspectRatio = aspectRatio; - m_frustumUpdated = false; - m_projectionMatrixUpdated = false; - } - - m_viewport.x = static_cast(width * m_targetRegion.x); - m_viewport.y = static_cast(height * m_targetRegion.y); - m_viewport.width = static_cast(vWidth); - m_viewport.height = static_cast(vHeight); - m_viewportUpdated = true; -} diff --git a/src/Nazara/Graphics/DeferredDOFPass.cpp b/src/Nazara/Graphics/DeferredDOFPass.cpp index 6b0e65c31..330b24737 100644 --- a/src/Nazara/Graphics/DeferredDOFPass.cpp +++ b/src/Nazara/Graphics/DeferredDOFPass.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #include #include diff --git a/src/Nazara/Graphics/DeferredFinalPass.cpp b/src/Nazara/Graphics/DeferredFinalPass.cpp index 67c84958b..3221ade69 100644 --- a/src/Nazara/Graphics/DeferredFinalPass.cpp +++ b/src/Nazara/Graphics/DeferredFinalPass.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #include #include diff --git a/src/Nazara/Graphics/DeferredFogPass.cpp b/src/Nazara/Graphics/DeferredFogPass.cpp index e99f4807c..e09967569 100644 --- a/src/Nazara/Graphics/DeferredFogPass.cpp +++ b/src/Nazara/Graphics/DeferredFogPass.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #include #include diff --git a/src/Nazara/Graphics/DeferredForwardPass.cpp b/src/Nazara/Graphics/DeferredForwardPass.cpp index 7a568645e..d708ca1df 100644 --- a/src/Nazara/Graphics/DeferredForwardPass.cpp +++ b/src/Nazara/Graphics/DeferredForwardPass.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include diff --git a/src/Nazara/Graphics/DeferredGeometryPass.cpp b/src/Nazara/Graphics/DeferredGeometryPass.cpp index 10fd2a43e..29733c75b 100644 --- a/src/Nazara/Graphics/DeferredGeometryPass.cpp +++ b/src/Nazara/Graphics/DeferredGeometryPass.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include diff --git a/src/Nazara/Graphics/DeferredPhongLightingPass.cpp b/src/Nazara/Graphics/DeferredPhongLightingPass.cpp index e63c19d61..4b7e179cb 100644 --- a/src/Nazara/Graphics/DeferredPhongLightingPass.cpp +++ b/src/Nazara/Graphics/DeferredPhongLightingPass.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include diff --git a/src/Nazara/Graphics/DeferredRenderTechnique.cpp b/src/Nazara/Graphics/DeferredRenderTechnique.cpp index 541e78e55..d5af6e2c8 100644 --- a/src/Nazara/Graphics/DeferredRenderTechnique.cpp +++ b/src/Nazara/Graphics/DeferredRenderTechnique.cpp @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #include diff --git a/src/Nazara/Graphics/ForwardRenderTechnique.cpp b/src/Nazara/Graphics/ForwardRenderTechnique.cpp index 656f34d5d..440ad7a5b 100644 --- a/src/Nazara/Graphics/ForwardRenderTechnique.cpp +++ b/src/Nazara/Graphics/ForwardRenderTechnique.cpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/Nazara/Graphics/Graphics.cpp b/src/Nazara/Graphics/Graphics.cpp index f81de8fce..355812aea 100644 --- a/src/Nazara/Graphics/Graphics.cpp +++ b/src/Nazara/Graphics/Graphics.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -81,6 +82,12 @@ bool NzGraphics::Initialize() return false; } + if (!NzSkyboxBackground::Initialize()) + { + NazaraError("Failed to initialize skybox backgrounds"); + return false; + } + // Loaders NzLoaders_OBJ_Register(); @@ -179,6 +186,7 @@ void NzGraphics::Uninitialize() NzParticleDeclaration::Uninitialize(); NzParticleController::Uninitialize(); NzMaterial::Uninitialize(); + NzSkyboxBackground::Uninitialize(); NazaraNotice("Uninitialized: Graphics module"); diff --git a/src/Nazara/Graphics/Light.cpp b/src/Nazara/Graphics/Light.cpp index 0bc491511..ee0e233ca 100644 --- a/src/Nazara/Graphics/Light.cpp +++ b/src/Nazara/Graphics/Light.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include diff --git a/src/Nazara/Graphics/Model.cpp b/src/Nazara/Graphics/Model.cpp index 082e05589..44829a4a6 100644 --- a/src/Nazara/Graphics/Model.cpp +++ b/src/Nazara/Graphics/Model.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #include #include diff --git a/src/Nazara/Graphics/ParticleEmitter.cpp b/src/Nazara/Graphics/ParticleEmitter.cpp index c38233630..6dae5c6fd 100644 --- a/src/Nazara/Graphics/ParticleEmitter.cpp +++ b/src/Nazara/Graphics/ParticleEmitter.cpp @@ -52,10 +52,9 @@ void NzParticleEmitter::Emit(NzParticleSystem& system, float elapsedTime) const if (m_lagCompensationEnabled) { // On va maintenant appliquer les contrôleurs - float accumulator = 0.f; float invEmissionRate = 1.f/m_emissionRate; for (unsigned int i = 1; i <= emissionCountInt; ++i) - system.ApplyControllers(mapper, std::min(m_emissionCount*i, particleCount), 20*invEmissionRate, accumulator); + system.ApplyControllers(mapper, std::min(m_emissionCount*i, particleCount), invEmissionRate); } } } diff --git a/src/Nazara/Graphics/ParticleSystem.cpp b/src/Nazara/Graphics/ParticleSystem.cpp index 3a7351b2c..ebfa3995e 100644 --- a/src/Nazara/Graphics/ParticleSystem.cpp +++ b/src/Nazara/Graphics/ParticleSystem.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -17,12 +16,9 @@ NzParticleSystem(maxParticleCount, NzParticleDeclaration::Get(layout)) { } -NzParticleSystem::NzParticleSystem(unsigned int maxParticleCount, const NzParticleDeclaration* declaration) : -m_declaration(declaration), -m_fixedStepEnabled(false), +NzParticleSystem::NzParticleSystem(unsigned int maxParticleCount, NzParticleDeclarationConstRef declaration) : +m_declaration(std::move(declaration)), m_processing(false), -m_stepAccumulator(0.f), -m_stepSize(1.f/60.f), m_maxParticleCount(maxParticleCount), m_particleCount(0) { @@ -35,15 +31,12 @@ m_particleCount(0) } NzParticleSystem::NzParticleSystem(const NzParticleSystem& system) : -NzSceneNode(system), +NzRenderable(system), m_controllers(system.m_controllers), m_generators(system.m_generators), m_declaration(system.m_declaration), m_renderer(system.m_renderer), -m_fixedStepEnabled(system.m_fixedStepEnabled), m_processing(false), -m_stepAccumulator(0.f), -m_stepSize(system.m_stepSize), m_maxParticleCount(system.m_maxParticleCount), m_particleCount(system.m_particleCount), m_particleSize(system.m_particleSize) @@ -58,24 +51,33 @@ m_particleSize(system.m_particleSize) NzParticleSystem::~NzParticleSystem() = default; -void NzParticleSystem::AddController(NzParticleController* controller) +void NzParticleSystem::AddController(NzParticleControllerRef controller) { - m_controllers.emplace_back(controller); + NazaraAssert(controller, "Invalid particle controller"); + + m_controllers.emplace_back(std::move(controller)); } void NzParticleSystem::AddEmitter(NzParticleEmitter* emitter) { + NazaraAssert(emitter, "Invalid particle emitter"); + m_emitters.emplace_back(emitter); } -void NzParticleSystem::AddGenerator(NzParticleGenerator* generator) +void NzParticleSystem::AddGenerator(NzParticleGeneratorRef generator) { - m_generators.emplace_back(generator); + NazaraAssert(generator, "Invalid particle generator"); + + m_generators.emplace_back(std::move(generator)); } -void NzParticleSystem::AddToRenderQueue(NzAbstractRenderQueue* renderQueue) const +void NzParticleSystem::AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const NzMatrix4f& transformMatrix) const { - ///FIXME: Vérifier le renderer + NazaraAssert(m_renderer, "Invalid particle renderer"); + NazaraAssert(renderQueue, "Invalid renderqueue"); + NazaraUnused(transformMatrix); + if (m_particleCount > 0) { NzParticleMapper mapper(m_buffer.data(), m_declaration); @@ -83,6 +85,36 @@ void NzParticleSystem::AddToRenderQueue(NzAbstractRenderQueue* renderQueue) cons } } +void NzParticleSystem::ApplyControllers(NzParticleMapper& mapper, unsigned int particleCount, float elapsedTime) +{ + m_processing = true; + + // Pour éviter un verrouillage en cas d'exception + NzCallOnExit onExit([this]() + { + m_processing = false; + }); + + for (NzParticleController* controller : m_controllers) + controller->Apply(*this, mapper, 0, particleCount-1, elapsedTime); + + onExit.CallAndReset(); + + // On tue maintenant les particules mortes durant la mise à jour + if (m_dyingParticles.size() < m_particleCount) + { + // On tue les particules depuis la dernière vers la première (en terme de place), le std::set étant trié via std::greater + // La raison est simple, étant donné que la mort d'une particule signifie le déplacement de la dernière particule du buffer, + // sans cette solution certaines particules pourraient échapper à la mort + for (unsigned int index : m_dyingParticles) + KillParticle(index); + } + else + KillParticles(); // Toutes les particules sont mortes, ceci est beaucoup plus rapide + + m_dyingParticles.clear(); +} + void* NzParticleSystem::CreateParticle() { return CreateParticles(1); @@ -93,7 +125,7 @@ void* NzParticleSystem::CreateParticles(unsigned int count) if (count == 0) return nullptr; - if (m_particleCount+count > m_maxParticleCount) + if (m_particleCount + count > m_maxParticleCount) return nullptr; unsigned int particlesIndex = m_particleCount; @@ -102,16 +134,6 @@ void* NzParticleSystem::CreateParticles(unsigned int count) return &m_buffer[particlesIndex*m_particleSize]; } -void NzParticleSystem::EnableFixedStep(bool fixedStep) -{ - // On teste pour empêcher que cette méthode ne remette systématiquement le step accumulator à zéro - if (m_fixedStepEnabled != fixedStep) - { - m_fixedStepEnabled = fixedStep; - m_stepAccumulator = 0.f; - } -} - void* NzParticleSystem::GenerateParticle() { return GenerateParticles(1); @@ -130,7 +152,7 @@ void* NzParticleSystem::GenerateParticles(unsigned int count) return ptr; } -const NzParticleDeclaration* NzParticleSystem::GetDeclaration() const +const NzParticleDeclarationConstRef& NzParticleSystem::GetDeclaration() const { return m_declaration; } @@ -155,21 +177,6 @@ unsigned int NzParticleSystem::GetParticleSize() const return m_particleSize; } -nzSceneNodeType NzParticleSystem::GetSceneNodeType() const -{ - return nzSceneNodeType_ParticleEmitter; -} - -bool NzParticleSystem::IsDrawable() const -{ - return true; -} - -bool NzParticleSystem::IsFixedStepEnabled() const -{ - return m_fixedStepEnabled; -} - void NzParticleSystem::KillParticle(unsigned int index) { ///FIXME: Vérifier index @@ -222,15 +229,36 @@ void NzParticleSystem::SetRenderer(NzParticleRenderer* renderer) m_renderer = renderer; } +void NzParticleSystem::Update(float elapsedTime) +{ + // Émission + for (NzParticleEmitter* emitter : m_emitters) + emitter->Emit(*this, elapsedTime); + + // Mise à jour + if (m_particleCount > 0) + { + ///TODO: Mettre à jour en utilisant des threads + NzParticleMapper mapper(m_buffer.data(), m_declaration); + ApplyControllers(mapper, m_particleCount, elapsedTime); + } +} + +void NzParticleSystem::UpdateBoundingVolume(const NzMatrix4f& transformMatrix) +{ + NazaraUnused(transformMatrix); + + // Nothing to do here (our bounding volume is global) +} + NzParticleSystem& NzParticleSystem::operator=(const NzParticleSystem& system) { NzErrorFlags flags(nzErrorFlag_ThrowException, true); - NzSceneNode::operator=(system); + NzRenderable::operator=(system); m_controllers = system.m_controllers; m_declaration = system.m_declaration; - m_fixedStepEnabled = system.m_fixedStepEnabled; m_generators = system.m_generators; m_maxParticleCount = system.m_maxParticleCount; m_particleCount = system.m_particleCount; @@ -252,61 +280,12 @@ NzParticleSystem& NzParticleSystem::operator=(const NzParticleSystem& system) return *this; } -void NzParticleSystem::ApplyControllers(NzParticleMapper& mapper, unsigned int particleCount, float elapsedTime, float& stepAccumulator) -{ - m_processing = true; - - // Pour éviter un verrouillage en cas d'exception - NzCallOnExit onExit([this]() - { - m_processing = false; - }); - - if (m_fixedStepEnabled) - { - stepAccumulator += elapsedTime; - while (stepAccumulator >= m_stepSize) - { - for (NzParticleController* controller : m_controllers) - controller->Apply(*this, mapper, 0, particleCount-1, m_stepSize); - - stepAccumulator -= m_stepSize; - } - } - else - { - for (NzParticleController* controller : m_controllers) - controller->Apply(*this, mapper, 0, particleCount-1, elapsedTime); - } - - onExit.CallAndReset(); - - // On tue maintenant les particules mortes durant la mise à jour - if (m_dyingParticles.size() < m_particleCount) - { - // On tue les particules depuis la dernière vers la première (en terme de place), le std::set étant trié via std::greater - // La raison est simple, étant donné que la mort d'une particule signifie le déplacement de la dernière particule du buffer, - // sans cette solution certaines particules pourraient échapper à la mort - for (unsigned int index : m_dyingParticles) - KillParticle(index); - } - else - KillParticles(); // Toutes les particules sont mortes, ceci est beaucoup plus rapide - - m_dyingParticles.clear(); -} - void NzParticleSystem::MakeBoundingVolume() const { ///TODO: Calculer l'AABB (prendre la taille des particules en compte s'il y a) m_boundingVolume.MakeInfinite(); } -void NzParticleSystem::Register() -{ - m_scene->RegisterForUpdate(this); -} - void NzParticleSystem::ResizeBuffer() { // Histoire de décrire un peu mieux l'erreur en cas d'échec @@ -323,24 +302,3 @@ void NzParticleSystem::ResizeBuffer() } } -void NzParticleSystem::Unregister() -{ - m_scene->UnregisterForUpdate(this); -} - -void NzParticleSystem::Update() -{ - float elapsedTime = m_scene->GetUpdateTime(); - - // Émission - for (NzParticleEmitter* emitter : m_emitters) - emitter->Emit(*this, elapsedTime); - - // Mise à jour - if (m_particleCount > 0) - { - ///TODO: Mettre à jour en utilisant des threads - NzParticleMapper mapper(m_buffer.data(), m_declaration); - ApplyControllers(mapper, m_particleCount, elapsedTime, m_stepAccumulator); - } -} diff --git a/src/Nazara/Graphics/Scene.cpp b/src/Nazara/Graphics/Scene.cpp deleted file mode 100644 index 207e89ac8..000000000 --- a/src/Nazara/Graphics/Scene.cpp +++ /dev/null @@ -1,465 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Engine - Graphics module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -NzScene::NzScene() : -m_ambientColor(25, 25, 25), -m_root(this), -m_viewer(nullptr), -m_backgroundEnabled(true), -m_update(false), -m_updatePerSecond(60) -{ -} - -void NzScene::AddToVisibilityList(NzUpdatable* object) -{ - m_visibleUpdateList.push_back(object); -} - -void NzScene::Clear() -{ - m_nodeMap.clear(); - m_nodes.clear(); -} - -void NzScene::Cull() -{ - #if NAZARA_GRAPHICS_SAFE - if (!m_viewer) - { - NazaraError("No viewer"); - return; - } - #endif - - NzAbstractRenderQueue* renderQueue = GetRenderTechnique()->GetRenderQueue(); - renderQueue->Clear(false); - - m_visibleUpdateList.clear(); - - // Frustum culling - RecursiveFrustumCull(renderQueue, m_viewer->GetFrustum(), &m_root); - - ///TODO: Occlusion culling -} - -void NzScene::Draw() -{ - #if NAZARA_GRAPHICS_SAFE - if (!m_viewer) - { - NazaraError("No viewer"); - return; - } - #endif - - m_viewer->ApplyView(); - - try - { - NzSceneData sceneData; - sceneData.ambientColor = m_ambientColor; - sceneData.background = (m_backgroundEnabled) ? m_background.get() : nullptr; - sceneData.viewer = m_viewer; - - NzErrorFlags errFlags(nzErrorFlag_ThrowException, true); - m_renderTechnique->Draw(sceneData); - } - catch (const std::exception& e) - { - NzString oldName = m_renderTechnique->GetName(); - - if (m_renderTechniqueRanking > 0) - { - m_renderTechnique.reset(NzRenderTechniques::GetByRanking(m_renderTechniqueRanking-1, &m_renderTechniqueRanking)); - NazaraError("Render technique \"" + oldName + "\" failed (" + NzString(e.what()) + "), falling back to \"" + m_renderTechnique->GetName() + '"'); - } - else - { - NzErrorFlags errFlags(nzErrorFlag_ThrowException); - NazaraError("Render technique \"" + oldName + "\" failed (" + NzString(e.what()) + ") and no fallback is available"); - } - - return; - } -} - -void NzScene::EnableBackground(bool enable) -{ - m_backgroundEnabled = enable; -} - -NzSceneNode* NzScene::FindNode(const NzString& name) -{ - auto it = m_nodeMap.find(name); - if (it == m_nodeMap.end()) - return nullptr; - - return it->second; -} - -const NzSceneNode* NzScene::FindNode(const NzString& name) const -{ - auto it = m_nodeMap.find(name); - if (it == m_nodeMap.end()) - return nullptr; - - return it->second; -} - -NzColor NzScene::GetAmbientColor() const -{ - return m_ambientColor; -} - -NzAbstractBackground* NzScene::GetBackground() const -{ - if (!m_background) - m_background.reset(new NzColorBackground); - - return m_background.get(); -} - -NzVector3f NzScene::GetBackward() const -{ - #if NAZARA_GRAPHICS_SAFE - if (!m_viewer) - { - NazaraError("No viewer"); - return NzVector3f::Backward(); - } - #endif - - //return -m_viewer->GetGlobalForward(); - return NzVector3f::Backward(); -} - -NzVector3f NzScene::GetDown() const -{ - #if NAZARA_GRAPHICS_SAFE - if (!m_viewer) - { - NazaraError("No viewer"); - return NzVector3f::Down(); - } - #endif - - //return -m_viewer->GetGlobalUp(); - return NzVector3f::Down(); -} - -NzVector3f NzScene::GetForward() const -{ - #if NAZARA_GRAPHICS_SAFE - if (!m_viewer) - { - NazaraError("No viewer"); - return NzVector3f::Forward(); - } - #endif - - //return m_viewer->GetGlobalForward(); - return NzVector3f::Forward(); -} - -NzVector3f NzScene::GetLeft() const -{ - #if NAZARA_GRAPHICS_SAFE - if (!m_viewer) - { - NazaraError("No viewer"); - return NzVector3f::Left(); - } - #endif - - //return -m_viewer->GetGlobalRight(); - return NzVector3f::Left(); -} - -NzAbstractRenderTechnique* NzScene::GetRenderTechnique() const -{ - if (!m_renderTechnique) - m_renderTechnique.reset(NzRenderTechniques::GetByRanking(-1, &m_renderTechniqueRanking)); - - return m_renderTechnique.get(); -} - -NzVector3f NzScene::GetRight() const -{ - #if NAZARA_GRAPHICS_SAFE - if (!m_viewer) - { - NazaraError("No viewer"); - return NzVector3f::Right(); - } - #endif - - //return m_viewer->GetGlobalRight(); - return NzVector3f::Right(); -} - -NzSceneNode& NzScene::GetRoot() -{ - return m_root; -} - -const NzSceneNode& NzScene::GetRoot() const -{ - return m_root; -} - -NzAbstractViewer* NzScene::GetViewer() const -{ - return m_viewer; -} - -NzVector3f NzScene::GetUp() const -{ - #if NAZARA_GRAPHICS_SAFE - if (!m_viewer) - { - NazaraError("No viewer"); - return NzVector3f::Up(); - } - #endif - - //return m_viewer->GetGlobalUp(); - return NzVector3f::Up(); -} - -float NzScene::GetUpdateTime() const -{ - return m_updateTime; -} - -unsigned int NzScene::GetUpdatePerSecond() const -{ - return m_updatePerSecond; -} - -bool NzScene::IsBackgroundEnabled() const -{ - return m_backgroundEnabled; -} - -void NzScene::RegisterForUpdate(NzUpdatable* object) -{ - #if NAZARA_GRAPHICS_SAFE - if (!object) - { - NazaraError("Invalid object"); - return; - } - #endif - - m_updateList.push_back(object); -} - -void NzScene::RemoveNode(NzSceneNode* node) -{ - if (!node) - return; - - // C'est moche mais je n'ai pas d'autre choix que d'utiliser un std::unique_ptr pour utiliser std::find - std::unique_ptr ptr(node); - auto it = std::find(m_nodes.begin(), m_nodes.end(), ptr); - ptr.release(); - - if (it == m_nodes.end()) - { - NazaraError("This scene node doesn't belong to this scene"); - return; - } - - NzString nodeName = node->GetName(); - if (!nodeName.IsEmpty()) - m_nodeMap.erase(nodeName); - - m_nodes.erase(it); -} - -void NzScene::RemoveNode(const NzString& name) -{ - RemoveNode(FindNode(name)); -} - -void NzScene::RenderFrame() -{ - try - { - NzErrorFlags errFlags(nzErrorFlag_ThrowException, true); - Update(); - Cull(); - UpdateVisible(); - Draw(); - } - catch (const std::exception& e) - { - NazaraError("Failed to render frame: " + NzString(e.what())); - } -} - -void NzScene::SetAmbientColor(const NzColor& color) -{ - m_ambientColor = color; -} - -void NzScene::SetBackground(NzAbstractBackground* background) -{ - m_background.reset(background); -} - -void NzScene::SetRenderTechnique(NzAbstractRenderTechnique* renderTechnique) -{ - m_renderTechnique.reset(renderTechnique); -} - -void NzScene::SetViewer(NzAbstractViewer* viewer) -{ - if (m_viewer != viewer) - { - m_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_root.InvalidateNode(); - } -} - -void NzScene::SetViewer(NzAbstractViewer& viewer) -{ - SetViewer(&viewer); -} - -void NzScene::SetUpdatePerSecond(unsigned int updatePerSecond) -{ - m_updatePerSecond = updatePerSecond; -} - -void NzScene::UnregisterForUpdate(NzUpdatable* object) -{ - #if NAZARA_GRAPHICS_SAFE - if (!object) - { - NazaraError("Invalid object"); - return; - } - #endif - - auto it = std::find(m_updateList.begin(), m_updateList.end(), object); - if (it != m_updateList.end()) - m_updateList.erase(it); -} - -void NzScene::Update() -{ - m_update = (m_updatePerSecond == 0 || m_updateClock.GetMilliseconds() > 1000/m_updatePerSecond); - if (m_update) - { - m_updateTime = m_updateClock.GetSeconds(); - m_updateClock.Restart(); - - for (NzUpdatable* updatable : m_updateList) - ///TODO: Multihreading - updatable->Update(); - } -} - -void NzScene::UpdateVisible() -{ - NzSkinningManager::Skin(); - - if (m_update) - { - for (NzUpdatable* node : m_visibleUpdateList) - node->Update(); - } -} - -NzScene::operator const NzSceneNode&() const -{ - return m_root; -} - -bool NzScene::ChangeNodeName(NzSceneNode* node, const NzString& newName) -{ - #ifdef NAZARA_DEBUG - std::unique_ptr ptr(node); - auto it = std::find(m_nodes.begin(), m_nodes.end(), ptr); - ptr.release(); - - if (it == m_nodes.end()) - { - NazaraInternalError("Node isn't part of the scene"); - return false; - } - #endif - - if (!newName.IsEmpty()) - { - auto pair = m_nodeMap.insert(std::make_pair(newName, node)); - if (!pair.second) - { - NazaraError("Name \"" + newName + "\" is already in use"); - return false; - } - } - - NzString oldName = node->GetName(); - if (!oldName.IsEmpty()) - m_nodeMap.erase(oldName); - - node->SetNameInternal(newName); - return true; -} - -bool NzScene::RegisterSceneNode(const NzString& name, NzSceneNode* node) -{ - if (!name.IsEmpty()) - { - if (m_nodeMap.find(name) != m_nodeMap.end()) - { - NazaraError("Node " + name + " is already registred"); - return false; - } - - m_nodeMap[name] = node; - } - - node->SetNameInternal(name); - node->SetParent(m_root, true); - - m_nodes.emplace_back(node); - return true; -} - -void NzScene::RecursiveFrustumCull(NzAbstractRenderQueue* renderQueue, const NzFrustumf& frustum, NzNode* node) -{ - for (NzNode* child : node->GetChilds()) - { - if (child->GetNodeType() == nzNodeType_Scene) - { - NzSceneNode* sceneNode = static_cast(child); - - ///TODO: Empêcher le rendu des enfants si le parent est cullé selon un flag - sceneNode->UpdateVisibility(frustum); - if (sceneNode->IsVisible()) - sceneNode->AddToRenderQueue(renderQueue); - } - - if (child->HasChilds()) - RecursiveFrustumCull(renderQueue, frustum, child); - } -} diff --git a/src/Nazara/Graphics/SceneNode.cpp b/src/Nazara/Graphics/SceneNode.cpp deleted file mode 100644 index e0e97e206..000000000 --- a/src/Nazara/Graphics/SceneNode.cpp +++ /dev/null @@ -1,293 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Engine - Graphics module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#include -#include -#include -#include - -///FIXME: Constructeur de copie - -NzSceneNode::NzSceneNode() : -m_scene(nullptr), -m_boundingVolumeUpdated(false), -m_drawingEnabled(true), -m_visible(false) -{ -} - -NzSceneNode::NzSceneNode(const NzSceneNode& sceneNode) : -NzNode(sceneNode), -m_scene(nullptr), -m_boundingVolumeUpdated(false), -m_drawingEnabled(sceneNode.m_drawingEnabled), -m_visible(false) -{ -} - -NzSceneNode::~NzSceneNode() = default; - -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(); -} - -const NzBoundingVolumef& NzSceneNode::GetBoundingVolume() const -{ - if (!m_boundingVolumeUpdated) - UpdateBoundingVolume(); - - return m_boundingVolume; -} - -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(); -} - -const NzString& NzSceneNode::GetName() const -{ - return m_name; -} - -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; -} - -bool NzSceneNode::IsVisible() const -{ - return m_visible; -} - -void NzSceneNode::Remove() -{ - if (m_scene) - m_scene->RemoveNode(this); - else - NazaraError("SceneNode::Remove() called on a template node"); -} - -bool NzSceneNode::SetName(const NzString& name) -{ - if (m_scene) - // On demande à la scène de changer notre nom - return m_scene->ChangeNodeName(this, name); - else - { - // Pas de scène ? Changeons notre nom nous-même - SetNameInternal(name); - return true; - } -} - -NzSceneNode& NzSceneNode::operator=(const NzSceneNode& sceneNode) -{ - NzNode::operator=(sceneNode); - - // La scène est affectée via le parenting du node - m_drawingEnabled = sceneNode.m_drawingEnabled; - m_visible = false; - - return *this; -} - -bool NzSceneNode::FrustumCull(const NzFrustumf& frustum) const -{ - return frustum.Contains(GetBoundingVolume()); -} - -void NzSceneNode::InvalidateNode() -{ - NzNode::InvalidateNode(); - - m_boundingVolumeUpdated = false; -} - -void NzSceneNode::OnParenting(const NzNode* parent) -{ - NzNode::OnParenting(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); - } - else - SetScene(nullptr); -} - -void NzSceneNode::OnVisibilityChange(bool visibility) -{ - NazaraUnused(visibility); - - ///TODO: Envoyer l'évènements aux listeners -} - -void NzSceneNode::RecursiveSetScene(NzScene* scene, NzNode* node) -{ - const std::vector& childs = node->GetChilds(); - for (NzNode* child : childs) - { - if (child->GetNodeType() == nzNodeType_Scene) - { - NzSceneNode* sceneNode = static_cast(child); - sceneNode->SetScene(scene); - } - - if (node->HasChilds()) - RecursiveSetScene(scene, node); - } -} - -void NzSceneNode::Register() -{ -} - -void NzSceneNode::SetNameInternal(const NzString& name) -{ - m_name = name; -} - -void NzSceneNode::SetScene(NzScene* scene) -{ - if (m_scene != scene) - { - if (m_scene) - Unregister(); - - m_scene = scene; - if (m_scene) - Register(); - - RecursiveSetScene(scene, this); - } -} - -void NzSceneNode::Unregister() -{ -} - -void NzSceneNode::Update() -{ -} - -void NzSceneNode::UpdateBoundingVolume() const -{ - if (m_boundingVolume.IsNull()) - MakeBoundingVolume(); - - if (!m_transformMatrixUpdated) - UpdateTransformMatrix(); - - m_boundingVolume.Update(m_transformMatrix); - m_boundingVolumeUpdated = true; -} - -void NzSceneNode::UpdateVisibility(const NzFrustumf& frustum) -{ - bool wasVisible = m_visible; - - if (m_drawingEnabled) - { - #if NAZARA_GRAPHICS_SAFE - if (!IsDrawable()) - { - NazaraError("SceneNode is not drawable"); - return; - } - #endif - - m_visible = FrustumCull(frustum); - } - else - m_visible = false; - - if (m_visible != wasVisible) - OnVisibilityChange(m_visible); -} diff --git a/src/Nazara/Graphics/SceneRoot.cpp b/src/Nazara/Graphics/SceneRoot.cpp deleted file mode 100644 index 7cd3b7741..000000000 --- a/src/Nazara/Graphics/SceneRoot.cpp +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Engine - Graphics module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#include -#include -#include - -NzSceneRoot::NzSceneRoot(NzScene* scene) -{ - m_scene = scene; -} - -NzSceneRoot::~NzSceneRoot() = default; - -void NzSceneRoot::AddToRenderQueue(NzAbstractRenderQueue* renderQueue) const -{ - NazaraUnused(renderQueue); - - NazaraInternalError("SceneNode::AddToRenderQueue() called on SceneRoot"); -} - -nzSceneNodeType NzSceneRoot::GetSceneNodeType() const -{ - return nzSceneNodeType_Root; -} - -bool NzSceneRoot::IsDrawable() const -{ - return true; -} - -NzSceneRoot* NzSceneRoot::Clone() const -{ - NazaraInternalError("SceneNode::Clone() called on SceneRoot"); - return nullptr; -} - -NzSceneRoot* NzSceneRoot::Create() const -{ - NazaraInternalError("SceneNode::Create() called on SceneRoot"); - return nullptr; -} - -void NzSceneRoot::MakeBoundingVolume() const -{ - m_boundingVolume.MakeInfinite(); -} - -void NzSceneRoot::Register() -{ - NazaraInternalError("SceneNode::Register() called on SceneRoot"); -} - -void NzSceneRoot::Unregister() -{ - NazaraInternalError("SceneNode::Unregister() called on SceneRoot"); -} diff --git a/src/Nazara/Graphics/SkeletalModel.cpp b/src/Nazara/Graphics/SkeletalModel.cpp index 5000484fa..9283e2c09 100644 --- a/src/Nazara/Graphics/SkeletalModel.cpp +++ b/src/Nazara/Graphics/SkeletalModel.cpp @@ -4,10 +4,8 @@ #include #include -#include #include #include -#include #include #include #include diff --git a/src/Nazara/Graphics/SkyboxBackground.cpp b/src/Nazara/Graphics/SkyboxBackground.cpp index e9e14ba56..a1b9d3607 100644 --- a/src/Nazara/Graphics/SkyboxBackground.cpp +++ b/src/Nazara/Graphics/SkyboxBackground.cpp @@ -2,14 +2,9 @@ // This file is part of the "Nazara Engine - Graphics module" // For conditions of distribution and use, see copyright notice in Config.hpp -#ifndef NAZARA_RENDERER_OPENGL -#define NAZARA_RENDERER_OPENGL // Nécessaire pour inclure les headers OpenGL -#endif - #include -#include -#include -#include +#include +#include #include #include #include @@ -19,221 +14,32 @@ namespace { - NzIndexBuffer* BuildIndexBuffer() - { - std::unique_ptr indexBuffer(new NzIndexBuffer(false, 36, nzDataStorage_Hardware, nzBufferUsage_Static)); - indexBuffer->SetPersistent(false); - - nzUInt16 indices[6*6] = - { - 0, 1, 2, 0, 2, 3, - 3, 2, 6, 3, 6, 7, - 7, 6, 5, 7, 5, 4, - 4, 5, 1, 4, 1, 0, - 0, 3, 7, 0, 7, 4, - 1, 6, 2, 1, 5, 6 - }; - - if (!indexBuffer->Fill(indices, 0, 36)) - { - NazaraError("Failed to create index buffer"); - return nullptr; - } - - return indexBuffer.release(); - } - - NzShader* BuildShader() - { - const char* fragmentSource110 = - "#version 110\n" - - "varying vec3 vTexCoord;\n" - - "uniform samplerCube Skybox;\n" - "uniform float VertexDepth;\n" - - "void main()\n" - "{\n" - " gl_FragColor = textureCube(Skybox, vTexCoord);\n" - " gl_FragDepth = VertexDepth;\n" - "}\n"; - - const char* fragmentSource140 = - "#version 140\n" - - "in vec3 vTexCoord;\n" - - "out vec4 RenderTarget0;\n" - - "uniform samplerCube Skybox;\n" - "uniform float VertexDepth;\n" - - "void main()\n" - "{\n" - " RenderTarget0 = texture(Skybox, vTexCoord);\n" - " gl_FragDepth = VertexDepth;\n" - "}\n"; - - const char* vertexSource110 = - "#version 110\n" - - "attribute vec3 VertexPosition;\n" - - "varying vec3 vTexCoord;\n" - - "uniform mat4 WorldViewProjMatrix;\n" - - "void main()\n" - "{\n" - " gl_Position = WorldViewProjMatrix * vec4(VertexPosition, 1.0);\n" - " vTexCoord = vec3(VertexPosition.x, VertexPosition.y, -VertexPosition.z);\n" - "}\n"; - - const char* vertexSource140 = - "#version 140\n" - - "in vec3 VertexPosition;\n" - - "out vec3 vTexCoord;\n" - - "uniform mat4 WorldViewProjMatrix;\n" - - "void main()\n" - "{\n" - " gl_Position = WorldViewProjMatrix * vec4(VertexPosition, 1.0);\n" - " vTexCoord = vec3(VertexPosition.x, VertexPosition.y, -VertexPosition.z);\n" - "}\n"; - - ///TODO: Remplacer ça par des ShaderNode - std::unique_ptr shader(new NzShader); - shader->SetPersistent(false); - - if (!shader->Create()) - { - NazaraError("Failed to create shader"); - return nullptr; - } - - bool useGLSL140 = (NzOpenGL::GetVersion() >= 310); - - if (!shader->AttachStageFromSource(nzShaderStage_Fragment, (useGLSL140) ? fragmentSource140 : fragmentSource110)) - { - NazaraError("Failed to load fragment shader"); - return nullptr; - } - - if (!shader->AttachStageFromSource(nzShaderStage_Vertex, (useGLSL140) ? vertexSource140 : vertexSource110)) - { - NazaraError("Failed to load vertex shader"); - return nullptr; - } - - if (!shader->Link()) - { - NazaraError("Failed to link shader"); - return nullptr; - } - - shader->SendInteger(shader->GetUniformLocation("Skybox"), 0); - shader->SendFloat(shader->GetUniformLocation("VertexDepth"), 1.f); - - return shader.release(); - } - - NzRenderStates BuildRenderStates() - { - NzRenderStates states; - states.depthFunc = nzRendererComparison_Equal; - states.faceCulling = nzFaceSide_Front; - states.parameters[nzRendererParameter_DepthBuffer] = true; - states.parameters[nzRendererParameter_DepthWrite] = false; - states.parameters[nzRendererParameter_FaceCulling] = true; - - return states; - } - - NzVertexBuffer* BuildVertexBuffer() - { - std::unique_ptr vertexBuffer(new NzVertexBuffer(NzVertexDeclaration::Get(nzVertexLayout_XYZ), 8, nzDataStorage_Hardware, nzBufferUsage_Static)); - vertexBuffer->SetPersistent(false); - - float vertices[8*(sizeof(float)*3)] = - { - -1.0, 1.0, 1.0, - -1.0, -1.0, 1.0, - 1.0, -1.0, 1.0, - 1.0, 1.0, 1.0, - -1.0, 1.0, -1.0, - -1.0, -1.0, -1.0, - 1.0, -1.0, -1.0, - 1.0, 1.0, -1.0, - }; - - if (!vertexBuffer->Fill(vertices, 0, 8)) - { - NazaraError("Failed to create vertex buffer"); - return nullptr; - } - - return vertexBuffer.release(); - } - - static NzIndexBuffer* s_indexBuffer = nullptr; - static NzShader* s_shader = nullptr; - static NzVertexBuffer* s_vertexBuffer = nullptr; + static NzIndexBufferRef s_indexBuffer; + static NzRenderStates s_renderStates; + static NzShaderRef s_shader; + static NzVertexBufferRef s_vertexBuffer; } -NzSkyboxBackground::NzSkyboxBackground() +NzSkyboxBackground::NzSkyboxBackground(NzTextureRef cubemapTexture) { - if (!s_indexBuffer) - s_indexBuffer = BuildIndexBuffer(); - - if (!s_shader) - s_shader = BuildShader(); - - if (!s_vertexBuffer) - s_vertexBuffer = BuildVertexBuffer(); - - m_indexBuffer = s_indexBuffer; m_sampler.SetWrapMode(nzSamplerWrap_Clamp); // Nécessaire pour ne pas voir les côtés - m_shader = s_shader; - m_vertexBuffer = s_vertexBuffer; -} -NzSkyboxBackground::NzSkyboxBackground(NzTexture* cubemapTexture) : -NzSkyboxBackground() -{ - SetTexture(cubemapTexture); -} - -NzSkyboxBackground::~NzSkyboxBackground() -{ - if (m_indexBuffer.Reset()) - s_indexBuffer = nullptr; - - if (m_shader.Reset()) - s_shader = nullptr; - - if (m_vertexBuffer.Reset()) - s_vertexBuffer = nullptr; + SetTexture(std::move(cubemapTexture)); } void NzSkyboxBackground::Draw(const NzAbstractViewer* viewer) const { - static NzRenderStates states(BuildRenderStates()); - NzMatrix4f skyboxMatrix(viewer->GetViewMatrix()); skyboxMatrix.SetTranslation(NzVector3f::Zero()); - NzRenderer::SetIndexBuffer(m_indexBuffer); + NzRenderer::SetIndexBuffer(s_indexBuffer); NzRenderer::SetMatrix(nzMatrixType_View, skyboxMatrix); NzRenderer::SetMatrix(nzMatrixType_World, NzMatrix4f::Scale(NzVector3f(viewer->GetZNear()))); - NzRenderer::SetRenderStates(states); - NzRenderer::SetShader(m_shader); + NzRenderer::SetRenderStates(s_renderStates); + NzRenderer::SetShader(s_shader); NzRenderer::SetTexture(0, m_texture); NzRenderer::SetTextureSampler(0, m_sampler); - NzRenderer::SetVertexBuffer(m_vertexBuffer); + NzRenderer::SetVertexBuffer(s_vertexBuffer); NzRenderer::DrawIndexedPrimitives(nzPrimitiveMode_TriangleList, 0, 36); @@ -245,29 +51,108 @@ nzBackgroundType NzSkyboxBackground::GetBackgroundType() const return nzBackgroundType_Skybox; } -NzTexture* NzSkyboxBackground::GetTexture() const +bool NzSkyboxBackground::Initialize() { - return m_texture; -} - -void NzSkyboxBackground::SetTexture(NzTexture* cubemapTexture) -{ - #if NAZARA_GRAPHICS_SAFE - if (cubemapTexture) + const nzUInt16 indices[6*6] = { - if (!cubemapTexture->IsValid()) - { - NazaraError("Texture must be valid"); - return; - } + 0, 1, 2, 0, 2, 3, + 3, 2, 6, 3, 6, 7, + 7, 6, 5, 7, 5, 4, + 4, 5, 1, 4, 1, 0, + 0, 3, 7, 0, 7, 4, + 1, 6, 2, 1, 5, 6 + }; - if (!cubemapTexture->IsCubemap()) - { - NazaraError("Texture must be a cubemap"); - return; - } + const float vertices[8 * 3 * sizeof(float)] = + { + -1.0, 1.0, 1.0, + -1.0, -1.0, 1.0, + 1.0, -1.0, 1.0, + 1.0, 1.0, 1.0, + -1.0, 1.0, -1.0, + -1.0, -1.0, -1.0, + 1.0, -1.0, -1.0, + 1.0, 1.0, -1.0, + }; + + ///TODO: Replace by ShaderNode (probably after Vulkan) + const char* fragmentShaderSource = + "#version 140\n" + + "in vec3 vTexCoord;\n" + + "out vec4 RenderTarget0;\n" + + "uniform samplerCube Skybox;\n" + "uniform float VertexDepth;\n" + + "void main()\n" + "{\n" + " RenderTarget0 = texture(Skybox, vTexCoord);\n" + " gl_FragDepth = VertexDepth;\n" + "}\n"; + + const char* vertexShaderSource = + "#version 140\n" + + "in vec3 VertexPosition;\n" + + "out vec3 vTexCoord;\n" + + "uniform mat4 WorldViewProjMatrix;\n" + + "void main()\n" + "{\n" + " gl_Position = WorldViewProjMatrix * vec4(VertexPosition, 1.0);\n" + " vTexCoord = vec3(VertexPosition.x, VertexPosition.y, -VertexPosition.z);\n" + "}\n"; + + try + { + NzErrorFlags flags(nzErrorFlag_ThrowException, true); + + // Index buffer + NzIndexBufferRef indexBuffer = NzIndexBuffer::New(false, 36, nzDataStorage_Hardware, nzBufferUsage_Static); + indexBuffer->Fill(indices, 0, 36); + + // Vertex buffer + NzVertexBufferRef vertexBuffer = NzVertexBuffer::New(NzVertexDeclaration::Get(nzVertexLayout_XYZ), 8, nzDataStorage_Hardware, nzBufferUsage_Static); + vertexBuffer->Fill(vertices, 0, 8); + + // Shader + NzShaderRef shader = NzShader::New(); + shader->Create(); + shader->AttachStageFromSource(nzShaderStage_Fragment, fragmentShaderSource); + shader->AttachStageFromSource(nzShaderStage_Vertex, vertexShaderSource); + shader->Link(); + + shader->SendInteger(shader->GetUniformLocation("Skybox"), 0); + shader->SendFloat(shader->GetUniformLocation("VertexDepth"), 1.f); + + // Renderstates + s_renderStates.depthFunc = nzRendererComparison_Equal; + s_renderStates.faceCulling = nzFaceSide_Front; + s_renderStates.parameters[nzRendererParameter_DepthBuffer] = true; + s_renderStates.parameters[nzRendererParameter_DepthWrite] = false; + s_renderStates.parameters[nzRendererParameter_FaceCulling] = true; + + // Exception-free zone + s_indexBuffer = std::move(indexBuffer); + s_shader = std::move(shader); + s_vertexBuffer = std::move(vertexBuffer); + } + catch (const std::exception& e) + { + NazaraError("Failed to initialise: " + NzString(e.what())); + return false; } - #endif - m_texture = cubemapTexture; + return true; +} + +void NzSkyboxBackground::Uninitialize() +{ + s_indexBuffer.Reset(); + s_shader.Reset(); + s_vertexBuffer.Reset(); } diff --git a/src/Nazara/Graphics/Sprite.cpp b/src/Nazara/Graphics/Sprite.cpp index 7ad9b797f..d0c5eed30 100644 --- a/src/Nazara/Graphics/Sprite.cpp +++ b/src/Nazara/Graphics/Sprite.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include diff --git a/src/Nazara/Graphics/TextureBackground.cpp b/src/Nazara/Graphics/TextureBackground.cpp index 3d5664a18..d1784b078 100644 --- a/src/Nazara/Graphics/TextureBackground.cpp +++ b/src/Nazara/Graphics/TextureBackground.cpp @@ -22,7 +22,7 @@ namespace } } -NzTextureBackground::NzTextureBackground() +NzTextureBackground::NzTextureBackground(NzTextureRef texture) { m_uberShader = NzUberShaderLibrary::Get("Basic"); @@ -37,12 +37,8 @@ NzTextureBackground::NzTextureBackground() m_materialDiffuseUniform = shader->GetUniformLocation("MaterialDiffuse"); m_materialDiffuseMapUniform = shader->GetUniformLocation("MaterialDiffuseMap"); m_vertexDepthUniform = shader->GetUniformLocation("VertexDepth"); -} -NzTextureBackground::NzTextureBackground(NzTexture* texture) : -NzTextureBackground() -{ - m_texture = texture; + SetTexture(std::move(texture)); } void NzTextureBackground::Draw(const NzAbstractViewer* viewer) const @@ -68,13 +64,3 @@ nzBackgroundType NzTextureBackground::GetBackgroundType() const { return nzBackgroundType_Texture; } - -NzTexture* NzTextureBackground::GetTexture() const -{ - return m_texture; -} - -void NzTextureBackground::SetTexture(NzTexture* texture) -{ - m_texture = texture; -} diff --git a/src/Nazara/Graphics/View.cpp b/src/Nazara/Graphics/View.cpp deleted file mode 100644 index b05f2ec3a..000000000 --- a/src/Nazara/Graphics/View.cpp +++ /dev/null @@ -1,408 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Engine - Graphics module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#include -#include -#include -#include -#include - -NzView::NzView() : -m_targetRegion(0.f, 0.f, 1.f, 1.f), -m_size(0.f), -m_target(nullptr), -m_frustumUpdated(false), -m_invViewProjMatrixUpdated(false), -m_projectionMatrixUpdated(false), -m_viewMatrixUpdated(false), -m_viewProjMatrixUpdated(false), -m_viewportUpdated(false), -m_zFar(1.f), -m_zNear(-1.f) -{ -} - -NzView::NzView(const NzVector2f& size) : -NzView() // On délègue -{ - SetSize(size); -} - -void NzView::EnsureFrustumUpdate() const -{ - if (!m_frustumUpdated) - UpdateFrustum(); -} - -void NzView::EnsureProjectionMatrixUpdate() const -{ - if (!m_projectionMatrixUpdated) - UpdateProjectionMatrix(); -} - -void NzView::EnsureViewMatrixUpdate() const -{ - if (!m_viewMatrixUpdated) - UpdateViewMatrix(); -} - -void NzView::EnsureViewportUpdate() const -{ - if (!m_viewportUpdated) - UpdateViewport(); -} - -float NzView::GetAspectRatio() const -{ - return 1.f; -} - -NzVector3f NzView::GetEyePosition() const -{ - return GetPosition(nzCoordSys_Global); -} - -NzVector3f NzView::GetForward() const -{ - return NzNode::GetForward(); -} - -const NzFrustumf& NzView::GetFrustum() const -{ - if (!m_frustumUpdated) - UpdateFrustum(); - - 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::GetInvViewProjMatrix() const -{ - if (!m_invViewProjMatrixUpdated) - UpdateInvViewProjMatrix(); - - return m_invViewProjMatrix; -} - -const NzMatrix4f& NzView::GetProjectionMatrix() const -{ - if (!m_projectionMatrixUpdated) - UpdateProjectionMatrix(); - - return m_projectionMatrix; -} - -const NzVector2f& NzView::GetSize() const -{ - return m_size; -} - -const NzRenderTarget* NzView::GetTarget() const -{ - return m_target; -} - -const NzRectf& NzView::GetTargetRegion() const -{ - return m_targetRegion; -} - -const NzMatrix4f& NzView::GetViewMatrix() const -{ - if (!m_viewMatrixUpdated) - UpdateViewMatrix(); - - return m_viewMatrix; -} - -const NzMatrix4f& NzView::GetViewProjMatrix() const -{ - if (!m_viewProjMatrixUpdated) - UpdateViewProjMatrix(); - - return m_viewProjMatrix; -} - -const NzRecti& NzView::GetViewport() const -{ - #if NAZARA_GRAPHICS_SAFE - if (!m_target) - { - NazaraError("Camera has no render target"); - return m_viewport; - } - #endif - - if (!m_viewportUpdated) - UpdateViewport(); - - return m_viewport; -} - -float NzView::GetZFar() const -{ - return m_zFar; -} - -float NzView::GetZNear() const -{ - return m_zNear; -} - -NzVector2f NzView::MapPixelToWorld(const NzVector2i& pixel) -{ - if (!m_invViewProjMatrixUpdated) - UpdateInvViewProjMatrix(); - - if (!m_viewportUpdated) - UpdateViewport(); - - // Conversion du viewport en flottant - NzRectf viewport(m_viewport); - - NzVector2f normalized; - normalized.x = -1.f + 2.f * (pixel.x - viewport.x) / viewport.width; - normalized.y = 1.f - 2.f * (pixel.y - viewport.y) / viewport.height; - - return m_invViewProjMatrix.Transform(normalized); -} - -NzVector2i NzView::MapWorldToPixel(const NzVector2f& coords) -{ - if (!m_viewProjMatrixUpdated) - UpdateViewProjMatrix(); - - if (!m_viewportUpdated) - UpdateViewport(); - - // Conversion du viewport en flottant - NzRectf viewport(m_viewport); - - NzVector2f normalized = m_viewProjMatrix.Transform(coords); - - NzVector2i pixel; - pixel.x = static_cast(( normalized.x + 1.f) * viewport.width / 2.f + viewport.x); - pixel.y = static_cast((-normalized.y + 1.f) * viewport.width / 2.f + viewport.y); - - return pixel; -} - -void NzView::SetSize(const NzVector2f& size) -{ - SetSize(size.x, size.y); -} - -void NzView::SetSize(float width, float height) -{ - m_size.Set(width, height); - m_projectionMatrixUpdated = false; -} - -void NzView::SetTarget(const NzRenderTarget* renderTarget) -{ - m_target = renderTarget; - if (m_target) - { - m_targetReleaseSlot.Connect(m_target->OnRenderTargetRelease, this, &NzView::OnRenderTargetRelease); - m_targetResizeSlot.Connect(m_target->OnRenderTargetSizeChange, this, &NzView::OnRenderTargetSizeChange); - } - else - { - m_targetReleaseSlot.Disconnect(); - m_targetResizeSlot.Disconnect(); - } -} - -void NzView::SetTarget(const NzRenderTarget& renderTarget) -{ - SetTarget(&renderTarget); -} - -void NzView::SetTargetRegion(const NzRectf& region) -{ - m_targetRegion = region; - - m_frustumUpdated = false; - m_invViewProjMatrixUpdated = false; - m_projectionMatrixUpdated = false; - m_viewProjMatrixUpdated = false; - m_viewportUpdated = false; -} - -void NzView::SetViewport(const NzRecti& viewport) -{ - #if NAZARA_GRAPHICS_SAFE - if (!m_target) - { - NazaraError("Camera has no render target"); - return; - } - #endif - - // On calcule la région nécessaire pour produire ce viewport avec la taille actuelle de la cible - float invWidth = 1.f/m_target->GetWidth(); - float invHeight = 1.f/m_target->GetHeight(); - - SetTargetRegion(NzRectf(invWidth * viewport.x, invHeight * viewport.y, invWidth * viewport.width, invHeight * viewport.height)); -} - -void NzView::SetZFar(float zFar) -{ - m_zFar = zFar; - - m_frustumUpdated = false; - m_invViewProjMatrixUpdated = false; - m_projectionMatrixUpdated = false; - m_viewProjMatrixUpdated = false; -} - -void NzView::SetZNear(float zNear) -{ - m_zNear = zNear; - - m_frustumUpdated = false; - m_invViewProjMatrixUpdated = false; - m_projectionMatrixUpdated = false; - m_viewProjMatrixUpdated = false; -} - -void NzView::ApplyView() const -{ - #if NAZARA_GRAPHICS_SAFE - if (!m_target) - { - NazaraError("Camera has no render target"); - return; - } - #endif - - if (!m_projectionMatrixUpdated) - UpdateProjectionMatrix(); - - if (!m_viewMatrixUpdated) - UpdateViewMatrix(); - - if (!m_viewportUpdated) - UpdateViewport(); - - NzRenderer::SetMatrix(nzMatrixType_Projection, m_projectionMatrix); - NzRenderer::SetMatrix(nzMatrixType_View, m_viewMatrix); - NzRenderer::SetTarget(m_target); - NzRenderer::SetViewport(m_viewport); -} - -void NzView::InvalidateNode() -{ - NzNode::InvalidateNode(); - - // Le frustum et la view matrix dépendent des paramètres du node, invalidons-les - m_frustumUpdated = false; - m_invViewProjMatrixUpdated = false; - m_viewMatrixUpdated = false; - m_viewProjMatrixUpdated = false; -} - -void NzView::OnRenderTargetRelease(const NzRenderTarget* renderTarget) -{ - if (renderTarget == m_target) - m_target = nullptr; - else - NazaraInternalError("Not listening to " + NzString::Pointer(renderTarget)); -} - -void NzView::OnRenderTargetSizeChange(const NzRenderTarget* renderTarget) -{ - if (renderTarget == m_target) - { - m_frustumUpdated = false; - m_projectionMatrixUpdated = false; - m_viewportUpdated = false; - } - else - NazaraInternalError("Not listening to " + NzString::Pointer(renderTarget)); -} - -void NzView::UpdateFrustum() const -{ - if (!m_projectionMatrixUpdated) - UpdateProjectionMatrix(); - - if (!m_viewMatrixUpdated) - UpdateViewMatrix(); - - m_frustum.Extract(m_viewMatrix, m_projectionMatrix); - m_frustumUpdated = true; -} - -void NzView::UpdateInvViewProjMatrix() const -{ - if (!m_viewProjMatrixUpdated) - UpdateViewProjMatrix(); - - m_viewProjMatrix.GetInverseAffine(&m_invViewProjMatrix); - m_invViewProjMatrixUpdated = true; -} - -void NzView::UpdateProjectionMatrix() const -{ - if (m_size.x <= 0.f || m_size.y <= 0.f) // Si la taille est nulle, on prendra la taille du viewport - { - if (!m_viewportUpdated) - UpdateViewport(); - - m_projectionMatrix.MakeOrtho(0.f, static_cast(m_viewport.width), 0.f, static_cast(m_viewport.height), m_zNear, m_zFar); - } - else - m_projectionMatrix.MakeOrtho(0.f, m_size.x, 0.f, m_size.y, m_zNear, m_zFar); - - m_projectionMatrixUpdated = true; -} - -void NzView::UpdateViewMatrix() const -{ - if (!m_derivedUpdated) - UpdateDerived(); - - m_viewMatrix.MakeViewMatrix(m_derivedPosition, m_derivedRotation); - m_viewMatrixUpdated = true; -} - -void NzView::UpdateViewProjMatrix() const -{ - if (!m_projectionMatrixUpdated) - UpdateProjectionMatrix(); - - if (!m_viewMatrixUpdated) - UpdateViewMatrix(); - - // La matrice de projection orthogonale est affine - m_viewProjMatrix = NzMatrix4f::ConcatenateAffine(m_viewMatrix, m_projectionMatrix); - m_viewProjMatrixUpdated = true; -} - -void NzView::UpdateViewport() const -{ - unsigned int width = m_target->GetWidth(); - unsigned int height = std::max(m_target->GetHeight(), 1U); - - m_viewport.x = static_cast(width * m_targetRegion.x); - m_viewport.y = static_cast(height * m_targetRegion.y); - m_viewport.width = static_cast(width * m_targetRegion.width); - m_viewport.height = static_cast(height * m_targetRegion.height); - m_viewportUpdated = true; -} diff --git a/src/Nazara/Physics/PhysObject.cpp b/src/Nazara/Physics/PhysObject.cpp index b33acb226..a0d1dd164 100644 --- a/src/Nazara/Physics/PhysObject.cpp +++ b/src/Nazara/Physics/PhysObject.cpp @@ -52,10 +52,10 @@ m_mass(0.f) NzPhysObject::NzPhysObject(NzPhysObject&& object) : m_matrix(std::move(object.m_matrix)), +m_geom(std::move(object.m_geom)), m_forceAccumulator(std::move(object.m_forceAccumulator)), m_torqueAccumulator(std::move(object.m_torqueAccumulator)), m_body(object.m_body), -m_geom(std::move(object.m_geom)), m_world(object.m_world), m_gravityFactor(object.m_gravityFactor), m_mass(object.m_mass) diff --git a/src/Nazara/Renderer/RenderWindow.cpp b/src/Nazara/Renderer/RenderWindow.cpp index 97eb793d4..9d69ad312 100644 --- a/src/Nazara/Renderer/RenderWindow.cpp +++ b/src/Nazara/Renderer/RenderWindow.cpp @@ -41,7 +41,7 @@ bool NzRenderWindow::CopyToImage(NzAbstractImage* image, const NzVector3ui& dstP } #endif - return CopyToImage(image, NzRectui(NzVector2ui(0U), GetSize())); + return CopyToImage(image, NzRectui(NzVector2ui(0U), GetSize()), dstPos); } bool NzRenderWindow::CopyToImage(NzAbstractImage* image, const NzRectui& rect, const NzVector3ui& dstPos) const diff --git a/src/Nazara/Utility/Formats/OBJParser.cpp b/src/Nazara/Utility/Formats/OBJParser.cpp index cb435e53c..439c41bd4 100644 --- a/src/Nazara/Utility/Formats/OBJParser.cpp +++ b/src/Nazara/Utility/Formats/OBJParser.cpp @@ -104,12 +104,12 @@ bool NzOBJParser::Parse() std::unordered_map> meshes; unsigned int matIndex = 0; - auto GetMaterial = [&meshes, &matIndex] (const NzString& meshName, const NzString& matName) -> FaceVec* + auto GetMaterial = [&meshes, &matIndex] (const NzString& mesh, const NzString& material) -> FaceVec* { - auto& map = meshes[meshName]; - auto it = map.find(matName); + auto& map = meshes[mesh]; + auto it = map.find(material); if (it == map.end()) - it = map.insert(std::make_pair(matName, MatPair(FaceVec(), matIndex++))).first; + it = map.insert(std::make_pair(material, MatPair(FaceVec(), matIndex++))).first; return &(it->second.first); };