Skip to content

Commit

Permalink
Tech ENABLE_GLBEGIN_GLEND_REMOVAL - Refactoring of GLModel to:
Browse files Browse the repository at this point in the history
1) allow for custom vertex data layout

2) allow for custom index data format

3) allow for any OpenGL primitive type

(cherry picked from commit prusa3d/PrusaSlicer@afcac6e)
  • Loading branch information
enricoturri1966 authored and Noisyfox committed Oct 22, 2023
1 parent dd5dafc commit fdf8b11
Show file tree
Hide file tree
Showing 27 changed files with 1,517 additions and 1,197 deletions.
4 changes: 4 additions & 0 deletions src/libslic3r/Color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ class ColorRGB
static const ColorRGB BLACK() { return { 0.0f, 0.0f, 0.0f }; }
static const ColorRGB BLUE() { return { 0.0f, 0.0f, 1.0f }; }
static const ColorRGB BLUEISH() { return { 0.5f, 0.5f, 1.0f }; }
static const ColorRGB CYAN() { return { 0.0f, 1.0f, 1.0f }; }
static const ColorRGB DARK_GRAY() { return { 0.25f, 0.25f, 0.25f }; }
static const ColorRGB DARK_YELLOW() { return { 0.5f, 0.5f, 0.0f }; }
static const ColorRGB GRAY() { return { 0.5f, 0.5f, 0.5f }; }
static const ColorRGB GREEN() { return { 0.0f, 1.0f, 0.0f }; }
static const ColorRGB GREENISH() { return { 0.5f, 1.0f, 0.5f }; }
static const ColorRGB LIGHT_GRAY() { return { 0.75f, 0.75f, 0.75f }; }
static const ColorRGB MAGENTA() { return { 1.0f, 0.0f, 1.0f }; }
static const ColorRGB ORANGE() { return { 0.92f, 0.50f, 0.26f }; }
static const ColorRGB RED() { return { 1.0f, 0.0f, 0.0f }; }
static const ColorRGB REDISH() { return { 1.0f, 0.5f, 0.5f }; }
Expand Down Expand Up @@ -112,12 +114,14 @@ class ColorRGBA
static const ColorRGBA BLACK() { return { 0.0f, 0.0f, 0.0f, 1.0f }; }
static const ColorRGBA BLUE() { return { 0.0f, 0.0f, 1.0f, 1.0f }; }
static const ColorRGBA BLUEISH() { return { 0.5f, 0.5f, 1.0f, 1.0f }; }
static const ColorRGBA CYAN() { return { 0.0f, 1.0f, 1.0f, 1.0f }; }
static const ColorRGBA DARK_GRAY() { return { 0.25f, 0.25f, 0.25f, 1.0f }; }
static const ColorRGBA DARK_YELLOW() { return { 0.5f, 0.5f, 0.0f, 1.0f }; }
static const ColorRGBA GRAY() { return { 0.5f, 0.5f, 0.5f, 1.0f }; }
static const ColorRGBA GREEN() { return { 0.0f, 1.0f, 0.0f, 1.0f }; }
static const ColorRGBA GREENISH() { return { 0.5f, 1.0f, 0.5f, 1.0f }; }
static const ColorRGBA LIGHT_GRAY() { return { 0.75f, 0.75f, 0.75f, 1.0f }; }
static const ColorRGBA MAGENTA() { return { 1.0f, 0.0f, 1.0f, 1.0f }; }
static const ColorRGBA ORANGE() { return { 0.923f, 0.504f, 0.264f, 1.0f }; }
static const ColorRGBA RED() { return { 1.0f, 0.0f, 0.0f, 1.0f }; }
static const ColorRGBA REDISH() { return { 1.0f, 0.5f, 0.5f, 1.0f }; }
Expand Down
44 changes: 21 additions & 23 deletions src/slic3r/GUI/3DBed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void Bed3D::load_render_colors()
RenderColor::colors[RenderCol_Axis_Z] = ImGuiWrapper::to_ImVec4(Bed3D::AXIS_Z_COLOR);
}

