Skip to content

Commit

Permalink
Initial commit of the grid renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Zang3th committed Apr 25, 2024
1 parent 675695b commit acd8fd3
Show file tree
Hide file tree
Showing 18 changed files with 228 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Apps/CellSim/CellSimApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace CS
}
Engine::Camera3D::Init();
Engine::CameraController3D::Init();
Engine::RenderManager::Init();
Engine::RenderManager::Init3D();

//Load shaders and textures
LoadResources();
Expand Down
2 changes: 1 addition & 1 deletion Apps/GreenWorld/GreenWorldApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace GW
}
Engine::Camera3D::Init();
Engine::CameraController3D::Init();
Engine::RenderManager::Init();
Engine::RenderManager::Init3D();

//Load shaders and textures
LoadResources();
Expand Down
32 changes: 8 additions & 24 deletions Apps/Liquefied/LiquefiedApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ namespace Liq
void LiquefiedApp::LoadResources()
{
//Shader
Engine::ResourceManager::LoadShader("SpriteShader", "../Res/Shader/GreenWorld/Sprite_VS.glsl", "../Res/Shader/GreenWorld/Sprite_FS.glsl");

//Texture
Engine::ResourceManager::LoadTextureToBuffer("BG_Texture", "../Res/Assets/Textures/Liquefied/Dark_Squares_1500.jpg");
Engine::ResourceManager::LoadShader("GridShader", "../Res/Shader/Liquefied/Grid_VS.glsl", "../Res/Shader/Liquefied/Grid_FS.glsl");
}

Engine::uint32 LiquefiedApp::InitModules()
Expand All @@ -21,19 +18,18 @@ namespace Liq
{
return EXIT_FAILURE;
}
Engine::RenderManager::Init();
Engine::RenderManager::Init2D();

//Load shaders and textures
LoadResources();

//Create pixel renderer
_pixelRenderer = Engine::RenderManager::AddPixelRenderer
//Create grid renderer
_gridRenderer = Engine::RenderManager::AddGridRenderer
(
Engine::LiquiefiedParams::SIMULATION_WIDTH,
Engine::LiquiefiedParams::SIMULATION_HEIGHT,
10,
"BG_Texture",
"SpriteShader"
"GridShader"
);

//Create UI
Expand Down Expand Up @@ -61,9 +57,7 @@ namespace Liq
color = Engine::Utility::GetScienticColor(val, 0.0f, 1.0f);
}

//Way to slow...
//ToDo: Build SpriteInstancer
//_pixelRenderer->Set(x, y, color);
//ToDo: Add GridRenderer-Set(...)
}
}
}
Expand Down Expand Up @@ -124,18 +118,8 @@ namespace Liq
{
Engine::PROFILE_SCOPE("Render pixels");

Engine::RenderManager::RenderPixels();
VisualizeSmoke();

//Test of the pixel renderer
/*if(Engine::Window::GetFrameCounter() == 0)
{
_pixelRenderer->SetScreen(COLOR_WHITE);
}
else if(Engine::Window::GetFrameCounter() == 80)
{
_pixelRenderer->ClearScreen();
}*/
_gridRenderer->Flush(nullptr);
//VisualizeSmoke();
}

