Graphics: Add billboard class
Former-commit-id: bc898f00aa78dd42f61d34ce743ed9a73dce55f6
This commit is contained in:
parent
8827ee8ff2
commit
95ae8ed82c
|
|
@ -0,0 +1,62 @@
|
||||||
|
// 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_BILLBOARD_HPP
|
||||||
|
#define NAZARA_BILLBOARD_HPP
|
||||||
|
|
||||||
|
#include <Nazara/Prerequesites.hpp>
|
||||||
|
#include <Nazara/Graphics/InstancedRenderable.hpp>
|
||||||
|
#include <Nazara/Graphics/Material.hpp>
|
||||||
|
|
||||||
|
class NzBillboard;
|
||||||
|
|
||||||
|
using NzBillboardConstRef = NzObjectRef<const NzBillboard>;
|
||||||
|
using NzBillboardLibrary = NzObjectLibrary<NzBillboard>;
|
||||||
|
using NzBillboardRef = NzObjectRef<NzBillboard>;
|
||||||
|
|
||||||
|
class NAZARA_GRAPHICS_API NzBillboard : public NzInstancedRenderable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
inline NzBillboard();
|
||||||
|
inline NzBillboard(NzMaterialRef material);
|
||||||
|
inline NzBillboard(NzTexture* texture);
|
||||||
|
inline NzBillboard(const NzBillboard& billboard);
|
||||||
|
~NzBillboard() = default;
|
||||||
|
|
||||||
|
void AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const InstanceData& instanceData) const override;
|
||||||
|
|
||||||
|
inline const NzColor& GetColor() const;
|
||||||
|
inline const NzMaterialRef& GetMaterial() const;
|
||||||
|
inline float GetRotation() const;
|
||||||
|
inline const NzVector2f& GetSize() const;
|
||||||
|
|
||||||
|
inline void SetColor(const NzColor& color);
|
||||||
|
inline void SetDefaultMaterial();
|
||||||
|
inline void SetMaterial(NzMaterialRef material, bool resizeBillboard = true);
|
||||||
|
inline void SetRotation(float rotation);
|
||||||
|
inline void SetSize(const NzVector2f& size);
|
||||||
|
inline void SetSize(float sizeX, float sizeY);
|
||||||
|
inline void SetTexture(NzTextureRef texture, bool resizeBillboard = true);
|
||||||
|
|
||||||
|
inline NzBillboard& operator=(const NzBillboard& billboard);
|
||||||
|
|
||||||
|
template<typename... Args> static NzBillboardRef New(Args&&... args);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void MakeBoundingVolume() const override;
|
||||||
|
|
||||||
|
NzColor m_color;
|
||||||
|
NzMaterialRef m_material;
|
||||||
|
NzVector2f m_sinCos;
|
||||||
|
NzVector2f m_size;
|
||||||
|
float m_rotation;
|
||||||
|
|
||||||
|
static NzBillboardLibrary::LibraryMap s_library;
|
||||||
|
};
|
||||||
|
|
||||||
|
#include <Nazara/Graphics/Billboard.inl>
|
||||||
|
|
||||||
|
#endif // NAZARA_BILLBOARD_HPP
|
||||||
|
|
@ -0,0 +1,141 @@
|
||||||
|
// 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 <memory>
|
||||||
|
#include <Nazara/Graphics/Debug.hpp>
|
||||||
|
|
||||||
|
inline NzBillboard::NzBillboard()
|
||||||
|
{
|
||||||
|
SetColor(NzColor::White);
|
||||||
|
SetDefaultMaterial();
|
||||||
|
SetRotation(0.f);
|
||||||
|
SetSize(64.f, 64.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline NzBillboard::NzBillboard(NzMaterialRef material)
|
||||||
|
{
|
||||||
|
SetColor(NzColor::White);
|
||||||
|
SetMaterial(std::move(material), true);
|
||||||
|
SetRotation(0.f);
|
||||||
|
SetSize(64.f, 64.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline NzBillboard::NzBillboard(NzTexture* texture)
|
||||||
|
{
|
||||||
|
SetColor(NzColor::White);
|
||||||
|
SetRotation(0.f);
|
||||||
|
SetSize(64.f, 64.f);
|
||||||
|
SetTexture(texture, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline NzBillboard::NzBillboard(const NzBillboard& billboard) :
|
||||||
|
NzInstancedRenderable(billboard),
|
||||||
|
m_color(billboard.m_color),
|
||||||
|
m_material(billboard.m_material),
|
||||||
|
m_rotation(billboard.m_rotation),
|
||||||
|
m_sinCos(billboard.m_sinCos),
|
||||||
|
m_size(billboard.m_size)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const NzColor& NzBillboard::GetColor() const
|
||||||
|
{
|
||||||
|
return m_color;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const NzMaterialRef& NzBillboard::GetMaterial() const
|
||||||
|
{
|
||||||
|
return m_material;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline float NzBillboard::GetRotation() const
|
||||||
|
{
|
||||||
|
return m_rotation;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const NzVector2f& NzBillboard::GetSize() const
|
||||||
|
{
|
||||||
|
return m_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzBillboard::SetColor(const NzColor& color)
|
||||||
|
{
|
||||||
|
m_color = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzBillboard::SetDefaultMaterial()
|
||||||
|
{
|
||||||
|
NzMaterialRef material = NzMaterial::New();
|
||||||
|
material->Enable(nzRendererParameter_FaceCulling, true);
|
||||||
|
material->EnableLighting(false);
|
||||||
|
|
||||||
|
SetMaterial(std::move(material));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzBillboard::SetMaterial(NzMaterialRef material, bool resizeBillboard)
|
||||||
|
{
|
||||||
|
m_material = std::move(material);
|
||||||
|
if (m_material && resizeBillboard)
|
||||||
|
{
|
||||||
|
NzTexture* diffuseMap = m_material->GetDiffuseMap();
|
||||||
|
if (diffuseMap && diffuseMap->IsValid())
|
||||||
|
SetSize(NzVector2f(NzVector2ui(diffuseMap->GetSize())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzBillboard::SetRotation(float rotation)
|
||||||
|
{
|
||||||
|
m_rotation = rotation;
|
||||||
|
m_sinCos.Set(std::sin(m_rotation), std::cos(m_rotation));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzBillboard::SetSize(const NzVector2f& size)
|
||||||
|
{
|
||||||
|
m_size = size;
|
||||||
|
|
||||||
|
// On invalide la bounding box
|
||||||
|
InvalidateBoundingVolume();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzBillboard::SetSize(float sizeX, float sizeY)
|
||||||
|
{
|
||||||
|
SetSize(NzVector2f(sizeX, sizeY));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void NzBillboard::SetTexture(NzTextureRef texture, bool resizeBillboard)
|
||||||
|
{
|
||||||
|
if (!m_material)
|
||||||
|
SetDefaultMaterial();
|
||||||
|
else if (m_material->GetReferenceCount() > 1)
|
||||||
|
m_material = NzMaterial::New(*m_material); // Copie
|
||||||
|
|
||||||
|
if (resizeBillboard && texture && texture->IsValid())
|
||||||
|
SetSize(NzVector2f(NzVector2ui(texture->GetSize())));
|
||||||
|
|
||||||
|
m_material->SetDiffuseMap(std::move(texture));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline NzBillboard& NzBillboard::operator=(const NzBillboard& billboard)
|
||||||
|
{
|
||||||
|
NzInstancedRenderable::operator=(billboard);
|
||||||
|
|
||||||
|
m_color = billboard.m_color;
|
||||||
|
m_material = billboard.m_material;
|
||||||
|
m_size = billboard.m_size;
|
||||||
|
|
||||||
|
InvalidateBoundingVolume();
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
NzBillboardRef NzBillboard::New(Args&&... args)
|
||||||
|
{
|
||||||
|
std::unique_ptr<NzBillboard> object(new NzBillboard(std::forward<Args>(args)...));
|
||||||
|
object->SetPersistent(false);
|
||||||
|
|
||||||
|
return object.release();
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Graphics/DebugOff.hpp>
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
// 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/Graphics/Billboard.hpp>
|
||||||
|
#include <Nazara/Graphics/AbstractRenderQueue.hpp>
|
||||||
|
#include <Nazara/Graphics/AbstractViewer.hpp>
|
||||||
|
#include <Nazara/Math/Algorithm.hpp>
|
||||||
|
#include <cstring>
|
||||||
|
#include <memory>
|
||||||
|
#include <Nazara/Graphics/Debug.hpp>
|
||||||
|
|
||||||
|
void NzBillboard::AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const InstanceData& instanceData) const
|
||||||
|
{
|
||||||
|
if (!m_material)
|
||||||
|
return;
|
||||||
|
|
||||||
|
renderQueue->AddBillboard(m_material, instanceData.transformMatrix.GetTranslation(), m_size, m_sinCos, m_color);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NzBillboard::MakeBoundingVolume() const
|
||||||
|
{
|
||||||
|
constexpr float sqrt2 = float(M_SQRT2);
|
||||||
|
|
||||||
|
m_boundingVolume.Set(NzVector3f(0.f), sqrt2*m_size.x*NzVector3f::Right() + sqrt2*m_size.y*NzVector3f::Down());
|
||||||
|
}
|
||||||
|
|
||||||
|
NzBillboardLibrary::LibraryMap NzBillboard::s_library;
|
||||||
Loading…
Reference in New Issue