From 52611beeab98ed5bc3591847a074128e51060d06 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sun, 14 Jan 2024 20:24:17 +0100 Subject: [PATCH] Renderer: Move RenderCommand to separate file --- src/inferno/application.cpp | 1 + src/inferno/render/render-command.cpp | 73 +++++++++++++++++++++++++++ src/inferno/render/render-command.h | 31 ++++++++++++ src/inferno/render/renderer.cpp | 59 +--------------------- src/inferno/render/renderer.h | 18 ------- src/inferno/window.cpp | 1 + 6 files changed, 107 insertions(+), 76 deletions(-) create mode 100644 src/inferno/render/render-command.cpp create mode 100644 src/inferno/render/render-command.h diff --git a/src/inferno/application.cpp b/src/inferno/application.cpp index 2496035..61862ee 100644 --- a/src/inferno/application.cpp +++ b/src/inferno/application.cpp @@ -23,6 +23,7 @@ #include "inferno/render/context.h" #include "inferno/render/font.h" // #include "inferno/render/gltf.h" +#include "inferno/render/render-command.h" #include "inferno/render/renderer.h" #include "inferno/render/shader.h" #include "inferno/render/texture.h" diff --git a/src/inferno/render/render-command.cpp b/src/inferno/render/render-command.cpp new file mode 100644 index 0000000..5cc29dd --- /dev/null +++ b/src/inferno/render/render-command.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2024 Riyyi + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include "glad/glad.h" +#include "ruc/format/log.h" + +#include "inferno/render/buffer.h" +#include "inferno/render/render-command.h" + +namespace Inferno { + +void RenderCommand::initialize() +{ + setDepthTest(true); + + // Enable transparency + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_BLEND); + + ruc::info("RenderCommand initialized"); +} + +void RenderCommand::destroy() +{ +} + +void RenderCommand::clear() +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +} + +void RenderCommand::clearColor(const glm::vec4& color) +{ + glClearColor(color.r, color.g, color.b, color.a); +} + +void RenderCommand::drawIndexed(const VertexArray& vertexArray, uint32_t indexCount) +{ + uint32_t count = indexCount ? indexCount : vertexArray.getIndexBuffer()->getCount(); + glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, nullptr); +} + +void RenderCommand::setViewport(int32_t x, int32_t y, uint32_t width, uint32_t height) +{ + glViewport(x, y, width, height); +} + +void RenderCommand::setDepthTest(bool enabled) +{ + // Set z-buffer / depth buffer + enabled ? glEnable(GL_DEPTH_TEST) : glDisable(GL_DEPTH_TEST); +} + +bool RenderCommand::depthTest() +{ + unsigned char depthTest = GL_FALSE; + glGetBooleanv(GL_DEPTH_TEST, &depthTest); + return depthTest == GL_TRUE; +} + +int32_t RenderCommand::textureUnitAmount() +{ + int32_t amount = 0; + glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &amount); + return amount; +} + +} // namespace Inferno diff --git a/src/inferno/render/render-command.h b/src/inferno/render/render-command.h new file mode 100644 index 0000000..c653095 --- /dev/null +++ b/src/inferno/render/render-command.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2024 Riyyi + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include "glm/ext/matrix_float4x4.hpp" // glm::mat4 + +namespace Inferno { + +class VertexArray; + +class RenderCommand { +public: + static void initialize(); + static void destroy(); + + static void clear(); + static void clearColor(const glm::vec4& color); + static void drawIndexed(const VertexArray& vertexArray, uint32_t indexCount = 0); + + static void setViewport(int32_t x, int32_t y, uint32_t width, uint32_t height); + static void setDepthTest(bool enabled); + + static bool depthTest(); + static int32_t textureUnitAmount(); +}; + +} // namespace Inferno diff --git a/src/inferno/render/renderer.cpp b/src/inferno/render/renderer.cpp index 908f68d..869597f 100644 --- a/src/inferno/render/renderer.cpp +++ b/src/inferno/render/renderer.cpp @@ -11,70 +11,13 @@ #include "ruc/format/log.h" #include "inferno/render/buffer.h" +#include "inferno/render/render-command.h" #include "inferno/render/renderer.h" #include "inferno/render/shader.h" #include "inferno/render/texture.h" namespace Inferno { -void RenderCommand::initialize() -{ - setDepthTest(true); - - // Enable transparency - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_BLEND); - - ruc::info("RenderCommand initialized"); -} - -void RenderCommand::destroy() -{ -} - -void RenderCommand::clear() -{ - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); -} - -void RenderCommand::clearColor(const glm::vec4& color) -{ - glClearColor(color.r, color.g, color.b, color.a); -} - -void RenderCommand::drawIndexed(const VertexArray& vertexArray, uint32_t indexCount) -{ - uint32_t count = indexCount ? indexCount : vertexArray.getIndexBuffer()->getCount(); - glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, nullptr); -} - -void RenderCommand::setViewport(int32_t x, int32_t y, uint32_t width, uint32_t height) -{ - glViewport(x, y, width, height); -} - -void RenderCommand::setDepthTest(bool enabled) -{ - // Set z-buffer / depth buffer - enabled ? glEnable(GL_DEPTH_TEST) : glDisable(GL_DEPTH_TEST); -} - -bool RenderCommand::depthTest() -{ - unsigned char depthTest = GL_FALSE; - glGetBooleanv(GL_DEPTH_TEST, &depthTest); - return depthTest == GL_TRUE; -} - -int32_t RenderCommand::textureUnitAmount() -{ - int32_t amount = 0; - glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &amount); - return amount; -} - -// ----------------------------------------- - uint32_t Renderer::m_supportedTextureUnitPerBatch = 0; void Renderer::initialize() diff --git a/src/inferno/render/renderer.h b/src/inferno/render/renderer.h index 77ea0eb..ce2795f 100644 --- a/src/inferno/render/renderer.h +++ b/src/inferno/render/renderer.h @@ -52,24 +52,6 @@ struct CharacterVertex { // ------------------------------------- -class RenderCommand { -public: - static void initialize(); - static void destroy(); - - static void clear(); - static void clearColor(const glm::vec4& color); - static void drawIndexed(const VertexArray& vertexArray, uint32_t indexCount = 0); - - static void setViewport(int32_t x, int32_t y, uint32_t width, uint32_t height); - static void setDepthTest(bool enabled); - - static bool depthTest(); - static int32_t textureUnitAmount(); -}; - -// ------------------------------------- - class Renderer { public: static constexpr const uint32_t vertexPerQuad = 4; diff --git a/src/inferno/window.cpp b/src/inferno/window.cpp index 0a0e960..5b0ff01 100644 --- a/src/inferno/window.cpp +++ b/src/inferno/window.cpp @@ -18,6 +18,7 @@ #include "inferno/io/input.h" #include "inferno/keycodes.h" #include "inferno/render/context.h" +#include "inferno/render/render-command.h" #include "inferno/render/renderer.h" #include "inferno/settings.h" #include "inferno/window.h"