Tech ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES - Use vertex attributes and matrices in shaders.

Shader: gouraud_light - Bed axes
This commit is contained in:
enricoturri1966 2022-03-03 09:21:52 +01:00
parent abb5f5f99a
commit 8315f43ed0
5 changed files with 88 additions and 0 deletions

View File

@ -0,0 +1,45 @@
#version 110
#define INTENSITY_CORRECTION 0.6
// normalized values for (-0.6/1.31, 0.6/1.31, 1./1.31)
const vec3 LIGHT_TOP_DIR = vec3(-0.4574957, 0.4574957, 0.7624929);
#define LIGHT_TOP_DIFFUSE (0.8 * INTENSITY_CORRECTION)
#define LIGHT_TOP_SPECULAR (0.125 * INTENSITY_CORRECTION)
#define LIGHT_TOP_SHININESS 20.0
// normalized values for (1./1.43, 0.2/1.43, 1./1.43)
const vec3 LIGHT_FRONT_DIR = vec3(0.6985074, 0.1397015, 0.6985074);
#define LIGHT_FRONT_DIFFUSE (0.3 * INTENSITY_CORRECTION)
#define INTENSITY_AMBIENT 0.3
attribute vec3 v_position;
attribute vec3 v_normal;
uniform mat4 view_model_matrix;
uniform mat4 projection_matrix;
uniform mat3 normal_matrix;
// x = tainted, y = specular;
varying vec2 intensity;
void main()
{
// First transform the normal into camera space and normalize the result.
vec3 normal = normalize(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.
float NdotL = max(dot(normal, LIGHT_TOP_DIR), 0.0);
intensity.x = INTENSITY_AMBIENT + NdotL * LIGHT_TOP_DIFFUSE;
vec4 position = view_model_matrix * vec4(v_position, 1.0);
intensity.y = LIGHT_TOP_SPECULAR * pow(max(dot(-normalize(position.xyz), reflect(-LIGHT_TOP_DIR, normal)), 0.0), LIGHT_TOP_SHININESS);
// Perform the same lighting calculation for the 2nd light source (no specular applied).
NdotL = max(dot(normal, LIGHT_FRONT_DIR), 0.0);
intensity.x += NdotL * LIGHT_FRONT_DIFFUSE;
gl_Position = projection_matrix * position;
}

View File

@ -109,17 +109,32 @@ const float Bed3D::Axes::DefaultTipLength = 5.0f;
void Bed3D::Axes::render() void Bed3D::Axes::render()
{ {
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
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);
shader->set_uniform("projection_matrix", camera.get_projection_matrix());
shader->set_uniform("normal_matrix", (Matrix3d)matrix.matrix().block(0, 0, 3, 3).inverse().transpose());
#else
auto render_axis = [this](const Transform3f& transform) { auto render_axis = [this](const Transform3f& transform) {
glsafe(::glPushMatrix()); glsafe(::glPushMatrix());
glsafe(::glMultMatrixf(transform.data())); glsafe(::glMultMatrixf(transform.data()));
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
m_arrow.render(); m_arrow.render();
#if !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
glsafe(::glPopMatrix()); glsafe(::glPopMatrix());
#endif // !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
}; };
if (!m_arrow.is_initialized()) if (!m_arrow.is_initialized())
m_arrow.init_from(stilized_arrow(16, DefaultTipRadius, DefaultTipLength, DefaultStemRadius, m_stem_length)); m_arrow.init_from(stilized_arrow(16, DefaultTipRadius, DefaultTipLength, DefaultStemRadius, m_stem_length));
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
GLShaderProgram* shader = wxGetApp().get_shader("gouraud_light_attr");
#else
GLShaderProgram* shader = wxGetApp().get_shader("gouraud_light"); GLShaderProgram* shader = wxGetApp().get_shader("gouraud_light");
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
if (shader == nullptr) if (shader == nullptr)
return; return;
@ -134,7 +149,11 @@ void Bed3D::Axes::render()
#else #else
m_arrow.set_color(-1, ColorRGBA::X()); m_arrow.set_color(-1, ColorRGBA::X());
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL #endif // ENABLE_GLBEGIN_GLEND_REMOVAL
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
render_axis(shader, Geometry::assemble_transform(m_origin, { 0.0, 0.5 * M_PI, 0.0 }));
#else
render_axis(Geometry::assemble_transform(m_origin, { 0.0, 0.5 * M_PI, 0.0 }).cast<float>()); render_axis(Geometry::assemble_transform(m_origin, { 0.0, 0.5 * M_PI, 0.0 }).cast<float>());
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
// y axis // y axis
#if ENABLE_GLBEGIN_GLEND_REMOVAL #if ENABLE_GLBEGIN_GLEND_REMOVAL
@ -142,7 +161,11 @@ void Bed3D::Axes::render()
#else #else
m_arrow.set_color(-1, ColorRGBA::Y()); m_arrow.set_color(-1, ColorRGBA::Y());
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL #endif // ENABLE_GLBEGIN_GLEND_REMOVAL
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
render_axis(shader, Geometry::assemble_transform(m_origin, { -0.5 * M_PI, 0.0, 0.0 }));
#else
render_axis(Geometry::assemble_transform(m_origin, { -0.5 * M_PI, 0.0, 0.0 }).cast<float>()); render_axis(Geometry::assemble_transform(m_origin, { -0.5 * M_PI, 0.0, 0.0 }).cast<float>());
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
// z axis // z axis
#if ENABLE_GLBEGIN_GLEND_REMOVAL #if ENABLE_GLBEGIN_GLEND_REMOVAL
@ -150,7 +173,11 @@ void Bed3D::Axes::render()
#else #else
m_arrow.set_color(-1, ColorRGBA::Z()); m_arrow.set_color(-1, ColorRGBA::Z());
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL #endif // ENABLE_GLBEGIN_GLEND_REMOVAL
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
render_axis(shader, Geometry::assemble_transform(m_origin));
#else
render_axis(Geometry::assemble_transform(m_origin).cast<float>()); render_axis(Geometry::assemble_transform(m_origin).cast<float>());
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
shader->stop_using(); shader->stop_using();

View File

@ -296,6 +296,13 @@ void GLShaderProgram::set_uniform(int id, const Matrix3f& value) const
glsafe(::glUniformMatrix3fv(id, 1, GL_FALSE, static_cast<const GLfloat*>(value.data()))); glsafe(::glUniformMatrix3fv(id, 1, GL_FALSE, static_cast<const GLfloat*>(value.data())));
} }
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
void GLShaderProgram::set_uniform(int id, const Matrix3d& value) const
{
set_uniform(id, (Matrix3f)value.cast<float>());
}
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
void GLShaderProgram::set_uniform(int id, const Vec3f& value) const void GLShaderProgram::set_uniform(int id, const Vec3f& value) const
{ {
if (id >= 0) if (id >= 0)

View File

@ -61,6 +61,9 @@ public:
void set_uniform(const char* name, const Transform3f& value) const { set_uniform(get_uniform_location(name), value); } void set_uniform(const char* name, const Transform3f& value) const { set_uniform(get_uniform_location(name), value); }
void set_uniform(const char* name, const Transform3d& value) const { set_uniform(get_uniform_location(name), value); } void set_uniform(const char* name, const Transform3d& value) const { set_uniform(get_uniform_location(name), value); }
void set_uniform(const char* name, const Matrix3f& value) const { set_uniform(get_uniform_location(name), value); } void set_uniform(const char* name, const Matrix3f& value) const { set_uniform(get_uniform_location(name), value); }
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
void set_uniform(const char* name, const Matrix3d& value) const { set_uniform(get_uniform_location(name), value); }
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
void set_uniform(const char* name, const Vec3f& value) const { set_uniform(get_uniform_location(name), value); } void set_uniform(const char* name, const Vec3f& value) const { set_uniform(get_uniform_location(name), value); }
void set_uniform(const char* name, const Vec3d& value) const { set_uniform(get_uniform_location(name), value); } void set_uniform(const char* name, const Vec3d& value) const { set_uniform(get_uniform_location(name), value); }
void set_uniform(const char* name, const ColorRGB& value) const { set_uniform(get_uniform_location(name), value); } void set_uniform(const char* name, const ColorRGB& value) const { set_uniform(get_uniform_location(name), value); }
@ -80,6 +83,9 @@ public:
void set_uniform(int id, const Transform3f& value) const; void set_uniform(int id, const Transform3f& value) const;
void set_uniform(int id, const Transform3d& value) const; void set_uniform(int id, const Transform3d& value) const;
void set_uniform(int id, const Matrix3f& value) const; void set_uniform(int id, const Matrix3f& value) const;
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
void set_uniform(int id, const Matrix3d& value) const;
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
void set_uniform(int id, const Vec3f& value) const; void set_uniform(int id, const Vec3f& value) const;
void set_uniform(int id, const Vec3d& value) const; void set_uniform(int id, const Vec3d& value) const;
void set_uniform(int id, const ColorRGB& value) const; void set_uniform(int id, const ColorRGB& value) const;

View File

@ -49,6 +49,9 @@ std::pair<bool, std::string> GLShadersManager::init()
valid &= append_shader("toolpaths_cog", { "toolpaths_cog.vs", "toolpaths_cog.fs" }); valid &= append_shader("toolpaths_cog", { "toolpaths_cog.vs", "toolpaths_cog.fs" });
#endif // ENABLE_SHOW_TOOLPATHS_COG #endif // ENABLE_SHOW_TOOLPATHS_COG
// used to render bed axes and model, selection hints, gcode sequential view marker model, preview shells, options in gcode preview // used to render bed axes and model, selection hints, gcode sequential view marker model, preview shells, options in gcode preview
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
valid &= append_shader("gouraud_light_attr", { "gouraud_light_attr.vs", "gouraud_light.fs" });
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
valid &= append_shader("gouraud_light", { "gouraud_light.vs", "gouraud_light.fs" }); valid &= append_shader("gouraud_light", { "gouraud_light.vs", "gouraud_light.fs" });
// used to render printbed // used to render printbed
valid &= append_shader("printbed", { "printbed.vs", "printbed.fs" }); valid &= append_shader("printbed", { "printbed.vs", "printbed.fs" });