Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of core::gl::camera #590

Merged
merged 5 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions core/include/cubos/core/gl/camera.hpp

This file was deleted.

20 changes: 20 additions & 0 deletions engine/include/cubos/engine/renderer/camera.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// @file
/// @brief Component @ref cubos::engine::Camera.
/// @ingroup renderer-plugin

#pragma once

#include <glm/glm.hpp>

namespace cubos::engine
{
/// @brief Component which defines parameters of a camera used to render the world.
/// @note Should be used with @ref LocalToWorld.
/// @ingroup renderer-plugin
struct [[cubos::component("cubos/camera", VecStorage)]] Camera
{
float fovY; ///< Vertical field of view in degrees.
float zNear; ///< Near clipping plane.
float zFar; ///< Far clipping plane.
};
} // namespace cubos::engine
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace cubos::engine
// Implement interface methods.

void onResize(glm::uvec2 size) override;
void onRender(const core::gl::Camera& camera, const RendererFrame& frame,
void onRender(const glm::mat4& view, const Viewport& viewport, const Camera& camera, const RendererFrame& frame,
core::gl::Framebuffer target) override;

private:
Expand Down
10 changes: 0 additions & 10 deletions engine/include/cubos/engine/renderer/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,6 @@ namespace cubos::engine
[[cubos::ignore]] RendererGrid handle = nullptr; ///< Handle to the uploaded grid - set automatically.
};

/// @brief Component which defines parameters of a camera used to render the world.
/// @note Should be used with @ref LocalToWorld.
/// @ingroup renderer-plugin
struct [[cubos::component("cubos/camera", VecStorage)]] Camera
{
float fovY; ///< Vertical field of view in degrees.
float zNear; ///< Near clipping plane.
float zFar; ///< Far clipping plane.
};

/// @brief Resource which identifies the camera entities to be used by the renderer.
/// @ingroup renderer-plugin
struct ActiveCameras
Expand Down
20 changes: 16 additions & 4 deletions engine/include/cubos/engine/renderer/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

#include <glm/glm.hpp>

#include <cubos/core/gl/camera.hpp>
#include <cubos/core/gl/grid.hpp>
#include <cubos/core/gl/palette.hpp>
#include <cubos/core/gl/render_device.hpp>
#include <cubos/core/gl/vertex.hpp>
#include <cubos/core/io/window.hpp>

#include <cubos/engine/renderer/camera.hpp>
#include <cubos/engine/renderer/pps/manager.hpp>

namespace cubos::engine
Expand Down Expand Up @@ -53,6 +53,13 @@ namespace cubos::engine
class BaseRenderer
{
public:
/// @brief Struct which holds the viewport information for a camera, to be used for drawing.
struct Viewport
{
glm::ivec2 position;
glm::ivec2 size;
};

virtual ~BaseRenderer() = default;

/// @brief Constructs.
Expand Down Expand Up @@ -82,11 +89,14 @@ namespace cubos::engine
glm::uvec2 size() const;

/// @brief Draws a frame.
/// @param view Camera view transform.
/// @param viewport Camera viewport.
/// @param camera Camera to use.
/// @param frame Frame to draw.
/// @param usePostProcessing Whether to use post processing.
/// @param target Target framebuffer to draw to.
void render(const core::gl::Camera& camera, const RendererFrame& frame, bool usePostProcessing = true,
void render(const glm::mat4& view, const Viewport& viewport, const engine::Camera& camera,
const RendererFrame& frame, bool usePostProcessing = true,
const core::gl::Framebuffer& target = nullptr);

/// @brief Gets a reference to the post processing manager.
Expand All @@ -109,11 +119,13 @@ namespace cubos::engine
/// When post processing is enabled, the target framebuffer will be the internal texture
/// which will be used for post processing.
///
/// @param view Camera view transform.
/// @param viewport Camera viewport.
/// @param camera Camera to use.
/// @param frame Frame to draw.
/// @param target Target framebuffer.
virtual void onRender(const core::gl::Camera& camera, const RendererFrame& frame,
core::gl::Framebuffer target) = 0;
virtual void onRender(const glm::mat4& view, const Viewport& viewport, const Camera& camera,
const RendererFrame& frame, core::gl::Framebuffer target) = 0;

private:
/// @brief Called when the internal texture used for post processing needs to be resized.
Expand Down
6 changes: 2 additions & 4 deletions engine/samples/voxels/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <cubos/engine/settings/settings.hpp>
#include <cubos/engine/renderer/directional_light.hpp>
#include <cubos/engine/renderer/plugin.hpp>
#include <cubos/engine/settings/settings.hpp>
#include <cubos/engine/transform/plugin.hpp>
#include <cubos/engine/voxels/plugin.hpp>

Expand Down Expand Up @@ -57,9 +57,7 @@ static void spawnCarSystem(Commands cmds, Read<Assets> assets)
glm::vec3 offset = glm::vec3(car->size().x, 0.0F, car->size().z) / -2.0F;

// Create the car entity
cmds.create()
.add(RenderableGrid{CarAsset, offset})
.add(LocalToWorld{});
cmds.create().add(RenderableGrid{CarAsset, offset}).add(LocalToWorld{});
}
/// [Spawn car system]

Expand Down
18 changes: 9 additions & 9 deletions engine/src/cubos/engine/renderer/deferred_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,8 @@ void DeferredRenderer::onResize(glm::uvec2 size)
}
}

