Skip to content

Commit

Permalink
added anti z-fighting capability to smooth-shader
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Aug 10, 2023
1 parent 7254741 commit 64bafda
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
29 changes: 16 additions & 13 deletions Arrangement_on_surface_2/demo/earth/Main_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,19 @@ void Main_widget::paintGL()
const auto projection = m_camera.get_projection_matrix();
const auto mvp = projection * view * model;

// compute the cutting plane
// remember that we are passing the local vertex positions of the sphere
// between the vertex and fragment shader stages, so we need to convert
// the camera-pos in world coords to sphere's local coords!
auto c = model.inverted() * m_camera.get_pos();
const auto d = c.length();
const auto r = 1.0f;
const auto sin_alpha = r / d;
const auto n = (c / d); // plane unit normal vector
const auto cos_beta = sin_alpha;
const auto p = (r * cos_beta) * n;
QVector4D plane(n.x(), n.y(), n.z(), -QVector3D::dotProduct(p, n));

// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// SPHERE
Expand All @@ -481,8 +494,9 @@ void Main_widget::paintGL()
auto& sp = m_sp_smooth;
sp.use();
sp.set_uniform("u_mvp", mvp);
QVector4D sphere_color(167. / 255, 205. / 255, 242. / 255, 1);
auto sphere_color = QVector4D(167, 205, 242, 255) / 255;
sp.set_uniform("u_color", sphere_color);
sp.set_uniform("u_plane", QVector4D(0,0,0,0));
//sp.set_uniform("u_color", m_sphere->get_color());

//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
Expand All @@ -494,6 +508,7 @@ void Main_widget::paintGL()
glDisable(GL_DEPTH_TEST);
QVector4D face_color(1, .5, 0, 1);
sp.set_uniform("u_color", face_color);
sp.set_uniform("u_plane", plane);
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
g_face_triangles->draw();

Expand Down Expand Up @@ -525,18 +540,6 @@ void Main_widget::paintGL()
sp.use();
sp.set_uniform("u_mvp", mvp);

// compute the cutting plane
// remember that we are passing the local vertex positions of the sphere
// between the vertex and fragment shader stages, so we need to convert
// the camera-pos in world coords to sphere's local coords!
auto c = model.inverted() * m_camera.get_pos();
const auto d = c.length();
const auto r = 1.0f;
const auto sin_alpha = r / d;
const auto n = (c / d); // plane unit normal vector
const auto cos_beta = sin_alpha;
const auto p = (r * cos_beta) * n;
QVector4D plane(n.x(), n.y(), n.z(), -QVector3D::dotProduct(p, n));
const QVector4D arc_color(0, 0.5, 1, 1);
glLineWidth(5);
sp.set_uniform("u_plane", plane);
Expand Down
5 changes: 5 additions & 0 deletions Arrangement_on_surface_2/demo/earth/shaders/smooth_fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
#version 330

uniform vec4 u_color;
uniform vec4 u_plane;

in vec3 v_pos;
in vec3 v_normal;

out vec4 out_color;


void main()
{
if( dot(u_plane, vec4(v_pos, 1)) < 0 )
discard;

const vec3 lightDir = normalize(vec3(1,.5,.5));

//float c = clamp(dot(lightDir,triNormal), 0, 1);
Expand Down
4 changes: 2 additions & 2 deletions Arrangement_on_surface_2/demo/earth/shaders/smooth_vs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ layout (location = 0) in vec3 a_pos;
layout (location = 1) in vec3 a_normal;

//out vec4 v_col;
//out vec3 v_pos;
out vec3 v_pos;
out vec3 v_normal;

uniform mat4 u_mvp;

void main()
{
//v_pos = a_pos;
v_pos = a_pos;
v_normal = a_normal;
gl_Position = u_mvp * vec4(a_pos.xyz, 1);
//gl_Position = vec4(pos.xyz, 1);
Expand Down

0 comments on commit 64bafda

Please sign in to comment.