Skip to content
Draft
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
1 change: 1 addition & 0 deletions src/modules/render/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(SHADERS
combine2
convolution
texture
planegrid
)
set(SRCS_SHADERS)
foreach (SHADER ${SHADERS})
Expand Down
43 changes: 35 additions & 8 deletions src/modules/render/GridRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@
*/

#include "GridRenderer.h"
#include "PlanegridShader.h"
#include "core/Color.h"
#include "core/GLM.h"
#include "core/GLMConst.h"
#include "core/Log.h"
#include "core/Trace.h"
#include "math/AABB.h"
#include "math/Plane.h"
#include "video/Buffer.h"
#include "video/Camera.h"
#include "video/Renderer.h"
#include "video/RendererInterface.h"
#include "video/ScopedState.h"
#include "video/Shader.h"
#include "video/Types.h"
#include "video/gl/GLTypes.h"

namespace render {

GridRenderer::GridRenderer(bool renderAABB, bool renderGrid, bool renderPlane)
: _renderAABB(renderAABB), _renderGrid(renderGrid), _renderPlane(renderPlane) {
: _planeShader(shader::PlanegridShader::getInstance()), _renderAABB(renderAABB), _renderGrid(renderGrid),
_renderPlane(renderPlane) {
}

bool GridRenderer::init() {
Expand All @@ -25,6 +33,14 @@ bool GridRenderer::init() {
return false;
}

if (!_planeShader.setup()) {
Log::error("Failed to setup color shader");
return false;
}
core_assert_always(_uniformBlock.create(_uniformBlockData));
core_assert_always(_planeShader.setUniformblock(_uniformBlock.getUniformblockUniformBuffer()));
_planeVAO = video::genVertexArray();

return true;
}

Expand Down Expand Up @@ -189,13 +205,21 @@ void GridRenderer::renderPlane(const video::Camera &camera) {
if (!_renderPlane) {
return;
}
if (_dirtyPlane) {
createPlane();
_dirtyPlane = false;
}
_shapeRenderer.hide(_plane, false);
_shapeRenderer.render(_plane, camera);
_shapeRenderer.hide(_plane, true);

video::ScopedState facecull(video::State::CullFace, false);
video::ScopedState depthdepth(video::State::DepthTest, true);
video::ScopedShader scopedShader(_planeShader);
video::bindVertexArray(_planeVAO);

_uniformBlockData.cameraPos = camera.eye();
_uniformBlockData.proj = camera.projectionMatrix();
_uniformBlockData.view = camera.viewMatrix();
// video::bindBuffer(video::BufferType::UniformBuffer, _uniformBlock.getUniformblockUniformBuffer().handle());
core_assert_always(_uniformBlock.update(_uniformBlockData));
core_assert_always(_planeShader.setUniformblock(_uniformBlock.getUniformblockUniformBuffer()));

video::drawArrays(video::Primitive::TriangleStrip, 4);
video::bindVertexArray(video::InvalidId);
}

void GridRenderer::renderForwardArrow(const video::Camera &camera) {
Expand All @@ -217,6 +241,9 @@ void GridRenderer::shutdown() {
_plane = -1;
_shapeRenderer.shutdown();
_shapeBuilder.shutdown();
_planeShader.shutdown();
_uniformBlock.shutdown();
video::deleteVertexArray(_planeVAO);
}

} // namespace render
8 changes: 8 additions & 0 deletions src/modules/render/GridRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

#pragma once

#include "PlanegridShader.h"
#include "core/Log.h"
#include "render/ShapeRenderer.h"
#include "video/ShapeBuilder.h"
#include "math/AABB.h"
#include "video/gl/GLTypes.h"

namespace video {
class Video;
Expand All @@ -26,6 +28,12 @@ class GridRenderer {
protected:
video::ShapeBuilder _shapeBuilder;
render::ShapeRenderer _shapeRenderer;

shader::PlanegridShader &_planeShader;
alignas(16) mutable shader::PlanegridData::UniformblockData _uniformBlockData;
mutable shader::PlanegridData _uniformBlock;
video::Id _planeVAO = video::InvalidId;

math::AABB<float> _aabb;

int32_t _aabbMeshIndex = -1;
Expand Down
63 changes: 63 additions & 0 deletions src/modules/render/shaders/planegrid.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// https://dev.to/javiersalcedopuyo/simple-infinite-grid-shader-5fah

$in vec4 v_position;
flat $in vec3 v_camera_pos;
$in vec2 v_coords;

out vec4 o_color;

const float grid_size = 100.0;
const float cell_size = 1.0;
const float half_cell_size = cell_size * 0.5;
const float cell_line_thickness = 0.005;

const float subcell_size = 0.1;
const float half_subcell_size = subcell_size * 0.5;
const float subcell_line_thickness = 0.001;

const vec4 cell_color = vec4(0.5, 0.5, 0.5, 1.0);
const vec4 subcell_color = vec4(0.75, 0.75, 0.75, 1.0);

const float height_to_fade_distance_ratio = 25.0;
const float min_fade_distance = grid_size * 0.05;
const float max_fade_distance = grid_size * 0.5;

// Fragment shader
void main() {
// Offset coordinates for grid alignment
vec2 cell_coords = mod(v_coords + half_cell_size, cell_size);
vec2 subcell_coords = mod(v_coords + half_subcell_size, subcell_size);

// Distance to edges
vec2 distance_to_cell = abs(cell_coords - half_cell_size);
vec2 distance_to_subcell = abs(subcell_coords - half_subcell_size);

// Line thickness adjustment
vec2 d = fwidth(v_coords);
float adjusted_cell_line_thickness = 0.5 * (cell_line_thickness + d.x);
float adjusted_subcell_line_thickness = 0.5 * (subcell_line_thickness + d.x);

vec4 color = vec4(0.0);
if (any(lessThan(distance_to_subcell, vec2(adjusted_subcell_line_thickness)))) {
color = subcell_color;
}
if (any(lessThan(distance_to_cell, vec2(adjusted_cell_line_thickness)))) {
color = cell_color;
}

// Fade out around the camera to hide visual artifacts
float opacity_falloff;
{
float distance_to_camera = length(v_coords - v_camera_pos.xz);
// Adjust the fade distance relative to the camera height
float fade_distance = abs(v_camera_pos.y) * height_to_fade_distance_ratio;
{
fade_distance = max(fade_distance, min_fade_distance);
fade_distance = min(fade_distance, max_fade_distance);
}
opacity_falloff = smoothstep(1.0, 0.0, distance_to_camera / fade_distance);
}

color.a *= opacity_falloff;
o_color = color;
}
31 changes: 31 additions & 0 deletions src/modules/render/shaders/planegrid.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// https://dev.to/javiersalcedopuyo/simple-infinite-grid-shader-5fah

layout(std140) uniform u_uniformblock {
vec3 camera_pos;
mat4 view;
mat4 proj;
};

const vec4 positions[4] = vec4[4](
vec4(-0.5, 0.0, 0.5, 1.0),
vec4( 0.5, 0.0, 0.5, 1.0),
vec4(-0.5, 0.0, -0.5, 1.0),
vec4( 0.5, 0.0, -0.5, 1.0)
);

$out vec4 v_position;
flat $out vec3 v_camera_pos;
$out vec2 v_coords;

// Vertex shader
void main() {
vec4 world_pos = positions[gl_VertexID];
world_pos.xyz *= grid_size;
world_pos.xz += camera_pos.xz; // Make the quad follows the camera for "infinity"

v_position = proj * view * world_pos;
v_camera_pos = camera_pos;
v_coords = world_pos.xz;

gl_Position = v_position;
}
Loading