Skip to content

Commit

Permalink
Tech ENABLE_LEGACY_OPENGL_REMOVAL - Fixed calculation of normal matri…
Browse files Browse the repository at this point in the history
…ces sent to shaders

(cherry picked from commit prusa3d/PrusaSlicer@c468dcb)
  • Loading branch information
enricoturri1966 authored and Noisyfox committed Oct 27, 2023
1 parent ff982b5 commit ccce5aa
Show file tree
Hide file tree
Showing 29 changed files with 175 additions and 152 deletions.
4 changes: 2 additions & 2 deletions resources/shaders/110/gouraud.vs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct SlopeDetection

uniform mat4 view_model_matrix;
uniform mat4 projection_matrix;
uniform mat3 normal_matrix;
uniform mat3 view_normal_matrix;
uniform mat4 volume_world_matrix;
uniform SlopeDetection slope;

Expand All @@ -51,7 +51,7 @@ varying vec3 eye_normal;
void main()
{
// First transform the normal into camera space and normalize the result.
eye_normal = normalize(normal_matrix * v_normal);
eye_normal = normalize(view_normal_matrix * v_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.
Expand Down
4 changes: 2 additions & 2 deletions resources/shaders/110/gouraud_light.vs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const vec3 LIGHT_FRONT_DIR = vec3(0.6985074, 0.1397015, 0.6985074);

uniform mat4 view_model_matrix;
uniform mat4 projection_matrix;
uniform mat3 normal_matrix;
uniform mat3 view_normal_matrix;

attribute vec3 v_position;
attribute vec3 v_normal;
Expand All @@ -27,7 +27,7 @@ varying vec2 intensity;
void main()
{
// First transform the normal into camera space and normalize the result.
vec3 normal = normalize(normal_matrix * v_normal);
vec3 normal = normalize(view_normal_matrix * v_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.
Expand Down
4 changes: 2 additions & 2 deletions resources/shaders/110/gouraud_light_instanced.vs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const vec3 LIGHT_FRONT_DIR = vec3(0.6985074, 0.1397015, 0.6985074);

uniform mat4 view_model_matrix;
uniform mat4 projection_matrix;
uniform mat3 normal_matrix;
uniform mat3 view_normal_matrix;

// vertex attributes
attribute vec3 v_position;
Expand All @@ -31,7 +31,7 @@ varying vec2 intensity;
void main()
{
// First transform the normal into camera space and normalize the result.
vec3 eye_normal = normalize(normal_matrix * v_normal);
vec3 eye_normal = normalize(view_normal_matrix * v_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.
Expand Down
4 changes: 2 additions & 2 deletions resources/shaders/110/mm_gouraud.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ uniform vec4 uniform_color;
uniform bool volume_mirrored;

uniform mat4 view_model_matrix;
uniform mat3 normal_matrix;
uniform mat3 view_normal_matrix;

varying vec3 clipping_planes_dots;
varying vec4 model_pos;
Expand Down Expand Up @@ -70,7 +70,7 @@ void main()
}
}
// First transform the normal into camera space and normalize the result.
vec3 eye_normal = normalize(normal_matrix * triangle_normal);
vec3 eye_normal = normalize(view_normal_matrix * triangle_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.
Expand Down
4 changes: 2 additions & 2 deletions resources/shaders/110/thumbnail.vs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const vec3 LIGHT_FRONT_DIR = vec3(0.6985074, 0.1397015, 0.6985074);

uniform mat4 view_model_matrix;
uniform mat4 projection_matrix;
uniform mat3 normal_matrix;
uniform mat3 view_normal_matrix;
uniform mat4 volume_world_matrix;

attribute vec3 v_position;
Expand All @@ -29,7 +29,7 @@ varying vec4 world_pos;
void main()
{
// First transform the normal into camera space and normalize the result.
vec3 normal = normalize(normal_matrix * v_normal);
vec3 normal = normalize(view_normal_matrix * v_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.
Expand Down
4 changes: 2 additions & 2 deletions resources/shaders/110/variable_layer_height.vs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const vec3 LIGHT_FRONT_DIR = vec3(0.6985074, 0.1397015, 0.6985074);

uniform mat4 view_model_matrix;
uniform mat4 projection_matrix;
uniform mat3 normal_matrix;
uniform mat3 view_normal_matrix;
uniform mat4 volume_world_matrix;
uniform float object_max_z;

Expand All @@ -38,7 +38,7 @@ void main()
// =====================================================

// First transform the normal into camera space and normalize the result.
vec3 normal = (object_max_z > 0.0) ? vec3(0.0, 0.0, 1.0) : normalize(normal_matrix * v_normal);
vec3 normal = (object_max_z > 0.0) ? vec3(0.0, 0.0, 1.0) : normalize(view_normal_matrix * v_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.
Expand Down
4 changes: 2 additions & 2 deletions resources/shaders/140/gouraud.vs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct SlopeDetection

uniform mat4 view_model_matrix;
uniform mat4 projection_matrix;
uniform mat3 normal_matrix;
uniform mat3 view_normal_matrix;
uniform mat4 volume_world_matrix;
uniform SlopeDetection slope;

Expand All @@ -51,7 +51,7 @@ out vec3 eye_normal;
void main()
{
// First transform the normal into camera space and normalize the result.
eye_normal = normalize(normal_matrix * v_normal);
eye_normal = normalize(view_normal_matrix * v_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.
Expand Down
4 changes: 2 additions & 2 deletions resources/shaders/140/gouraud_light.vs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const vec3 LIGHT_FRONT_DIR = vec3(0.6985074, 0.1397015, 0.6985074);

uniform mat4 view_model_matrix;
uniform mat4 projection_matrix;
uniform mat3 normal_matrix;
uniform mat3 view_normal_matrix;

in vec3 v_position;
in vec3 v_normal;
Expand All @@ -27,7 +27,7 @@ out vec2 intensity;
void main()
{
// First transform the normal into camera space and normalize the result.
vec3 normal = normalize(normal_matrix * v_normal);
vec3 normal = normalize(view_normal_matrix * v_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.
Expand Down
4 changes: 2 additions & 2 deletions resources/shaders/140/gouraud_light_instanced.vs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const vec3 LIGHT_FRONT_DIR = vec3(0.6985074, 0.1397015, 0.6985074);

uniform mat4 view_model_matrix;
uniform mat4 projection_matrix;
uniform mat3 normal_matrix;
uniform mat3 view_normal_matrix;

// vertex attributes
in vec3 v_position;
Expand All @@ -31,7 +31,7 @@ out vec2 intensity;
void main()
{
// First transform the normal into camera space and normalize the result.
vec3 eye_normal = normalize(normal_matrix * v_normal);
vec3 eye_normal = normalize(view_normal_matrix * v_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.
Expand Down
4 changes: 2 additions & 2 deletions resources/shaders/140/mm_gouraud.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ uniform vec4 uniform_color;
uniform bool volume_mirrored;

uniform mat4 view_model_matrix;
uniform mat3 normal_matrix;
uniform mat3 view_normal_matrix;

in vec3 clipping_planes_dots;
in vec4 model_pos;
Expand Down Expand Up @@ -70,7 +70,7 @@ void main()
}
}
// First transform the normal into camera space and normalize the result.
vec3 eye_normal = normalize(normal_matrix * triangle_normal);
vec3 eye_normal = normalize(view_normal_matrix * triangle_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.
Expand Down
4 changes: 2 additions & 2 deletions resources/shaders/140/thumbnail.vs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const vec3 LIGHT_FRONT_DIR = vec3(0.6985074, 0.1397015, 0.6985074);

uniform mat4 view_model_matrix;
uniform mat4 projection_matrix;
uniform mat3 normal_matrix;
uniform mat3 view_normal_matrix;
uniform mat4 volume_world_matrix;

in vec3 v_position;
Expand All @@ -29,7 +29,7 @@ out vec4 world_pos;
void main()
{
// First transform the normal into camera space and normalize the result.
vec3 normal = normalize(normal_matrix * v_normal);
vec3 normal = normalize(view_normal_matrix * v_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.
Expand Down
4 changes: 2 additions & 2 deletions resources/shaders/140/variable_layer_height.vs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const vec3 LIGHT_FRONT_DIR = vec3(0.6985074, 0.1397015, 0.6985074);

uniform mat4 view_model_matrix;
uniform mat4 projection_matrix;
uniform mat3 normal_matrix;
uniform mat3 view_normal_matrix;
uniform mat4 volume_world_matrix;
uniform float object_max_z;

Expand All @@ -38,7 +38,7 @@ void main()
// =====================================================

// First transform the normal into camera space and normalize the result.
vec3 normal = (object_max_z > 0.0) ? vec3(0.0, 0.0, 1.0) : normalize(normal_matrix * v_normal);
vec3 normal = (object_max_z > 0.0) ? vec3(0.0, 0.0, 1.0) : normalize(view_normal_matrix * v_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.
Expand Down
14 changes: 8 additions & 6 deletions src/slic3r/GUI/3DBed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,11 @@ void Bed3D::Axes::render()
{
auto render_axis = [this](GLShaderProgram* shader, const Transform3d& transform) {
const Camera& camera = wxGetApp().plater()->get_camera();
const Transform3d matrix = camera.get_view_matrix() * transform;
shader->set_uniform("view_model_matrix", matrix);
const Transform3d& view_matrix = camera.get_view_matrix();
shader->set_uniform("view_model_matrix", view_matrix * transform);
shader->set_uniform("projection_matrix", camera.get_projection_matrix());
shader->set_uniform("normal_matrix", (Matrix3d)matrix.matrix().block(0, 0, 3, 3).inverse().transpose());
const Matrix3d view_normal_matrix = view_matrix.matrix().block(0, 0, 3, 3) * transform.matrix().block(0, 0, 3, 3).inverse().transpose();
shader->set_uniform("view_normal_matrix", view_normal_matrix);
m_arrow.render();
};

Expand Down Expand Up @@ -675,10 +676,11 @@ void Bed3D::render_model(const Transform3d& view_matrix, const Transform3d& proj
if (shader != nullptr) {
shader->start_using();
shader->set_uniform("emission_factor", 0.0f);
const Transform3d matrix = view_matrix * Geometry::assemble_transform(m_model_offset);
shader->set_uniform("view_model_matrix", matrix);
const Transform3d model_matrix = Geometry::assemble_transform(m_model_offset);
shader->set_uniform("view_model_matrix", view_matrix * model_matrix);
shader->set_uniform("projection_matrix", projection_matrix);
shader->set_uniform("normal_matrix", (Matrix3d)matrix.matrix().block(0, 0, 3, 3).inverse().transpose());
const Matrix3d view_normal_matrix = view_matrix.matrix().block(0, 0, 3, 3) * model_matrix.matrix().block(0, 0, 3, 3).inverse().transpose();
shader->set_uniform("view_normal_matrix", view_normal_matrix);
m_model.render();
shader->stop_using();
}
Expand Down
11 changes: 6 additions & 5 deletions src/slic3r/GUI/3DScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -912,13 +912,14 @@ void GLVolumeCollection::render(GLVolumeCollection::ERenderType type, bool disab
glcheck();

volume.first->model.set_color(volume.first->render_color);
const Transform3d matrix = view_matrix * volume.first->world_matrix();
shader->set_uniform("view_model_matrix", matrix);
const Transform3d model_matrix = volume.first->world_matrix();
shader->set_uniform("view_model_matrix", view_matrix * model_matrix);
shader->set_uniform("projection_matrix", projection_matrix);
shader->set_uniform("normal_matrix", (Matrix3d)matrix.matrix().block(0, 0, 3, 3).inverse().transpose());
//BBS: add outline related logic
const Matrix3d view_normal_matrix = view_matrix.matrix().block(0, 0, 3, 3) * model_matrix.matrix().block(0, 0, 3, 3).inverse().transpose();
shader->set_uniform("view_normal_matrix", view_normal_matrix);
//BBS: add outline related logic
if (with_outline && volume.first->selected)
volume.first->render_with_outline(matrix);
volume.first->render_with_outline(view_matrix * model_matrix);
else
volume.first->render();

Expand Down
18 changes: 9 additions & 9 deletions src/slic3r/GUI/GCodeViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,12 @@ void GCodeViewer::SequentialView::Marker::render(int canvas_width, int canvas_he
shader->set_uniform("emission_factor", 0.0f);

const Camera& camera = wxGetApp().plater()->get_camera();
const Transform3d matrix = camera.get_view_matrix() * m_world_transform.cast<double>();
shader->set_uniform("view_model_matrix", matrix);
const Transform3d& view_matrix = camera.get_view_matrix();
const Transform3d model_matrix = m_world_transform.cast<double>();
shader->set_uniform("view_model_matrix", view_matrix * model_matrix);
shader->set_uniform("projection_matrix", camera.get_projection_matrix());
shader->set_uniform("normal_matrix", (Matrix3d)matrix.matrix().block(0, 0, 3, 3).inverse().transpose());
const Matrix3d view_normal_matrix = view_matrix.matrix().block(0, 0, 3, 3) * model_matrix.matrix().block(0, 0, 3, 3).inverse().transpose();
shader->set_uniform("view_normal_matrix", view_normal_matrix);

m_model.render();

Expand Down Expand Up @@ -3887,10 +3889,9 @@ void GCodeViewer::render_toolpaths()

shader->start_using();

const Transform3d& view_matrix = camera.get_view_matrix();
shader->set_uniform("view_model_matrix", view_matrix);
shader->set_uniform("view_model_matrix", camera.get_view_matrix());
shader->set_uniform("projection_matrix", camera.get_projection_matrix());
shader->set_uniform("normal_matrix", (Matrix3d)view_matrix.matrix().block(0, 0, 3, 3).inverse().transpose());
shader->set_uniform("view_normal_matrix", (Matrix3d)Matrix3d::Identity());

if (buffer.render_primitive_type == TBuffer::ERenderPrimitiveType::InstancedModel) {
shader->set_uniform("emission_factor", 0.25f);
Expand Down Expand Up @@ -3974,10 +3975,9 @@ void GCodeViewer::render_toolpaths()

shader->start_using();

const Transform3d& view_matrix = camera.get_view_matrix();
shader->set_uniform("view_model_matrix", view_matrix);
shader->set_uniform("view_model_matrix", camera.get_view_matrix());
shader->set_uniform("projection_matrix", camera.get_projection_matrix());
shader->set_uniform("normal_matrix", (Matrix3d)view_matrix.matrix().block(0, 0, 3, 3).inverse().transpose());
shader->set_uniform("view_normal_matrix", (Matrix3d)Matrix3d::Identity());

const int position_id = shader->get_attrib_location("v_position");
const int normal_id = shader->get_attrib_location("v_normal");
Expand Down
24 changes: 14 additions & 10 deletions src/slic3r/GUI/GLCanvas3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ void GLCanvas3D::LayersEditing::render_active_object_annotations(const GLCanvas3
shader->set_uniform("object_max_z", m_object_max_z);
shader->set_uniform("view_model_matrix", Transform3d::Identity());
shader->set_uniform("projection_matrix", Transform3d::Identity());
shader->set_uniform("normal_matrix", (Matrix3d)Eigen::Matrix3d::Identity());
shader->set_uniform("view_normal_matrix", (Matrix3d)Matrix3d::Identity());

glsafe(::glPixelStorei(GL_UNPACK_ALIGNMENT, 1));
glsafe(::glBindTexture(GL_TEXTURE_2D, m_z_texture_id));
Expand Down Expand Up @@ -569,9 +569,11 @@ void GLCanvas3D::LayersEditing::render_volumes(const GLCanvas3D& canvas, const G

shader->set_uniform("volume_world_matrix", glvolume->world_matrix());
shader->set_uniform("object_max_z", 0.0f);
const Transform3d view_model_matrix = camera.get_view_matrix() * glvolume->world_matrix();
shader->set_uniform("view_model_matrix", view_model_matrix);
shader->set_uniform("normal_matrix", (Matrix3d)view_model_matrix.matrix().block(0, 0, 3, 3).inverse().transpose());
const Transform3d& view_matrix = camera.get_view_matrix();
const Transform3d model_matrix = glvolume->world_matrix();
shader->set_uniform("view_model_matrix", view_matrix * model_matrix);
const Matrix3d view_normal_matrix = view_matrix.matrix().block(0, 0, 3, 3) * model_matrix.matrix().block(0, 0, 3, 3).inverse().transpose();
shader->set_uniform("view_normal_matrix", view_normal_matrix);

glvolume->render();
}
Expand Down Expand Up @@ -5597,10 +5599,11 @@ void GLCanvas3D::render_thumbnail_internal(ThumbnailData& thumbnail_data, const

const bool is_active = vol->is_active;
vol->is_active = true;
const Transform3d matrix = view_matrix * vol->world_matrix();
shader->set_uniform("view_model_matrix", matrix);
const Transform3d model_matrix = vol->world_matrix();
shader->set_uniform("view_model_matrix", view_matrix * model_matrix);
shader->set_uniform("projection_matrix", projection_matrix);
shader->set_uniform("normal_matrix", (Matrix3d) matrix.matrix().block(0, 0, 3, 3).inverse().transpose());
const Matrix3d view_normal_matrix = view_matrix.matrix().block(0, 0, 3, 3) * model_matrix.matrix().block(0, 0, 3, 3).inverse().transpose();
shader->set_uniform("view_normal_matrix", view_normal_matrix);
vol->simple_render(shader, model_objects, extruder_colors);
vol->is_active = is_active;
}
Expand Down Expand Up @@ -5634,10 +5637,11 @@ void GLCanvas3D::render_thumbnail_internal(ThumbnailData& thumbnail_data, const
// the volume may have been deactivated by an active gizmo
const bool is_active = vol->is_active;
vol->is_active = true;
const Transform3d matrix = view_matrix * vol->world_matrix();
shader->set_uniform("view_model_matrix", matrix);
const Transform3d model_matrix = vol->world_matrix();
shader->set_uniform("view_model_matrix", view_matrix * model_matrix);
shader->set_uniform("projection_matrix", projection_matrix);
shader->set_uniform("normal_matrix", (Matrix3d) matrix.matrix().block(0, 0, 3, 3).inverse().transpose());
const Matrix3d view_normal_matrix = view_matrix.matrix().block(0, 0, 3, 3) * model_matrix.matrix().block(0, 0, 3, 3).inverse().transpose();
shader->set_uniform("view_normal_matrix", view_normal_matrix);
vol->simple_render(shader, model_objects, extruder_colors);
vol->is_active = is_active;
}
Expand Down
Loading

0 comments on commit ccce5aa

Please sign in to comment.