Skip to content

Commit

Permalink
Added RGB cardinal-axis lines and brighter lines every 10 units. #56
Browse files Browse the repository at this point in the history
  • Loading branch information
MStachowicz committed Jun 13, 2023
1 parent c288758 commit 49e05ca
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
5 changes: 2 additions & 3 deletions source/OpenGL/GLSL/grid.vert
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#version 420 core

layout (location = 0) in vec3 VertexPosition;
layout (location = 4) in vec4 VertexColour;

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;
FragmentColour = VertexColour;
}
45 changes: 34 additions & 11 deletions source/OpenGL/GridRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,46 @@ 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);
constexpr int Size = 1000; // Used for the size and number of lines to draw.
constexpr float transparency = 0.7f;
constexpr glm::vec4 primary_line_colour = glm::vec4{0.5f, 0.5f, 0.5f, transparency};
constexpr glm::vec4 secondary_line_colour = glm::vec4{0.2f, 0.2f, 0.2f, transparency};
constexpr glm::vec4 red = glm::vec4{1.f, 0.f, 0.f, transparency};
constexpr glm::vec4 green = glm::vec4{0.f, 1.f, 0.f, transparency};
constexpr glm::vec4 blue = glm::vec4{0.f, 0.f, 1.f, transparency};

// XZ-plane
for (auto i = -Count; i <= Count; i++)
m_line_points.reserve((Size * 8) + 6);

{ // Cardinal axis lines
m_line_points.push_back({glm::vec3{-Size, 0.f, 0.f}, red});
m_line_points.push_back({glm::vec3{Size, 0.f, 0.f}, red});
m_line_points.push_back({glm::vec3{0.f, -Size, 0.f}, green});
m_line_points.push_back({glm::vec3{0.f, Size, 0.f}, green});
m_line_points.push_back({glm::vec3{0.f, 0.f, -Size}, blue});
m_line_points.push_back({glm::vec3{0.f, 0.f, Size}, blue});
}

// XZ-plane lines
for (auto i = -Size; i <= Size; 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}});
if (i % 10 == 0)
{
m_line_points.push_back({glm::vec3{-Size, 0.f, i}, primary_line_colour});
m_line_points.push_back({glm::vec3{Size, 0.f, i }, primary_line_colour});
m_line_points.push_back({glm::vec3{i, 0.f, Size }, primary_line_colour});
m_line_points.push_back({glm::vec3{i, 0.f, -Size}, primary_line_colour});
}
else if (i != 0) // Ignore 0, cardinal axis lines are added above.
{
m_line_points.push_back({glm::vec3{-Size, 0.f, i}, secondary_line_colour});
m_line_points.push_back({glm::vec3{Size, 0.f, i }, secondary_line_colour});
m_line_points.push_back({glm::vec3{i, 0.f, Size }, secondary_line_colour});
m_line_points.push_back({glm::vec3{i, 0.f, -Size}, secondary_line_colour});
}
}

m_line_VAO.bind();
Expand All @@ -30,7 +54,6 @@ namespace OpenGL
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);
Expand Down
4 changes: 2 additions & 2 deletions source/OpenGL/GridRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ namespace OpenGL
{
struct GridVert
{
constexpr inline static std::array<VertexAttribute, 1> Attributes = { VertexAttribute::Position3D };
constexpr inline static std::array<VertexAttribute, 2> Attributes = { VertexAttribute::Position3D, VertexAttribute::ColourRGBA };
glm::vec3 m_position = glm::vec3{0.f};
glm::vec4 m_colour = glm::vec4{1.f};
};

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

0 comments on commit 49e05ca

Please sign in to comment.