Ndk: Add support for 2D rendering

Former-commit-id: bdf9257816c8f48b8c0679647978480785720053
This commit is contained in:
Lynix
2015-09-18 12:34:05 +02:00
parent dd7afa970f
commit 859544eaa7
10 changed files with 140 additions and 11 deletions

View File

@@ -116,9 +116,21 @@ namespace Ndk
void CameraComponent::UpdateProjectionMatrix() const
{
EnsureViewportUpdate(); // Can affect aspect ratio
switch (m_projectionType)
{
case nzProjectionType_Orthogonal:
EnsureViewportUpdate();
m_projectionMatrix.MakeOrtho(0.f, static_cast<float>(m_viewport.width), 0.f, static_cast<float>(m_viewport.height), m_zNear, m_zFar);
break;
case nzProjectionType_Perspective:
EnsureViewportUpdate(); // Can affect aspect ratio
m_projectionMatrix.MakePerspective(m_fov, m_aspectRatio, m_zNear, m_zFar);
break;
}
m_projectionMatrix.MakePerspective(m_fov, m_aspectRatio, m_zNear, m_zFar);
m_projectionMatrixUpdated = true;
}
@@ -153,7 +165,8 @@ namespace Ndk
{
m_aspectRatio = aspectRatio;
InvalidateProjectionMatrix();
if (m_projectionType == nzProjectionType_Perspective)
InvalidateProjectionMatrix();
}
// Convert it back to int

View File

@@ -3,15 +3,16 @@
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
#include <NDK/Components/GraphicsComponent.hpp>
#include <NDK/World.hpp>
#include <NDK/Systems/RenderSystem.hpp>
#include <NDK/Components/NodeComponent.hpp>
namespace Ndk
{
void GraphicsComponent::InvalidateRenderableData(const NzInstancedRenderable* renderable, nzUInt32 flags, unsigned int index)
{
NazaraUnused(renderable);
NazaraAssert(index < m_renderables.size(), "Invalid renderable index");
NazaraUnused(renderable);
Renderable& r = m_renderables[index];
r.dataUpdated = false;
@@ -66,7 +67,9 @@ namespace Ndk
{
NazaraAssert(m_entity && m_entity->HasComponent<NodeComponent>(), "GraphicsComponent requires NodeComponent");
m_transformMatrix = m_entity->GetComponent<NodeComponent>().GetTransformMatrix();
Ndk::RenderSystem& renderSystem = m_entity->GetWorld()->GetSystem<Ndk::RenderSystem>();
m_transformMatrix = renderSystem.GetCoordinateSystemMatrix() * m_entity->GetComponent<NodeComponent>().GetTransformMatrix();
m_transformMatrixUpdated = true;
}

View File

@@ -11,7 +11,9 @@
namespace Ndk
{
RenderSystem::RenderSystem()
RenderSystem::RenderSystem() :
m_coordinateSystemMatrix(NzMatrix4f::Identity()),
m_coordinateSystemInvalidated(true)
{
SetDefaultBackground(NzColorBackground::New());
SetUpdateRate(0.f);
@@ -54,6 +56,18 @@ namespace Ndk
{
NazaraUnused(elapsedTime);
// Invalidate every renderable if the coordinate system changed
if (m_coordinateSystemInvalidated)
{
for (const Ndk::EntityHandle& drawable : m_drawables)
{
GraphicsComponent& graphicsComponent = drawable->GetComponent<GraphicsComponent>();
graphicsComponent.InvalidateTransformMatrix();
}
m_coordinateSystemInvalidated = false;
}
for (const Ndk::EntityHandle& camera : m_cameras)
{
CameraComponent& camComponent = camera->GetComponent<CameraComponent>();
@@ -76,7 +90,8 @@ namespace Ndk
LightComponent& lightComponent = light->GetComponent<LightComponent>();
NodeComponent& drawableNode = light->GetComponent<NodeComponent>();
lightComponent.AddToRenderQueue(renderQueue, drawableNode.GetTransformMatrix());
///TODO: Cache somehow?
lightComponent.AddToRenderQueue(renderQueue, m_coordinateSystemMatrix * drawableNode.GetTransformMatrix());
}
NzSceneData sceneData;