added anti z-fighting capability to smooth-shader

This commit is contained in:
denizdiktas 2023-08-10 12:57:17 +03:00
parent 72547412db
commit 64bafda02f
3 changed files with 23 additions and 15 deletions

View File

@ -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
@ -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);
@ -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();
@ -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);

View File

@ -2,7 +2,9 @@
#version 330
uniform vec4 u_color;
uniform vec4 u_plane;
in vec3 v_pos;
in vec3 v_normal;
out vec4 out_color;
@ -10,6 +12,9 @@ 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);

View File

@ -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);