Skip to content

Commit f350417

Browse files
committed
1 parent f1141af commit f350417

File tree

6 files changed

+137
-8
lines changed

6 files changed

+137
-8
lines changed

src/modules/render/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set(SHADERS
1010
combine2
1111
convolution
1212
texture
13+
planegrid
1314
)
1415
set(SRCS_SHADERS)
1516
foreach (SHADER ${SHADERS})

src/modules/render/GridRenderer.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
#include "GridRenderer.h"
6+
#include "PlanegridShader.h"
67
#include "core/Color.h"
78
#include "core/GLM.h"
89
#include "core/GLMConst.h"
@@ -11,12 +12,17 @@
1112
#include "math/AABB.h"
1213
#include "math/Plane.h"
1314
#include "video/Camera.h"
15+
#include "video/RendererInterface.h"
1416
#include "video/ScopedState.h"
17+
#include "video/Shader.h"
18+
#include "video/Types.h"
19+
#include "video/gl/GLTypes.h"
1520

1621
namespace render {
1722

1823
GridRenderer::GridRenderer(bool renderAABB, bool renderGrid, bool renderPlane)
19-
: _renderAABB(renderAABB), _renderGrid(renderGrid), _renderPlane(renderPlane) {
24+
: _planeShader(shader::PlanegridShader::getInstance()), _renderAABB(renderAABB), _renderGrid(renderGrid),
25+
_renderPlane(renderPlane) {
2026
}
2127

2228
bool GridRenderer::init() {
@@ -25,6 +31,13 @@ bool GridRenderer::init() {
2531
return false;
2632
}
2733

34+
if (!_planeShader.setup()) {
35+
Log::error("Failed to setup color shader");
36+
return false;
37+
}
38+
core_assert_always(_uniformBlock.create(_uniformBlockData));
39+
core_assert_always(_planeShader.setUniformblock(_uniformBlock.getUniformblockUniformBuffer()));
40+
2841
return true;
2942
}
3043

@@ -189,13 +202,20 @@ void GridRenderer::renderPlane(const video::Camera &camera) {
189202
if (!_renderPlane) {
190203
return;
191204
}
192-
if (_dirtyPlane) {
193-
createPlane();
194-
_dirtyPlane = false;
195-
}
196-
_shapeRenderer.hide(_plane, false);
197-
_shapeRenderer.render(_plane, camera);
198-
_shapeRenderer.hide(_plane, true);
205+
video::ScopedShader scopedShader(_planeShader);
206+
_uniformBlockData.cameraPos = camera.eye();
207+
_uniformBlockData.proj = camera.projectionMatrix();
208+
_uniformBlockData.view = camera.viewMatrix();
209+
core_assert_always(_uniformBlock.update(_uniformBlockData));
210+
core_assert_always(_planeShader.setUniformblock(_uniformBlock.getUniformblockUniformBuffer()));
211+
212+
video::bindVertexArray(video::InvalidId);
213+
core_assert(video::boundVertexArray() == video::InvalidId);
214+
video::unbindBuffer(video::BufferType::IndexBuffer);
215+
core_assert(video::boundBuffer(video::BufferType::IndexBuffer) == video::InvalidId);
216+
video::unbindBuffer(video::BufferType::ArrayBuffer);
217+
core_assert(video::boundBuffer(video::BufferType::ArrayBuffer) == video::InvalidId);
218+
video::drawArrays(video::Primitive::TriangleStrip, 4);
199219
}
200220

201221
void GridRenderer::renderForwardArrow(const video::Camera &camera) {
@@ -217,6 +237,8 @@ void GridRenderer::shutdown() {
217237
_plane = -1;
218238
_shapeRenderer.shutdown();
219239
_shapeBuilder.shutdown();
240+
_planeShader.shutdown();
241+
_uniformBlock.shutdown();
220242
}
221243

222244
} // namespace render

src/modules/render/GridRenderer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#pragma once
66

7+
#include "PlanegridShader.h"
78
#include "core/Log.h"
89
#include "render/ShapeRenderer.h"
910
#include "video/ShapeBuilder.h"
@@ -26,6 +27,9 @@ class GridRenderer {
2627
protected:
2728
video::ShapeBuilder _shapeBuilder;
2829
render::ShapeRenderer _shapeRenderer;
30+
shader::PlanegridShader &_planeShader;
31+
alignas(16) mutable shader::PlanegridData::UniformblockData _uniformBlockData;
32+
mutable shader::PlanegridData _uniformBlock;
2933
math::AABB<float> _aabb;
3034

3135
int32_t _aabbMeshIndex = -1;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const float grid_size = 100.0;
2+
const float cell_size = 1.0;
3+
const float half_cell_size = cell_size * 0.5;
4+
const float cell_line_thickness = 0.005;
5+
6+
const float subcell_size = 0.1;
7+
const float half_subcell_size = subcell_size * 0.5;
8+
const float subcell_line_thickness = 0.001;
9+
10+
const vec4 cell_color = vec4(0.75, 0.75, 0.75, 0.5);
11+
const vec4 subcell_color = vec4(0.5, 0.5, 0.5, 0.5);
12+
13+
const float height_to_fade_distance_ratio = 25.0;
14+
const float min_fade_distance = grid_size * 0.05;
15+
const float max_fade_distance = grid_size * 0.5;
16+
17+
const vec4 positions[4] = vec4[4](
18+
vec4(-0.5, 0.0, 0.5, 1.0),
19+
vec4( 0.5, 0.0, 0.5, 1.0),
20+
vec4(-0.5, 0.0, -0.5, 1.0),
21+
vec4( 0.5, 0.0, -0.5, 1.0)
22+
);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// https://dev.to/javiersalcedopuyo/simple-infinite-grid-shader-5fah
2+
3+
#extension GL_ARB_shading_language_420pack : enable
4+
5+
in VertexOut {
6+
vec4 position;
7+
vec3 camera_pos;
8+
vec2 coords;
9+
} v_in;
10+
11+
out vec4 o_color;
12+
13+
#include "planeconstant.glsl"
14+
15+
// Fragment shader
16+
void main() {
17+
// Offset coordinates for grid alignment
18+
vec2 cell_coords = mod(v_in.coords + half_cell_size, cell_size);
19+
vec2 subcell_coords = mod(v_in.coords + half_subcell_size, subcell_size);
20+
21+
// Distance to edges
22+
vec2 distance_to_cell = abs(cell_coords - half_cell_size);
23+
vec2 distance_to_subcell = abs(subcell_coords - half_subcell_size);
24+
25+
// Line thickness adjustment
26+
vec2 d = fwidth(v_in.coords);
27+
float adjusted_cell_line_thickness = 0.5 * (cell_line_thickness + d.x);
28+
float adjusted_subcell_line_thickness = 0.5 * (subcell_line_thickness + d.x);
29+
30+
vec4 color = vec4(0.0);
31+
if (any(lessThan(distance_to_subcell, vec2(adjusted_subcell_line_thickness)))) {
32+
color = subcell_color;
33+
}
34+
if (any(lessThan(distance_to_cell, vec2(adjusted_cell_line_thickness)))) {
35+
color = cell_color;
36+
}
37+
38+
// Fade out effect
39+
float opacity_falloff;
40+
{
41+
float distance_to_camera = length(v_in.coords - v_in.camera_pos.xz);
42+
float fade_distance = abs(v_in.camera_pos.y) * height_to_fade_distance_ratio;
43+
fade_distance = clamp(fade_distance, min_fade_distance, max_fade_distance);
44+
opacity_falloff = smoothstep(1.0, 0.0, distance_to_camera / fade_distance);
45+
}
46+
47+
o_color = color * opacity_falloff;
48+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// https://dev.to/javiersalcedopuyo/simple-infinite-grid-shader-5fah
2+
#extension GL_ARB_shading_language_420pack : enable
3+
4+
layout(std140, binding = 0) uniform u_uniformblock {
5+
vec3 camera_pos;
6+
mat4 view;
7+
mat4 proj;
8+
};
9+
10+
#include "planeconstant.glsl"
11+
12+
// Vertex input and output
13+
layout(location = 0) in uint a_vertex_id;
14+
15+
out VertexOut {
16+
vec4 position;
17+
vec3 camera_pos;
18+
vec2 coords;
19+
} v_out;
20+
21+
// Vertex shader
22+
void main() {
23+
vec4 world_pos = positions[a_vertex_id];
24+
world_pos.xyz *= grid_size;
25+
world_pos.xz += camera_pos.xz; // Make the quad follows the camera for "infinity"
26+
27+
v_out.position = proj * view * world_pos;
28+
v_out.camera_pos = camera_pos;
29+
v_out.coords = world_pos.xz;
30+
31+
gl_Position = v_out.position;
32+
}

0 commit comments

Comments
 (0)