void DeferredRenderer::onRender(const Camera& camera, const RendererFrame& frame, Framebuffer target)
void DeferredRenderer::onRender(const glm::mat4& view, const Viewport& viewport, const Camera& camera,
const RendererFrame& frame, Framebuffer target)
{
// Steps:
// 1. Prepare the MVP matrix.
Expand All @@ -624,9 +625,9 @@ void DeferredRenderer::onRender(const Camera& camera, const RendererFrame& frame

// 1. Prepare the MVP matrix.
MVP mvp;
mvp.v = camera.view;
mvp.p = glm::perspective(glm::radians(camera.fovY), float(camera.viewportSize.x) / float(camera.viewportSize.y),
camera.zNear, camera.zFar);
mvp.v = view;
mvp.p = glm::perspective(glm::radians(camera.fovY), float(viewport.size.x) / float(viewport.size.y), camera.zNear,
camera.zFar);

// 2. Fill the light buffer with the light data.
// First map the buffer.
Expand Down Expand Up @@ -696,8 +697,7 @@ void DeferredRenderer::onRender(const Camera& camera, const RendererFrame& frame
mLightsBuffer->unmap();

// 3. Set the renderer state.
mRenderDevice.setViewport(camera.viewportPosition.x, camera.viewportPosition.y, camera.viewportSize.x,
camera.viewportSize.y);
mRenderDevice.setViewport(viewport.position.x, viewport.position.y, viewport.size.x, viewport.size.y);

// 4. Geometry pass.
// 4.1. Set the geometry pass state.
Expand Down Expand Up @@ -787,9 +787,9 @@ void DeferredRenderer::onRender(const Camera& camera, const RendererFrame& frame
mSsaoTexBp->bind(mSampler);
}
mUVScaleBp->setConstant(
glm::vec2((float)camera.viewportSize.x / (float)mSize.x, (float)camera.viewportSize.y / (float)mSize.y));
mUVOffsetBp->setConstant(glm::vec2((float)camera.viewportPosition.x / (float)mSize.x,
(float)camera.viewportPosition.y / (float)mSize.y));
glm::vec2((float)viewport.size.x / (float)mSize.x, (float)viewport.size.y / (float)mSize.y));
mUVOffsetBp->setConstant(
glm::vec2((float)viewport.position.x / (float)mSize.x, (float)viewport.position.y / (float)mSize.y));
mSkyGradientBottomBp->setConstant(frame.skyGradient(0));
mSkyGradientTopBp->setConstant(frame.skyGradient(1));
mInvVBp->setConstant(glm::inverse(mvp.v));
Expand Down
24 changes: 13 additions & 11 deletions engine/src/cubos/engine/renderer/plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <cubos/core/ecs/query.hpp>
#include <cubos/core/gl/camera.hpp>

#include <cubos/engine/renderer/deferred_renderer.hpp>
#include <cubos/engine/renderer/directional_light.hpp>
Expand Down Expand Up @@ -96,13 +95,13 @@ static void frameEnvironment(Write<RendererFrame> frame, Read<RendererEnvironmen
/// @param position Viewport position.
/// @param size Viewport size.
/// @param count How many cameras need to be fitted in to the given viewport.
/// @param cameras Output array where the viewports will be set.
static void splitViewport(glm::ivec2 position, glm::ivec2 size, int count, cubos::core::gl::Camera* cameras)
/// @param viewport Output array where the viewports will be set.
static void splitViewport(glm::ivec2 position, glm::ivec2 size, int count, BaseRenderer::Viewport* viewports)
{
if (count == 1)
{
cameras[0].viewportPosition = position;
cameras[0].viewportSize = size;
viewports[0].position = position;
viewports[0].size = size;
}
else if (count >= 2)
{
Expand All @@ -121,15 +120,18 @@ static void splitViewport(glm::ivec2 position, glm::ivec2 size, int count, cubos
splitOffset = {0, size.y / 2};
}

splitViewport(position, splitSize, count / 2, cameras);
splitViewport(position + splitOffset, splitSize, (count + 1) / 2, &cameras[count / 2]);
splitViewport(position, splitSize, count / 2, viewports);
splitViewport(position + splitOffset, splitSize, (count + 1) / 2, &viewports[count / 2]);
}
}

static void draw(Write<Renderer> renderer, Read<ActiveCameras> activeCameras, Write<RendererFrame> frame,
Query<Read<LocalToWorld>, Read<Camera>> query)
{
cubos::core::gl::Camera cameras[4]{};
Camera cameras[4]{};
glm::mat4 views[4]{};
BaseRenderer::Viewport viewports[4]{};

int cameraCount = 0;

RiscadoA marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < 4; ++i) // NOLINT(modernize-loop-convert)
Expand All @@ -145,12 +147,12 @@ static void draw(Write<Renderer> renderer, Read<ActiveCameras> activeCameras, Wr
cameras[cameraCount].fovY = camera->fovY;
cameras[cameraCount].zNear = camera->zNear;
cameras[cameraCount].zFar = camera->zFar;
cameras[cameraCount].view = glm::inverse(localToWorld->mat);
views[cameraCount] = glm::inverse(localToWorld->mat);
cameraCount += 1;
}
}

splitViewport({0, 0}, (*renderer)->size(), cameraCount, cameras);
splitViewport({0, 0}, (*renderer)->size(), cameraCount, viewports);

if (cameraCount == 0)
{
Expand All @@ -159,7 +161,7 @@ static void draw(Write<Renderer> renderer, Read<ActiveCameras> activeCameras, Wr

for (int i = 0; i < cameraCount; ++i)
{
(*renderer)->render(cameras[i], *frame);
(*renderer)->render(views[i], viewports[i], cameras[i], *frame);
}

frame->clear();
Expand Down
8 changes: 4 additions & 4 deletions engine/src/cubos/engine/renderer/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ glm::uvec2 BaseRenderer::size() const
return mSize;
}

void BaseRenderer::render(const core::gl::Camera& camera, const RendererFrame& frame, bool usePostProcessing,
const core::gl::Framebuffer& target)
void BaseRenderer::render(const glm::mat4& view, const Viewport& viewport, const engine::Camera& camera,
const RendererFrame& frame, bool usePostProcessing, const core::gl::Framebuffer& target)
{
if (usePostProcessing && mPpsManager.passCount() > 0)
{
this->onRender(camera, frame, mFramebuffer);
this->onRender(view, viewport, camera, frame, mFramebuffer);
mPpsManager.provideInput(PostProcessingInput::Lighting, mTexture);
mPpsManager.execute(target);
}
else
{
this->onRender(camera, frame, target);
this->onRender(view, viewport, camera, frame, target);
}
}

Expand Down
Loading