Tech ENABLE_GLBEGIN_GLEND_REMOVAL - 1st installment - Selection bounding box

This commit is contained in:
enricoturri1966 2022-01-18 10:42:46 +01:00
parent 8be67bc4d6
commit 22f38235ea
8 changed files with 201 additions and 19 deletions

View file

@ -132,9 +132,8 @@ bool Selection::init()
{
m_arrow.init_from(straight_arrow(10.0f, 5.0f, 5.0f, 10.0f, 1.0f));
m_curved_arrow.init_from(circular_arrow(16, 10.0f, 5.0f, 10.0f, 5.0f, 1.0f));
#if ENABLE_RENDER_SELECTION_CENTER
m_vbo_sphere.init_from(make_sphere(0.75, 2*PI/24));
m_vbo_sphere.init_from(its_make_sphere(0.75, PI / 12.0));
#endif // ENABLE_RENDER_SELECTION_CENTER
return true;
@ -1256,20 +1255,29 @@ void Selection::erase()
}
}
#if ENABLE_GLBEGIN_GLEND_REMOVAL
void Selection::render(float scale_factor)
#else
void Selection::render(float scale_factor) const
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
{
if (!m_valid || is_empty())
return;
#if ENABLE_GLBEGIN_GLEND_REMOVAL
m_scale_factor = scale_factor;
render_bounding_box(get_bounding_box(), ColorRGB::WHITE());
#else
*const_cast<float*>(&m_scale_factor) = scale_factor;
// render cumulative bounding box of selected volumes
render_selected_volumes();
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
render_synchronized_volumes();
}
#if ENABLE_RENDER_SELECTION_CENTER
void Selection::render_center(bool gizmo_is_dragging) const
void Selection::render_center(bool gizmo_is_dragging)
{
if (!m_valid || is_empty())
return;
@ -1278,10 +1286,22 @@ void Selection::render_center(bool gizmo_is_dragging) const
glsafe(::glDisable(GL_DEPTH_TEST));
glsafe(::glColor3f(1.0f, 1.0f, 1.0f));
glsafe(::glPushMatrix());
glsafe(::glTranslated(center(0), center(1), center(2)));
glsafe(::glTranslated(center.x(), center.y(), center.z()));
#if ENABLE_GLBEGIN_GLEND_REMOVAL
GLShaderProgram* shader = wxGetApp().get_shader("flat");
if (shader == nullptr)
return;
shader->start_using();
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
m_vbo_sphere.set_color(-1, ColorRGBA::WHITE());
m_vbo_sphere.render();
#if ENABLE_GLBEGIN_GLEND_REMOVAL
shader->stop_using();
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
glsafe(::glPopMatrix());
}
#endif // ENABLE_RENDER_SELECTION_CENTER
@ -1798,50 +1818,171 @@ void Selection::do_remove_object(unsigned int object_idx)
}
}
#if !ENABLE_GLBEGIN_GLEND_REMOVAL
void Selection::render_selected_volumes() const
{
float color[3] = { 1.0f, 1.0f, 1.0f };
render_bounding_box(get_bounding_box(), color);
}
#endif // !ENABLE_GLBEGIN_GLEND_REMOVAL
#if ENABLE_GLBEGIN_GLEND_REMOVAL
void Selection::render_synchronized_volumes()
#else
void Selection::render_synchronized_volumes() const
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
{
if (m_mode == Instance)
return;
#if !ENABLE_GLBEGIN_GLEND_REMOVAL
float color[3] = { 1.0f, 1.0f, 0.0f };
#endif // !ENABLE_GLBEGIN_GLEND_REMOVAL
for (unsigned int i : m_list) {
const GLVolume* volume = (*m_volumes)[i];
int object_idx = volume->object_idx();
int volume_idx = volume->volume_idx();
const GLVolume& volume = *(*m_volumes)[i];
int object_idx = volume.object_idx();
int volume_idx = volume.volume_idx();
for (unsigned int j = 0; j < (unsigned int)m_volumes->size(); ++j) {
if (i == j)
continue;
const GLVolume* v = (*m_volumes)[j];
if (v->object_idx() != object_idx || v->volume_idx() != volume_idx)
const GLVolume& v = *(*m_volumes)[j];
if (v.object_idx() != object_idx || v.volume_idx() != volume_idx)
continue;
render_bounding_box(v->transformed_convex_hull_bounding_box(), color);
#if ENABLE_GLBEGIN_GLEND_REMOVAL
render_bounding_box(v.transformed_convex_hull_bounding_box(), ColorRGB::YELLOW());
#else
render_bounding_box(v.transformed_convex_hull_bounding_box(), color);
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
}
}
}
void Selection::render_bounding_box(const BoundingBoxf3& box, float* color) const
#if ENABLE_GLBEGIN_GLEND_REMOVAL
void Selection::render_bounding_box(const BoundingBoxf3& box, const ColorRGB& color)
{
#else
void Selection::render_bounding_box(const BoundingBoxf3 & box, float* color) const
{
if (color == nullptr)
return;
Vec3f b_min = box.min.cast<float>();
Vec3f b_max = box.max.cast<float>();
Vec3f size = 0.2f * box.size().cast<float>();
const Vec3f b_min = box.min.cast<float>();
const Vec3f b_max = box.max.cast<float>();
const Vec3f size = 0.2f * box.size().cast<float>();
glsafe(::glEnable(GL_DEPTH_TEST));
glsafe(::glColor3fv(color));
glsafe(::glLineWidth(2.0f * m_scale_factor));
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
#if ENABLE_GLBEGIN_GLEND_REMOVAL
auto is_approx = [](const Vec3d& v1, const Vec3d& v2) {
for (int i = 0; i < 3; ++i) {
if (std::abs(v1[i] - v2[i]) > EPSILON)
return false;
}
return true;
};
const BoundingBoxf3& curr_box = m_box.get_bounding_box();
if (!m_box.is_initialized() || !is_approx(box.min, curr_box.min) || !is_approx(box.max, curr_box.max)) {
m_box.reset();
const Vec3f b_min = box.min.cast<float>();
const Vec3f b_max = box.max.cast<float>();
const Vec3f size = 0.2f * box.size().cast<float>();
GLModel::InitializationData init_data;
GUI::GLModel::InitializationData::Entity entity;
entity.type = GUI::GLModel::PrimitiveType::Lines;
entity.positions.reserve(48);
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x() + size.x(), b_min.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y() + size.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y(), b_min.z() + size.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x() - size.x(), b_min.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y() + size.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y(), b_min.z() + size.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x() - size.x(), b_max.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y() - size.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y(), b_min.z() + size.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x() + size.x(), b_max.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y() - size.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y(), b_min.z() + size.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x() + size.x(), b_min.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y() + size.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y(), b_max.z() - size.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x() - size.x(), b_min.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y() + size.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y(), b_max.z() - size.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x() - size.x(), b_max.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y() - size.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y(), b_max.z() - size.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x() + size.x(), b_max.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y() - size.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y(), b_max.z() - size.z()));
entity.normals.reserve(48);
for (size_t i = 0; i < 48; ++i) {
entity.normals.emplace_back(Vec3f::UnitZ());
}
entity.indices.reserve(48);
for (size_t i = 0; i < 48; ++i) {
entity.indices.emplace_back(i);
}
init_data.entities.emplace_back(entity);
m_box.init_from(init_data);
}
glsafe(::glEnable(GL_DEPTH_TEST));
glsafe(::glColor3fv(color));
glsafe(::glLineWidth(2.0f * m_scale_factor));
GLShaderProgram* shader = wxGetApp().get_shader("flat");
if (shader == nullptr)
return;
shader->start_using();
m_box.set_color(-1, to_rgba(color));
m_box.render();
shader->stop_using();
#else
::glBegin(GL_LINES);
::glVertex3f(b_min(0), b_min(1), b_min(2)); ::glVertex3f(b_min(0) + size(0), b_min(1), b_min(2));
@ -1877,6 +2018,7 @@ void Selection::render_bounding_box(const BoundingBoxf3& box, float* color) cons
::glVertex3f(b_min(0), b_max(1), b_max(2)); ::glVertex3f(b_min(0), b_max(1), b_max(2) - size(2));
glsafe(::glEnd());
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
}
static ColorRGBA get_color(Axis axis)