Techs ENABLE_OPENGL_ES - Added shader dashed_lines to render dashed lines

This commit is contained in:
enricoturri1966 2022-04-05 13:53:25 +02:00
parent 3bf470c1ac
commit 1383927bae
5 changed files with 83 additions and 26 deletions

View File

@ -0,0 +1,20 @@
#version 110
precision highp float;
// see as reference: https://stackoverflow.com/questions/52928678/dashed-line-in-opengl3
uniform float dash_size;
uniform float gap_size;
uniform vec4 uniform_color;
varying float coord_s;
void main()
{
float inv_stride = 1.0 / (dash_size + gap_size);
if (gap_size > 0.0 && fract(coord_s * inv_stride) > dash_size * inv_stride)
discard;
gl_FragColor = uniform_color;
}

View File

@ -0,0 +1,17 @@
#version 110
// see as reference: https://stackoverflow.com/questions/52928678/dashed-line-in-opengl3
uniform mat4 view_model_matrix;
uniform mat4 projection_matrix;
// v_position.w = coordinate along the line
attribute vec4 v_position;
varying float coord_s;
void main()
{
coord_s = v_position.w;
gl_Position = projection_matrix * view_model_matrix * vec4(v_position.xyz, 1.0);
}

View File

@ -135,11 +135,13 @@ namespace GUI {
#endif // !ENABLE_GL_CORE_PROFILE && !ENABLE_OPENGL_ES
#if ENABLE_LEGACY_OPENGL_REMOVAL
#if ENABLE_GL_CORE_PROFILE
#if ENABLE_OPENGL_ES
GLShaderProgram* shader = wxGetApp().get_shader("dashed_lines");
#elif ENABLE_GL_CORE_PROFILE
GLShaderProgram* shader = wxGetApp().get_shader("dashed_thick_lines");
#else
GLShaderProgram* shader = wxGetApp().get_shader("flat");
#endif // ENABLE_GL_CORE_PROFILE
#endif // ENABLE_OPENGL_ES
if (shader != nullptr) {
shader->start_using();
@ -149,7 +151,18 @@ namespace GUI {
m_rectangle.reset();
GLModel::Geometry init_data;
#if ENABLE_GL_CORE_PROFILE
#if ENABLE_OPENGL_ES
if (wxGetApp().is_gl_version_greater_or_equal_to(3, 1)) {
init_data.format = { GLModel::Geometry::EPrimitiveType::Lines, GLModel::Geometry::EVertexLayout::P4 };
init_data.reserve_vertices(8);
init_data.reserve_indices(8);
}
else {
init_data.format = { GLModel::Geometry::EPrimitiveType::LineLoop, GLModel::Geometry::EVertexLayout::P2 };
init_data.reserve_vertices(4);
init_data.reserve_indices(4);
}
#elif ENABLE_GL_CORE_PROFILE
init_data.format = { GLModel::Geometry::EPrimitiveType::Lines, GLModel::Geometry::EVertexLayout::P4 };
init_data.reserve_vertices(8);
init_data.reserve_indices(8);
@ -157,10 +170,10 @@ namespace GUI {
init_data.format = { GLModel::Geometry::EPrimitiveType::LineLoop, GLModel::Geometry::EVertexLayout::P2 };
init_data.reserve_vertices(4);
init_data.reserve_indices(4);
#endif // ENABLE_GL_CORE_PROFILE
#endif // ENABLE_OPENGL_ES
// vertices
#if ENABLE_GL_CORE_PROFILE
#if ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES
const float width = right - left;
const float height = top - bottom;
float perimeter = 0.0f;
@ -197,20 +210,23 @@ namespace GUI {
init_data.add_index(1);
init_data.add_index(2);
init_data.add_index(3);
#endif // ENABLE_GL_CORE_PROFILE
#endif // ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES
m_rectangle.init_from(std::move(init_data));
}
shader->set_uniform("view_model_matrix", Transform3d::Identity());
shader->set_uniform("projection_matrix", Transform3d::Identity());
#if ENABLE_GL_CORE_PROFILE
#if ENABLE_OPENGL_ES
shader->set_uniform("dash_size", 0.01f);
shader->set_uniform("gap_size", 0.0075f);
#elif ENABLE_GL_CORE_PROFILE
const std::array<int, 4>& viewport = wxGetApp().plater()->get_camera().get_viewport();
shader->set_uniform("viewport_size", Vec2d(double(viewport[2]), double(viewport[3])));
shader->set_uniform("width", 0.25f);
shader->set_uniform("dash_size", 0.01f);
shader->set_uniform("gap_size", 0.0075f);
#endif // ENABLE_GL_CORE_PROFILE
#endif // ENABLE_OPENGL_ES
m_rectangle.set_color(ColorRGBA((m_state == EState::Select) ? 0.3f : 1.0f, (m_state == EState::Select) ? 1.0f : 0.3f, 0.3f, 1.0f));
m_rectangle.render();

View File

@ -49,7 +49,10 @@ std::pair<bool, std::string> GLShadersManager::init()
valid &= append_shader("flat_texture", { prefix + "flat_texture.vs", prefix + "flat_texture.fs" });
// used to render 3D scene background
valid &= append_shader("background", { prefix + "background.vs", prefix + "background.fs" });
#if ENABLE_GL_CORE_PROFILE
#if ENABLE_OPENGL_ES
// used to render dashed lines
valid &= append_shader("dashed_lines", { prefix + "dashed_lines.vs", prefix + "dashed_lines.fs" });
#elif ENABLE_GL_CORE_PROFILE
// used to render thick and/or dashed lines
valid &= append_shader("dashed_thick_lines", { prefix + "dashed_thick_lines.vs", prefix + "dashed_thick_lines.fs", prefix + "dashed_thick_lines.gs" });
#endif // ENABLE_GL_CORE_PROFILE

View File

@ -225,30 +225,26 @@ void GLGizmoPainterBase::render_cursor_circle()
GLModel::Geometry init_data;
static const unsigned int StepsCount = 32;
static const float StepSize = 2.0f * float(PI) / float(StepsCount);
#if ENABLE_GL_CORE_PROFILE
#if ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES
init_data.format = { GLModel::Geometry::EPrimitiveType::Lines, GLModel::Geometry::EVertexLayout::P4 };
#else
init_data.format = { GLModel::Geometry::EPrimitiveType::LineLoop, GLModel::Geometry::EVertexLayout::P2 };
#endif // ENABLE_GL_CORE_PROFILE
init_data.color = { 0.0f, 1.0f, 0.3f, 1.0f };
#if ENABLE_GL_CORE_PROFILE
init_data.reserve_vertices(2 * StepsCount);
init_data.reserve_indices(2 * StepsCount);
#else
init_data.format = { GLModel::Geometry::EPrimitiveType::LineLoop, GLModel::Geometry::EVertexLayout::P2 };
init_data.reserve_vertices(StepsCount);
init_data.reserve_indices(StepsCount);
#endif // ENABLE_GL_CORE_PROFILE
#endif // ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES
init_data.color = { 0.0f, 1.0f, 0.3f, 1.0f };
// vertices + indices
#if ENABLE_GL_CORE_PROFILE
#if ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES
float perimeter = 0.0f;
#endif // ENABLE_GL_CORE_PROFILE
#endif // ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES
for (unsigned int i = 0; i < StepsCount; ++i) {
#if ENABLE_GL_CORE_PROFILE
#if ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES
const float angle_i = float(i) * StepSize;
const unsigned int j = (i + 1) % StepsCount;
const float angle_j = float(j) * StepSize;
const float angle_j = float((i + 1) % StepsCount) * StepSize;
const Vec2d v_i(2.0f * ((center.x() + ::cos(angle_i) * radius) * cnv_inv_width - 0.5f), -2.0f * ((center.y() + ::sin(angle_i) * radius) * cnv_inv_height - 0.5f));
const Vec2d v_j(2.0f * ((center.x() + ::cos(angle_j) * radius) * cnv_inv_width - 0.5f), -2.0f * ((center.y() + ::sin(angle_j) * radius) * cnv_inv_height - 0.5f));
init_data.add_vertex(Vec4f(v_i.x(), v_i.y(), 0.0f, perimeter));
@ -260,28 +256,33 @@ void GLGizmoPainterBase::render_cursor_circle()
init_data.add_vertex(Vec2f(2.0f * ((center.x() + ::cos(angle) * radius) * cnv_inv_width - 0.5f),
-2.0f * ((center.y() + ::sin(angle) * radius) * cnv_inv_height - 0.5f)));
init_data.add_index(i);
#endif // ENABLE_GL_CORE_PROFILE
#endif // ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES
}
m_circle.init_from(std::move(init_data));
}
#if ENABLE_GL_CORE_PROFILE
#if ENABLE_OPENGL_ES
GLShaderProgram* shader = wxGetApp().get_shader("dashed_lines");
#elif ENABLE_GL_CORE_PROFILE
GLShaderProgram* shader = wxGetApp().get_shader("dashed_thick_lines");
#else
GLShaderProgram* shader = GUI::wxGetApp().get_shader("flat");
#endif // ENABLE_GL_CORE_PROFILE
#endif // ENABLE_OPENGL_ES
if (shader != nullptr) {
shader->start_using();
shader->set_uniform("view_model_matrix", Transform3d::Identity());
shader->set_uniform("projection_matrix", Transform3d::Identity());
#if ENABLE_GL_CORE_PROFILE
#if ENABLE_OPENGL_ES
shader->set_uniform("dash_size", 0.01f);
shader->set_uniform("gap_size", 0.0075f);
#elif ENABLE_GL_CORE_PROFILE
const std::array<int, 4>& viewport = wxGetApp().plater()->get_camera().get_viewport();
shader->set_uniform("viewport_size", Vec2d(double(viewport[2]), double(viewport[3])));
shader->set_uniform("width", 0.25f);
shader->set_uniform("dash_size", 0.01f);
shader->set_uniform("gap_size", 0.0075f);
#endif // ENABLE_GL_CORE_PROFILE
#endif // ENABLE_OPENGL_ES
m_circle.render();
shader->stop_using();
}