From 90760e0d075b892c596d039e9cf8db7b9d5ec61d Mon Sep 17 00:00:00 2001 From: "lane.wei" Date: Thu, 6 Apr 2023 21:21:44 +0800 Subject: [PATCH] ENH: refine the thumbnail rendering 1. add specific shader for thumbnail 2. do not render the negative-z part 3. fix the sinking related issue under cli Change-Id: Ia59083437544c1c0bd924c811274ccbb137e9eb7 --- resources/shaders/thumbnail.fs | 16 +++++++++++ resources/shaders/thumbnail.vs | 43 +++++++++++++++++++++++++++++ src/BambuStudio.cpp | 12 ++++---- src/slic3r/GUI/GLCanvas3D.cpp | 13 +++++++-- src/slic3r/GUI/GLShadersManager.cpp | 2 ++ src/slic3r/GUI/OpenGLManager.cpp | 11 ++++++-- 6 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 resources/shaders/thumbnail.fs create mode 100644 resources/shaders/thumbnail.vs diff --git a/resources/shaders/thumbnail.fs b/resources/shaders/thumbnail.fs new file mode 100644 index 000000000..4b269734e --- /dev/null +++ b/resources/shaders/thumbnail.fs @@ -0,0 +1,16 @@ +#version 110 + +uniform vec4 uniform_color; +uniform float emission_factor; + +// x = tainted, y = specular; +varying vec2 intensity; +//varying float drop; +varying vec4 world_pos; + +void main() +{ + if (world_pos.z < 0.0) + discard; + gl_FragColor = vec4(vec3(intensity.y) + uniform_color.rgb * (intensity.x + emission_factor), uniform_color.a); +} diff --git a/resources/shaders/thumbnail.vs b/resources/shaders/thumbnail.vs new file mode 100644 index 000000000..a02248f28 --- /dev/null +++ b/resources/shaders/thumbnail.vs @@ -0,0 +1,43 @@ +#version 110 + +#define INTENSITY_CORRECTION 0.6 + +// normalized values for (-0.6/1.31, 0.6/1.31, 1./1.31) +const vec3 LIGHT_TOP_DIR = vec3(-0.4574957, 0.4574957, 0.7624929); +#define LIGHT_TOP_DIFFUSE (0.8 * INTENSITY_CORRECTION) +#define LIGHT_TOP_SPECULAR (0.125 * INTENSITY_CORRECTION) +#define LIGHT_TOP_SHININESS 20.0 + +// normalized values for (1./1.43, 0.2/1.43, 1./1.43) +const vec3 LIGHT_FRONT_DIR = vec3(0.6985074, 0.1397015, 0.6985074); +#define LIGHT_FRONT_DIFFUSE (0.3 * INTENSITY_CORRECTION) + +#define INTENSITY_AMBIENT 0.3 + +uniform mat4 volume_world_matrix; +// x = tainted, y = specular; +varying vec2 intensity; +varying vec4 world_pos; + +void main() +{ + // First transform the normal into camera space and normalize the result. + vec3 normal = normalize(gl_NormalMatrix * gl_Normal); + + // Compute the cos of the angle between the normal and lights direction. The light is directional so the direction is constant for every vertex. + // Since these two are normalized the cosine is the dot product. We also need to clamp the result to the [0,1] range. + float NdotL = max(dot(normal, LIGHT_TOP_DIR), 0.0); + + intensity.x = INTENSITY_AMBIENT + NdotL * LIGHT_TOP_DIFFUSE; + vec3 position = (gl_ModelViewMatrix * gl_Vertex).xyz; + intensity.y = LIGHT_TOP_SPECULAR * pow(max(dot(-normalize(position), reflect(-LIGHT_TOP_DIR, normal)), 0.0), LIGHT_TOP_SHININESS); + + // Perform the same lighting calculation for the 2nd light source (no specular applied). + NdotL = max(dot(normal, LIGHT_FRONT_DIR), 0.0); + intensity.x += NdotL * LIGHT_FRONT_DIFFUSE; + + // Point in homogenous coordinates. + world_pos = volume_world_matrix * gl_Vertex; + + gl_Position = ftransform(); +} diff --git a/src/BambuStudio.cpp b/src/BambuStudio.cpp index adfc3aac2..a56d650aa 100644 --- a/src/BambuStudio.cpp +++ b/src/BambuStudio.cpp @@ -1765,11 +1765,11 @@ int CLI::run(int argc, char **argv) // All transforms have been dealt with. Now ensure that the objects are on bed. // (Unless the user said otherwise.) - //BBS: current only support models on bed + //BBS: current only support models on bed, 0407 sinking supported //if (m_config.opt_bool("ensure_on_bed")) - for (auto &model : m_models) - for (auto &o : model.objects) - o->ensure_on_bed(); + // for (auto &model : m_models) + // for (auto &o : model.objects) + // o->ensure_on_bed(); // loop through action options bool export_to_3mf = false, load_slicedata = false, export_slicedata = false, export_slicedata_error = false; @@ -2092,7 +2092,7 @@ int CLI::run(int argc, char **argv) outfile = outfile_final; }*/ // Run the post-processing scripts if defined. - run_post_process_scripts(outfile, print->full_print_config()); + //run_post_process_scripts(outfile, print->full_print_config()); BOOST_LOG_TRIVIAL(info) << "Slicing result exported to " << outfile << std::endl; part_plate->update_slice_result_valid_state(true); #if defined(__linux__) || defined(__LINUX__) @@ -2359,7 +2359,7 @@ int CLI::run(int argc, char **argv) } ThumbnailsParams thumbnail_params; - GLShaderProgram* shader = opengl_mgr.get_shader("gouraud_light"); + GLShaderProgram* shader = opengl_mgr.get_shader("thumbnail"); if (!shader) { BOOST_LOG_TRIVIAL(error) << boost::format("can not get shader for rendering thumbnail"); } diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index d16aaae1f..4aa80e018 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -1983,7 +1983,7 @@ void GLCanvas3D::render_thumbnail(ThumbnailData& thumbnail_data, unsigned int w, void GLCanvas3D::render_thumbnail(ThumbnailData& thumbnail_data, unsigned int w, unsigned int h, const ThumbnailsParams& thumbnail_params, const GLVolumeCollection& volumes, Camera::EType camera_type, bool use_top_view, bool for_picking) { - GLShaderProgram* shader = wxGetApp().get_shader("gouraud_light"); + GLShaderProgram* shader = wxGetApp().get_shader("thumbnail"); ModelObjectPtrs& model_objects = GUI::wxGetApp().model().objects; std::vector> colors = ::get_extruders_colors(); switch (OpenGLManager::get_framebuffers_type()) @@ -5429,7 +5429,14 @@ void GLCanvas3D::render_thumbnail_internal(ThumbnailData& thumbnail_data, const auto is_visible = [plate_idx, plate_build_volume](const GLVolume& v) { bool ret = v.printable; if (plate_idx >= 0) { - ret &= plate_build_volume.contains(v.transformed_convex_hull_bounding_box()); + bool contained = false; + BoundingBoxf3 plate_bbox = plate_build_volume; + plate_bbox.min(2) = -1e10; + const BoundingBoxf3& volume_bbox = v.transformed_convex_hull_bounding_box(); + if (plate_bbox.contains(volume_bbox) && (volume_bbox.max(2) > 0)) { + contained = true; + } + ret &= contained; } else { ret &= (!v.shader_outside_printer_detection_enabled || !v.is_outside); @@ -5461,6 +5468,7 @@ void GLCanvas3D::render_thumbnail_internal(ThumbnailData& thumbnail_data, const volumes_box.merge(vol->transformed_bounding_box()); } } + volumes_box.min.z() = -Slic3r::BuildVolume::SceneEpsilon; double width = volumes_box.max.x() - volumes_box.min.x(); double depth = volumes_box.max.y() - volumes_box.min.y(); double height = volumes_box.max.z() - volumes_box.min.z(); @@ -5592,6 +5600,7 @@ void GLCanvas3D::render_thumbnail_internal(ThumbnailData& thumbnail_data, const curr_color[3] = vol->color[3]; shader->set_uniform("uniform_color", curr_color); + shader->set_uniform("volume_world_matrix", vol->world_matrix()); //BBS set all volume to orange //shader->set_uniform("uniform_color", orange); /*if (plate_idx > 0) { diff --git a/src/slic3r/GUI/GLShadersManager.cpp b/src/slic3r/GUI/GLShadersManager.cpp index c7d1c2647..107fcb562 100644 --- a/src/slic3r/GUI/GLShadersManager.cpp +++ b/src/slic3r/GUI/GLShadersManager.cpp @@ -35,6 +35,8 @@ std::pair GLShadersManager::init() // used to render bed axes and model, selection hints, gcode sequential view marker model, preview shells, options in gcode preview valid &= append_shader("gouraud_light", { "gouraud_light.vs", "gouraud_light.fs" }); + //used to render thumbnail + valid &= append_shader("thumbnail", { "thumbnail.vs", "thumbnail.fs" }); // used to render first layer for calibration valid &= append_shader("cali", { "cali.vs", "cali.fs"}); // used to render printbed diff --git a/src/slic3r/GUI/OpenGLManager.cpp b/src/slic3r/GUI/OpenGLManager.cpp index 1e6b6cd7e..5d8e90ead 100644 --- a/src/slic3r/GUI/OpenGLManager.cpp +++ b/src/slic3r/GUI/OpenGLManager.cpp @@ -251,14 +251,19 @@ bool OpenGLManager::init_gl() if (GLEW_ARB_framebuffer_object) { s_framebuffers_type = EFramebufferType::Arb; BOOST_LOG_TRIVIAL(info) << "Found Framebuffer Type ARB."<< std::endl; - } - else if (GLEW_EXT_framebuffer_object) + } + else if (GLEW_EXT_framebuffer_object) { + BOOST_LOG_TRIVIAL(info) << "Found Framebuffer Type Ext."<< std::endl; s_framebuffers_type = EFramebufferType::Ext; - else + } + else { s_framebuffers_type = EFramebufferType::Unknown; + BOOST_LOG_TRIVIAL(warning) << "Found Framebuffer Type unknown!"<< std::endl; + } bool valid_version = s_gl_info.is_version_greater_or_equal_to(2, 0); if (!valid_version) { + BOOST_LOG_TRIVIAL(warning) << "Found opengl version <= 2.0"<< std::endl; // Complain about the OpenGL version. wxString message = from_u8((boost::format( _utf8(L("The application cannot run normally because OpenGL version is lower than 2.0.\n")))).str());