Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions resources/shaders/110/background.vs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#version 110

attribute vec3 v_position;
attribute vec2 v_tex_coord;

varying vec2 tex_coord;

void main()
{
tex_coord = v_tex_coord;
tex_coord = v_position.xy * 0.5 + 0.5;
gl_Position = vec4(v_position, 1.0);
}
7 changes: 5 additions & 2 deletions resources/shaders/110/flat_texture.vs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

uniform mat4 view_model_matrix;
uniform mat4 projection_matrix;
uniform mat3 u_uvTransformMatrix;

attribute vec3 v_position;
attribute vec2 v_tex_coord;

varying vec2 tex_coord;

void main()
{
tex_coord = v_tex_coord;
vec2 texCoord;
texCoord.x = v_position.x;
texCoord.y = -v_position.y;
tex_coord = (u_uvTransformMatrix * vec3(texCoord, 1.0)).xy;
gl_Position = projection_matrix * view_model_matrix * vec4(v_position, 1.0);
}
3 changes: 1 addition & 2 deletions resources/shaders/140/background.vs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#version 140

in vec3 v_position;
in vec2 v_tex_coord;

out vec2 tex_coord;

void main()
{
tex_coord = v_tex_coord;
tex_coord = v_position.xy * 0.5 + 0.5;
gl_Position = vec4(v_position, 1.0);
}
7 changes: 5 additions & 2 deletions resources/shaders/140/flat_texture.vs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

uniform mat4 view_model_matrix;
uniform mat4 projection_matrix;
uniform mat3 u_uvTransformMatrix;

in vec3 v_position;
in vec2 v_tex_coord;

out vec2 tex_coord;