void Bed3D::Axes::render() const
void Bed3D::Axes::render()
{
auto render_axis = [this](const Transform3f& transform) {
glsafe(::glPushMatrix());
Expand All @@ -167,7 +167,7 @@ void Bed3D::Axes::render() const
};

if (!m_arrow.is_initialized())
const_cast<GLModel*>(&m_arrow)->init_from(stilized_arrow(16, DefaultTipRadius, DefaultTipLength, DefaultStemRadius, m_stem_length));
m_arrow.init_from(stilized_arrow(16, DefaultTipRadius, DefaultTipLength, DefaultStemRadius, m_stem_length));

GLShaderProgram* shader = wxGetApp().get_shader("gouraud_light");
if (shader == nullptr)
Expand All @@ -179,15 +179,15 @@ void Bed3D::Axes::render() const
shader->set_uniform("emission_factor", 0.0f);

// x axis
const_cast<GLModel*>(&m_arrow)->set_color(-1, AXIS_X_COLOR);
m_arrow.set_color(AXIS_X_COLOR);
render_axis(Geometry::assemble_transform(m_origin, { 0.0, 0.5 * M_PI, 0.0 }).cast<float>());

// y axis
const_cast<GLModel*>(&m_arrow)->set_color(-1, AXIS_Y_COLOR);
m_arrow.set_color(AXIS_Y_COLOR);
render_axis(Geometry::assemble_transform(m_origin, { -0.5 * M_PI, 0.0, 0.0 }).cast<float>());

// z axis
const_cast<GLModel*>(&m_arrow)->set_color(-1, AXIS_Z_COLOR);
m_arrow.set_color(AXIS_Z_COLOR);
render_axis(Geometry::assemble_transform(m_origin).cast<float>());

shader->stop_using();
Expand Down Expand Up @@ -340,14 +340,14 @@ void Bed3D::render(GLCanvas3D& canvas, bool bottom, float scale_factor, bool sho
void Bed3D::render_internal(GLCanvas3D& canvas, bool bottom, float scale_factor,
bool show_axes)
{
float* factor = const_cast<float*>(&m_scale_factor);
*factor = scale_factor;
m_scale_factor = scale_factor;

if (show_axes)
render_axes();

glsafe(::glEnable(GL_DEPTH_TEST));

m_model.set_color(-1, m_is_dark ? DEFAULT_MODEL_COLOR_DARK : DEFAULT_MODEL_COLOR);
m_model.set_color(m_is_dark ? DEFAULT_MODEL_COLOR_DARK : DEFAULT_MODEL_COLOR);

switch (m_type)
{
Expand Down Expand Up @@ -456,13 +456,13 @@ std::tuple<Bed3D::Type, std::string, std::string> Bed3D::detect_type(const Point
return { Type::Custom, {}, {} };
}

void Bed3D::render_axes() const
void Bed3D::render_axes()
{
if (m_build_volume.valid())
m_axes.render();
}

void Bed3D::render_system(GLCanvas3D& canvas, bool bottom) const
void Bed3D::render_system(GLCanvas3D& canvas, bool bottom)
{
if (!bottom)
render_model();
Expand All @@ -471,7 +471,7 @@ void Bed3D::render_system(GLCanvas3D& canvas, bool bottom) const
render_texture(bottom, canvas);*/
}

/*void Bed3D::render_texture(bool bottom, GLCanvas3D& canvas) const
/*void Bed3D::render_texture(bool bottom, GLCanvas3D& canvas)
{
GLTexture* texture = const_cast<GLTexture*>(&m_texture);
GLTexture* temp_texture = const_cast<GLTexture*>(&m_temp_texture);
Expand Down Expand Up @@ -656,34 +656,32 @@ GeometryBuffer Bed3D::update_bed_triangles() const
return new_triangles;
}

void Bed3D::render_model() const
void Bed3D::render_model()
{
if (m_model_filename.empty())
return;

GLModel* model = const_cast<GLModel*>(&m_model);

if (model->get_filename() != m_model_filename && model->init_from_file(m_model_filename)) {
model->set_color(-1, m_is_dark ? DEFAULT_MODEL_COLOR_DARK : DEFAULT_MODEL_COLOR);
if (m_model.get_filename() != m_model_filename && m_model.init_from_file(m_model_filename)) {
m_model.set_color(m_is_dark ? DEFAULT_MODEL_COLOR_DARK : DEFAULT_MODEL_COLOR);

update_model_offset();
}

if (!model->get_filename().empty()) {
if (!m_model.get_filename().empty()) {
GLShaderProgram* shader = wxGetApp().get_shader("gouraud_light");
if (shader != nullptr) {
shader->start_using();
shader->set_uniform("emission_factor", 0.0f);
glsafe(::glPushMatrix());
glsafe(::glTranslated(m_model_offset.x(), m_model_offset.y(), m_model_offset.z()));
model->render();
m_model.render();
glsafe(::glPopMatrix());
shader->stop_using();
}
}
}

void Bed3D::render_custom(GLCanvas3D& canvas, bool bottom) const
void Bed3D::render_custom(GLCanvas3D& canvas, bool bottom)
{
if (m_model_filename.empty()) {
render_default(bottom);
Expand All @@ -697,15 +695,15 @@ void Bed3D::render_custom(GLCanvas3D& canvas, bool bottom) const
render_texture(bottom, canvas);*/
}

void Bed3D::render_default(bool bottom) const
void Bed3D::render_default(bool bottom)
{
bool picking = false;
const_cast<GLTexture*>(&m_texture)->reset();
m_texture.reset();

unsigned int triangles_vcount = m_triangles.get_vertices_count();
const unsigned int triangles_vcount = m_triangles.get_vertices_count();
GeometryBuffer default_triangles = update_bed_triangles();
if (triangles_vcount > 0) {
bool has_model = !m_model.get_filename().empty();
const bool has_model = !m_model.get_filename().empty();

glsafe(::glEnable(GL_DEPTH_TEST));
glsafe(::glEnable(GL_BLEND));
Expand Down
14 changes: 7 additions & 7 deletions src/slic3r/GUI/3DBed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Bed3D
m_arrow.reset();
}
float get_total_length() const { return m_stem_length + DefaultTipLength; }
void render() const;
void render();
};

public:
Expand Down Expand Up @@ -159,12 +159,12 @@ class Bed3D
static std::tuple<Type, std::string, std::string> detect_type(const Pointfs& shape);
void render_internal(GLCanvas3D& canvas, bool bottom, float scale_factor,
bool show_axes);
void render_axes() const;
void render_system(GLCanvas3D& canvas, bool bottom) const;
//void render_texture(bool bottom, GLCanvas3D& canvas) const;
void render_model() const;
void render_custom(GLCanvas3D& canvas, bool bottom) const;
void render_default(bool bottom) const;
void render_axes();
void render_system(GLCanvas3D& canvas, bool bottom);
//void render_texture(bool bottom, GLCanvas3D& canvas);
void render_model();
void render_custom(GLCanvas3D& canvas, bool bottom);
void render_default(bool bottom);
void release_VBOs();
};

Expand Down
77 changes: 43 additions & 34 deletions src/slic3r/GUI/3DScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,10 @@ void GLVolume::SinkingContours::render()

void GLVolume::SinkingContours::update()
{
int object_idx = m_parent.object_idx();
Model& model = GUI::wxGetApp().plater()->model();
const int object_idx = m_parent.object_idx();
const Model& model = GUI::wxGetApp().plater()->model();

if (0 <= object_idx && object_idx < (int)model.objects.size() && m_parent.is_sinking() && !m_parent.is_below_printbed()) {
if (0 <= object_idx && object_idx < int(model.objects.size()) && m_parent.is_sinking() && !m_parent.is_below_printbed()) {
const BoundingBoxf3& box = m_parent.transformed_convex_hull_bounding_box();
if (!m_old_box.size().isApprox(box.size()) || m_old_box.min.z() != box.min.z()) {
m_old_box = box;
Expand All @@ -344,28 +344,25 @@ void GLVolume::SinkingContours::update()
const TriangleMesh& mesh = model.objects[object_idx]->volumes[m_parent.volume_idx()]->mesh();

m_model.reset();
GUI::GLModel::InitializationData init_data;
GUI::GLModel::Geometry init_data;
init_data.format = { GUI::GLModel::Geometry::EPrimitiveType::Triangles, GUI::GLModel::Geometry::EVertexLayout::P3, GUI::GLModel::Geometry::EIndexType::UINT };
init_data.color = ColorRGBA::WHITE();
unsigned int vertices_counter = 0;
MeshSlicingParams slicing_params;
slicing_params.trafo = m_parent.world_matrix();
Polygons polygons = union_(slice_mesh(mesh.its, 0.0f, slicing_params));
for (ExPolygon &expoly : diff_ex(expand(polygons, float(scale_(HalfWidth))), shrink(polygons, float(scale_(HalfWidth))))) {
GUI::GLModel::InitializationData::Entity entity;
entity.type = GUI::GLModel::PrimitiveType::Triangles;
const Polygons polygons = union_(slice_mesh(mesh.its, 0.0f, slicing_params));
for (const ExPolygon& expoly : diff_ex(expand(polygons, float(scale_(HalfWidth))), shrink(polygons, float(scale_(HalfWidth))))) {
const std::vector<Vec3d> triangulation = triangulate_expolygon_3d(expoly);
init_data.vertices.reserve(init_data.vertices.size() + triangulation.size() * GUI::GLModel::Geometry::vertex_stride_floats(init_data.format));
init_data.indices.reserve(init_data.indices.size() + triangulation.size() * GUI::GLModel::Geometry::index_stride_bytes(init_data.format));
for (const Vec3d& v : triangulation) {
entity.positions.emplace_back(v.cast<float>() + Vec3f(0.0f, 0.0f, 0.015f)); // add a small positive z to avoid z-fighting
entity.normals.emplace_back(Vec3f::UnitZ());
const size_t positions_count = entity.positions.size();
if (positions_count % 3 == 0) {
entity.indices.emplace_back(positions_count - 3);
entity.indices.emplace_back(positions_count - 2);
entity.indices.emplace_back(positions_count - 1);
}
init_data.add_vertex((Vec3f)(v.cast<float>() + 0.015f * Vec3f::UnitZ())); // add a small positive z to avoid z-fighting
++vertices_counter;
if (vertices_counter % 3 == 0)
init_data.add_uint_triangle(vertices_counter - 3, vertices_counter - 2, vertices_counter - 1);
}
init_data.entities.emplace_back(entity);
}

m_model.init_from(init_data);
m_model.init_from(std::move(init_data));
}
else
m_shift = box.center() - m_old_box.center();
Expand Down Expand Up @@ -1247,6 +1244,9 @@ void GLVolumeCollection::render(
if (shader == nullptr)
return;

GLShaderProgram* sink_shader = GUI::wxGetApp().get_shader("flat");
GLShaderProgram* edges_shader = GUI::wxGetApp().get_shader("flat");

if (type == ERenderType::Transparent) {
glsafe(::glEnable(GL_BLEND));
glsafe(::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
Expand All @@ -1272,13 +1272,18 @@ void GLVolumeCollection::render(
#endif // ENABLE_MODIFIERS_ALWAYS_TRANSPARENT

// render sinking contours of non-hovered volumes
if (m_show_sinking_contours)
if (volume.first->is_sinking() && !volume.first->is_below_printbed() &&
volume.first->hover == GLVolume::HS_None && !volume.first->force_sinking_contours) {
shader->stop_using();
volume.first->render_sinking_contours();
shader->start_using();
shader->stop_using();
if (sink_shader != nullptr) {
sink_shader->start_using();
if (m_show_sinking_contours) {
if (volume.first->is_sinking() && !volume.first->is_below_printbed() &&
volume.first->hover == GLVolume::HS_None && !volume.first->force_sinking_contours) {
volume.first->render_sinking_contours();
}
}
sink_shader->stop_using();
}
shader->start_using();

glsafe(::glEnableClientState(GL_VERTEX_ARRAY));
glsafe(::glEnableClientState(GL_NORMAL_ARRAY));
Expand Down Expand Up @@ -1341,17 +1346,21 @@ void GLVolumeCollection::render(
}

if (m_show_sinking_contours) {
for (GLVolumeWithIdAndZ& volume : to_render) {
// render sinking contours of hovered/displaced volumes
if (volume.first->is_sinking() && !volume.first->is_below_printbed() &&
(volume.first->hover != GLVolume::HS_None || volume.first->force_sinking_contours)) {
shader->stop_using();
glsafe(::glDepthFunc(GL_ALWAYS));
volume.first->render_sinking_contours();
glsafe(::glDepthFunc(GL_LESS));
shader->start_using();
shader->stop_using();
if (sink_shader != nullptr) {
sink_shader->start_using();
for (GLVolumeWithIdAndZ& volume : to_render) {
// render sinking contours of hovered/displaced volumes
if (volume.first->is_sinking() && !volume.first->is_below_printbed() &&
(volume.first->hover != GLVolume::HS_None || volume.first->force_sinking_contours)) {
glsafe(::glDepthFunc(GL_ALWAYS));
volume.first->render_sinking_contours();
glsafe(::glDepthFunc(GL_LESS));
}
}
sink_shader->start_using();
}
shader->start_using();
}

if (disable_cullface)
Expand Down
Loading

0 comments on commit fdf8b11

Please sign in to comment.