{
Expand Down
2 changes: 1 addition & 1 deletion Apps/Liquefied/LiquefiedApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Liq
{
private:
double _timeElapsed = 0.0;
Engine::PixelRenderer* _pixelRenderer = nullptr;
Engine::GridRenderer* _gridRenderer = nullptr;
Engine::Scope<LiquefiedInterface> _interface;
Engine::Scope<Engine::FluidSimulator> _fluidSimulator;

Expand Down
9 changes: 6 additions & 3 deletions Apps/Liquefied/LiquefiedInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ namespace Liq

if(ImGui::Begin("Bufferbar", nullptr, _windowFlags))
{
ImGui::SetCursorPosX(25.0f);
ImGui::SetCursorPosY(25.0f);
ImGui::Checkbox("Scientific Colors", &Engine::LiquiefiedParams::scientificColorScheme);
ImGui::SetCursorPosX(25.0f);
ImGui::SetCursorPosY(25.0f);
ImGui::Checkbox("Wireframe Rendering", &Engine::UIParams::wireframeRendering);
ImGui::SetCursorPosY(25.0f);
ImGui::SetCursorPosX(250.0f);
ImGui::Checkbox("Scientific Colors", &Engine::LiquiefiedParams::scientificColorScheme);
}
ImGui::End();
}
Expand Down
2 changes: 1 addition & 1 deletion Engine/Application/Interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Engine
public:
float _windowAlphaValue = 0.5f;
float _menuBarHeight = 25.0f;
float _sidebarWidth = 415.0f;
float _sidebarWidth = 420.0f;

Interface();
virtual ~Interface();
Expand Down
108 changes: 108 additions & 0 deletions Engine/Rendering/Renderer/GridRenderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include "GridRenderer.hpp"

namespace Engine
{
// ----- Private -----

GridRenderer::GridRenderer
(
const uint32 width,
const uint32 height,
const uint32 quadSize,
const std::string& shader
)
: _gridWidth(width), _gridHeight(height), _quadSize(quadSize), _quadAmountTotal(_gridWidth * _gridHeight),
_defaultColor(glm::vec3(1.0f, 1.0f, 1.0f)), _shader(ResourceManager::GetShader(shader)),
_orthoProj(glm::ortho(0.0f, (float)WindowParams::WIDTH, 0.0f, (float)WindowParams::HEIGHT, -1.0f, 1.0f)),
_model(glm::scale(glm::mat4(1.0f), glm::vec3(glm::vec2((float)_quadSize), 0.0f)))
{
Logger::Info("Created", "Renderer", __func__);
InitGpuStorage();
}

void GridRenderer::InitGpuStorage()
{
//Create vertice data
static constexpr float vertices[] =
{
0.0f, 1.0f,
1.0f, 0.0f,
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f
};

//Create and bind vao
_vao = MakeScope<VertexArray>();
_vao->Bind();

//Create vbo's, send it data and configure vao
_vboVert = MakeScope<VertexBuffer>(vertices, sizeof(vertices), GL_STATIC_DRAW);
_vao->DefineAttributes(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), nullptr);

//Unbind everything
_vao->Unbind();
_vboVert->Unbind();
}

void UpdateGpuStorage()
{
//...
}

// ----- Public -----

void GridRenderer::Flush(Renderer* renderer)
{
//Check for Wireframe-Mode
if(UIParams::wireframeRendering)
GLRenderSettings::EnableWireframe();
else
GLRenderSettings::DisableWireframe();

//Bind shader
_shader->Bind();

//Bind vao and vbo
_vao->Bind();
_vboVert->Bind();

//Set uniforms
_shader->SetUniformVec3f("color", _defaultColor);
_shader->SetUniformMat4f("projection", _orthoProj);
_shader->SetUniformMat4f("model", _model);

//Render quad
GLCall(glDrawArraysInstanced(GL_TRIANGLES, 0, 6, _quadAmountTotal));

//Unbind vao and vbo
_vboVert->Unbind();
_vao->Unbind();

//Unbind shader
_shader->Unbind();

//ToDo: Update render stats
}

void GridRenderer::Set(const uint32 x, const uint32 y, const glm::vec3& color) const
{
//...
}

void GridRenderer::Reset(const uint32 x, const uint32 y) const
{
//...
}

void GridRenderer::SetScreen(const glm::vec3& color) const
{
//...
}

void GridRenderer::ClearScreen() const
{
//...
}
}
40 changes: 40 additions & 0 deletions Engine/Rendering/Renderer/GridRenderer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include "Renderer.hpp"
#include "glm.hpp"
#include "Sprite.hpp"
#include "Shader.hpp"
#include "GLRenderSettings.hpp"
#include "GlobalParams.hpp"
#include "ResourceManager.hpp"