void main()
{
tex_coord = v_tex_coord;
vec2 texCoord;
texCoord.x = v_position.x;
texCoord.y = -v_position.y;
tex_coord = (u_uvTransformMatrix * vec3(texCoord, 1.0)).xy;
gl_Position = projection_matrix * view_model_matrix * vec4(v_position, 1.0);
}
2 changes: 2 additions & 0 deletions src/slic3r/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,8 @@ set(SLIC3R_GUI_SOURCES
Utils/Flashforge.hpp
Utils/FontConfigHelp.cpp
Utils/FontConfigHelp.hpp
Utils/Frustum.cpp
Utils/Frustum.hpp
Utils/HexFile.cpp
Utils/HexFile.hpp
Utils/Http.cpp
Expand Down
13 changes: 10 additions & 3 deletions src/slic3r/GUI/3DScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "Plater.hpp"
#include "BitmapCache.hpp"
#include "Camera.hpp"

#include "Frustum.hpp"
#include "libslic3r/BuildVolume.hpp"
#include "libslic3r/ExtrusionEntity.hpp"
#include "libslic3r/ExtrusionEntityCollection.hpp"
Expand Down Expand Up @@ -878,12 +878,13 @@ int GLVolumeCollection::get_selection_support_threshold_angle(bool& enable_suppo
// BBS: add outline drawing logic
void GLVolumeCollection::render(GLVolumeCollection::ERenderType type,
bool disable_cullface,
const Transform3d& view_matrix,
const Transform3d& projection_matrix,
const GUI::Camera& camera,
const GUI::Size& cnv_size,
std::function<bool(const GLVolume&)> filter_func,
bool partly_inside_enable) const
{
const Transform3d& view_matrix = camera.get_view_matrix();
const Transform3d& projection_matrix = camera.get_projection_matrix();
GLVolumeWithIdAndZList to_render = volumes_to_render(volumes, type, view_matrix, filter_func);
if (to_render.empty())
return;
Expand All @@ -905,6 +906,12 @@ void GLVolumeCollection::render(GLVolumeCollection::ERenderType type,
glsafe(::glDisable(GL_CULL_FACE));

for (GLVolumeWithIdAndZ& volume : to_render) {
//CPU Frustum culling
auto _worldAABB = volume.first->transformed_bounding_box();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

变量名不符合规范

if (!camera.GetFrustum().Intersects(_worldAABB))
{
continue;
}
#if ENABLE_MODIFIERS_ALWAYS_TRANSPARENT
if (type == ERenderType::Transparent) {
volume.first->force_transparent = true;
Expand Down
4 changes: 2 additions & 2 deletions src/slic3r/GUI/3DScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extern Slic3r::ColorRGBA adjust_color_for_rendering(const Slic3r::C
namespace Slic3r {
namespace GUI {
class Size;
class Camera;
}

class SLAPrintObject;
Expand Down Expand Up @@ -475,8 +476,7 @@ class GLVolumeCollection
//BBS: add outline drawing logic
void render(ERenderType type,
bool disable_cullface,
const Transform3d & view_matrix,
const Transform3d& projection_matrix,
const GUI::Camera& camera,
const GUI::Size& cnv_size,
std::function<bool(const GLVolume &)> filter_func = std::function<bool(const GLVolume &)>(),
bool partly_inside_enable =true
Expand Down
57 changes: 57 additions & 0 deletions src/slic3r/GUI/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,63 @@ void Camera::look_at(const Vec3d& position, const Vec3d& target, const Vec3d& up
update_zenit();
}

void Camera::UpdateFrustum()
{
// fast extraction of frustum planes from the view-projection matrix
// 1. plane equation: ax + by + cz + d = 0, vec3(a,b,c) is normal of plane, d is scalar of plane, distance to origin of world frame
// 2. -w' < x' < w' -w' < y' < w' -w' < z' < w'
const Matrix4d& vp = m_projection_matrix.matrix() * m_view_matrix.matrix();
const Eigen::Matrix4d& vp_matrix = vp.eval();
const double* vp_data = vp_matrix.data();
// left
float a = vp_data[0 * 4 + 3] + vp_data[0 * 4 + 0];
float b = vp_data[1 * 4 + 3] + vp_data[1 * 4 + 0];
float c = vp_data[2 * 4 + 3] + vp_data[2 * 4 + 0];
float d = vp_data[3 * 4 + 3] + vp_data[3 * 4 + 0];
m_frustum.planes[0].SetEquation(a, b, c, d);
m_frustum.planes[0].Normalize();

// right
a = vp_data[0 * 4 + 3] - vp_data[0 * 4 + 0];
b = vp_data[1 * 4 + 3] - vp_data[1 * 4 + 0];
c = vp_data[2 * 4 + 3] - vp_data[2 * 4 + 0];
d = vp_data[3 * 4 + 3] - vp_data[3 * 4 + 0];
m_frustum.planes[1].SetEquation(a, b, c, d);
m_frustum.planes[1].Normalize();

// bottom
a = vp_data[0 * 4 + 3] + vp_data[0 * 4 + 1];
b = vp_data[1 * 4 + 3] + vp_data[1 * 4 + 1];
c = vp_data[2 * 4 + 3] + vp_data[2 * 4 + 1];
d = vp_data[3 * 4 + 3] + vp_data[3 * 4 + 1];
m_frustum.planes[2].SetEquation(a, b, c, d);
m_frustum.planes[2].Normalize();

// top
a = vp_data[0 * 4 + 3] - vp_data[0 * 4 + 1];
b = vp_data[1 * 4 + 3] - vp_data[1 * 4 + 1];
c = vp_data[2 * 4 + 3] - vp_data[2 * 4 + 1];
d = vp_data[3 * 4 + 3] - vp_data[3 * 4 + 1];
m_frustum.planes[3].SetEquation(a, b, c, d);
m_frustum.planes[3].Normalize();

// near
a = vp_data[0 * 4 + 3] + vp_data[0 * 4 + 2];
b = vp_data[1 * 4 + 3] + vp_data[1 * 4 + 2];
c = vp_data[2 * 4 + 3] + vp_data[2 * 4 + 2];
d = vp_data[3 * 4 + 3] + vp_data[3 * 4 + 2];
m_frustum.planes[4].SetEquation(a, b, c, d);
m_frustum.planes[4].Normalize();

// far
a = vp_data[0 * 4 + 3] - vp_data[0 * 4 + 2];
b = vp_data[1 * 4 + 3] - vp_data[1 * 4 + 2];
c = vp_data[2 * 4 + 3] - vp_data[2 * 4 + 2];
d = vp_data[3 * 4 + 3] - vp_data[3 * 4 + 2];
m_frustum.planes[5].SetEquation(a, b, c, d);
m_frustum.planes[5].Normalize();
}

void Camera::set_default_orientation()
{
m_zenit = 45.0f;
Expand Down
14 changes: 13 additions & 1 deletion src/slic3r/GUI/Camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "libslic3r/BoundingBox.hpp"
#include "3DScene.hpp"
#include <array>
#include "../Utils/Frustum.hpp"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不建议使用相对路径,直接用 #include "Utils/Frustum.hpp"


namespace Slic3r {
namespace GUI {
Expand Down Expand Up @@ -56,6 +56,7 @@ struct Camera
std::pair<double, double> m_frustrum_zs;

BoundingBoxf3 m_scene_box;
Frustum m_frustum;

public:
Camera() { set_default_orientation(); }
Expand Down Expand Up @@ -167,6 +168,17 @@ struct Camera
double max_zoom() const { return 250.0; }
double min_zoom() const { return 0.2 * calc_zoom_to_bounding_box_factor(m_scene_box); }

/// <summary>
/// get the current frustrum planes. The planes are in world space and normalized (the normal vector has unit length).
/// </summary>
/// <returns></returns>
const Frustum& GetFrustum() const { return m_frustum; }

/// <summary>
/// extract the planes of the frustrum from the current projection and view matrix and update m_frustrum
/// </summary>
void UpdateFrustum();

private:
// returns tight values for nearZ and farZ plane around the given bounding box
// the camera MUST be outside of the bounding box in eye coordinate of the given box
Expand Down
2 changes: 1 addition & 1 deletion src/slic3r/GUI/GCodeViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4040,7 +4040,7 @@ void GCodeViewer::render_shells(int canvas_width, int canvas_height)
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::Transparent, false, camera.get_view_matrix(), camera.get_projection_matrix(), {canvas_width, canvas_height});
m_shells.volumes.render(GLVolumeCollection::ERenderType::Transparent, false, camera, {canvas_width, canvas_height});
shader->set_uniform("emission_factor", 0.0f);
shader->stop_using();

Expand Down
18 changes: 9 additions & 9 deletions src/slic3r/GUI/GLCanvas3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1927,7 +1927,7 @@ void GLCanvas3D::render(bool only_init)
}

camera.apply_projection(_max_bounding_box(true, true, true));

camera.UpdateFrustum();
wxGetApp().imgui()->new_frame();

if (m_picking_enabled) {
Expand Down Expand Up @@ -7266,15 +7266,15 @@ void GLCanvas3D::_render_background()
m_background.reset();

GLModel::Geometry init_data;
init_data.format = { GLModel::Geometry::EPrimitiveType::Triangles, GLModel::Geometry::EVertexLayout::P2T2 };
init_data.format = { GLModel::Geometry::EPrimitiveType::Triangles, GLModel::Geometry::EVertexLayout::P2 };
init_data.reserve_vertices(4);
init_data.reserve_indices(6);

// vertices
init_data.add_vertex(Vec2f(-1.0f, -1.0f), Vec2f(0.0f, 0.0f));
init_data.add_vertex(Vec2f(1.0f, -1.0f), Vec2f(1.0f, 0.0f));
init_data.add_vertex(Vec2f(1.0f, 1.0f), Vec2f(1.0f, 1.0f));
init_data.add_vertex(Vec2f(-1.0f, 1.0f), Vec2f(0.0f, 1.0f));
init_data.add_vertex(Vec2f(-1.0f, -1.0f));
init_data.add_vertex(Vec2f(1.0f, -1.0f));
init_data.add_vertex(Vec2f(1.0f, 1.0f));
init_data.add_vertex(Vec2f(-1.0f, 1.0f));

// indices
init_data.add_triangle(0, 1, 2);
Expand Down Expand Up @@ -7415,7 +7415,7 @@ void GLCanvas3D::_render_objects(GLVolumeCollection::ERenderType type, bool with
if (m_picking_enabled && m_layers_editing.is_enabled() && (m_layers_editing.last_object_id != -1) && (m_layers_editing.object_max_z() > 0.0f)) {
int object_id = m_layers_editing.last_object_id;
const Camera& camera = wxGetApp().plater()->get_camera();
m_volumes.render(type, false, camera.get_view_matrix(), camera.get_projection_matrix(), cvn_size, [object_id](const GLVolume& volume) {
m_volumes.render(type, false, camera, cvn_size, [object_id](const GLVolume& volume) {
// Which volume to paint without the layer height profile shader?
return volume.is_active && (volume.is_modifier || volume.composite_id.object_id != object_id);
});
Expand All @@ -7431,7 +7431,7 @@ void GLCanvas3D::_render_objects(GLVolumeCollection::ERenderType type, bool with
//BBS:add assemble view related logic
// do not cull backfaces to show broken geometry, if any
const Camera& camera = wxGetApp().plater()->get_camera();
m_volumes.render(type, m_picking_enabled, camera.get_view_matrix(), camera.get_projection_matrix(), cvn_size, [this, canvas_type](const GLVolume& volume) {
m_volumes.render(type, m_picking_enabled, camera, cvn_size, [this, canvas_type](const GLVolume& volume) {
if (canvas_type == ECanvasType::CanvasAssembleView) {
return !volume.is_modifier && !volume.is_wipe_tower;
}
Expand Down Expand Up @@ -7466,7 +7466,7 @@ void GLCanvas3D::_render_objects(GLVolumeCollection::ERenderType type, bool with
}*/
const Camera& camera = wxGetApp().plater()->get_camera();
//BBS:add assemble view related logic
m_volumes.render(type, false, camera.get_view_matrix(), camera.get_projection_matrix(), cvn_size, [canvas_type](const GLVolume& volume) {
m_volumes.render(type, false, camera, cvn_size, [canvas_type](const GLVolume& volume) {
if (canvas_type == ECanvasType::CanvasAssembleView) {
return !volume.is_modifier;
}
Expand Down
75 changes: 56 additions & 19 deletions src/slic3r/GUI/GLTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,32 @@ void GLTexture::render_texture(unsigned int tex_id, float left, float right, flo

void GLTexture::render_sub_texture(unsigned int tex_id, float left, float right, float bottom, float top, const GLTexture::Quad_UVs& uvs)
{
GLModel& model = InitModelForRenderImage();

// position and scale from normalized unit quad
const float center_x = (left + right) * 0.5f;
const float center_y = (bottom + top) * 0.5f;
const float scale_x = (right - left);
const float scale_y = (top - bottom);

Transform3d model_matrix = Transform3d::Identity();
model_matrix.data()[3 * 4 + 0] = center_x;
model_matrix.data()[3 * 4 + 1] = center_y;
model_matrix.data()[0 * 4 + 0] = scale_x;
model_matrix.data()[1 * 4 + 1] = scale_y;

// UV transform — maps unit UVs to sprite atlas sub-region
const float center_u = (uvs.right_bottom.u + uvs.left_bottom.u) * 0.5f;
const float center_v = (uvs.right_top.v + uvs.right_bottom.v) * 0.5f;
const float scale_u = (uvs.right_bottom.u - uvs.left_bottom.u);
const float scale_v = (uvs.right_bottom.v - uvs.right_top.v);

Matrix3f uv_matrix = Matrix3f::Identity();
uv_matrix(0, 2) = center_u;
uv_matrix(1, 2) = center_v;
uv_matrix(0, 0) = scale_u;
uv_matrix(1, 1) = scale_v;

glsafe(::glEnable(GL_BLEND));
glsafe(::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));

Expand All @@ -662,29 +688,12 @@ void GLTexture::render_sub_texture(unsigned int tex_id, float left, float right,

glsafe(::glBindTexture(GL_TEXTURE_2D, (GLuint)tex_id));

GLModel::Geometry init_data;
init_data.format = { GLModel::Geometry::EPrimitiveType::Triangles, GLModel::Geometry::EVertexLayout::P2T2 };
init_data.reserve_vertices(4);
init_data.reserve_indices(6);

// vertices
init_data.add_vertex(Vec2f(left, bottom), Vec2f(uvs.left_bottom.u, uvs.left_bottom.v));
init_data.add_vertex(Vec2f(right, bottom), Vec2f(uvs.right_bottom.u, uvs.right_bottom.v));
init_data.add_vertex(Vec2f(right, top), Vec2f(uvs.right_top.u, uvs.right_top.v));
init_data.add_vertex(Vec2f(left, top), Vec2f(uvs.left_top.u, uvs.left_top.v));

// indices
init_data.add_triangle(0, 1, 2);
init_data.add_triangle(2, 3, 0);

GLModel model;
model.init_from(std::move(init_data));

GLShaderProgram* shader = wxGetApp().get_shader("flat_texture");
if (shader != nullptr) {
shader->start_using();
shader->set_uniform("view_model_matrix", Transform3d::Identity());
shader->set_uniform("view_model_matrix", model_matrix);
shader->set_uniform("projection_matrix", Transform3d::Identity());
shader->set_uniform("u_uvTransformMatrix", uv_matrix);
model.render();
shader->stop_using();
}
Expand All @@ -695,6 +704,34 @@ void GLTexture::render_sub_texture(unsigned int tex_id, float left, float right,
glsafe(::glDisable(GL_BLEND));
}

GLModel& GLTexture::GetModelForRenderImage()
{
static GLModel g_model_for_render_image;
return g_model_for_render_image;
}

GLModel& GLTexture::InitModelForRenderImage()
{
auto& model = GetModelForRenderImage();
if (!model.is_initialized()) {
GLModel::Geometry init_data;
init_data.format = { GLModel::Geometry::EPrimitiveType::Triangles, GLModel::Geometry::EVertexLayout::P3 };
init_data.reserve_vertices(4);
init_data.reserve_indices(6);

init_data.add_vertex(Vec3f(-0.5f, -0.5f, 0.0f)/*, Vec2f(-0.5f, 0.5f)*/);
init_data.add_vertex(Vec3f( 0.5f, -0.5f, 0.0f)/*, Vec2f( 0.5f, 0.5f)*/);
init_data.add_vertex(Vec3f( 0.5f, 0.5f, 0.0f)/*, Vec2f( 0.5f, -0.5f)*/);
init_data.add_vertex(Vec3f(-0.5f, 0.5f, 0.0f)/*, Vec2f(-0.5f, -0.5f)*/);

init_data.add_triangle(0, 1, 2);
init_data.add_triangle(2, 3, 0);

model.init_from(std::move(init_data));
}
return model;
}

static bool to_squared_power_of_two(const std::string& filename, int max_size_px, int& w, int& h)
{
auto is_power_of_two = [](int v) { return v != 0 && (v & (v - 1)) == 0; };
Expand Down
Loading