Graphics: Add Billboard support
This commit is contained in:
@@ -32,6 +32,7 @@
|
||||
#include <Nazara/Graphics/AbstractViewer.hpp>
|
||||
#include <Nazara/Graphics/Algorithm.hpp>
|
||||
#include <Nazara/Graphics/BakedFrameGraph.hpp>
|
||||
#include <Nazara/Graphics/Billboard.hpp>
|
||||
#include <Nazara/Graphics/Camera.hpp>
|
||||
#include <Nazara/Graphics/Config.hpp>
|
||||
#include <Nazara/Graphics/DebugDrawPipelinePass.hpp>
|
||||
|
||||
66
include/Nazara/Graphics/Billboard.hpp
Normal file
66
include/Nazara/Graphics/Billboard.hpp
Normal file
@@ -0,0 +1,66 @@
|
||||
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// 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_GRAPHICS_BILLBOARD_HPP
|
||||
#define NAZARA_GRAPHICS_BILLBOARD_HPP
|
||||
|
||||
#include <NazaraUtils/Prerequisites.hpp>
|
||||
#include <Nazara/Graphics/Config.hpp>
|
||||
#include <Nazara/Graphics/InstancedRenderable.hpp>
|
||||
#include <Nazara/Renderer/RenderPipeline.hpp>
|
||||
#include <Nazara/Utility/VertexDeclaration.hpp>
|
||||
#include <Nazara/Utility/VertexStruct.hpp>
|
||||
#include <array>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_GRAPHICS_API Billboard : public InstancedRenderable
|
||||
{
|
||||
public:
|
||||
Billboard(std::shared_ptr<MaterialInstance> material);
|
||||
Billboard(std::shared_ptr<MaterialInstance> material, const Vector2f& size);
|
||||
Billboard(const Billboard&) = delete;
|
||||
Billboard(Billboard&&) noexcept = default;
|
||||
~Billboard() = default;
|
||||
|
||||
void BuildElement(ElementRendererRegistry& registry, const ElementData& elementData, std::size_t passIndex, std::vector<RenderElementOwner>& elements) const override;
|
||||
|
||||
inline const Color& GetColor() const;
|
||||
inline const Color& GetCornerColor(RectCorner corner) const;
|
||||
const std::shared_ptr<MaterialInstance>& GetMaterial(std::size_t i = 0) const override;
|
||||
std::size_t GetMaterialCount() const override;
|
||||
inline RadianAnglef GetRotation() const;
|
||||
inline const Vector2f& GetSize() const;
|
||||
inline const Rectf& GetTextureCoords() const;
|
||||
Vector3ui GetTextureSize() const;
|
||||
|
||||
inline void SetColor(const Color& color);
|
||||
inline void SetCornerColor(RectCorner corner, const Color& color);
|
||||
inline void SetMaterial(std::shared_ptr<MaterialInstance> material);
|
||||
inline void SetRotation(RadianAnglef rotation);
|
||||
inline void SetSize(const Vector2f& size);
|
||||
inline void SetTextureCoords(const Rectf& textureCoords);
|
||||
inline void SetTextureRect(const Rectf& textureRect);
|
||||
|
||||
Billboard& operator=(const Billboard&) = delete;
|
||||
Billboard& operator=(Billboard&&) noexcept = default;
|
||||
|
||||
private:
|
||||
inline void UpdateVertices();
|
||||
|
||||
std::array<VertexStruct_UV_SizeSinCos_Color, 4> m_vertices;
|
||||
std::shared_ptr<MaterialInstance> m_material;
|
||||
Color m_color;
|
||||
EnumArray<RectCorner, Color> m_cornerColor;
|
||||
RadianAnglef m_rotation;
|
||||
Rectf m_textureCoords;
|
||||
Vector2f m_size;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Graphics/Billboard.inl>
|
||||
|
||||
#endif // NAZARA_GRAPHICS_BILLBOARD_HPP
|
||||
117
include/Nazara/Graphics/Billboard.inl
Normal file
117
include/Nazara/Graphics/Billboard.inl
Normal file
@@ -0,0 +1,117 @@
|
||||
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Graphics module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <cassert>
|
||||
#include <Nazara/Graphics/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline const Color& Billboard::GetColor() const
|
||||
{
|
||||
return m_color;
|
||||
}
|
||||
|
||||
inline const Color& Billboard::GetCornerColor(RectCorner corner) const
|
||||
{
|
||||
return m_cornerColor[corner];
|
||||
}
|
||||
|
||||
inline RadianAnglef Billboard::GetRotation() const
|
||||
{
|
||||
return m_rotation;
|
||||
}
|
||||
|
||||
inline const Rectf& Billboard::GetTextureCoords() const
|
||||
{
|
||||
return m_textureCoords;
|
||||
}
|
||||
|
||||
inline const Vector2f& Billboard::GetSize() const
|
||||
{
|
||||
return m_size;
|
||||
}
|
||||
|
||||
inline void Billboard::SetColor(const Color& color)
|
||||
{
|
||||
m_color = color;
|
||||
|
||||
UpdateVertices();
|
||||
}
|
||||
|
||||
inline void Billboard::SetCornerColor(RectCorner corner, const Color& color)
|
||||
{
|
||||
m_cornerColor[corner] = color;
|
||||
|
||||
UpdateVertices();
|
||||
}
|
||||
|
||||
inline void Billboard::SetMaterial(std::shared_ptr<MaterialInstance> material)
|
||||
{
|
||||
assert(material);
|
||||
|
||||
if (m_material != material)
|
||||
{
|
||||
OnMaterialInvalidated(this, 0, material);
|
||||
m_material = std::move(material);
|
||||
|
||||
OnElementInvalidated(this);
|
||||
}
|
||||
}
|
||||
|
||||
inline void Billboard::SetRotation(RadianAnglef rotation)
|
||||
{
|
||||
m_rotation = rotation;
|
||||
|
||||
UpdateVertices();
|
||||
}
|
||||
|
||||
inline void Billboard::SetSize(const Vector2f& size)
|
||||
{
|
||||
m_size = size;
|
||||
|
||||
UpdateVertices();
|
||||
}
|
||||
|
||||
inline void Billboard::SetTextureCoords(const Rectf& textureCoords)
|
||||
{
|
||||
m_textureCoords = textureCoords;
|
||||
|
||||
UpdateVertices();
|
||||
}
|
||||
|
||||
inline void Billboard::SetTextureRect(const Rectf& textureRect)
|
||||
{
|
||||
Vector2f invTextureSize = 1.f / Vector2f(Vector2ui(GetTextureSize()));
|
||||
return SetTextureCoords(Rectf(textureRect.x * invTextureSize.x, textureRect.y * invTextureSize.y, textureRect.width * invTextureSize.x, textureRect.height * invTextureSize.y));
|
||||
}
|
||||
|
||||
inline void Billboard::UpdateVertices()
|
||||
{
|
||||
VertexStruct_UV_SizeSinCos_Color* vertices = m_vertices.data();
|
||||
|
||||
EnumArray<RectCorner, Vector2f> cornerExtent;
|
||||
cornerExtent[RectCorner::LeftBottom] = Vector2f(0.f, 0.f);
|
||||
cornerExtent[RectCorner::RightBottom] = Vector2f(1.f, 0.f);
|
||||
cornerExtent[RectCorner::LeftTop] = Vector2f(0.f, 1.f);
|
||||
cornerExtent[RectCorner::RightTop] = Vector2f(1.f, 1.f);
|
||||
|
||||
auto [sin, cos] = m_rotation.GetSinCos();
|
||||
|
||||
for (RectCorner corner : { RectCorner::LeftBottom, RectCorner::RightBottom, RectCorner::LeftTop, RectCorner::RightTop })
|
||||
{
|
||||
vertices->color = m_color * m_cornerColor[corner];
|
||||
vertices->sizeSinCos = Vector4f(m_size.x, m_size.y, sin, cos);
|
||||
vertices->uv = m_textureCoords.GetCorner(corner);
|
||||
|
||||
vertices++;
|
||||
}
|
||||
|
||||
Vector2f halfSize = m_size * 0.5f;
|
||||
float maxExtent = std::max(halfSize.x, halfSize.y);
|
||||
UpdateAABB(Boxf(-maxExtent, -maxExtent, -maxExtent, maxExtent * 2.0f, maxExtent * 2.0f, maxExtent * 2.0f));
|
||||
OnElementInvalidated(this);
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Graphics/DebugOff.hpp>
|
||||
@@ -390,6 +390,7 @@ namespace Nz
|
||||
JointWeights,
|
||||
Normal,
|
||||
Position,
|
||||
SizeSinCos,
|
||||
Tangent,
|
||||
TexCoord,
|
||||
Userdata,
|
||||
@@ -406,6 +407,7 @@ namespace Nz
|
||||
enum class VertexLayout
|
||||
{
|
||||
// Predefined declarations for rendering
|
||||
UV_SizeSinCos,
|
||||
XY,
|
||||
XY_Color,
|
||||
XY_UV,
|
||||
@@ -416,6 +418,7 @@ namespace Nz
|
||||
XYZ_Normal_UV,
|
||||
XYZ_Normal_UV_Tangent,
|
||||
XYZ_Normal_UV_Tangent_Skinning,
|
||||
UV_SizeSinCos_Color,
|
||||
XYZ_UV,
|
||||
|
||||
// Predefined declarations for instancing
|
||||
|
||||
@@ -68,6 +68,17 @@ namespace Nz
|
||||
Vector3f tangent;
|
||||
};
|
||||
|
||||
struct VertexStruct_UV_SizeSinCos
|
||||
{
|
||||
Vector2f uv;
|
||||
Vector4f sizeSinCos; //< width, height, sin, cos
|
||||
};
|
||||
|
||||
struct VertexStruct_UV_SizeSinCos_Color : VertexStruct_UV_SizeSinCos
|
||||
{
|
||||
Color color;
|
||||
};
|
||||
|
||||
struct VertexStruct_XYZ_UV : VertexStruct_XYZ
|
||||
{
|
||||
Vector2f uv;
|
||||
|
||||
Reference in New Issue
Block a user