mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-11-30 08:55:26 -05:00
Offer the Sliced Objects as a Solid Model While Dragging the Preview (#15818)
* Offer the Sliced Objects as a Solid Model While Dragging The G-code preview draws every toolpath segment as an instanced box, and its frame cost is linear in the number of segments drawn. On a plate of large objects that is enough that dragging the camera or a preview slider cannot keep up, and no amount of per-segment work changes that; only drawing fewer segments does. The objects themselves are cheaper: a mesh costs its triangles once, however many layers it has, and the preview already loads the objects as shells for its translucent ghost. A new preference, "Only render solid model when dragging" (off by default), draws those shells opaque in their filament colours instead of the toolpaths while the user drags. The visible layer range still holds. The shells are cut at the range's top and bottom through the gouraud shader's z range, and libvgcode keeps a second index buffer holding just the range's bottom and top layers, filled in the same walk as the full one, which is drawn afterwards so that it caps the cut with what was really printed there. Switching between the two sets is a buffer binding, never a rebuild. The prime tower is added to the shells from its sliced mesh while the preference is on, positioned as the print placed it; it keeps its opaque colour and so stays out of the translucent ghost. Supports have no mesh and are not shown. Dragging is the camera, the navigator or either slider being held; a slider reports it from ImGui's active id, since its dirty flag is raised and consumed inside one frame. A wheel step holds the solid model for a 150 ms settle time, with the frame that restores the toolpaths scheduled for when it runs out. The switch is decided at the top of the frame, before the cached scene is consulted, and a frame that switches redraws the scene. A drag cut short by focus or capture loss is ended explicitly, and a button release wakes the idle loop, since on some platforms no idle event follows it until the next input. With the preference off, nothing is built and the preview is unchanged. * Release the Reduced Set When Unused and Share the Drag Check With the Scene Cache The reduced index buffers were only re-uploaded while the preference was on, so the last set stayed allocated until the next load once it was switched off, and their size was missing from get_used_gpu_memory(). They are now uploaded on every rebuild, empty when nothing was built, and counted. The scene cache and the solid model both asked whether the user was dragging, with different lists: the cache knew about gizmos and the rectangle selection, the solid model about the navigator and the sliders. One is_user_interacting() now answers both. The tooltip says that negative volumes are not cut out of the solid model, since load_shells() drops every non-model-part volume, and the preferences handler keeps the dimming comment with the branch it documents. * Let the User Choose What the Preview Draws While Dragging The solid model is one answer to a preview that cannot keep up with a drag; a reviewer asked for the two that keep the drag view made of toolpaths. The checkbox is now a combobox, "Simplify preview while dragging", with Off, Solid model, Skip layers and Outer walls, and a spin, "Draw one layer in every N", for the toolpath modes. Off is the default, and with it nothing is built. Skip layers keeps one layer in every N. Outer walls keeps the outer and overhang perimeters of those layers, so the prime tower and supports, whose segments have other roles, drop out of it. Both keep the bottom and top layers of the visible range whole, as the solid model does, so the faces the range cuts open stay what was printed there. libvgcode takes the choice as EReducedDetailMode and a stride; the sets are rebuilt when either changes, and drawing from the reduced one is still a buffer binding. A mode change reaches the loaded preview at once, as the checkbox did. * Add a Shell-Only Drag Mode That Keeps What Can Be Seen From Outside A third mode of the drag preference: the reduced set keeps every role but the sparse infill, internal solid infill, internal bridge infill and gap fill, which lie under the skins or between the walls, on every layer. Walls, top and bottom surfaces, bridges, supports and the prime tower are drawn whole, so from outside the print looks as it does at rest, and the two end layers of the visible range stay whole as in every mode. The roles come from the slicer, so the classification is exact wherever the slicer's is. What a role cannot tell apart is the inside of the prime tower or of a support from its outside; both are kept whole. A profile with no top or bottom shell layers exposes infill, which this mode hides. * Keep Only the Solid Model and Shell Modes While Dragging The skip-layers and outer-walls modes drew one layer in every N, and every visual complaint on the review came from the gaps that leaves at close range. The shell mode draws every layer and looks the same from outside, and the solid model is the one mode whose cost does not depend on the toolpaths, so the two of them cover what the stride modes were for. The layer stride goes with them; a saved preference naming a removed mode falls back to off. * Hide Infill in the Shell Drag Mode Only Where the Profile Covers It A profile with no top or bottom shell layers, or no walls, leaves the infill on the surface, so hiding it changed the picture. The viewer now reads the print's default region settings when it loads a print and leaves the infill in place for such a print; per-object overrides are not consulted. * Keep the Ghost and Cap the Cut of the Solid Model While Dragging In the solid model mode the translucent ghost disappeared for the length of every drag, since the scene drew either it or the solid; it is now drawn in every mode, so the part above the cut stays in view. The cut itself was open: a sparse layer on the cut showed, between its lines, whatever lay below inside the hollow shell, the first layer or the bed, which moved with the camera and read as a flickering shadow; and a face of the model in the cut layer's own plane fought that layer's toolpaths pixel by pixel. The solid now runs from the top of the range's bottom layer to the bottom of its top layer, and a stencil pass caps each cut plane, body by body, with a quad in the body's colour darkened where the cut has opened it: back faces less front faces, counted with the depth test off, which leaves a closed body at zero. * Leave the Preview Untouched While Dragging With the Mode Off The reduced flag was raised on every drag whatever the mode. libvgcode still drew the full set with the mode off, but the canvas reads the flag: it redrew the scene on the first and last frame of each drag, scheduled an extra frame after every wheel burst, and ended a drag that lost focus or capture. The flag is now raised only when a mode is set. The realistic view's shadow-caster pass draws whichever set is bound, and the static shadow map's key did not say which, so a map built during a drag stayed thinned after release; the key now includes it. * Tidy the Drag Mode: Cheaper Caps and No Leftovers The solid model's cap quads are built once per frame and recoloured per body instead of being rebuilt for each, and the stencil count draws each body directly: going through the collection's render built the full preset config and sorted every volume once per body, per frame. A one-line settings wrapper is inlined, the function that added or removed the tower with the mode is folded into its one caller, the two libvgcode getters nobody calls are removed, and the wheel's settle time is set where the wheel is handled. The Shell only tooltip names internal bridges, which the mode hides. * Cut the Solid Model at the Toolpaths' Heights on a Printer With a Z Offset The cut heights took the printer's z offset out of the layer heights, which the G-code processor has already taken it out of, so on a printer with a z offset the solid was cut one offset away from the layers drawn as toolpaths: a gap under the top one and an overlap with the bottom one, and the caps sat off the solid's cut plane. The layer heights are in the shells' frame as they are. * Test That AppConfig Keeps Only the Preview Drag Modes It Knows A missing mode becomes off, the three modes survive, and a value from an earlier revision or an empty one falls back to off. * Hold the Navigator's Drag for as Long as the Button Is Held The navigator reports a drag only on a frame the mouse moved in, and the flag is written by the overlay, after the frame has chosen which toolpath set to draw. A still frame in the middle of a drag dropped the flag, so the next one drew the full set, and a release with no motion left the reduced set on screen until the next mouse event. The flag now holds while the button does, and the drag check reads the button itself. * Keep the Toolpaths While Dragging a Print Whose Layers Do Not Rise The solid model is cut at the heights of the layer range's two ends, one range for the whole plate. Printed object by object, the layer heights start over with each object, so those two heights do not bound the range: an earlier object was cut at a later one's height, and a range across two objects could come out empty. Such a print keeps its toolpaths while dragging, as a plate with no shells does. * Cut the Solid Model Past a Layer With No Extrusion A layer with no extrusion has a height of zero, and the solid model read its cut heights from the layers at the ends of the range as they were: with such a layer under the top of the range the whole solid was cut away. The nearest layer below with a height stands in for it. * Keep the Toolpaths While Dragging a Preview Cut by the Section View The section view cuts the toolpaths open to show their inside. The solid model was drawn with no clipping plane, so while dragging it covered the section whole and the section came back on release. A preview with a section active now keeps its toolpaths while dragging, as a print made object by object does; the shell mode draws through the same plane as the full set and is unaffected. * Keep the Supports While Dragging in the Solid Model Mode The solid model stands in for the sliced objects, which are the only shells the preview loads: a support has no mesh, so the moment a drag started everything under an overhang vanished and the object sat on nothing until release. The reduced set now keeps the support roles whole on every layer, as the shell mode does, and the supports draw as toolpaths beside the solid. * Draw the Layers Down to the Nearest Infill Whole at Each End of the Range The reduced set kept the two end layers of the visible range whole, and the solid model was cut right under the top one, as though every layer carried every role. Infill combination prints the sparse infill of a few layers in one and leaves the others walls alone, and a support layer with no object layer at its height gets a layer of its own, so a cut under such a layer capped the print where at rest the infill of the layer below shows through: a flat disc inside the walls while dragging, the infill grid on release. Each end of the range now runs to the nearest layer with sparse infill, at most eight layers deep, in both modes, and the solid is cut at the edges of those bands. The layer table is the whole plate's, so another object's solid infill on a layer does not end the band while sparse infill is within reach; a print with none in reach falls back to the nearest layer with any infill, else keeps the end layer alone. The layer table records which layers have infill when the toolpaths are loaded, and the bands are covered by tests. * Colour the Solid Model as the View Colours the Toolpaths The solid model and its caps wore the filament colours whatever the view, so a drag in the Line type view turned an orange-walled, red-filled print into a block of its filament colour. The solid stands in for the toolpaths, so it now takes their colours: in a view that colours by something other than the filament, the body the outer wall's, each cap the infill's of the layer it is cut under, sparse before solid, and the prime tower its own role's, each sampled from the first vertex of the role through the viewer. The filament views keep every body its own colour. The colours are resolved when a drag starts and when the view type or the end bands change. * Close the End Bands Object by Object The end bands of the reduced set stopped at the nearest layer with sparse infill anywhere on the plate. Two objects rarely combine their infill on the same layers, so where one object still had sparse infill on a layer the other's walls-only layer closed the band, and that object was capped right under it: a flat disc inside the cone's walls while the funnel beside it printed its last infill. The G-code labels its moves with their object, so the wrapper that feeds libvgcode now tallies walls and infill per object and layer and hands the viewer the layers whose infill closes every object printed on them; the bands stop at those. Sparse infill closes an object on a layer. Solid infill closes it only where the object has no sparse infill left in the direction the band looks from, its top shells as seen from above and its bottom shells as seen from below, since a layer whose sparse infill was combined into another still carries the solid infill under its skins and is open; an object with no sparse infill anywhere is closed by any infill. A range from the print's first layer has no bottom band, as nothing is cut under that layer. A preview built from the print, or one whose G-code labels no object, carries no such list and keeps the judgement by the roles of the whole layer. The G-code processor read the object only from the "start printing object" line that Bambu printers add; every printer writes "printing object <name> id:<n> copy <m>", so it reads that too, and takes the id after the last "id:" so that a colon in the name does not mislead it. The printable-area check that groups extrusions by that id now sees the objects of such a print one by one, as it does for a Bambu print, instead of as one. * Colour the Solid Model From the Grade Most of the Role Was Printed At The solid model took each colour from the first vertex of the role in the sampled layer. The first vertex of an outer wall is the start of its extrusion, still on the acceleration ramp, so under Actual speed and Actual flow the whole solid showed the slowest grade of the legend, and under Fan speed the body colour jumped between drags with whichever layer happened to be the top cut. Viewer::find_extrusion_vertex() now takes a layer interval and, in a view that grades the toolpaths by a value, returns the extrusion of the role at the length-weighted median of that value over up to eight layers spread across the interval; in any other view the first extrusion found, as before. The body and the prime tower are sampled over the layers the solid stands for, each cap in its own cut layer. * Count Each Cap of the Solid Model Against Its Own Cut With the bottom slider above the first layer the solid model is cut at both ends, and the one stencil count that both caps were drawn through counted the body with both cuts applied. An open tube counts a view ray through both openings as closed, so the top cap had a hole through which the infill toolpaths showed, and a ray in through a wall and out through the bottom opening as open, so from a low angle the top cap's quad, which spans the bounding box, was drawn in the air beside the body. Each cap now gets its own count, with the body cut at that cap alone and whole at its other end, which is odd exactly where the cap plane lies inside the body from any viewpoint. The second body pass per volume only runs when the bottom is cut. * Let the Solid Model Cast and Take the Realistic View's Shadows The realistic view's shadow map was built from the toolpath segments alone, and while dragging in the solid model mode those are only the end bands and the supports, so the solid threw no shadow at all. Its own pass also forced the shadow intensity to zero, so it took none either. In that mode the shells, cut at the solid's heights, are now drawn depth-only into the same map through the flat clip shader, and the solid samples the map back with the light the canvas passed on, so a body shades the plate, its neighbours and the end-band toolpaths as the toolpaths it stands in for would, and takes their shadows in turn. The map's key also carries the drag mode, so a map built with the solid is not kept for the shell mode. * Keep the Reduced Set Up While a Navigator Click Turns the Camera A click on a face or a corner of the 3D navigator makes it turn the camera over the forty frames that follow, with no button held. Each of those frames was drawn with the full toolpaths, since nothing counted as a drag, and on a weak GPU the turn took seconds. The overlay reports each of those frames as a change that is not a drag, so the canvas holds a flag for as long as that lasts and counts it as interacting; the frame after the last one is requested so that the full toolpaths come back on their own. * Let the Cut Faces of the Solid Model Cast Their Shadow The solid model was drawn into the shadow map as the shells cut at the same heights, open where it is cut, on the ground that from the light an open body's far wall closes its outline. It does, but not its depth: a ray through the opening passes the end band's sparse infill lines and reaches the far wall, so on a flank below the light's side the solid carried the lattice of those lines, where the toolpaths it stands in for are in one uniform shadow under the opaque layers above. The shells now cast through a shader of their own: a back face that a ray reached through a cut is drawn at the depth of that cut, the nearest of the cut planes on the way back toward the light, and a front face keeps its own depth. A ray reaches a back face through a lit wall as well, and the cut's depth would then shadow that wall, so the front faces are drawn on their own first, into a depth texture of the viewer's, for the shader to tell the two apart. The shadow map has no stencil, so the camera pass's cap count could not be reused here. * Hold the Solid Model's Body Colour While a Slider Drag Moves the Span A slider drag shows the solid model like a camera drag does, and the body took a fresh sample at every step, from eight layers spread over the span of the moment. Where the walls' speed or fan differs from layer to layer, each step fell on another mix of layers and the length-weighted median flipped with it, so the body jumped between the grades of the legend for as long as the slider moved. The body and the tower are now sampled once, as the drag begins, and held until it ends; the next drag samples them afresh over the span it finds. Each cap still follows its own layer, as the toolpaths of that layer would. * Colour Each Body of the Solid Model From Its Own Toolpaths The solid model took one body colour for every object on the plate, sampled from all the walls of the layers it stands for, so an object printed at its own speeds wore the grade of the others while its toolpaths showed another. The viewer's sampler now takes a rectangle of the XY plane and counts only the extrusions that end inside it, and each shell volume samples its body and caps within its own footprint; the tower likewise, from its own role. Objects that overlap in XY share their samples, which is as close as the toolpaths come to naming their object. * Dim and Grey the Solid Model With the Layers It Stands For With "Dim lower layers" on, the toolpaths around the inspected layers are drawn at the preference's brightness, and with it off they go dark grey while the moves slider is short of the top layer's end. The solid model, which stands in for exactly those layers, was drawn at full brightness either way. The layers the solid stands for are never the ones a slider thumb lights, so its bodies, caps and tower now take the same brightness or grey the toolpath shader gives the rest of the print, decided by the rule that shader is given. * Cut the Solid Model by the Section View While the section view was on, a drag kept the full toolpaths, since the solid model would have covered what the view cuts them open to show. That held even where the plane cut nothing, so with the view on the drag mode gave nothing at all. The solid now takes the section plane as the toolpaths do: the bodies are clipped by it, the two height caps are drawn on its kept side, and the section face gets a cap of its own, counted on the body cut by that plane alone and drawn between the two heights, in the body's colour darkened like the others. What the plane cuts away casts no shadow. * Keep the Travels Between the End Layers of the Solid Model With Travel shown in the legend, the solid model kept the travels only on the layers at the ends of the range, so they vanished on every drag. They are kept on every layer now, as the supports are. The rule moves into Utils so the cases can be tested. * Drag Belt Printer Previews With the Toolpaths Instead of the Solid Model The solid model is cut by horizontal planes at the end layers' heights, but a belt printer's layers are planes tilted at the belt angle, with heights in the G-code's frame, so the cuts missed the body. On belt printers a drag keeps the toolpaths, as it does for a print whose layer heights start over per object. * Cast the Solid Model's Cut Faces Into the Shadow Map A band cut at both ends let the light in through the top cut and out through the bottom one past every wall, so only the top band's infill lines cast and the bed showed their grid. The cut faces are now counted and drawn into the shadow map as the visible pass draws them: in a depth and stencil target of the viewer's, since the map has no stencil, then copied into the map. This replaces the back-face depth rewrite of the cut caster shader, and both passes build the caps through one helper. * Describe the Solid Model's Cut-Face Shadows, Travels, Belt Fallback and Section Cut * Keep Parts Above the Visible Range Out of the Solid Model's Cut-Face Shadow The light's frustum ends at the highest visible layer, so its near plane cut through parts above the range (a bunny's ears) during the cut-face stencil count: only their far faces were counted, the count read them as open and the bottom cap cast an ear-shaped shadow on the bed. Clamp depth during the count so every face is counted. * Say in the Drag Option That Belt Printers Keep Their Toolpaths The solid model handles the section view, and belt printers fall back to the toolpaths; the description said the opposite of both. --------- Co-authored-by: Rodrigo Faselli <162915171+RF47@users.noreply.github.com>
This commit is contained in:
1 parent
320f1bb7fc
commit
46f7b0e719
35 files changed
+2053
-28
No files matched your search
@@ -39,6 +39,7 @@
|
||||
#include "slic3r/GUI/ImGuiWrapper.hpp"
|
||||
#include "slic3r/GUI/GLModel.hpp"
|
||||
#include "slic3r/GUI/GLShader.hpp"
|
||||
#include "slic3r/GUI/OpenGLManager.hpp"
|
||||
#include "slic3r/GUI/Gizmos/GLGizmoUtils.hpp"
|
||||
#include "libslic3r/Point.hpp"
|
||||
#include <math.h>
|
||||
@@ -59,6 +60,8 @@
|
||||
#include "libslic3r/GCode/GCodeProcessor.hpp"
|
||||
#include "libvgcode/include/GCodeInputData.hpp"
|
||||
#include "libvgcode/include/ColorRange.hpp"
|
||||
#include <cfloat>
|
||||
#include <initializer_list>
|
||||
#include <optional>
|
||||
#include "libslic3r_version.h"
|
||||
#include <wx/busycursor.h>
|
||||
@@ -1153,6 +1156,7 @@ GCodeViewer::GCodeViewer()
|
||||
GCodeViewer::~GCodeViewer()
|
||||
{
|
||||
reset();
|
||||
release_caster_depth();
|
||||
if (m_moves_slider) {
|
||||
delete m_moves_slider;
|
||||
m_moves_slider = nullptr;
|
||||
@@ -1381,6 +1385,8 @@ void GCodeViewer::load_as_gcode(const GCodeProcessorResult& gcode_result, const
|
||||
if (current_top_layer_only != required_top_layer_only)
|
||||
m_viewer.toggle_top_layer_only_view_range();
|
||||
|
||||
read_reduced_detail_preferences();
|
||||
|
||||
// ORCA: darken the layers the preview layer slider is not scrubbed to
|
||||
m_viewer.set_dim_previous_layers(get_app_config()->get_bool("preview_dim_previous_layers"));
|
||||
m_viewer.set_dim_previous_layers_brightness(0.01f * std::stoi(get_app_config()->get("preview_dim_previous_layers_brightness")));
|
||||
@@ -1650,6 +1656,20 @@ void GCodeViewer::load_as_gcode(const GCodeProcessorResult& gcode_result, const
|
||||
m_belt_layer_zs.clear();
|
||||
}
|
||||
|
||||
// The solid model is cut at the heights of the layer range's ends, which bound the range only
|
||||
// if the layers never drop: printed object by object, the heights start over with each object.
|
||||
// Two layers may share a height, as layers of objects with different layer heights can.
|
||||
m_layers_z_rising = true;
|
||||
float prev_z = 0.0f;
|
||||
for (size_t i = 0; i < m_viewer.get_layers_count() && m_layers_z_rising; ++i) {
|
||||
const float z = m_viewer.get_layer_z(i);
|
||||
// a layer with no extrusion has no height
|
||||
if (z == 0.0f)
|
||||
continue;
|
||||
m_layers_z_rising = z >= prev_z;
|
||||
prev_z = z;
|
||||
}
|
||||
|
||||
// #if !VGCODE_ENABLE_COG_AND_TOOL_MARKERS
|
||||
// const size_t vertices_count = m_viewer.get_vertices_count();
|
||||
// m_cog.reset();
|
||||
@@ -1742,6 +1762,10 @@ void GCodeViewer::load_as_gcode(const GCodeProcessorResult& gcode_result, const
|
||||
|
||||
// BBS: data for rendering color arrangement recommendation
|
||||
m_nozzle_nums = print.config().option<ConfigOptionFloats>("nozzle_diameter")->values.size();
|
||||
// the shell drag mode hides the infill only where the profile covers it: with no top or bottom
|
||||
// shell, or no walls, the infill is the surface. Per-object settings are not consulted.
|
||||
const PrintRegionConfig& region = print.default_region_config();
|
||||
m_viewer.set_reduced_detail_hide_infill(region.top_shell_layers.value > 0 && region.bottom_shell_layers.value > 0 && region.wall_loops.value > 0);
|
||||
// Orca hack: Hide filament group for non-bbl printers
|
||||
if (!print.is_BBL_printer()) m_nozzle_nums = 1;
|
||||
std::vector<int> filament_maps = print.get_filament_maps();
|
||||
@@ -1918,6 +1942,7 @@ void GCodeViewer::reset_shell()
|
||||
{
|
||||
m_shells.volumes.clear();
|
||||
m_shells.print_id = -1;
|
||||
m_shells.with_wipe_tower = false;
|
||||
m_shell_bounding_box = BoundingBoxf3();
|
||||
}
|
||||
|
||||
@@ -2178,6 +2203,10 @@ void GCodeViewer::render_scene(int canvas_width, int canvas_height)
|
||||
{
|
||||
glsafe(::glEnable(GL_DEPTH_TEST));
|
||||
render_shells();
|
||||
// while dragging in the solid model mode, the objects stand in for their toolpaths, cut to the
|
||||
// visible layer range; the toolpath set then holds only the range's bottom and top layers
|
||||
if (m_viewer.is_reduced_detail() && solid_model_enabled())
|
||||
render_solid_model(canvas_width, canvas_height);
|
||||
|
||||
if (m_viewer.get_extrusion_roles_count() == 0)
|
||||
return;
|
||||
@@ -2428,6 +2457,9 @@ size_t GCodeViewer::shadow_casters_signature() const
|
||||
m_paths_bounding_box.max.x(), m_paths_bounding_box.max.y(), m_paths_bounding_box.max.z() })
|
||||
boost::hash_combine(hash, value);
|
||||
boost::hash_combine(hash, m_viewer.is_top_layer_only_view_range());
|
||||
// the casters are drawn from the reduced set while dragging, with the solid model in its mode
|
||||
boost::hash_combine(hash, m_viewer.is_reduced_detail());
|
||||
boost::hash_combine(hash, static_cast<int>(m_reduced_detail_mode));
|
||||
for (size_t i = 0; i < libvgcode::GCODE_EXTRUSION_ROLES_COUNT; ++i)
|
||||
boost::hash_combine(hash, m_viewer.is_extrusion_role_visible(libvgcode::EGCodeExtrusionRole(i)));
|
||||
boost::hash_combine(hash, m_viewer.is_option_visible(libvgcode::EOptionType::Travels));
|
||||
@@ -2446,10 +2478,187 @@ void GCodeViewer::render_shadow_casters(const Transform3d& light_view_matrix, co
|
||||
libvgcode::convert(static_cast<Matrix4f>(light_view_matrix.matrix().cast<float>())),
|
||||
libvgcode::convert(static_cast<Matrix4f>(light_projection_matrix.matrix().cast<float>())),
|
||||
libvgcode::convert(static_cast<Vec3f>(light_position.cast<float>())));
|
||||
if (m_viewer.is_reduced_detail() && solid_model_enabled())
|
||||
render_solid_model_casters(light_view_matrix, light_projection_matrix);
|
||||
}
|
||||
|
||||
// The stencil count that tells where a body cut at one cap alone is open: with the depth test off,
|
||||
// its back faces add one and its front faces take one away, so a pixel where the ray leaves the
|
||||
// body through the opening ends nonzero. The caller draws the body between the two. Depth clamping
|
||||
// keeps the faces past the near plane in the count: the light's frustum ends at the top visible
|
||||
// layer, so without it a part above the range keeps only its far faces and counts as open.
|
||||
static void begin_cap_count()
|
||||
{
|
||||
glsafe(::glEnable(GL_DEPTH_CLAMP));
|
||||
glsafe(::glStencilMask(0xFF));
|
||||
glsafe(::glClear(GL_STENCIL_BUFFER_BIT));
|
||||
glsafe(::glStencilFunc(GL_ALWAYS, 0, 0xFF));
|
||||
glsafe(::glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_KEEP, GL_DECR_WRAP));
|
||||
glsafe(::glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_INCR_WRAP));
|
||||
glsafe(::glDepthMask(GL_FALSE));
|
||||
glsafe(::glDisable(GL_DEPTH_TEST));
|
||||
glsafe(::glDisable(GL_CULL_FACE));
|
||||
}
|
||||
|
||||
// After the count, what is drawn next lands only where the body was open.
|
||||
static void end_cap_count()
|
||||
{
|
||||
glsafe(::glDisable(GL_DEPTH_CLAMP));
|
||||
glsafe(::glEnable(GL_DEPTH_TEST));
|
||||
glsafe(::glDepthMask(GL_TRUE));
|
||||
glsafe(::glStencilFunc(GL_NOTEQUAL, 0, 0xFF));
|
||||
glsafe(::glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP));
|
||||
glsafe(::glStencilMask(0x00));
|
||||
}
|
||||
|
||||
// The solid casts as the toolpaths it stands in for would: the shells, cut at the same heights,
|
||||
// drawn depth-only into the shadow map the canvas has bound, and the cut faces with them. Without
|
||||
// its cut faces an open body lets the light through: down onto the bed through a band cut at both
|
||||
// ends, or onto its own far wall, with the end band's lines throwing their shadow through the
|
||||
// hollow. The cut faces are counted and drawn as render_solid_model() draws them, which takes a
|
||||
// stencil the map has not got, so they go into a depth target of the viewer's first, the map's
|
||||
// size, and are copied into the map from there.
|
||||
void GCodeViewer::render_solid_model_casters(const Transform3d& light_view_matrix, const Transform3d& light_projection_matrix)
|
||||
{
|
||||
const std::optional<std::array<float, 2>> z_range = solid_model_z_range();
|
||||
GLShaderProgram* shader = wxGetApp().get_shader("flat_clip");
|
||||
if (m_shells.volumes.empty() || !z_range.has_value() || shader == nullptr)
|
||||
return;
|
||||
|
||||
// the walls, cut by the section view as the camera sees it, so what it cuts away casts nothing
|
||||
shader->start_using();
|
||||
shader->set_uniform("projection_matrix", light_projection_matrix);
|
||||
shader->set_uniform("clipping_plane", m_clipping_plane);
|
||||
shader->set_uniform("z_range", *z_range);
|
||||
for (GLVolume* volume : m_shells.volumes.volumes) {
|
||||
const Transform3d world_matrix = volume->world_matrix();
|
||||
shader->set_uniform("volume_world_matrix", world_matrix);
|
||||
shader->set_uniform("view_model_matrix", light_view_matrix * world_matrix);
|
||||
volume->render();
|
||||
}
|
||||
shader->stop_using();
|
||||
|
||||
GLShaderProgram* copy_shader = wxGetApp().get_shader("depth_copy");
|
||||
GLint viewport[4] = { 0, 0, 0, 0 };
|
||||
glsafe(::glGetIntegerv(GL_VIEWPORT, viewport));
|
||||
// without a target of its own the body casts open
|
||||
if (copy_shader == nullptr || !init_caster_depth(viewport[2], viewport[3]))
|
||||
return;
|
||||
std::array<SolidModelCap, 3> caps;
|
||||
init_solid_model_caps(*z_range, caps);
|
||||
|
||||
GLint map_fbo = 0;
|
||||
glsafe(::glGetIntegerv(GL_FRAMEBUFFER_BINDING, &map_fbo));
|
||||
glsafe(::glBindFramebuffer(GL_FRAMEBUFFER, m_caster_fbo));
|
||||
glsafe(::glStencilMask(0xFF));
|
||||
glsafe(::glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
|
||||
glsafe(::glEnable(GL_STENCIL_TEST));
|
||||
shader->start_using();
|
||||
shader->set_uniform("projection_matrix", light_projection_matrix);
|
||||
for (GLVolume* volume : m_shells.volumes.volumes) {
|
||||
const Transform3d world_matrix = volume->world_matrix();
|
||||
for (SolidModelCap& cap : caps) {
|
||||
if (!cap.quad.is_initialized())
|
||||
continue;
|
||||
begin_cap_count();
|
||||
shader->set_uniform("z_range", cap.count_z_range);
|
||||
shader->set_uniform("clipping_plane", cap.count_plane);
|
||||
shader->set_uniform("volume_world_matrix", world_matrix);
|
||||
shader->set_uniform("view_model_matrix", light_view_matrix * world_matrix);
|
||||
volume->render();
|
||||
end_cap_count();
|
||||
shader->set_uniform("z_range", cap.quad_z_range);
|
||||
shader->set_uniform("clipping_plane", cap.quad_plane);
|
||||
shader->set_uniform("volume_world_matrix", Transform3d::Identity());
|
||||
shader->set_uniform("view_model_matrix", light_view_matrix);
|
||||
cap.quad.render();
|
||||
}
|
||||
}
|
||||
shader->stop_using();
|
||||
glsafe(::glStencilMask(0xFF));
|
||||
glsafe(::glDisable(GL_STENCIL_TEST));
|
||||
|
||||
// into the map, where nearer than what is there
|
||||
glsafe(::glBindFramebuffer(GL_FRAMEBUFFER, static_cast<GLuint>(map_fbo)));
|
||||
GLModel screen;
|
||||
GLModel::Geometry data;
|
||||
data.format = { GLModel::Geometry::EPrimitiveType::Triangles, GLModel::Geometry::EVertexLayout::P3 };
|
||||
data.add_vertex(Vec3f(-1.0f, -1.0f, 0.0f));
|
||||
data.add_vertex(Vec3f(1.0f, -1.0f, 0.0f));
|
||||
data.add_vertex(Vec3f(1.0f, 1.0f, 0.0f));
|
||||
data.add_vertex(Vec3f(-1.0f, 1.0f, 0.0f));
|
||||
data.add_triangle(0, 1, 2);
|
||||
data.add_triangle(0, 2, 3);
|
||||
screen.init_from(std::move(data));
|
||||
// bound on a unit of its own, with the volumes' own bindings left to unit 0
|
||||
glsafe(::glActiveTexture(GL_TEXTURE6));
|
||||
glsafe(::glBindTexture(GL_TEXTURE_2D, m_caster_depth));
|
||||
glsafe(::glActiveTexture(GL_TEXTURE0));
|
||||
copy_shader->start_using();
|
||||
copy_shader->set_uniform("depth_texture", 6);
|
||||
copy_shader->set_uniform("map_size", std::array<float, 2>{ static_cast<float>(viewport[2]), static_cast<float>(viewport[3]) });
|
||||
screen.render();
|
||||
copy_shader->stop_using();
|
||||
glsafe(::glActiveTexture(GL_TEXTURE6));
|
||||
glsafe(::glBindTexture(GL_TEXTURE_2D, 0));
|
||||
glsafe(::glActiveTexture(GL_TEXTURE0));
|
||||
}
|
||||
|
||||
// The depth and stencil target the caster pass draws the cut faces into, the size of the shadow
|
||||
// map they are copied into.
|
||||
bool GCodeViewer::init_caster_depth(int width, int height)
|
||||
{
|
||||
if (OpenGLManager::get_framebuffers_type() != OpenGLManager::EFramebufferType::Arb || width <= 0 || height <= 0)
|
||||
return false;
|
||||
if (m_caster_depth != 0 && m_caster_width == width && m_caster_height == height)
|
||||
return true;
|
||||
if (m_caster_depth == 0) {
|
||||
glsafe(::glGenTextures(1, &m_caster_depth));
|
||||
glsafe(::glBindTexture(GL_TEXTURE_2D, m_caster_depth));
|
||||
glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
|
||||
glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
|
||||
glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
|
||||
glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
|
||||
}
|
||||
else
|
||||
glsafe(::glBindTexture(GL_TEXTURE_2D, m_caster_depth));
|
||||
glsafe(::glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, width, height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr));
|
||||
glsafe(::glBindTexture(GL_TEXTURE_2D, 0));
|
||||
m_caster_width = width;
|
||||
m_caster_height = height;
|
||||
|
||||
GLint bound_fbo = 0;
|
||||
glsafe(::glGetIntegerv(GL_FRAMEBUFFER_BINDING, &bound_fbo));
|
||||
if (m_caster_fbo == 0)
|
||||
glsafe(::glGenFramebuffers(1, &m_caster_fbo));
|
||||
glsafe(::glBindFramebuffer(GL_FRAMEBUFFER, m_caster_fbo));
|
||||
glsafe(::glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, m_caster_depth, 0));
|
||||
glsafe(::glDrawBuffer(GL_NONE));
|
||||
glsafe(::glReadBuffer(GL_NONE));
|
||||
const bool complete = ::glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE;
|
||||
glsafe(::glBindFramebuffer(GL_FRAMEBUFFER, static_cast<GLuint>(bound_fbo)));
|
||||
if (!complete)
|
||||
release_caster_depth();
|
||||
return complete;
|
||||
}
|
||||
|
||||
void GCodeViewer::release_caster_depth()
|
||||
{
|
||||
if (m_caster_fbo != 0) {
|
||||
glsafe(::glDeleteFramebuffers(1, &m_caster_fbo));
|
||||
m_caster_fbo = 0;
|
||||
}
|
||||
if (m_caster_depth != 0) {
|
||||
glsafe(::glDeleteTextures(1, &m_caster_depth));
|
||||
m_caster_depth = 0;
|
||||
}
|
||||
m_caster_width = 0;
|
||||
m_caster_height = 0;
|
||||
}
|
||||
|
||||
void GCodeViewer::set_shadow_map(int texture_unit, const Transform3d& light_view_projection, float intensity, float texel_size)
|
||||
{
|
||||
m_shadow_map = { texture_unit, light_view_projection, intensity, texel_size };
|
||||
m_viewer.set_shadow_map(texture_unit,
|
||||
libvgcode::convert(static_cast<Matrix4f>(light_view_projection.matrix().cast<float>())),
|
||||
intensity, texel_size);
|
||||
@@ -2457,6 +2666,7 @@ void GCodeViewer::set_shadow_map(int texture_unit, const Transform3d& light_view
|
||||
|
||||
void GCodeViewer::set_light_top_dir(const Vec3d& direction)
|
||||
{
|
||||
m_light_top_dir = direction;
|
||||
m_viewer.set_light_top_dir(libvgcode::convert(static_cast<Vec3f>(direction.cast<float>())));
|
||||
}
|
||||
|
||||
@@ -2759,6 +2969,51 @@ void GCodeViewer::update_layers_slider_mode()
|
||||
// TODO m_layers_slider->SetModeAndOnlyExtruder(one_extruder_printed_model, only_extruder);
|
||||
}
|
||||
|
||||
void GCodeViewer::set_interacting(bool interacting)
|
||||
{
|
||||
// With the mode off nothing changes while dragging. The solid model needs shells to stand in
|
||||
// for the toolpaths and a single height to cut them at; a belt printer's layers are tilted
|
||||
// planes, which no height cuts.
|
||||
const bool usable = m_reduced_detail_mode != libvgcode::EReducedDetailMode::Off &&
|
||||
(!solid_model_enabled() || (!m_shells.volumes.empty() && m_layers_z_rising && !m_belt_view_enabled));
|
||||
// sampled afresh as each drag begins: the legend may have recoloured the toolpaths, and the
|
||||
// sliders may have moved the span, since the last one
|
||||
if (interacting && !m_viewer.is_reduced_detail())
|
||||
m_solid_model_colors.valid = false;
|
||||
m_viewer.set_reduced_detail(interacting && usable);
|
||||
}
|
||||
|
||||
void GCodeViewer::read_reduced_detail_preferences()
|
||||
{
|
||||
m_reduced_detail_mode = reduced_detail_mode_from_string(get_app_config()->get("preview_reduced_detail_mode"));
|
||||
m_viewer.set_reduced_detail_mode(m_reduced_detail_mode);
|
||||
}
|
||||
|
||||
void GCodeViewer::set_reduced_detail_mode(const std::string& mode)
|
||||
{
|
||||
const bool was_solid = solid_model_enabled();
|
||||
m_reduced_detail_mode = reduced_detail_mode_from_string(mode);
|
||||
m_viewer.set_reduced_detail_mode(m_reduced_detail_mode);
|
||||
|
||||
// Only the prime tower comes and goes with the solid model: a full reload would drop the shells
|
||||
// whenever the print has moved on since they were loaded, leaving the solid model nothing to draw.
|
||||
if (was_solid == solid_model_enabled() || m_shells.print_id == -1 || wxGetApp().plater() == nullptr)
|
||||
return;
|
||||
// the shells are loaded from the current plate's print, which is not the plater's own
|
||||
const Print& print = wxGetApp().plater()->get_partplate_list().get_current_fff_print();
|
||||
if (static_cast<int>(print.id().id) == m_shells.print_id)
|
||||
update_shell_wipe_tower(print, m_gl_data_initialized);
|
||||
}
|
||||
|
||||
libvgcode::EReducedDetailMode GCodeViewer::reduced_detail_mode_from_string(const std::string& mode)
|
||||
{
|
||||
if (mode == "solid")
|
||||
return libvgcode::EReducedDetailMode::EndLayersOnly;
|
||||
if (mode == "shell")
|
||||
return libvgcode::EReducedDetailMode::ShellOnly;
|
||||
return libvgcode::EReducedDetailMode::Off;
|
||||
}
|
||||
|
||||
void GCodeViewer::set_layers_z_range(const std::array<unsigned int, 2>& layers_z_range)
|
||||
{
|
||||
m_viewer.set_layers_view_range(static_cast<uint32_t>(layers_z_range[0]), static_cast<uint32_t>(layers_z_range[1]));
|
||||
@@ -3083,7 +3338,11 @@ void GCodeViewer::export_toolpaths_to_obj(const char* filename) const
|
||||
void GCodeViewer::load_shells(const Print& print, bool initialized, bool force_previewing)
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(": initialized=%1%, force_previewing=%2%")%initialized %force_previewing;
|
||||
// the shells can load before the first G-code does, so the preferences are read here as well
|
||||
read_reduced_detail_preferences();
|
||||
if ((print.id().id == m_shells.print_id)&&(print.get_modified_count() == m_shells.print_modify_count)) {
|
||||
// the prime tower comes and goes on its own, without reloading the objects
|
||||
update_shell_wipe_tower(print, initialized);
|
||||
//BBS: update force previewing logic
|
||||
if (force_previewing)
|
||||
m_shells.previewing = force_previewing;
|
||||
@@ -3192,6 +3451,7 @@ void GCodeViewer::load_shells(const Print& print, bool initialized, bool force_p
|
||||
m_shells.print_id = print.id().id;
|
||||
m_shells.print_modify_count = print.get_modified_count();
|
||||
m_shells.previewing = true;
|
||||
update_shell_wipe_tower(print, initialized);
|
||||
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << boost::format(": shell loaded, id change to %1%, modify_count %2%, object count %3%, glvolume count %4%")
|
||||
% m_shells.print_id % m_shells.print_modify_count % object_count %m_shells.volumes.volumes.size();
|
||||
}
|
||||
@@ -3215,6 +3475,40 @@ void GCodeViewer::render_translucent_toolpaths(int canvas_width, int canvas_heig
|
||||
render_markers(canvas_width, canvas_height);
|
||||
}
|
||||
|
||||
// The prime tower as it was sliced, so that the solid model shows what the print shows. It keeps its
|
||||
// opaque colour, so it never appears among the translucent shells, and stays out of their bounding box.
|
||||
void GCodeViewer::update_shell_wipe_tower(const Print& print, bool initialized)
|
||||
{
|
||||
const bool with_wipe_tower = solid_model_enabled() && print.is_step_done(psWipeTower) && print.wipe_tower_data().wipe_tower_mesh_data;
|
||||
if (with_wipe_tower == m_shells.with_wipe_tower)
|
||||
return;
|
||||
m_shells.with_wipe_tower = with_wipe_tower;
|
||||
GLVolumePtrs& volumes = m_shells.volumes.volumes;
|
||||
if (!with_wipe_tower) {
|
||||
volumes.erase(std::remove_if(volumes.begin(), volumes.end(), [](GLVolume* volume) {
|
||||
if (!volume->is_wipe_tower)
|
||||
return false;
|
||||
delete volume;
|
||||
return true;
|
||||
}), volumes.end());
|
||||
return;
|
||||
}
|
||||
const PrintConfig& config = print.config();
|
||||
const int plate_idx = print.get_plate_index();
|
||||
const Vec3d plate_origin = print.get_plate_origin();
|
||||
const float x = static_cast<float>(config.wipe_tower_x.get_at(plate_idx) + plate_origin.x());
|
||||
const float y = static_cast<float>(config.wipe_tower_y.get_at(plate_idx) + plate_origin.y());
|
||||
const size_t first_new = volumes.size();
|
||||
m_shells.volumes.load_real_wipe_tower_preview(1000 + plate_idx, x, y, print.wipe_tower_data().wipe_tower_mesh_data->real_wipe_tower_mesh,
|
||||
print.wipe_tower_data().wipe_tower_mesh_data->real_brim_mesh, true,
|
||||
static_cast<float>(config.wipe_tower_rotation_angle), false, initialized);
|
||||
for (size_t i = first_new; i < volumes.size(); ++i) {
|
||||
volumes[i]->zoom_to_volumes = false;
|
||||
volumes[i]->force_native_color = true;
|
||||
volumes[i]->set_render_color();
|
||||
}
|
||||
}
|
||||
|
||||
void GCodeViewer::render_toolpaths()
|
||||
{
|
||||
const Camera& camera = wxGetApp().plater()->get_camera();
|
||||
@@ -3422,6 +3716,288 @@ void GCodeViewer::render_shells()
|
||||
glsafe(::glDepthMask(GL_TRUE));
|
||||
}
|
||||
|
||||
// The sliced objects and the prime tower drawn opaque, in their filament colours, cut to the
|
||||
// visible layer range by the shader's z range. The toolpaths of the range's bottom and top layers
|
||||
// are drawn afterwards and cap the cut.
|
||||
// The layers at the range's ends are drawn as toolpaths, so the solid runs from the top of the
|
||||
// bottom ones to the bottom of the top ones. A face of the model in either plane would otherwise
|
||||
// fight the toolpaths for the same pixels and flicker while dragging.
|
||||
// The layer heights are the toolpaths', which the printer's z offset is already taken out of, so
|
||||
// they are in the shells' frame as they are.
|
||||
// A layer with no extrusion has no height of its own; the nearest one below stands in for it.
|
||||
std::optional<std::array<float, 2>> GCodeViewer::solid_model_z_range() const
|
||||
{
|
||||
auto layer_top = [this](size_t id) {
|
||||
float z = m_viewer.get_layer_z(id);
|
||||
while (z == 0.0f && id > 0)
|
||||
z = m_viewer.get_layer_z(--id);
|
||||
return z;
|
||||
};
|
||||
const libvgcode::Interval whole = m_viewer.get_reduced_detail_whole_layers();
|
||||
const float z_top = (whole[1] > 0) ? layer_top(whole[1] - 1) - 0.001f : -FLT_MAX;
|
||||
const float z_bottom = layer_top(whole[0]) + 0.001f;
|
||||
// the toolpaths cover the whole range
|
||||
if (z_top <= z_bottom)
|
||||
return std::nullopt;
|
||||
return std::array<float, 2>{ z_bottom, z_top };
|
||||
}
|
||||
|
||||
void GCodeViewer::init_solid_model_caps(const std::array<float, 2>& z_range, std::array<SolidModelCap, 3>& caps) const
|
||||
{
|
||||
const auto [z_bottom, z_top] = z_range;
|
||||
auto init_quad = [](SolidModelCap& cap, const Vec3f& center, const Vec3f& u, const Vec3f& v) {
|
||||
GLModel::Geometry data;
|
||||
data.format = { GLModel::Geometry::EPrimitiveType::Triangles, GLModel::Geometry::EVertexLayout::P3 };
|
||||
data.reserve_vertices(4);
|
||||
data.reserve_indices(6);
|
||||
data.add_vertex(Vec3f(center - u - v));
|
||||
data.add_vertex(Vec3f(center + u - v));
|
||||
data.add_vertex(Vec3f(center + u + v));
|
||||
data.add_vertex(Vec3f(center - u + v));
|
||||
data.add_triangle(0, 1, 2);
|
||||
data.add_triangle(0, 2, 3);
|
||||
cap.quad.init_from(std::move(data));
|
||||
};
|
||||
const Vec3f box_min = m_paths_bounding_box.min.cast<float>();
|
||||
const Vec3f box_max = m_paths_bounding_box.max.cast<float>();
|
||||
const Vec3f box_center = 0.5f * (box_min + box_max);
|
||||
const float margin = 1.0f;
|
||||
const Vec3f half_x(0.5f * (box_max.x() - box_min.x()) + margin, 0.0f, 0.0f);
|
||||
const Vec3f half_y(0.0f, 0.5f * (box_max.y() - box_min.y()) + margin, 0.0f);
|
||||
if (m_viewer.get_layers_view_range()[0] > 0) {
|
||||
SolidModelCap& cap = caps[0];
|
||||
init_quad(cap, Vec3f(box_center.x(), box_center.y(), z_bottom), half_x, half_y);
|
||||
cap.count_z_range = { z_bottom, FLT_MAX };
|
||||
cap.quad_plane = m_clipping_plane;
|
||||
cap.color_index = 0;
|
||||
}
|
||||
if (z_top > -FLT_MAX) {
|
||||
SolidModelCap& cap = caps[1];
|
||||
init_quad(cap, Vec3f(box_center.x(), box_center.y(), z_top), half_x, half_y);
|
||||
cap.count_z_range = { -FLT_MAX, z_top };
|
||||
cap.quad_plane = m_clipping_plane;
|
||||
cap.color_index = 1;
|
||||
}
|
||||
// the section view cuts the solid as it cuts the toolpaths; nothing else sets a plane on the shells
|
||||
if (m_clipping_plane[0] != 0.0f || m_clipping_plane[1] != 0.0f || m_clipping_plane[2] != 0.0f) {
|
||||
SolidModelCap& cap = caps[2];
|
||||
const Vec3f normal(m_clipping_plane[0], m_clipping_plane[1], m_clipping_plane[2]);
|
||||
const Vec3f on_plane = box_center - (normal.dot(box_center) + m_clipping_plane[3]) * normal;
|
||||
// the in-plane axes, turned from the unit axis the normal leans on least
|
||||
const Vec3f leaning = normal.cwiseAbs();
|
||||
const Vec3f axis = (leaning.x() <= leaning.y() && leaning.x() <= leaning.z()) ? Vec3f::UnitX() :
|
||||
(leaning.y() <= leaning.z()) ? Vec3f::UnitY() : Vec3f::UnitZ();
|
||||
const Vec3f u = normal.cross(axis).normalized();
|
||||
const Vec3f v = normal.cross(u);
|
||||
const float reach = 0.5f * (box_max - box_min).norm() + margin;
|
||||
init_quad(cap, on_plane, reach * u, reach * v);
|
||||
cap.count_plane = m_clipping_plane;
|
||||
cap.quad_z_range = { z_bottom, z_top };
|
||||
}
|
||||
}
|
||||
|
||||
void GCodeViewer::render_solid_model(int canvas_width, int canvas_height)
|
||||
{
|
||||
if (m_shells.volumes.empty())
|
||||
return;
|
||||
// gouraud_light has no z range, so it could not cut the model
|
||||
GLShaderProgram* shader = wxGetApp().get_shader("gouraud");
|
||||
if (shader == nullptr)
|
||||
return;
|
||||
const std::optional<std::array<float, 2>> z_range = solid_model_z_range();
|
||||
if (!z_range.has_value())
|
||||
return;
|
||||
const auto [z_bottom, z_top] = *z_range;
|
||||
const libvgcode::Interval& layers = m_viewer.get_layers_view_range();
|
||||
const libvgcode::Interval whole = m_viewer.get_reduced_detail_whole_layers();
|
||||
|
||||
// the volumes wear the solid's colours for the frame, over their own filament colours
|
||||
update_solid_model_colors(whole);
|
||||
const SolidModelColors& colors = m_solid_model_colors;
|
||||
// The layers the solid stands for are never the ones a slider thumb lights, so the solid
|
||||
// wears what the toolpath shader gives the rest of the print: dimmed to the preference's
|
||||
// brightness while the viewer dims the layers around the inspected ones, dark grey while the
|
||||
// moves slider is short of the top layer's end; see libvgcode::ViewerImpl::set_layer_colors().
|
||||
const bool top_layer_only = m_viewer.is_top_layer_only_view_range();
|
||||
const bool moves_short = m_viewer.get_view_full_range()[1] != m_viewer.get_view_visible_range()[1];
|
||||
const bool inspecting = layers[0] > 0 || layers[1] + 1 < m_viewer.get_layers_count() || moves_short;
|
||||
const bool dimmed = top_layer_only && inspecting && m_viewer.is_dim_previous_layers();
|
||||
const bool greyed = !dimmed && top_layer_only && moves_short;
|
||||
auto shade = [dimmed, greyed, brightness = m_viewer.get_dim_previous_layers_brightness()](const ColorRGBA& color) {
|
||||
if (greyed)
|
||||
return ColorRGBA(64.0f / 255.0f, 64.0f / 255.0f, 64.0f / 255.0f, 1.0f);
|
||||
if (dimmed)
|
||||
return ColorRGBA(brightness * color.r(), brightness * color.g(), brightness * color.b(), 1.0f);
|
||||
return ColorRGBA(color.r(), color.g(), color.b(), 1.0f);
|
||||
};
|
||||
std::vector<ColorRGBA> own_colors;
|
||||
own_colors.reserve(m_shells.volumes.volumes.size());
|
||||
// the tower draws itself in its own colours and leaves the volume's at the default; one list per volume
|
||||
std::vector<std::vector<ColorRGBA>> own_tower_colors(m_shells.volumes.volumes.size());
|
||||
for (size_t k = 0; k < m_shells.volumes.volumes.size(); ++k) {
|
||||
GLVolume* volume = m_shells.volumes.volumes[k];
|
||||
own_colors.push_back(volume->color);
|
||||
const std::optional<ColorRGBA>& body = colors.bodies[k].body;
|
||||
if (auto* tower = dynamic_cast<GLWipeTowerVolume*>(volume); tower != nullptr) {
|
||||
own_tower_colors[k] = tower->colors();
|
||||
for (ColorRGBA& color : tower->colors())
|
||||
color = shade(body.value_or(color));
|
||||
}
|
||||
volume->color = shade(body.value_or(volume->color));
|
||||
volume->set_render_color();
|
||||
}
|
||||
m_shells.volumes.set_z_range(z_bottom, z_top);
|
||||
// the section view cuts the solid as it cuts the toolpaths
|
||||
m_shells.volumes.set_clipping_plane({ m_clipping_plane[0], m_clipping_plane[1], m_clipping_plane[2], m_clipping_plane[3] });
|
||||
|
||||
shader->start_using();
|
||||
// The realistic view's shadow map, bound by the canvas for the toolpaths, shades the solid
|
||||
// too, under the same light; both are off when the map is not there. The 3D view leaves its
|
||||
// own settings on the shared program, so each is set here.
|
||||
shader->set_uniform("shadow_map", m_shadow_map.texture_unit);
|
||||
shader->set_uniform("shadow_light_vp", m_shadow_map.light_view_projection);
|
||||
shader->set_uniform("shadow_map_texel", m_shadow_map.texel_size);
|
||||
shader->set_uniform("shadow_intensity", m_shadow_map.intensity);
|
||||
shader->set_uniform("use_static_light", true);
|
||||
shader->set_uniform("static_light_dir", m_light_top_dir);
|
||||
const Camera& camera = wxGetApp().plater()->get_camera();
|
||||
shader->set_uniform("z_far", camera.get_far_z());
|
||||
shader->set_uniform("z_near", camera.get_near_z());
|
||||
m_shells.volumes.render(GLVolumeCollection::ERenderType::Opaque, false, camera.get_view_matrix(), camera.get_projection_matrix());
|
||||
shader->stop_using();
|
||||
|
||||
// The cut faces, one cap of one body at a time. A pixel looks into a body through a cut where,
|
||||
// with the depth test off and the body cut at that cap alone, its back faces outnumber its
|
||||
// front faces: zero where the ray misses the body or crosses it whole, one where it leaves
|
||||
// through the opening, which is where the cap plane lies inside the body. Counted with two
|
||||
// cuts at once the body is an open tube, and a ray through both openings would count as
|
||||
// closed, one in through a wall and out through the other opening as open. There a quad in
|
||||
// the cut plane, in the colour of the infill it is cut under darkened, or the body's own,
|
||||
// stands in for the material, so that a sparse layer on the cut shows a solid inside between
|
||||
// its lines rather than whatever lies further down. The section view's plane is one more
|
||||
// cut: its cap is counted on the body cut by it alone and drawn between the heights of the
|
||||
// other two, and theirs are drawn on its kept side.
|
||||
GLShaderProgram* cap_shader = wxGetApp().get_shader("flat_clip");
|
||||
if (cap_shader != nullptr) {
|
||||
std::array<SolidModelCap, 3> caps;
|
||||
init_solid_model_caps(*z_range, caps);
|
||||
glsafe(::glEnable(GL_STENCIL_TEST));
|
||||
for (size_t n = 0; n < m_shells.volumes.volumes.size(); ++n) {
|
||||
GLVolume* volume = m_shells.volumes.volumes[n];
|
||||
const auto* tower = dynamic_cast<const GLWipeTowerVolume*>(volume);
|
||||
const Transform3d world_matrix = volume->world_matrix();
|
||||
for (SolidModelCap& cap : caps) {
|
||||
if (!cap.quad.is_initialized())
|
||||
continue;
|
||||
begin_cap_count();
|
||||
glsafe(::glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE));
|
||||
// The body is drawn directly, on the uniforms the pass above left on the program: the
|
||||
// collection's render would build the full preset config and sort every volume, per body.
|
||||
shader->start_using();
|
||||
shader->set_uniform("z_range", cap.count_z_range);
|
||||
shader->set_uniform("clipping_plane", cap.count_plane);
|
||||
shader->set_uniform("volume_world_matrix", world_matrix);
|
||||
shader->set_uniform("view_model_matrix", camera.get_view_matrix() * world_matrix);
|
||||
volume->render();
|
||||
shader->stop_using();
|
||||
glsafe(::glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE));
|
||||
end_cap_count();
|
||||
|
||||
ColorRGBA inside = volume->color;
|
||||
if (tower != nullptr && !tower->colors().empty())
|
||||
inside = tower->colors().front();
|
||||
if (cap.color_index.has_value()) {
|
||||
if (const std::optional<ColorRGBA>& sampled = colors.bodies[n].caps[*cap.color_index]; sampled.has_value())
|
||||
inside = shade(*sampled);
|
||||
}
|
||||
inside = ColorRGBA(0.55f * inside.r(), 0.55f * inside.g(), 0.55f * inside.b(), 1.0f);
|
||||
cap_shader->start_using();
|
||||
cap_shader->set_uniform("view_model_matrix", camera.get_view_matrix());
|
||||
cap_shader->set_uniform("projection_matrix", camera.get_projection_matrix());
|
||||
cap_shader->set_uniform("volume_world_matrix", Transform3d::Identity());
|
||||
cap_shader->set_uniform("z_range", cap.quad_z_range);
|
||||
cap_shader->set_uniform("clipping_plane", cap.quad_plane);
|
||||
cap.quad.set_color(inside);
|
||||
cap.quad.render();
|
||||
cap_shader->stop_using();
|
||||
}
|
||||
}
|
||||
glsafe(::glEnable(GL_CULL_FACE));
|
||||
glsafe(::glStencilMask(0xFF));
|
||||
glsafe(::glDisable(GL_STENCIL_TEST));
|
||||
}
|
||||
|
||||
m_shells.volumes.set_z_range(-FLT_MAX, FLT_MAX);
|
||||
for (size_t k = 0; k < m_shells.volumes.volumes.size(); ++k) {
|
||||
GLVolume* volume = m_shells.volumes.volumes[k];
|
||||
if (auto* tower = dynamic_cast<GLWipeTowerVolume*>(volume); tower != nullptr)
|
||||
tower->colors() = own_tower_colors[k];
|
||||
volume->color = own_colors[k];
|
||||
volume->set_render_color();
|
||||
}
|
||||
}
|
||||
|
||||
// The solid stands in for the toolpaths, so it takes their colours in the view that colours them
|
||||
// by something other than the filament: each body the outer wall's, each cap the infill's of the
|
||||
// layer it is cut under, sparse if the layer has any, else solid, and the tower its own role's,
|
||||
// each from the toolpaths within the body's own footprint, so that objects printed at different
|
||||
// grades keep their own. The views that colour by filament leave every body its own colour. The
|
||||
// bodies are sampled over the layers the solid stands for and each cap in its own layer, each
|
||||
// colour that of the vertex the viewer picks to represent the role there, so a view that grades
|
||||
// the toolpaths, by speed or fan, shows the solid in the grade most of that role was printed at.
|
||||
// The bodies are sampled once, as the drag begins, and held while a slider drag moves the span:
|
||||
// sampled again at every step, the grade would flip with the layers the sample happens to fall
|
||||
// on. Each cap follows its own layer.
|
||||
void GCodeViewer::update_solid_model_colors(const libvgcode::Interval& whole_layers)
|
||||
{
|
||||
SolidModelColors& colors = m_solid_model_colors;
|
||||
const libvgcode::EViewType view_type = m_viewer.get_view_type();
|
||||
const std::vector<GLVolume*>& volumes = m_shells.volumes.volumes;
|
||||
const bool held = colors.valid && colors.view_type == view_type && colors.bodies.size() == volumes.size();
|
||||
if (held && colors.whole_layers == whole_layers)
|
||||
return;
|
||||
if (!held) {
|
||||
colors = SolidModelColors();
|
||||
colors.valid = true;
|
||||
colors.view_type = view_type;
|
||||
colors.bodies.resize(volumes.size());
|
||||
}
|
||||
colors.whole_layers = whole_layers;
|
||||
if (view_type == libvgcode::EViewType::Summary || view_type == libvgcode::EViewType::ColorPrint || view_type == libvgcode::EViewType::Tool)
|
||||
return;
|
||||
|
||||
using Role = libvgcode::EGCodeExtrusionRole;
|
||||
auto sample = [this](const libvgcode::Interval& layers, std::initializer_list<Role> roles, const libvgcode::XYBox& box) -> std::optional<ColorRGBA> {
|
||||
for (Role role : roles) {
|
||||
const size_t id = m_viewer.find_extrusion_vertex(layers, role, box);
|
||||
if (id < m_viewer.get_vertices_count())
|
||||
return libvgcode::convert(m_viewer.get_vertex_color(m_viewer.get_vertex_at(id)));
|
||||
}
|
||||
return std::nullopt;
|
||||
};
|
||||
const std::initializer_list<Role> walls = { Role::ExternalPerimeter, Role::Perimeter, Role::OverhangPerimeter };
|
||||
const std::initializer_list<Role> tower_role = { Role::WipeTower };
|
||||
const std::initializer_list<Role> infill = { Role::InternalInfill, Role::SolidInfill, Role::TopSolidInfill, Role::BottomSurface,
|
||||
Role::BridgeInfill, Role::InternalBridgeInfill, Role::Ironing };
|
||||
for (size_t n = 0; n < volumes.size(); ++n) {
|
||||
const GLVolume* volume = volumes[n];
|
||||
const bool tower = dynamic_cast<const GLWipeTowerVolume*>(volume) != nullptr;
|
||||
// the walls' centrelines lie within the outline, pushed out by a compensation at most
|
||||
const BoundingBoxf3& footprint = volume->transformed_bounding_box();
|
||||
const float margin = 1.0f;
|
||||
const libvgcode::XYBox box{ { static_cast<float>(footprint.min.x()) - margin, static_cast<float>(footprint.min.y()) - margin },
|
||||
{ static_cast<float>(footprint.max.x()) + margin, static_cast<float>(footprint.max.y()) + margin } };
|
||||
SolidModelColors::Body& body = colors.bodies[n];
|
||||
if (!held)
|
||||
body.body = sample(whole_layers, tower ? tower_role : walls, box);
|
||||
for (size_t k = 0; k < 2; ++k) {
|
||||
body.caps[k] = tower ? std::nullopt : sample({ whole_layers[k], whole_layers[k] }, infill, box);
|
||||
if (!body.caps[k].has_value())
|
||||
body.caps[k] = body.body;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//BBS
|
||||
void GCodeViewer::render_all_plates_stats(const std::vector<const GCodeProcessorResult*>& gcode_result_list, bool show /*= true*/) const {
|
||||
if (!show)
|
||||
|
||||
Reference in new issue
Block a user