Skip to content

Commit

Permalink
z-fighting solved with silhouette plane
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Jun 14, 2023
1 parent 5808019 commit db222f0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions Arrangement_on_surface_2/demo/earth/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Camera

void set_pos(const QVector3D& pos) { m_pos = pos; }
void set_pos(float x, float y, float z) { m_pos = QVector3D(x,y,z); }
const QVector3D& get_pos() const { return m_pos; }

void perspective(float fov, float aspect_ratio, float z_near, float z_far);

Expand Down
4 changes: 2 additions & 2 deletions Arrangement_on_surface_2/demo/earth/Geodesic_arcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Geodesic_arcs::Geodesic_arcs()
const double error = 0.001;
std::vector<Approximate_point_2> v;

const auto& xcv = xcvs[0];
const auto& xcv = xcvs[1];
//for (const auto& xcv : xcvs)
{
auto oi2 = approx(xcv, error, std::back_insert_iterator(v));
Expand Down Expand Up @@ -155,7 +155,7 @@ void Geodesic_arcs::draw()
{
glBindVertexArray(m_vao);
{
glDrawArrays(GL_LINES, 0, m_num_arc_points);
glDrawArrays(GL_LINE_STRIP, 0, m_num_arc_points);
}
glBindVertexArray(0);
}
26 changes: 25 additions & 1 deletion Arrangement_on_surface_2/demo/earth/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ void MainWidget::init_sp_color_only()
}


std::ostream& operator << (std::ostream& os, const QVector4D& v)
{
os << v.x() << ", " << v.y() << ", " << v.z() << ", " << v.w();
return os;
}

void MainWidget::resizeGL(int w, int h)
{
Expand All @@ -151,6 +156,8 @@ void MainWidget::paintGL()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// SPHERE
{
glEnable(GL_DEPTH_TEST);

auto& sp = m_sp_smooth;
sp.use();
sp.set_uniform("u_mvp", mvp);
Expand All @@ -163,12 +170,29 @@ void MainWidget::paintGL()

// WORLD COORDINATE AXES & GEODESIC ARCS
{
glDisable(GL_DEPTH_TEST);

auto& sp = m_sp_color_only;
sp.use();
sp.set_uniform("u_mvp", mvp);

// compute the cutting plane
auto c = 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));
std::cout << plane << std::endl;
sp.set_uniform("u_plane", plane);
glEnable(GL_DEPTH_TEST);
m_geodesic_arcs->draw();

glEnable(GL_DEPTH_TEST);
sp.set_uniform("u_plane", QVector4D(0,0,0,0));// ad-hoc
m_world_coord_axes->draw();
m_geodesic_arcs->draw();

sp.unuse();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@

#version 330

uniform vec4 u_plane;

in vec3 v_color;
in vec3 v_pos;
out vec4 out_color;


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

out_color = vec4(v_color, 1);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ layout (location = 1) in vec3 a_color;
uniform mat4 u_mvp;

out vec3 v_color;
out vec3 v_pos;


void main()
{
v_color = a_color;
v_pos = a_pos;
gl_Position = u_mvp * vec4(a_pos.xyz, 1);
}

0 comments on commit db222f0

Please sign in to comment.