Merge remote-tracking branch 'origin/NDK' into NDK-ShadowMapping
Former-commit-id: d58c9f21d0de14e06321ed38b237b9fffcc0b2b8
This commit is contained in:
commit
52972b0514
|
|
@ -8,56 +8,60 @@
|
||||||
#define NAZARA_SPRITE_HPP
|
#define NAZARA_SPRITE_HPP
|
||||||
|
|
||||||
#include <Nazara/Prerequesites.hpp>
|
#include <Nazara/Prerequesites.hpp>
|
||||||
|
#include <Nazara/Graphics/InstancedRenderable.hpp>
|
||||||
#include <Nazara/Graphics/Material.hpp>
|
#include <Nazara/Graphics/Material.hpp>
|
||||||
#include <Nazara/Graphics/SceneNode.hpp>
|
#include <Nazara/Graphics/SceneNode.hpp>
|
||||||
#include <Nazara/Utility/VertexStruct.hpp>
|
#include <Nazara/Utility/VertexStruct.hpp>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
class NAZARA_GRAPHICS_API NzSprite : public NzSceneNode
|
class NzSprite;
|
||||||
|
|
||||||
|
using NzSpriteConstRef = NzObjectRef<const NzSprite>;
|
||||||
|
using NzSpriteLibrary = NzObjectLibrary<NzSprite>;
|
||||||
|
using NzSpriteRef = NzObjectRef<NzSprite>;
|
||||||
|
|
||||||
|
class NAZARA_GRAPHICS_API NzSprite : public NzInstancedRenderable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
NzSprite();
|
inline NzSprite();
|
||||||
NzSprite(NzTexture* texture);
|
inline NzSprite(NzMaterialRef material);
|
||||||
NzSprite(const NzSprite& sprite);
|
inline NzSprite(NzTexture* texture);
|
||||||
|
inline NzSprite(const NzSprite& sprite);
|
||||||
~NzSprite() = default;
|
~NzSprite() = default;
|
||||||
|
|
||||||
void AddToRenderQueue(NzAbstractRenderQueue* renderQueue) const override;
|
void AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const InstanceData& instanceData) const override;
|
||||||
|
|
||||||
NzSprite* Clone() const;
|
inline const NzColor& GetColor() const;
|
||||||
NzSprite* Create() const;
|
inline const NzMaterialRef& GetMaterial() const;
|
||||||
|
inline const NzVector2f& GetSize() const;
|
||||||
|
inline const NzRectf& GetTextureCoords() const;
|
||||||
|
|
||||||
const NzColor& GetColor() const;
|
inline void SetColor(const NzColor& color);
|
||||||
NzMaterial* GetMaterial() const;
|
inline void SetDefaultMaterial();
|
||||||
nzSceneNodeType GetSceneNodeType() const override;
|
inline void SetMaterial(NzMaterialRef material, bool resizeSprite = true);
|
||||||
const NzVector2f& GetSize() const;
|
inline void SetSize(const NzVector2f& size);
|
||||||
const NzRectf& GetTextureCoords() const;
|
inline void SetSize(float sizeX, float sizeY);
|
||||||
|
inline void SetTexture(NzTextureRef texture, bool resizeSprite = true);
|
||||||
|
inline void SetTextureCoords(const NzRectf& coords);
|
||||||
|
inline void SetTextureRect(const NzRectui& rect);
|
||||||
|
|
||||||
bool IsDrawable() const;
|
inline NzSprite& operator=(const NzSprite& sprite);
|
||||||
|
|
||||||
void SetColor(const NzColor& color);
|
template<typename... Args> static NzSpriteRef New(Args&&... args);
|
||||||
void SetDefaultMaterial();
|
|
||||||
void SetMaterial(NzMaterial* material, bool resizeSprite = true);
|
|
||||||
void SetSize(const NzVector2f& size);
|
|
||||||
void SetSize(float sizeX, float sizeY);
|
|
||||||
void SetTexture(NzTexture* texture, bool resizeSprite = true);
|
|
||||||
void SetTextureCoords(const NzRectf& coords);
|
|
||||||
void SetTextureRect(const NzRectui& rect);
|
|
||||||
|
|
||||||
NzSprite& operator=(const NzSprite& sprite);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void InvalidateNode() override;
|
inline void InvalidateVertices();
|
||||||
void MakeBoundingVolume() const override;
|
void MakeBoundingVolume() const override;
|
||||||
void Register() override;
|
void UpdateData(InstanceData* instanceData) const override;
|
||||||
void Unregister() override;
|
|
||||||
void UpdateVertices() const;
|
|
||||||
|
|
||||||
NzColor m_color;
|
NzColor m_color;
|
||||||
NzMaterialRef m_material;
|
NzMaterialRef m_material;
|
||||||
NzRectf m_textureCoords;
|
NzRectf m_textureCoords;
|
||||||
NzVector2f m_size;
|
NzVector2f m_size;
|
||||||
mutable std::array<NzVertexStruct_XYZ_Color_UV, 4> m_vertices;
|
|
||||||
mutable bool m_verticesUpdated;
|
static NzSpriteLibrary::LibraryMap s_library;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#include <Nazara/Graphics/Sprite.inl>
|
||||||
|
|
||||||
#endif // NAZARA_SPRITE_HPP
|
#endif // NAZARA_SPRITE_HPP
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,165 @@
|
||||||
|
// 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 <Nazara/Core/Error.hpp>
|
||||||
|
#include <memory>
|
||||||
|
#include <Nazara/Renderer/Debug.hpp>
|
||||||
|
|
||||||
|
inline NzSprite::NzSprite() :
|
||||||
|
m_color(NzColor::White),
|
||||||
|
m_textureCoords(0.f, 0.f, 1.f, 1.f),
|
||||||
|
m_size(64.f, 64.f)
|
||||||
|
{
|
||||||
|
SetDefaultMaterial();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline NzSprite::NzSprite(NzMaterialRef material) :
|
||||||
|
m_color(NzColor::White),
|
||||||
|
m_textureCoords(0.f, 0.f, 1.f, 1.f),
|
||||||
|
m_size(64.f, 64.f)
|
||||||
|
{
|
||||||
|
SetMaterial(std::move(material), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline NzSprite::NzSprite(NzTexture* texture) :
|
||||||
|
m_color(NzColor::White),
|
||||||
|
m_textureCoords(0.f, 0.f, 1.f, 1.f),
|
||||||
|
m_size(64.f, 64.f)
|
||||||
|
{
|
||||||
|
SetTexture(texture, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline NzSprite::NzSprite(const NzSprite& sprite) :
|
||||||
|
NzInstancedRenderable(sprite),
|
||||||
|
m_color(sprite.m_color),
|
||||||
|
m_material(sprite.m_material),
|
||||||
|
m_textureCoords(sprite.m_textureCoords),
|
||||||
|
m_size(sprite.m_size)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const NzColor& NzSprite::GetColor() const
|
||||||
|
{
|
||||||
|
return m_color;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const NzMaterialRef& NzSprite::GetMaterial() const
|
||||||
|
{
|
||||||
|
return m_material;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const NzVector2f& NzSprite::GetSize() const
|
||||||
|
{
|
||||||
|
return m_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const NzRectf& NzSprite::GetTextureCoords() const
|
||||||
|
{
|
||||||
|
return m_textureCoords;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzSprite::SetColor(const NzColor& color)
|
||||||
|
{
|
||||||
|
m_color = color;
|
||||||
|
|
||||||
|
InvalidateVertices();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzSprite::SetDefaultMaterial()
|
||||||
|
{
|
||||||
|
NzMaterialRef material = NzMaterial::New();
|
||||||
|
material->Enable(nzRendererParameter_FaceCulling, false);
|
||||||
|
material->EnableLighting(false);
|
||||||
|
|
||||||
|
SetMaterial(material);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzSprite::SetMaterial(NzMaterialRef material, bool resizeSprite)
|
||||||
|
{
|
||||||
|
m_material = std::move(material);
|
||||||
|
if (m_material && resizeSprite)
|
||||||
|
{
|
||||||
|
NzTexture* diffuseMap = m_material->GetDiffuseMap();
|
||||||
|
if (diffuseMap && diffuseMap->IsValid())
|
||||||
|
SetSize(NzVector2f(NzVector2ui(diffuseMap->GetSize())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzSprite::SetSize(const NzVector2f& size)
|
||||||
|
{
|
||||||
|
m_size = size;
|
||||||
|
|
||||||
|
// On invalide la bounding box
|
||||||
|
InvalidateBoundingVolume();
|
||||||
|
InvalidateVertices();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzSprite::SetSize(float sizeX, float sizeY)
|
||||||
|
{
|
||||||
|
SetSize(NzVector2f(sizeX, sizeY));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzSprite::SetTexture(NzTextureRef texture, bool resizeSprite)
|
||||||
|
{
|
||||||
|
if (!m_material)
|
||||||
|
SetDefaultMaterial();
|
||||||
|
else if (m_material->GetReferenceCount() > 1)
|
||||||
|
m_material = NzMaterial::New(*m_material); // Copie
|
||||||
|
|
||||||
|
if (resizeSprite && texture && texture->IsValid())
|
||||||
|
SetSize(NzVector2f(NzVector2ui(texture->GetSize())));
|
||||||
|
|
||||||
|
m_material->SetDiffuseMap(std::move(texture));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzSprite::SetTextureCoords(const NzRectf& coords)
|
||||||
|
{
|
||||||
|
m_textureCoords = coords;
|
||||||
|
InvalidateVertices();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzSprite::SetTextureRect(const NzRectui& rect)
|
||||||
|
{
|
||||||
|
NazaraAssert(material, "Sprite has no material");
|
||||||
|
NazaraAssert(m_material->HasDiffuseMap(), "Sprite material has no diffuse map");
|
||||||
|
|
||||||
|
NzTexture* diffuseMap = m_material->GetDiffuseMap();
|
||||||
|
|
||||||
|
float invWidth = 1.f/diffuseMap->GetWidth();
|
||||||
|
float invHeight = 1.f/diffuseMap->GetHeight();
|
||||||
|
|
||||||
|
SetTextureCoords(NzRectf(invWidth*rect.x, invHeight*rect.y, invWidth*rect.width, invHeight*rect.height));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline NzSprite& NzSprite::operator=(const NzSprite& sprite)
|
||||||
|
{
|
||||||
|
NzInstancedRenderable::operator=(sprite);
|
||||||
|
|
||||||
|
m_color = sprite.m_color;
|
||||||
|
m_material = sprite.m_material;
|
||||||
|
m_textureCoords = sprite.m_textureCoords;
|
||||||
|
m_size = sprite.m_size;
|
||||||
|
|
||||||
|
// On ne copie pas les sommets finaux car il est très probable que nos paramètres soient modifiés et qu'ils doivent être régénérés de toute façon
|
||||||
|
InvalidateBoundingVolume();
|
||||||
|
InvalidateVertices();
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzSprite::InvalidateVertices()
|
||||||
|
{
|
||||||
|
InvalidateInstanceData(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
NzSpriteRef NzSprite::New(Args&&... args)
|
||||||
|
{
|
||||||
|
std::unique_ptr<NzSprite> object(new NzSprite(std::forward<Args>(args)...));
|
||||||
|
object->SetPersistent(false);
|
||||||
|
|
||||||
|
return object.release();
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Renderer/DebugOff.hpp>
|
||||||
|
|
@ -25,36 +25,31 @@ using NzTextSpriteRef = NzObjectRef<NzTextSprite>;
|
||||||
class NAZARA_GRAPHICS_API NzTextSprite : public NzInstancedRenderable
|
class NAZARA_GRAPHICS_API NzTextSprite : public NzInstancedRenderable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
NzTextSprite();
|
inline NzTextSprite();
|
||||||
NzTextSprite(const NzTextSprite& sprite);
|
inline NzTextSprite(const NzTextSprite& sprite);
|
||||||
~NzTextSprite() = default;
|
~NzTextSprite() = default;
|
||||||
|
|
||||||
void AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const InstanceData& instanceData) const override;
|
void AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const InstanceData& instanceData) const override;
|
||||||
|
|
||||||
void Clear();
|
inline void Clear();
|
||||||
|
|
||||||
NzTextSprite* Clone() const;
|
inline const NzColor& GetColor() const;
|
||||||
NzTextSprite* Create() const;
|
inline const NzMaterialRef& GetMaterial() const;
|
||||||
|
inline float GetScale() const;
|
||||||
|
|
||||||
const NzColor& GetColor() const;
|
inline void SetColor(const NzColor& color);
|
||||||
NzMaterial* GetMaterial() const;
|
inline void SetDefaultMaterial();
|
||||||
float GetScale() const;
|
inline void SetMaterial(NzMaterialRef material);
|
||||||
|
inline void SetScale(float scale);
|
||||||
void InvalidateVertices();
|
|
||||||
bool IsDrawable() const;
|
|
||||||
|
|
||||||
void SetColor(const NzColor& color);
|
|
||||||
void SetDefaultMaterial();
|
|
||||||
void SetMaterial(NzMaterial* material);
|
|
||||||
void SetScale(float scale);
|
|
||||||
|
|
||||||
void Update(const NzAbstractTextDrawer& drawer);
|
void Update(const NzAbstractTextDrawer& drawer);
|
||||||
|
|
||||||
NzTextSprite& operator=(const NzTextSprite& text);
|
inline NzTextSprite& operator=(const NzTextSprite& text);
|
||||||
|
|
||||||
template<typename... Args> static NzTextSpriteRef New(Args&&... args);
|
template<typename... Args> static NzTextSpriteRef New(Args&&... args);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
inline void InvalidateVertices();
|
||||||
void MakeBoundingVolume() const override;
|
void MakeBoundingVolume() const override;
|
||||||
void OnAtlasInvalidated(const NzAbstractAtlas* atlas);
|
void OnAtlasInvalidated(const NzAbstractAtlas* atlas);
|
||||||
void OnAtlasLayerChange(const NzAbstractAtlas* atlas, NzAbstractImage* oldLayer, NzAbstractImage* newLayer);
|
void OnAtlasLayerChange(const NzAbstractAtlas* atlas, NzAbstractImage* oldLayer, NzAbstractImage* newLayer);
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,123 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <Nazara/Renderer/Debug.hpp>
|
#include <Nazara/Renderer/Debug.hpp>
|
||||||
|
|
||||||
|
inline NzTextSprite::NzTextSprite() :
|
||||||
|
m_color(NzColor::White),
|
||||||
|
m_scale(1.f)
|
||||||
|
{
|
||||||
|
SetDefaultMaterial();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline NzTextSprite::NzTextSprite(const NzTextSprite& sprite) :
|
||||||
|
NzInstancedRenderable(sprite),
|
||||||
|
m_renderInfos(sprite.m_renderInfos),
|
||||||
|
m_localVertices(sprite.m_localVertices),
|
||||||
|
m_color(sprite.m_color),
|
||||||
|
m_material(sprite.m_material),
|
||||||
|
m_localBounds(sprite.m_localBounds),
|
||||||
|
m_scale(sprite.m_scale)
|
||||||
|
{
|
||||||
|
for (auto it = sprite.m_atlases.begin(); it != sprite.m_atlases.end(); ++it)
|
||||||
|
{
|
||||||
|
const NzAbstractAtlas* atlas = it->first;
|
||||||
|
AtlasSlots& slots = m_atlases[atlas];
|
||||||
|
|
||||||
|
slots.clearSlot.Connect(atlas->OnAtlasCleared, this, &NzTextSprite::OnAtlasInvalidated);
|
||||||
|
slots.layerChangeSlot.Connect(atlas->OnAtlasLayerChange, this, &NzTextSprite::OnAtlasLayerChange);
|
||||||
|
slots.releaseSlot.Connect(atlas->OnAtlasRelease, this, &NzTextSprite::OnAtlasInvalidated);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzTextSprite::Clear()
|
||||||
|
{
|
||||||
|
m_atlases.clear();
|
||||||
|
m_boundingVolume.MakeNull();
|
||||||
|
m_localVertices.clear();
|
||||||
|
m_renderInfos.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const NzColor& NzTextSprite::GetColor() const
|
||||||
|
{
|
||||||
|
return m_color;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const NzMaterialRef& NzTextSprite::GetMaterial() const
|
||||||
|
{
|
||||||
|
return m_material;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline float NzTextSprite::GetScale() const
|
||||||
|
{
|
||||||
|
return m_scale;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzTextSprite::SetColor(const NzColor& color)
|
||||||
|
{
|
||||||
|
m_color = color;
|
||||||
|
|
||||||
|
InvalidateVertices();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzTextSprite::SetDefaultMaterial()
|
||||||
|
{
|
||||||
|
NzMaterialRef material = NzMaterial::New();
|
||||||
|
material->Enable(nzRendererParameter_Blend, true);
|
||||||
|
material->Enable(nzRendererParameter_DepthWrite, false);
|
||||||
|
material->Enable(nzRendererParameter_FaceCulling, false);
|
||||||
|
material->EnableLighting(false);
|
||||||
|
material->SetDstBlend(nzBlendFunc_InvSrcAlpha);
|
||||||
|
material->SetSrcBlend(nzBlendFunc_SrcAlpha);
|
||||||
|
|
||||||
|
SetMaterial(material);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzTextSprite::SetMaterial(NzMaterialRef material)
|
||||||
|
{
|
||||||
|
m_material = std::move(material);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzTextSprite::SetScale(float scale)
|
||||||
|
{
|
||||||
|
m_scale = scale;
|
||||||
|
|
||||||
|
InvalidateVertices();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzTextSprite::InvalidateVertices()
|
||||||
|
{
|
||||||
|
InvalidateInstanceData(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline NzTextSprite& NzTextSprite::operator=(const NzTextSprite& text)
|
||||||
|
{
|
||||||
|
NzInstancedRenderable::operator=(text);
|
||||||
|
|
||||||
|
m_atlases.clear();
|
||||||
|
|
||||||
|
m_color = text.m_color;
|
||||||
|
m_material = text.m_material;
|
||||||
|
m_renderInfos = text.m_renderInfos;
|
||||||
|
m_localBounds = text.m_localBounds;
|
||||||
|
m_localVertices = text.m_localVertices;
|
||||||
|
m_scale = text.m_scale;
|
||||||
|
|
||||||
|
// Connect to the slots of the new atlases
|
||||||
|
for (auto it = text.m_atlases.begin(); it != text.m_atlases.end(); ++it)
|
||||||
|
{
|
||||||
|
const NzAbstractAtlas* atlas = it->first;
|
||||||
|
AtlasSlots& slots = m_atlases[atlas];
|
||||||
|
|
||||||
|
slots.clearSlot.Connect(atlas->OnAtlasCleared, this, &NzTextSprite::OnAtlasInvalidated);
|
||||||
|
slots.layerChangeSlot.Connect(atlas->OnAtlasLayerChange, this, &NzTextSprite::OnAtlasLayerChange);
|
||||||
|
slots.releaseSlot.Connect(atlas->OnAtlasRelease, this, &NzTextSprite::OnAtlasInvalidated);
|
||||||
|
}
|
||||||
|
|
||||||
|
InvalidateBoundingVolume();
|
||||||
|
InvalidateVertices();
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
NzTextSpriteRef NzTextSprite::New(Args&&... args)
|
NzTextSpriteRef NzTextSprite::New(Args&&... args)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -10,230 +10,42 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <Nazara/Graphics/Debug.hpp>
|
#include <Nazara/Graphics/Debug.hpp>
|
||||||
|
|
||||||
NzSprite::NzSprite() :
|
void NzSprite::AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const InstanceData& instanceData) const
|
||||||
m_color(NzColor::White),
|
|
||||||
m_textureCoords(0.f, 0.f, 1.f, 1.f),
|
|
||||||
m_size(64.f, 64.f),
|
|
||||||
m_verticesUpdated(false)
|
|
||||||
{
|
|
||||||
SetDefaultMaterial();
|
|
||||||
}
|
|
||||||
|
|
||||||
NzSprite::NzSprite(NzTexture* texture) :
|
|
||||||
m_color(NzColor::White),
|
|
||||||
m_textureCoords(0.f, 0.f, 1.f, 1.f),
|
|
||||||
m_size(64.f, 64.f),
|
|
||||||
m_verticesUpdated(false)
|
|
||||||
{
|
|
||||||
SetTexture(texture, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
NzSprite::NzSprite(const NzSprite& sprite) :
|
|
||||||
NzSceneNode(sprite),
|
|
||||||
m_color(sprite.m_color),
|
|
||||||
m_material(sprite.m_material),
|
|
||||||
m_textureCoords(sprite.m_textureCoords),
|
|
||||||
m_size(sprite.m_size),
|
|
||||||
m_vertices(sprite.m_vertices),
|
|
||||||
m_verticesUpdated(sprite.m_verticesUpdated)
|
|
||||||
{
|
|
||||||
SetParent(sprite.GetParent());
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzSprite::AddToRenderQueue(NzAbstractRenderQueue* renderQueue) const
|
|
||||||
{
|
|
||||||
if (!m_verticesUpdated)
|
|
||||||
UpdateVertices();
|
|
||||||
|
|
||||||
renderQueue->AddSprites(m_material, m_vertices.data(), 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
NzSprite* NzSprite::Clone() const
|
|
||||||
{
|
|
||||||
return new NzSprite(*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
NzSprite* NzSprite::Create() const
|
|
||||||
{
|
|
||||||
return new NzSprite;
|
|
||||||
}
|
|
||||||
|
|
||||||
const NzColor& NzSprite::GetColor() const
|
|
||||||
{
|
|
||||||
return m_color;
|
|
||||||
}
|
|
||||||
|
|
||||||
NzMaterial* NzSprite::GetMaterial() const
|
|
||||||
{
|
|
||||||
return m_material;
|
|
||||||
}
|
|
||||||
|
|
||||||
nzSceneNodeType NzSprite::GetSceneNodeType() const
|
|
||||||
{
|
|
||||||
return nzSceneNodeType_Sprite;
|
|
||||||
}
|
|
||||||
|
|
||||||
const NzVector2f& NzSprite::GetSize() const
|
|
||||||
{
|
|
||||||
return m_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
const NzRectf& NzSprite::GetTextureCoords() const
|
|
||||||
{
|
|
||||||
return m_textureCoords;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool NzSprite::IsDrawable() const
|
|
||||||
{
|
|
||||||
return m_material != nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzSprite::SetColor(const NzColor& color)
|
|
||||||
{
|
|
||||||
m_color = color;
|
|
||||||
m_verticesUpdated = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzSprite::SetDefaultMaterial()
|
|
||||||
{
|
|
||||||
NzMaterialRef material = NzMaterial::New();
|
|
||||||
material->Enable(nzRendererParameter_FaceCulling, false);
|
|
||||||
material->EnableLighting(false);
|
|
||||||
|
|
||||||
SetMaterial(material);
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzSprite::SetMaterial(NzMaterial* material, bool resizeSprite)
|
|
||||||
{
|
|
||||||
m_material = material;
|
|
||||||
if (m_material && resizeSprite)
|
|
||||||
{
|
|
||||||
NzTexture* diffuseMap = m_material->GetDiffuseMap();
|
|
||||||
if (diffuseMap && diffuseMap->IsValid())
|
|
||||||
SetSize(NzVector2f(NzVector2ui(diffuseMap->GetSize())));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzSprite::SetSize(const NzVector2f& size)
|
|
||||||
{
|
|
||||||
m_size = size;
|
|
||||||
|
|
||||||
// On invalide la bounding box
|
|
||||||
m_boundingVolume.MakeNull();
|
|
||||||
m_boundingVolumeUpdated = false;
|
|
||||||
m_verticesUpdated = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzSprite::SetSize(float sizeX, float sizeY)
|
|
||||||
{
|
|
||||||
SetSize(NzVector2f(sizeX, sizeY));
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzSprite::SetTexture(NzTexture* texture, bool resizeSprite)
|
|
||||||
{
|
{
|
||||||
if (!m_material)
|
if (!m_material)
|
||||||
SetDefaultMaterial();
|
|
||||||
else if (m_material->GetReferenceCount() > 1)
|
|
||||||
m_material = NzMaterial::New(*m_material); // Copie
|
|
||||||
|
|
||||||
m_material->SetDiffuseMap(texture);
|
|
||||||
if (resizeSprite && texture && texture->IsValid())
|
|
||||||
SetSize(NzVector2f(NzVector2ui(texture->GetSize())));
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzSprite::SetTextureCoords(const NzRectf& coords)
|
|
||||||
{
|
|
||||||
m_textureCoords = coords;
|
|
||||||
m_verticesUpdated = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzSprite::SetTextureRect(const NzRectui& rect)
|
|
||||||
{
|
|
||||||
#if NAZARA_GRAPHICS_SAFE
|
|
||||||
if (!m_material)
|
|
||||||
{
|
|
||||||
NazaraError("Sprite has no material");
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
if (!m_material->HasDiffuseMap())
|
const NzVertexStruct_XYZ_Color_UV* vertices = reinterpret_cast<const NzVertexStruct_XYZ_Color_UV*>(instanceData.data.data());
|
||||||
{
|
renderQueue->AddSprites(m_material, vertices, 1);
|
||||||
NazaraError("Sprite material has no diffuse map");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
NzTexture* diffuseMap = m_material->GetDiffuseMap();
|
|
||||||
|
|
||||||
float invWidth = 1.f/diffuseMap->GetWidth();
|
|
||||||
float invHeight = 1.f/diffuseMap->GetHeight();
|
|
||||||
|
|
||||||
SetTextureCoords(NzRectf(invWidth*rect.x, invHeight*rect.y, invWidth*rect.width, invHeight*rect.height));
|
|
||||||
}
|
|
||||||
|
|
||||||
NzSprite& NzSprite::operator=(const NzSprite& sprite)
|
|
||||||
{
|
|
||||||
NzSceneNode::operator=(sprite);
|
|
||||||
|
|
||||||
m_color = sprite.m_color;
|
|
||||||
m_material = sprite.m_material;
|
|
||||||
m_textureCoords = sprite.m_textureCoords;
|
|
||||||
m_size = sprite.m_size;
|
|
||||||
|
|
||||||
// On ne copie pas les sommets finaux car il est très probable que nos paramètres soient modifiés et qu'ils doivent être régénérés de toute façon
|
|
||||||
m_verticesUpdated = false;
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzSprite::InvalidateNode()
|
|
||||||
{
|
|
||||||
NzSceneNode::InvalidateNode();
|
|
||||||
|
|
||||||
m_verticesUpdated = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzSprite::Register()
|
|
||||||
{
|
|
||||||
// Le changement de scène peut affecter les sommets
|
|
||||||
m_verticesUpdated = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzSprite::Unregister()
|
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NzSprite::MakeBoundingVolume() const
|
void NzSprite::MakeBoundingVolume() const
|
||||||
{
|
{
|
||||||
NzVector3f down = (m_scene) ? m_scene->GetDown() : NzVector3f::Down();
|
m_boundingVolume.Set(NzVector3f(0.f), m_size.x*NzVector3f::Right() + m_size.y*NzVector3f::Down());
|
||||||
NzVector3f right = (m_scene) ? m_scene->GetRight() : NzVector3f::Right();
|
|
||||||
|
|
||||||
m_boundingVolume.Set(NzVector3f(0.f), m_size.x*right + m_size.y*down);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NzSprite::UpdateVertices() const
|
void NzSprite::UpdateData(InstanceData* instanceData) const
|
||||||
{
|
{
|
||||||
if (!m_transformMatrixUpdated)
|
instanceData->data.resize(4 * sizeof(NzVertexStruct_XYZ_Color_UV));
|
||||||
UpdateTransformMatrix();
|
NzVertexStruct_XYZ_Color_UV* vertices = reinterpret_cast<NzVertexStruct_XYZ_Color_UV*>(instanceData->data.data());
|
||||||
|
|
||||||
NzVector3f down = (m_scene) ? m_scene->GetDown() : NzVector3f::Down();
|
NzSparsePtr<NzColor> colorPtr(&vertices[0].color, sizeof(NzVertexStruct_XYZ_Color_UV));
|
||||||
NzVector3f right = (m_scene) ? m_scene->GetRight() : NzVector3f::Right();
|
NzSparsePtr<NzVector3f> posPtr(&vertices[0].position, sizeof(NzVertexStruct_XYZ_Color_UV));
|
||||||
|
NzSparsePtr<NzVector2f> texCoordPtr(&vertices[0].uv, sizeof(NzVertexStruct_XYZ_Color_UV));
|
||||||
|
|
||||||
m_vertices[0].color = m_color;
|
*colorPtr++ = m_color;
|
||||||
m_vertices[0].position = m_transformMatrix.Transform(NzVector3f(0.f));
|
*posPtr++ = instanceData->transformMatrix.Transform(NzVector3f(0.f));
|
||||||
m_vertices[0].uv.Set(m_textureCoords.GetCorner(nzRectCorner_LeftTop));
|
*texCoordPtr++ = m_textureCoords.GetCorner(nzRectCorner_LeftTop);
|
||||||
|
|
||||||
m_vertices[1].color = m_color;
|
*colorPtr++ = m_color;
|
||||||
m_vertices[1].position = m_transformMatrix.Transform(m_size.x*right);
|
*posPtr++ = instanceData->transformMatrix.Transform(m_size.x*NzVector3f::Right());
|
||||||
m_vertices[1].uv.Set(m_textureCoords.GetCorner(nzRectCorner_RightTop));
|
*texCoordPtr++ = m_textureCoords.GetCorner(nzRectCorner_RightTop);
|
||||||
|
|
||||||
m_vertices[2].color = m_color;
|
*colorPtr++ = m_color;
|
||||||
m_vertices[2].position = m_transformMatrix.Transform(m_size.y*down);
|
*posPtr++ = instanceData->transformMatrix.Transform(m_size.y*NzVector3f::Down());
|
||||||
m_vertices[2].uv.Set(m_textureCoords.GetCorner(nzRectCorner_LeftBottom));
|
*texCoordPtr++ = m_textureCoords.GetCorner(nzRectCorner_LeftBottom);
|
||||||
|
|
||||||
m_vertices[3].color = m_color;
|
*colorPtr++ = m_color;
|
||||||
m_vertices[3].position = m_transformMatrix.Transform(m_size.x*right + m_size.y*down);
|
*posPtr++ = instanceData->transformMatrix.Transform(m_size.x*NzVector3f::Right() + m_size.y*NzVector3f::Down());
|
||||||
m_vertices[3].uv.Set(m_textureCoords.GetCorner(nzRectCorner_RightBottom));
|
*texCoordPtr++ = m_textureCoords.GetCorner(nzRectCorner_RightBottom);
|
||||||
|
|
||||||
m_verticesUpdated = true;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,35 +11,11 @@
|
||||||
#include <Nazara/Utility/Font.hpp>
|
#include <Nazara/Utility/Font.hpp>
|
||||||
#include <Nazara/Graphics/Debug.hpp>
|
#include <Nazara/Graphics/Debug.hpp>
|
||||||
|
|
||||||
NzTextSprite::NzTextSprite() :
|
|
||||||
m_color(NzColor::White),
|
|
||||||
m_scale(1.f)
|
|
||||||
{
|
|
||||||
SetDefaultMaterial();
|
|
||||||
}
|
|
||||||
|
|
||||||
NzTextSprite::NzTextSprite(const NzTextSprite& sprite) :
|
|
||||||
NzInstancedRenderable(sprite),
|
|
||||||
m_renderInfos(sprite.m_renderInfos),
|
|
||||||
m_localVertices(sprite.m_localVertices),
|
|
||||||
m_color(sprite.m_color),
|
|
||||||
m_material(sprite.m_material),
|
|
||||||
m_localBounds(sprite.m_localBounds),
|
|
||||||
m_scale(sprite.m_scale)
|
|
||||||
{
|
|
||||||
for (auto it = sprite.m_atlases.begin(); it != sprite.m_atlases.end(); ++it)
|
|
||||||
{
|
|
||||||
const NzAbstractAtlas* atlas = it->first;
|
|
||||||
AtlasSlots& slots = m_atlases[atlas];
|
|
||||||
|
|
||||||
slots.clearSlot.Connect(atlas->OnAtlasCleared, this, &NzTextSprite::OnAtlasInvalidated);
|
|
||||||
slots.layerChangeSlot.Connect(atlas->OnAtlasLayerChange, this, &NzTextSprite::OnAtlasLayerChange);
|
|
||||||
slots.releaseSlot.Connect(atlas->OnAtlasRelease, this, &NzTextSprite::OnAtlasInvalidated);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzTextSprite::AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const InstanceData& instanceData) const
|
void NzTextSprite::AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const InstanceData& instanceData) const
|
||||||
{
|
{
|
||||||
|
if (!m_material)
|
||||||
|
return;
|
||||||
|
|
||||||
for (auto& pair : m_renderInfos)
|
for (auto& pair : m_renderInfos)
|
||||||
{
|
{
|
||||||
NzTexture* overlay = pair.first;
|
NzTexture* overlay = pair.first;
|
||||||
|
|
@ -53,81 +29,6 @@ void NzTextSprite::AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const In
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void NzTextSprite::Clear()
|
|
||||||
{
|
|
||||||
m_atlases.clear();
|
|
||||||
m_boundingVolume.MakeNull();
|
|
||||||
m_localVertices.clear();
|
|
||||||
m_renderInfos.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
NzTextSprite* NzTextSprite::Clone() const
|
|
||||||
{
|
|
||||||
return new NzTextSprite(*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
NzTextSprite* NzTextSprite::Create() const
|
|
||||||
{
|
|
||||||
return new NzTextSprite;
|
|
||||||
}
|
|
||||||
|
|
||||||
const NzColor& NzTextSprite::GetColor() const
|
|
||||||
{
|
|
||||||
return m_color;
|
|
||||||
}
|
|
||||||
|
|
||||||
NzMaterial* NzTextSprite::GetMaterial() const
|
|
||||||
{
|
|
||||||
return m_material;
|
|
||||||
}
|
|
||||||
|
|
||||||
float NzTextSprite::GetScale() const
|
|
||||||
{
|
|
||||||
return m_scale;
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzTextSprite::InvalidateVertices()
|
|
||||||
{
|
|
||||||
InvalidateInstanceData(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool NzTextSprite::IsDrawable() const
|
|
||||||
{
|
|
||||||
return m_material != nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzTextSprite::SetColor(const NzColor& color)
|
|
||||||
{
|
|
||||||
m_color = color;
|
|
||||||
|
|
||||||
InvalidateVertices();
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzTextSprite::SetDefaultMaterial()
|
|
||||||
{
|
|
||||||
NzMaterialRef material = NzMaterial::New();
|
|
||||||
material->Enable(nzRendererParameter_Blend, true);
|
|
||||||
material->Enable(nzRendererParameter_DepthWrite, false);
|
|
||||||
material->Enable(nzRendererParameter_FaceCulling, false);
|
|
||||||
material->EnableLighting(false);
|
|
||||||
material->SetDstBlend(nzBlendFunc_InvSrcAlpha);
|
|
||||||
material->SetSrcBlend(nzBlendFunc_SrcAlpha);
|
|
||||||
|
|
||||||
SetMaterial(material);
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzTextSprite::SetMaterial(NzMaterial* material)
|
|
||||||
{
|
|
||||||
m_material = material;
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzTextSprite::SetScale(float scale)
|
|
||||||
{
|
|
||||||
m_scale = scale;
|
|
||||||
|
|
||||||
InvalidateInstanceData(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzTextSprite::Update(const NzAbstractTextDrawer& drawer)
|
void NzTextSprite::Update(const NzAbstractTextDrawer& drawer)
|
||||||
{
|
{
|
||||||
m_atlases.clear();
|
m_atlases.clear();
|
||||||
|
|
@ -142,15 +43,7 @@ void NzTextSprite::Update(const NzAbstractTextDrawer& drawer)
|
||||||
{
|
{
|
||||||
NzFont* font = drawer.GetFont(i);
|
NzFont* font = drawer.GetFont(i);
|
||||||
const NzAbstractAtlas* atlas = font->GetAtlas().get();
|
const NzAbstractAtlas* atlas = font->GetAtlas().get();
|
||||||
|
NazaraAssert(atlas->GetStorage() & nzDataStorage_Hardware, "Font uses a non-hardware atlas which cannot be used by text sprites");
|
||||||
#if NAZARA_GRAPHICS_SAFE
|
|
||||||
if ((atlas->GetStorage() & nzDataStorage_Hardware) == 0)
|
|
||||||
{
|
|
||||||
// Cet atlas ne nous donnera pas de texture, nous ne pouvons pas l'utiliser
|
|
||||||
NazaraError("Font " + NzString::Pointer(font) + " uses a non-hardware atlas which cannot be used by text sprites");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (m_atlases.find(atlas) == m_atlases.end())
|
if (m_atlases.find(atlas) == m_atlases.end())
|
||||||
{
|
{
|
||||||
|
|
@ -243,36 +136,6 @@ void NzTextSprite::Update(const NzAbstractTextDrawer& drawer)
|
||||||
clearOnFail.Reset();
|
clearOnFail.Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
NzTextSprite& NzTextSprite::operator=(const NzTextSprite& text)
|
|
||||||
{
|
|
||||||
NzInstancedRenderable::operator=(text);
|
|
||||||
|
|
||||||
m_atlases.clear();
|
|
||||||
|
|
||||||
m_color = text.m_color;
|
|
||||||
m_material = text.m_material;
|
|
||||||
m_renderInfos = text.m_renderInfos;
|
|
||||||
m_localBounds = text.m_localBounds;
|
|
||||||
m_localVertices = text.m_localVertices;
|
|
||||||
m_scale = text.m_scale;
|
|
||||||
|
|
||||||
// Connect to the slots of the new atlases
|
|
||||||
for (auto it = text.m_atlases.begin(); it != text.m_atlases.end(); ++it)
|
|
||||||
{
|
|
||||||
const NzAbstractAtlas* atlas = it->first;
|
|
||||||
AtlasSlots& slots = m_atlases[atlas];
|
|
||||||
|
|
||||||
slots.clearSlot.Connect(atlas->OnAtlasCleared, this, &NzTextSprite::OnAtlasInvalidated);
|
|
||||||
slots.layerChangeSlot.Connect(atlas->OnAtlasLayerChange, this, &NzTextSprite::OnAtlasLayerChange);
|
|
||||||
slots.releaseSlot.Connect(atlas->OnAtlasRelease, this, &NzTextSprite::OnAtlasInvalidated);
|
|
||||||
}
|
|
||||||
|
|
||||||
InvalidateBoundingVolume();
|
|
||||||
InvalidateVertices();
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzTextSprite::MakeBoundingVolume() const
|
void NzTextSprite::MakeBoundingVolume() const
|
||||||
{
|
{
|
||||||
NzRectf bounds(m_localBounds);
|
NzRectf bounds(m_localBounds);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue