Skip to content

Commit

Permalink
render
Browse files Browse the repository at this point in the history
  • Loading branch information
mgerhardy committed Dec 1, 2024
1 parent e8d3d3e commit 4851dbc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 39 deletions.
17 changes: 10 additions & 7 deletions src/modules/render/GridRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "math/AABB.h"
#include "math/Plane.h"
#include "video/Camera.h"
#include "video/Renderer.h"
#include "video/RendererInterface.h"
#include "video/ScopedState.h"
#include "video/Shader.h"
Expand All @@ -37,6 +38,7 @@ bool GridRenderer::init() {
}
core_assert_always(_uniformBlock.create(_uniformBlockData));
core_assert_always(_planeShader.setUniformblock(_uniformBlock.getUniformblockUniformBuffer()));
_planeVAO = video::genVertexArray();

return true;
}
Expand Down Expand Up @@ -202,19 +204,19 @@ void GridRenderer::renderPlane(const video::Camera &camera) {
if (!_renderPlane) {
return;
}

video::ScopedState facecull(video::State::CullFace, false);
video::ScopedState depthdepth(video::State::DepthTest, true);
video::ScopedShader scopedShader(_planeShader);
_uniformBlockData.cameraPos = camera.eye();
video::bindVertexArray(_planeVAO);

_uniformBlockData.cameraPos = camera.worldPosition();
_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::bindVertexArray(video::InvalidId);
core_assert(video::boundVertexArray() == video::InvalidId);
video::unbindBuffer(video::BufferType::IndexBuffer);
core_assert(video::boundBuffer(video::BufferType::IndexBuffer) == video::InvalidId);
video::unbindBuffer(video::BufferType::ArrayBuffer);
core_assert(video::boundBuffer(video::BufferType::ArrayBuffer) == video::InvalidId);
video::drawArrays(video::Primitive::TriangleStrip, 4);
}

Expand All @@ -239,6 +241,7 @@ void GridRenderer::shutdown() {
_shapeBuilder.shutdown();
_planeShader.shutdown();
_uniformBlock.shutdown();
video::deleteVertexArray(_planeVAO);
}

} // namespace render
4 changes: 4 additions & 0 deletions src/modules/render/GridRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "render/ShapeRenderer.h"
#include "video/ShapeBuilder.h"
#include "math/AABB.h"
#include "video/gl/GLTypes.h"

namespace video {
class Video;
Expand All @@ -27,9 +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
4 changes: 2 additions & 2 deletions src/modules/render/shaders/planeconstant.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ 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.75, 0.75, 0.75, 0.5);
const vec4 subcell_color = vec4(0.5, 0.5, 0.5, 0.5);
const vec4 cell_color = vec4(0.75, 0.75, 0.75, 0.7);
const vec4 subcell_color = vec4(0.5, 0.5, 0.5, 0.7);

const float height_to_fade_distance_ratio = 25.0;
const float min_fade_distance = grid_size * 0.05;
Expand Down
31 changes: 16 additions & 15 deletions src/modules/render/shaders/planegrid.frag
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
// https://dev.to/javiersalcedopuyo/simple-infinite-grid-shader-5fah

#extension GL_ARB_shading_language_420pack : enable

in VertexOut {
vec4 position;
vec3 camera_pos;
vec2 coords;
} v_in;
$in vec4 v_position;
flat $in vec3 v_camera_pos;
$in vec2 v_coords;

out vec4 o_color;

Expand All @@ -15,15 +11,15 @@ out vec4 o_color;
// Fragment shader
void main() {
// Offset coordinates for grid alignment
vec2 cell_coords = mod(v_in.coords + half_cell_size, cell_size);
vec2 subcell_coords = mod(v_in.coords + half_subcell_size, subcell_size);
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_in.coords);
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);

Expand All @@ -35,14 +31,19 @@ void main() {
color = cell_color;
}

// Fade out effect
// Fade out around the camera to hide visual artifacts
float opacity_falloff;
{
float distance_to_camera = length(v_in.coords - v_in.camera_pos.xz);
float fade_distance = abs(v_in.camera_pos.y) * height_to_fade_distance_ratio;
fade_distance = clamp(fade_distance, min_fade_distance, max_fade_distance);
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);
}

o_color = color * opacity_falloff;
color.a *= opacity_falloff;
o_color = color;
}
24 changes: 9 additions & 15 deletions src/modules/render/shaders/planegrid.vert
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
// https://dev.to/javiersalcedopuyo/simple-infinite-grid-shader-5fah
#extension GL_ARB_shading_language_420pack : enable

layout(std140, binding = 0) uniform u_uniformblock {
layout(std140) uniform u_uniformblock {
vec3 camera_pos;
mat4 view;
mat4 proj;
};

#include "planeconstant.glsl"

// Vertex input and output
layout(location = 0) in uint a_vertex_id;

out VertexOut {
vec4 position;
vec3 camera_pos;
vec2 coords;
} v_out;
$out vec4 v_position;
flat $out vec3 v_camera_pos;
$out vec2 v_coords;

// Vertex shader
void main() {
vec4 world_pos = positions[a_vertex_id];
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_out.position = proj * view * world_pos;
v_out.camera_pos = camera_pos;
v_out.coords = world_pos.xz;
v_position = proj * view * world_pos;
v_camera_pos = camera_pos;
v_coords = world_pos.xz;

gl_Position = v_out.position;
gl_Position = v_position;
}

0 comments on commit 4851dbc

Please sign in to comment.