Skip to content

Commit

Permalink
Added a basic grid renderer for XZ plane. #56
Browse files Browse the repository at this point in the history
  • Loading branch information
MStachowicz committed Jun 12, 2023
1 parent 90ee242 commit c288758
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ PRIVATE ImGui
add_library(OpenGL
source/OpenGL/OpenGLRenderer.hpp
source/OpenGL/OpenGLRenderer.cpp
source/OpenGL/GridRenderer.hpp
source/OpenGL/GridRenderer.cpp
source/OpenGL/DebugRenderer.hpp
source/OpenGL/DebugRenderer.cpp
source/OpenGL/GLState.hpp
Expand Down
1 change: 1 addition & 0 deletions source/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Application::Application(Platform::Input& p_input, Platform::Window& p_window) n
, mMeshSystem{mTextureSystem}
, mSceneSystem{mTextureSystem, mMeshSystem}
, mOpenGLRenderer{m_window, mSceneSystem, mMeshSystem, mTextureSystem}
, m_grid_renderer{}
, mCollisionSystem{mSceneSystem, mMeshSystem}
, mPhysicsSystem{mSceneSystem, mCollisionSystem}
, mInputSystem{m_input, m_window, mSceneSystem}
Expand Down
3 changes: 3 additions & 0 deletions source/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// OpenGL
#include "OpenGLRenderer.hpp"
#include "DebugRenderer.hpp"
#include "OpenGL/GridRenderer.hpp"

// Utility
#include "File.hpp"
Expand Down Expand Up @@ -50,6 +51,7 @@ class Application
System::SceneSystem mSceneSystem;

OpenGL::OpenGLRenderer mOpenGLRenderer;
OpenGL::GridRenderer m_grid_renderer;

System::CollisionSystem mCollisionSystem;
System::PhysicsSystem mPhysicsSystem;
Expand Down Expand Up @@ -129,6 +131,7 @@ class Application
m_window.start_ImGui_frame();
mOpenGLRenderer.start_frame();

m_grid_renderer.draw();
mOpenGLRenderer.draw();
mEditor.draw(durationSinceLastRenderTick);
OpenGL::DebugRenderer::render(mSceneSystem);
Expand Down
9 changes: 9 additions & 0 deletions source/OpenGL/GLSL/grid.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#version 330 core

in vec4 FragmentColour;
out vec4 Colour;

void main()
{
Colour = FragmentColour;
}
19 changes: 19 additions & 0 deletions source/OpenGL/GLSL/grid.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 420 core

layout (location = 0) in vec3 VertexPosition;

layout(shared) uniform ViewProperties
{
mat4 view;
mat4 projection;
} viewProperties;

uniform vec4 colour;

out vec4 FragmentColour;

void main()
{
gl_Position = viewProperties.projection * viewProperties.view * vec4(VertexPosition, 1.0);
FragmentColour = colour;
}
44 changes: 44 additions & 0 deletions source/OpenGL/GridRenderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "GridRenderer.hpp"

namespace OpenGL
{
GridRenderer::GridRenderer() noexcept
: m_line_points{}
, m_colour{1.f, 1.f, 1.f, 0.7f}
, m_grid_shader{"grid"}
, m_line_VAO{}
, m_line_VBO{}
{
constexpr int Count = 1000;
constexpr int Size = 100; // #Todo Should be camera z-far
m_line_points.reserve(Count * 8);

// XZ-plane
for (auto i = -Count; i <= Count; i++)
{
m_line_points.push_back({glm::vec3{-Size, 0.f, i}});
m_line_points.push_back({glm::vec3{Size, 0.f, i}});
m_line_points.push_back({glm::vec3{i, 0.f, Size}});
m_line_points.push_back({glm::vec3{i, 0.f, -Size}});
}

m_line_VAO.bind();
m_line_VBO.set_data(m_line_points);
}
void GridRenderer::draw()
{
if (!m_line_points.empty())
{
m_grid_shader.use();
m_grid_shader.set_uniform("colour", m_colour);
toggle_cull_face(false);
set_depth_test(true);
set_depth_test_type(DepthTestType::Less);
set_polygon_mode(PolygonMode::Fill);

m_line_VAO.bind();
m_line_VBO.bind();
draw_arrays(PrimitiveMode::Lines, 0, static_cast<GLsizei>(m_line_points.size() * 2));
}
}
}
30 changes: 30 additions & 0 deletions source/OpenGL/GridRenderer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "OpenGL/Types.hpp"
#include "OpenGL/Shader.hpp"

#include "glm/vec3.hpp"
#include "glm/vec4.hpp"

#include <array>
#include <vector>

namespace OpenGL
{
class GridRenderer
{
struct GridVert
{
constexpr inline static std::array<VertexAttribute, 1> Attributes = { VertexAttribute::Position3D };
glm::vec3 m_position = glm::vec3{0.f};
};

std::vector<GridVert> m_line_points;
glm::vec4 m_colour;
Shader m_grid_shader;
VAO m_line_VAO;
VBO m_line_VBO;

public:
GridRenderer() noexcept;
void draw();
};
}

0 comments on commit c288758

Please sign in to comment.