Skip to content

Commit 4851dbc

Browse files
committed
render
1 parent e8d3d3e commit 4851dbc

File tree

5 files changed

+41
-39
lines changed

5 files changed

+41
-39
lines changed

src/modules/render/GridRenderer.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "math/AABB.h"
1313
#include "math/Plane.h"
1414
#include "video/Camera.h"
15+
#include "video/Renderer.h"
1516
#include "video/RendererInterface.h"
1617
#include "video/ScopedState.h"
1718
#include "video/Shader.h"
@@ -37,6 +38,7 @@ bool GridRenderer::init() {
3738
}
3839
core_assert_always(_uniformBlock.create(_uniformBlockData));
3940
core_assert_always(_planeShader.setUniformblock(_uniformBlock.getUniformblockUniformBuffer()));
41+
_planeVAO = video::genVertexArray();
4042

4143
return true;
4244
}
@@ -202,19 +204,19 @@ void GridRenderer::renderPlane(const video::Camera &camera) {
202204
if (!_renderPlane) {
203205
return;
204206
}
207+
208+
video::ScopedState facecull(video::State::CullFace, false);
209+
video::ScopedState depthdepth(video::State::DepthTest, true);
205210
video::ScopedShader scopedShader(_planeShader);
206-
_uniformBlockData.cameraPos = camera.eye();
211+
video::bindVertexArray(_planeVAO);
212+
213+
_uniformBlockData.cameraPos = camera.worldPosition();
207214
_uniformBlockData.proj = camera.projectionMatrix();
208215
_uniformBlockData.view = camera.viewMatrix();
216+
// video::bindBuffer(video::BufferType::UniformBuffer, _uniformBlock.getUniformblockUniformBuffer().handle());
209217
core_assert_always(_uniformBlock.update(_uniformBlockData));
210218
core_assert_always(_planeShader.setUniformblock(_uniformBlock.getUniformblockUniformBuffer()));
211219

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);
218220
video::drawArrays(video::Primitive::TriangleStrip, 4);
219221
}
220222

@@ -239,6 +241,7 @@ void GridRenderer::shutdown() {
239241
_shapeBuilder.shutdown();
240242
_planeShader.shutdown();
241243
_uniformBlock.shutdown();
244+
video::deleteVertexArray(_planeVAO);
242245
}
243246

244247
} // namespace render

src/modules/render/GridRenderer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "render/ShapeRenderer.h"
1010
#include "video/ShapeBuilder.h"
1111
#include "math/AABB.h"
12+
#include "video/gl/GLTypes.h"
1213

1314
namespace video {
1415
class Video;
@@ -27,9 +28,12 @@ class GridRenderer {
2728
protected:
2829
video::ShapeBuilder _shapeBuilder;
2930
render::ShapeRenderer _shapeRenderer;
31+
3032
shader::PlanegridShader &_planeShader;
3133
alignas(16) mutable shader::PlanegridData::UniformblockData _uniformBlockData;
3234
mutable shader::PlanegridData _uniformBlock;
35+
video::Id _planeVAO = video::InvalidId;
36+
3337
math::AABB<float> _aabb;
3438

3539
int32_t _aabbMeshIndex = -1;

src/modules/render/shaders/planeconstant.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const float subcell_size = 0.1;
77
const float half_subcell_size = subcell_size * 0.5;
88
const float subcell_line_thickness = 0.001;
99

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);
10+
const vec4 cell_color = vec4(0.75, 0.75, 0.75, 0.7);
11+
const vec4 subcell_color = vec4(0.5, 0.5, 0.5, 0.7);
1212

1313
const float height_to_fade_distance_ratio = 25.0;
1414
const float min_fade_distance = grid_size * 0.05;
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
// https://dev.to/javiersalcedopuyo/simple-infinite-grid-shader-5fah
22

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;
3+
$in vec4 v_position;
4+
flat $in vec3 v_camera_pos;
5+
$in vec2 v_coords;
106

117
out vec4 o_color;
128

@@ -15,15 +11,15 @@ out vec4 o_color;
1511
// Fragment shader
1612
void main() {
1713
// 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);
14+
vec2 cell_coords = mod(v_coords + half_cell_size, cell_size);
15+
vec2 subcell_coords = mod(v_coords + half_subcell_size, subcell_size);
2016

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

2521
// Line thickness adjustment
26-
vec2 d = fwidth(v_in.coords);
22+
vec2 d = fwidth(v_coords);
2723
float adjusted_cell_line_thickness = 0.5 * (cell_line_thickness + d.x);
2824
float adjusted_subcell_line_thickness = 0.5 * (subcell_line_thickness + d.x);
2925

@@ -35,14 +31,19 @@ void main() {
3531
color = cell_color;
3632
}
3733

38-
// Fade out effect
34+
// Fade out around the camera to hide visual artifacts
3935
float opacity_falloff;
4036
{
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);
37+
float distance_to_camera = length(v_coords - v_camera_pos.xz);
38+
// Adjust the fade distance relative to the camera height
39+
float fade_distance = abs(v_camera_pos.y) * height_to_fade_distance_ratio;
40+
{
41+
fade_distance = max(fade_distance, min_fade_distance);
42+
fade_distance = min(fade_distance, max_fade_distance);
43+
}
4444
opacity_falloff = smoothstep(1.0, 0.0, distance_to_camera / fade_distance);
4545
}
4646

47-
o_color = color * opacity_falloff;
47+
color.a *= opacity_falloff;
48+
o_color = color;
4849
}
Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
11
// https://dev.to/javiersalcedopuyo/simple-infinite-grid-shader-5fah
2-
#extension GL_ARB_shading_language_420pack : enable
32

4-
layout(std140, binding = 0) uniform u_uniformblock {
3+
layout(std140) uniform u_uniformblock {
54
vec3 camera_pos;
65
mat4 view;
76
mat4 proj;
87
};
98

109
#include "planeconstant.glsl"
1110

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;
11+
$out vec4 v_position;
12+
flat $out vec3 v_camera_pos;
13+
$out vec2 v_coords;
2014

2115
// Vertex shader
2216
void main() {
23-
vec4 world_pos = positions[a_vertex_id];
17+
vec4 world_pos = positions[gl_VertexID];
2418
world_pos.xyz *= grid_size;
2519
world_pos.xz += camera_pos.xz; // Make the quad follows the camera for "infinity"
2620

27-
v_out.position = proj * view * world_pos;
28-
v_out.camera_pos = camera_pos;
29-
v_out.coords = world_pos.xz;
21+
v_position = proj * view * world_pos;
22+
v_camera_pos = camera_pos;
23+
v_coords = world_pos.xz;
3024

31-
gl_Position = v_out.position;
25+
gl_Position = v_position;
3226
}

0 commit comments

Comments
 (0)