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
This commit is contained in:
lane.wei 2023-04-06 21:21:44 +08:00 committed by Lane.Wei
parent 031d61faca
commit 90760e0d07
6 changed files with 86 additions and 11 deletions

View File

@ -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);
}

View File

@ -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();
}

View File

@ -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. // All transforms have been dealt with. Now ensure that the objects are on bed.
// (Unless the user said otherwise.) // (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")) //if (m_config.opt_bool("ensure_on_bed"))
for (auto &model : m_models) // for (auto &model : m_models)
for (auto &o : model.objects) // for (auto &o : model.objects)
o->ensure_on_bed(); // o->ensure_on_bed();
// loop through action options // loop through action options
bool export_to_3mf = false, load_slicedata = false, export_slicedata = false, export_slicedata_error = false; 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; outfile = outfile_final;
}*/ }*/
// Run the post-processing scripts if defined. // 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; BOOST_LOG_TRIVIAL(info) << "Slicing result exported to " << outfile << std::endl;
part_plate->update_slice_result_valid_state(true); part_plate->update_slice_result_valid_state(true);
#if defined(__linux__) || defined(__LINUX__) #if defined(__linux__) || defined(__LINUX__)
@ -2359,7 +2359,7 @@ int CLI::run(int argc, char **argv)
} }
ThumbnailsParams thumbnail_params; ThumbnailsParams thumbnail_params;
GLShaderProgram* shader = opengl_mgr.get_shader("gouraud_light"); GLShaderProgram* shader = opengl_mgr.get_shader("thumbnail");
if (!shader) { if (!shader) {
BOOST_LOG_TRIVIAL(error) << boost::format("can not get shader for rendering thumbnail"); BOOST_LOG_TRIVIAL(error) << boost::format("can not get shader for rendering thumbnail");
} }

View File

@ -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, 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) 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; ModelObjectPtrs& model_objects = GUI::wxGetApp().model().objects;
std::vector<std::array<float, 4>> colors = ::get_extruders_colors(); std::vector<std::array<float, 4>> colors = ::get_extruders_colors();
switch (OpenGLManager::get_framebuffers_type()) 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) { auto is_visible = [plate_idx, plate_build_volume](const GLVolume& v) {
bool ret = v.printable; bool ret = v.printable;
if (plate_idx >= 0) { 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 { else {
ret &= (!v.shader_outside_printer_detection_enabled || !v.is_outside); 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.merge(vol->transformed_bounding_box());
} }
} }
volumes_box.min.z() = -Slic3r::BuildVolume::SceneEpsilon;
double width = volumes_box.max.x() - volumes_box.min.x(); double width = volumes_box.max.x() - volumes_box.min.x();
double depth = volumes_box.max.y() - volumes_box.min.y(); double depth = volumes_box.max.y() - volumes_box.min.y();
double height = volumes_box.max.z() - volumes_box.min.z(); 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]; curr_color[3] = vol->color[3];
shader->set_uniform("uniform_color", curr_color); shader->set_uniform("uniform_color", curr_color);
shader->set_uniform("volume_world_matrix", vol->world_matrix());
//BBS set all volume to orange //BBS set all volume to orange
//shader->set_uniform("uniform_color", orange); //shader->set_uniform("uniform_color", orange);
/*if (plate_idx > 0) { /*if (plate_idx > 0) {

View File

@ -35,6 +35,8 @@ std::pair<bool, std::string> GLShadersManager::init()
// used to render bed axes and model, selection hints, gcode sequential view marker model, preview shells, options in gcode preview // 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" }); 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 // used to render first layer for calibration
valid &= append_shader("cali", { "cali.vs", "cali.fs"}); valid &= append_shader("cali", { "cali.vs", "cali.fs"});
// used to render printbed // used to render printbed

View File

@ -252,13 +252,18 @@ bool OpenGLManager::init_gl()
s_framebuffers_type = EFramebufferType::Arb; s_framebuffers_type = EFramebufferType::Arb;
BOOST_LOG_TRIVIAL(info) << "Found Framebuffer Type ARB."<< std::endl; 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; s_framebuffers_type = EFramebufferType::Ext;
else }
else {
s_framebuffers_type = EFramebufferType::Unknown; 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); bool valid_version = s_gl_info.is_version_greater_or_equal_to(2, 0);
if (!valid_version) { if (!valid_version) {
BOOST_LOG_TRIVIAL(warning) << "Found opengl version <= 2.0"<< std::endl;
// Complain about the OpenGL version. // Complain about the OpenGL version.
wxString message = from_u8((boost::format( wxString message = from_u8((boost::format(
_utf8(L("The application cannot run normally because OpenGL version is lower than 2.0.\n")))).str()); _utf8(L("The application cannot run normally because OpenGL version is lower than 2.0.\n")))).str());