Merge branch 'master' into fs_emboss

This commit is contained in:
Filip Sykala 2022-03-22 15:48:51 +01:00
commit a7d3195ec0
5 changed files with 59 additions and 16 deletions

View file

@ -3,16 +3,17 @@
const vec3 back_color_dark = vec3(0.235, 0.235, 0.235);
const vec3 back_color_light = vec3(0.365, 0.365, 0.365);
uniform sampler2D texture;
uniform sampler2D in_texture;
uniform bool transparent_background;
uniform bool svg_source;
in vec2 tex_coord;
out vec4 frag_color;
vec4 svg_color()
{
// takes foreground from texture
vec4 fore_color = texture(texture, tex_coord);
vec4 fore_color = texture(in_texture, tex_coord);
// calculates radial gradient
vec3 back_color = vec3(mix(back_color_light, back_color_dark, smoothstep(0.0, 0.5, length(abs(tex_coord.xy) - vec2(0.5)))));
@ -24,11 +25,11 @@ vec4 svg_color()
vec4 non_svg_color()
{
// takes foreground from texture
vec4 color = texture(texture, tex_coord);
vec4 color = texture(in_texture, tex_coord);
return vec4(color.rgb, transparent_background ? color.a * 0.25 : color.a);
}
void main()
{
gl_FragColor = svg_source ? svg_color() : non_svg_color();
frag_color = svg_source ? svg_color() : non_svg_color();
}

View file

@ -109,12 +109,14 @@ void Camera::apply_viewport(int x, int y, unsigned int w, unsigned int h)
glsafe(::glGetIntegerv(GL_VIEWPORT, m_viewport.data()));
}
#if !ENABLE_LEGACY_OPENGL_REMOVAL
void Camera::apply_view_matrix()
{
glsafe(::glMatrixMode(GL_MODELVIEW));
glsafe(::glLoadIdentity());
glsafe(::glMultMatrixd(m_view_matrix.data()));
}
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
void Camera::apply_projection(const BoundingBoxf3& box, double near_z, double far_z)
{
@ -123,9 +125,11 @@ void Camera::apply_projection(const BoundingBoxf3& box, double near_z, double fa
const double old_distance = m_distance;
m_frustrum_zs = calc_tight_frustrum_zs_around(box);
#if !ENABLE_LEGACY_OPENGL_REMOVAL
if (m_distance != old_distance)
// the camera has been moved re-apply view matrix
apply_view_matrix();
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
if (near_z > 0.0)
m_frustrum_zs.first = std::max(std::min(m_frustrum_zs.first, near_z), FrustrumMinNearZ);
@ -159,6 +163,35 @@ void Camera::apply_projection(const BoundingBoxf3& box, double near_z, double fa
}
}
#if ENABLE_LEGACY_OPENGL_REMOVAL
switch (m_type)
{
default:
case EType::Ortho:
{
const double dz = m_frustrum_zs.second - m_frustrum_zs.first;
const double zz = m_frustrum_zs.first + m_frustrum_zs.second;
m_projection_matrix.matrix() << 1.0 / w, 0.0, 0.0, 0.0,
0.0, 1.0 / h, 0.0, 0.0,
0.0, 0.0, -2.0 / dz, -zz / dz,
0.0, 0.0, 0.0, 1.0;
break;
}
case EType::Perspective:
{
const double n = m_frustrum_zs.first;
const double f = m_frustrum_zs.second;
const double dz = f - n;
const double zz = n + f;
const double fn = n * f;
m_projection_matrix.matrix() << n / w, 0.0, 0.0, 0.0,
0.0, n / h, 0.0, 0.0,
0.0, 0.0, -zz / dz, -2.0 * fn / dz,
0.0, 0.0, -1.0, 0.0;
break;
}
}
#else
glsafe(::glMatrixMode(GL_PROJECTION));
glsafe(::glLoadIdentity());
@ -179,6 +212,7 @@ void Camera::apply_projection(const BoundingBoxf3& box, double near_z, double fa
glsafe(::glGetDoublev(GL_PROJECTION_MATRIX, m_projection_matrix.data()));
glsafe(::glMatrixMode(GL_MODELVIEW));
#endif // ENABLE_LEGACY_OPENGL_REMOVAL
}
void Camera::zoom_to_box(const BoundingBoxf3& box, double margin_factor)
@ -298,8 +332,8 @@ std::pair<double, double> Camera::calc_tight_frustrum_zs_around(const BoundingBo
// box in eye space
const BoundingBoxf3 eye_box = box.transformed(m_view_matrix);
near_z = -eye_box.max(2);
far_z = -eye_box.min(2);
near_z = -eye_box.max.z();
far_z = -eye_box.min.z();
// apply margin
near_z -= FrustrumZMargin;
@ -465,19 +499,19 @@ void Camera::look_at(const Vec3d& position, const Vec3d& target, const Vec3d& up
m_distance = (position - target).norm();
const Vec3d new_position = m_target + m_distance * unit_z;
m_view_matrix(0, 0) = unit_x(0);
m_view_matrix(0, 1) = unit_x(1);
m_view_matrix(0, 2) = unit_x(2);
m_view_matrix(0, 0) = unit_x.x();
m_view_matrix(0, 1) = unit_x.y();
m_view_matrix(0, 2) = unit_x.z();
m_view_matrix(0, 3) = -unit_x.dot(new_position);
m_view_matrix(1, 0) = unit_y(0);
m_view_matrix(1, 1) = unit_y(1);
m_view_matrix(1, 2) = unit_y(2);
m_view_matrix(1, 0) = unit_y.x();
m_view_matrix(1, 1) = unit_y.y();
m_view_matrix(1, 2) = unit_y.z();
m_view_matrix(1, 3) = -unit_y.dot(new_position);
m_view_matrix(2, 0) = unit_z(0);
m_view_matrix(2, 1) = unit_z(1);
m_view_matrix(2, 2) = unit_z(2);
m_view_matrix(2, 0) = unit_z.x();
m_view_matrix(2, 1) = unit_z.y();
m_view_matrix(2, 2) = unit_z.z();
m_view_matrix(2, 3) = -unit_z.dot(new_position);
m_view_matrix(3, 0) = 0.0;

View file

@ -92,7 +92,9 @@ public:
double get_fov() const;
void apply_viewport(int x, int y, unsigned int w, unsigned int h);
#if !ENABLE_LEGACY_OPENGL_REMOVAL
void apply_view_matrix();
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
// Calculates and applies the projection matrix tighting the frustrum z range around the given box.
// If larger z span is needed, pass the desired values of near and far z (negative values are ignored)
void apply_projection(const BoundingBoxf3& box, double near_z = -1.0, double far_z = -1.0);

View file

@ -1662,13 +1662,17 @@ void GLCanvas3D::render()
camera.requires_zoom_to_bed = false;
}
#if !ENABLE_LEGACY_OPENGL_REMOVAL
camera.apply_view_matrix();
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
camera.apply_projection(_max_bounding_box(true, true));
#if !ENABLE_LEGACY_OPENGL_REMOVAL
GLfloat position_cam[4] = { 1.0f, 0.0f, 1.0f, 0.0f };
glsafe(::glLightfv(GL_LIGHT1, GL_POSITION, position_cam));
GLfloat position_top[4] = { -0.5f, -0.5f, 1.0f, 0.0f };
glsafe(::glLightfv(GL_LIGHT0, GL_POSITION, position_top));
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
wxGetApp().imgui()->new_frame();
@ -4569,7 +4573,9 @@ void GLCanvas3D::_render_thumbnail_internal(ThumbnailData& thumbnail_data, const
camera.set_scene_box(scene_bounding_box());
camera.apply_viewport(0, 0, thumbnail_data.width, thumbnail_data.height);
camera.zoom_to_box(volumes_box);
#if !ENABLE_LEGACY_OPENGL_REMOVAL
camera.apply_view_matrix();
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
#if ENABLE_GL_SHADERS_ATTRIBUTES
const Transform3d& view_matrix = camera.get_view_matrix();

View file

@ -35,7 +35,7 @@ std::pair<bool, std::string> GLShadersManager::init()
#if ENABLE_LEGACY_OPENGL_REMOVAL
#if ENABLE_GL_SHADERS_ATTRIBUTES
const std::string prefix = !GUI::wxGetApp().is_gl_version_greater_or_equal_to(3, 1) ? "140/" : "110/";
const std::string prefix = GUI::wxGetApp().is_gl_version_greater_or_equal_to(3, 1) ? "140/" : "110/";
// imgui shader
valid &= append_shader("imgui", { prefix + "imgui.vs", prefix + "imgui.fs" });
// basic shader, used to render all what was previously rendered using the immediate mode