namespace Engine
{
class GridRenderer final : public Renderer
{
friend class RenderManager;

private:
uint32 _gridWidth, _gridHeight, _quadSize, _quadAmountTotal;
glm::vec3 _defaultColor;
Shader* _shader;

Scope<VertexArray> _vao;
Scope<VertexBuffer> _vboVert, _vboColor;
glm::mat4 _orthoProj, _model;

std::vector<glm::vec3> _colorStorage;

void InitGpuStorage();
void UpdateGpuStorage();

public:
void Flush(Renderer* renderer) override;
void Set(uint32 x, uint32 y, const glm::vec3& color) const;
void Reset(uint32 x, uint32 y) const;
void SetScreen(const glm::vec3& color) const;
void ClearScreen() const;

GridRenderer(uint32 width, uint32 height, uint32 quadSize, const std::string& shader);
};
}
6 changes: 3 additions & 3 deletions Engine/Rendering/Renderer/ParticleRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ namespace Engine

void ParticleRenderer::InitGpuStorage()
{
//Create data
static const float vertices[] =
//Create vertice data
static constexpr float vertices[] =
{
-0.5f, 0.5f,
-0.5f, -0.5f,
Expand Down Expand Up @@ -112,7 +112,7 @@ namespace Engine
}
}

glm::mat4 ParticleRenderer::GetModelViewMatrix(Particle* particle)
glm::mat4 ParticleRenderer::GetModelViewMatrix(const Particle* particle)
{
//Create model matrix
glm::mat4 model(1.0f);
Expand Down
2 changes: 1 addition & 1 deletion Engine/Rendering/Renderer/ParticleRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace Engine
void InitGpuStorage();
void UpdateGpuStorage();
void GenerateParticles();
glm::mat4 GetModelViewMatrix(Particle* particle);
glm::mat4 GetModelViewMatrix(const Particle* particle);

public:
void Flush(Renderer* sceneRenderer) override;
Expand Down
8 changes: 1 addition & 7 deletions Engine/Rendering/Renderer/PixelRenderer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "PixelRenderer.hpp"

#include <ResourceManager.hpp>

namespace Engine
{
// ----- Private -----
Expand All @@ -27,8 +25,6 @@ namespace Engine

void PixelRenderer::Flush(Renderer* renderer)
{
GLRenderSettings::DisableCulling();

//Commit texture changes
_canvasSprite.GetTexture()->CommitModifications();

Expand All @@ -38,8 +34,6 @@ namespace Engine

//Increase render pass counter
RenderStatistics::spritePasses++;

GLRenderSettings::EnableCulling();
}

void PixelRenderer::Set(const uint32 x, const uint32 y, const glm::vec3& color) const
Expand Down Expand Up @@ -89,4 +83,4 @@ namespace Engine
{
ResetArea(0, _width * _pxSize, 0, _height * _pxSize);
}
}
}
3 changes: 1 addition & 2 deletions Engine/Rendering/Renderer/PixelRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
#include "Shader.hpp"
#include "GLRenderSettings.hpp"
#include "GlobalParams.hpp"

#include <vector>
#include "ResourceManager.hpp"

namespace Engine
{
Expand Down
22 changes: 21 additions & 1 deletion Engine/Rendering/Renderer/RenderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ namespace Engine
{
// ----- Public -----

void RenderManager::Init()
void RenderManager::Init2D()
{
_rendererStorage.clear();
_rendererStorage.reserve(5);
_initialized = true;
}

void RenderManager::Init3D()
{
_rendererStorage.clear();
_rendererStorage.reserve(10);
Expand Down Expand Up @@ -114,6 +121,14 @@ namespace Engine
return _pixelRenderer;
}

GridRenderer* RenderManager::AddGridRenderer(uint32 width, uint32 height, uint32 quadSize, const std::string& shader)
{
_gridRenderer = new GridRenderer(width, height, quadSize, shader);
_rendererStorage.push_back(_gridRenderer);

return _gridRenderer;
}

void RenderManager::RenderScene()
{
_sceneRenderer->Flush(nullptr);
Expand Down Expand Up @@ -148,4 +163,9 @@ namespace Engine
{
_pixelRenderer->Flush(nullptr);
}

void RenderManager::RenderGrid()
{
_gridRenderer->Flush(nullptr);
}
}
Loading

0 comments on commit acd8fd3

Please sign in to comment.