Skip to content

Commit

Permalink
noggit: world: fix cursor plane not visible underwater by rendering i…
Browse files Browse the repository at this point in the history
…t before the water and making it write to the depth buffer, add checker board pattern to be able to see the water under it still
  • Loading branch information
Adspartan committed May 28, 2024
1 parent 894a2bc commit 95092f9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 44 deletions.
92 changes: 48 additions & 44 deletions src/noggit/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,53 @@ void World::draw ( math::matrix_4x4 const& model_view
gl.enable(GL_BLEND);
gl.blendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// render before the water and enable depth right
// so it's visible under water
// the checker board pattern is used to see the water under it
if (angled_mode || use_ref_pos)
{
opengl::scoped::depth_mask_setter<GL_TRUE> const depth_mask;

math::degrees orient = math::degrees(orientation);
math::degrees incl = math::degrees(angle);
math::vector_4d color = cursor_color;

color.w = 0.75f;

float radius = 1.2f * brush_radius;

if (angled_mode && !use_ref_pos)
{
math::vector_3d pos = cursor_pos;
pos.y += 0.1f; // to avoid z-fighting with the ground
_square_render.draw(mvp, pos, radius, incl, orient, color);
}
else if (use_ref_pos)
{
if (angled_mode)
{
math::vector_3d pos = cursor_pos;
pos.y = misc::angledHeight(ref_pos, pos, incl, orient);
pos.y += 0.1f;
_square_render.draw(mvp, pos, radius, incl, orient, color);

// display the plane when the cursor is far from ref_point
if (misc::dist(pos.x, pos.z, ref_pos.x, ref_pos.z) > 10.f + radius)
{
math::vector_3d ref = ref_pos;
ref.y += 0.1f;
_square_render.draw(mvp, ref, 10.f, incl, orient, color);
}
}
else
{
math::vector_3d pos = cursor_pos;
pos.y = ref_pos.y + 0.1f;
_square_render.draw(mvp, pos, radius, math::degrees(0.f), math::degrees(0.f), color);
}
}
}

if (terrainMode == editing_mode::object && has_multiple_model_selected())
{
opengl::scoped::bool_setter<GL_DEPTH_TEST, GL_FALSE> const disable_depth_test;
Expand Down Expand Up @@ -1541,50 +1588,7 @@ void World::draw ( math::matrix_4x4 const& model_view
gl.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

if (angled_mode || use_ref_pos)
{
opengl::scoped::bool_setter<GL_CULL_FACE, GL_FALSE> cull;
opengl::scoped::depth_mask_setter<GL_FALSE> const depth_mask;

math::degrees orient = math::degrees(orientation);
math::degrees incl = math::degrees(angle);
math::vector_4d color = cursor_color;
// always half transparent regardless or the cursor transparency
color.w = 0.5f;

float radius = 1.2f * brush_radius;

if (angled_mode && !use_ref_pos)
{
math::vector_3d pos = cursor_pos;
pos.y += 0.1f; // to avoid z-fighting with the ground
_square_render.draw(mvp, pos, radius, incl, orient, color);
}
else if (use_ref_pos)
{
if (angled_mode)
{
math::vector_3d pos = cursor_pos;
pos.y = misc::angledHeight(ref_pos, pos, incl, orient);
pos.y += 0.1f;
_square_render.draw(mvp, pos, radius, incl, orient, color);

// display the plane when the cursor is far from ref_point
if (misc::dist(pos.x, pos.z, ref_pos.x, ref_pos.z) > 10.f + radius)
{
math::vector_3d ref = ref_pos;
ref.y += 0.1f;
_square_render.draw(mvp, ref, 10.f, incl, orient, color);
}
}
else
{
math::vector_3d pos = cursor_pos;
pos.y = ref_pos.y + 0.1f;
_square_render.draw(mvp, pos, radius, math::degrees(0.f), math::degrees(0.f), color);
}
}
}


// draw last because of the transparency
if (draw_mfbo)
Expand Down
19 changes: 19 additions & 0 deletions src/opengl/primitives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ out vec4 out_color;
void main()
{
if(gl_FragCoord.x < 0.5)
{
discard;
}
out_color = color;
}
)code"
Expand Down Expand Up @@ -267,6 +271,8 @@ void main()
in vec4 position;
out vec3 f_pos;
uniform mat4 model_view_projection;
uniform vec3 origin;
uniform float radius;
Expand All @@ -286,6 +292,8 @@ void main()
pos.xyz += origin;
gl_Position = model_view_projection * pos;
f_pos = position.xyz;
}
)code"
}
Expand All @@ -295,10 +303,21 @@ void main()
uniform vec4 color;
in vec3 f_pos;
out vec4 out_color;
void main()
{
bool discard_x = mod((f_pos.x + 1.) * 5., 2.) < 1.;
bool discard_y = mod((f_pos.z + 1.) * 5., 2.) < 1.;
// discard in a checker board pattern
if(discard_x != discard_y)
{
discard;
}
out_color = color;
}
)code"
Expand Down

0 comments on commit 95092f9

Please sign in to comment.