1st installment of tech ENABLE_ENVIRONMENT_MAP

This commit is contained in:
enricoturri1966 2020-05-28 15:27:29 +02:00
parent 4b6bcd7028
commit c63e03c367
11 changed files with 93 additions and 6 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

View File

@ -17,6 +17,9 @@ struct SlopeDetection
uniform vec4 uniform_color; uniform vec4 uniform_color;
uniform SlopeDetection slope; uniform SlopeDetection slope;
uniform sampler2D environment_tex;
uniform bool use_environment_tex;
varying vec3 clipping_planes_dots; varying vec3 clipping_planes_dots;
// x = tainted, y = specular; // x = tainted, y = specular;
@ -26,6 +29,7 @@ varying vec3 delta_box_min;
varying vec3 delta_box_max; varying vec3 delta_box_max;
varying float world_normal_z; varying float world_normal_z;
varying vec3 eye_normal;
vec3 slope_color() vec3 slope_color()
{ {
@ -40,5 +44,8 @@ void main()
vec3 color = slope.actived ? slope_color() : uniform_color.rgb; vec3 color = slope.actived ? slope_color() : uniform_color.rgb;
// if the fragment is outside the print volume -> use darker color // if the fragment is outside the print volume -> use darker color
color = (any(lessThan(delta_box_min, ZERO)) || any(greaterThan(delta_box_max, ZERO))) ? mix(color, ZERO, 0.3333) : color; color = (any(lessThan(delta_box_min, ZERO)) || any(greaterThan(delta_box_max, ZERO))) ? mix(color, ZERO, 0.3333) : color;
gl_FragColor = vec4(vec3(intensity.y, intensity.y, intensity.y) + color * intensity.x, uniform_color.a); if (use_environment_tex)
gl_FragColor = vec4(0.45 * texture2D(environment_tex, normalize(eye_normal).xy * 0.5 + 0.5).xyz + 0.8 * color * intensity.x, uniform_color.a);
else
gl_FragColor = vec4(vec3(intensity.y) + color * intensity.x, uniform_color.a);
} }

View File

@ -51,22 +51,23 @@ varying vec3 delta_box_max;
varying vec3 clipping_planes_dots; varying vec3 clipping_planes_dots;
varying float world_normal_z; varying float world_normal_z;
varying vec3 eye_normal;
void main() void main()
{ {
// First transform the normal into camera space and normalize the result. // First transform the normal into camera space and normalize the result.
vec3 normal = normalize(gl_NormalMatrix * gl_Normal); eye_normal = normalize(gl_NormalMatrix * gl_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. // 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. // 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); float NdotL = max(dot(eye_normal, LIGHT_TOP_DIR), 0.0);
intensity.x = INTENSITY_AMBIENT + NdotL * LIGHT_TOP_DIFFUSE; intensity.x = INTENSITY_AMBIENT + NdotL * LIGHT_TOP_DIFFUSE;
vec3 position = (gl_ModelViewMatrix * gl_Vertex).xyz; vec3 position = (gl_ModelViewMatrix * gl_Vertex).xyz;
intensity.y = LIGHT_TOP_SPECULAR * pow(max(dot(-normalize(position), reflect(-LIGHT_TOP_DIR, normal)), 0.0), LIGHT_TOP_SHININESS); intensity.y = LIGHT_TOP_SPECULAR * pow(max(dot(-normalize(position), reflect(-LIGHT_TOP_DIR, eye_normal)), 0.0), LIGHT_TOP_SHININESS);
// Perform the same lighting calculation for the 2nd light source (no specular applied). // Perform the same lighting calculation for the 2nd light source (no specular applied).
NdotL = max(dot(normal, LIGHT_FRONT_DIR), 0.0); NdotL = max(dot(eye_normal, LIGHT_FRONT_DIR), 0.0);
intensity.x += NdotL * LIGHT_FRONT_DIFFUSE; intensity.x += NdotL * LIGHT_FRONT_DIFFUSE;
// compute deltas for out of print volume detection (world coordinates) // compute deltas for out of print volume detection (world coordinates)

View File

@ -42,5 +42,8 @@
// Enable rendering of objects colored by facets' slope // Enable rendering of objects colored by facets' slope
#define ENABLE_SLOPE_RENDERING (1 && ENABLE_2_3_0_ALPHA1) #define ENABLE_SLOPE_RENDERING (1 && ENABLE_2_3_0_ALPHA1)
// Enable rendering of objects using environment map
#define ENABLE_ENVIRONMENT_MAP (1 && ENABLE_2_3_0_ALPHA1)
#endif // _prusaslicer_technologies_h_ #endif // _prusaslicer_technologies_h_

View File

@ -1,6 +1,11 @@
#include <GL/glew.h> #include <GL/glew.h>
#include "3DScene.hpp" #include "3DScene.hpp"
#if ENABLE_ENVIRONMENT_MAP
#include "GUI_App.hpp"
#include "Plater.hpp"
#include "AppConfig.hpp"
#endif // ENABLE_ENVIRONMENT_MAP
#include "libslic3r/ExtrusionEntity.hpp" #include "libslic3r/ExtrusionEntity.hpp"
#include "libslic3r/ExtrusionEntityCollection.hpp" #include "libslic3r/ExtrusionEntityCollection.hpp"
@ -663,6 +668,10 @@ void GLVolumeCollection::render(GLVolumeCollection::ERenderType type, bool disab
GLint slope_normal_matrix_id = (current_program_id > 0) ? ::glGetUniformLocation(current_program_id, "slope.volume_world_normal_matrix") : -1; GLint slope_normal_matrix_id = (current_program_id > 0) ? ::glGetUniformLocation(current_program_id, "slope.volume_world_normal_matrix") : -1;
GLint slope_z_range_id = (current_program_id > 0) ? ::glGetUniformLocation(current_program_id, "slope.z_range") : -1; GLint slope_z_range_id = (current_program_id > 0) ? ::glGetUniformLocation(current_program_id, "slope.z_range") : -1;
#endif // ENABLE_SLOPE_RENDERING #endif // ENABLE_SLOPE_RENDERING
#if ENABLE_ENVIRONMENT_MAP
GLint use_environment_tex_id = (current_program_id > 0) ? ::glGetUniformLocation(current_program_id, "use_environment_tex") : -1;
#endif // ENABLE_ENVIRONMENT_MAP
glcheck(); glcheck();
if (print_box_min_id != -1) if (print_box_min_id != -1)
@ -682,6 +691,18 @@ void GLVolumeCollection::render(GLVolumeCollection::ERenderType type, bool disab
glsafe(::glUniform2fv(slope_z_range_id, 1, (const GLfloat*)m_slope.z_range.data())); glsafe(::glUniform2fv(slope_z_range_id, 1, (const GLfloat*)m_slope.z_range.data()));
#endif // ENABLE_SLOPE_RENDERING #endif // ENABLE_SLOPE_RENDERING
#if ENABLE_ENVIRONMENT_MAP
unsigned int environment_texture_id = GUI::wxGetApp().plater()->get_environment_texture_id();
bool use_environment_texture = current_program_id > 0 && environment_texture_id > 0 && GUI::wxGetApp().app_config->get("use_environment_map") == "1";
if (use_environment_tex_id != -1)
{
glsafe(::glUniform1i(use_environment_tex_id, use_environment_texture ? 1 : 0));
if (use_environment_texture)
glsafe(::glBindTexture(GL_TEXTURE_2D, environment_texture_id));
}
#endif // ENABLE_ENVIRONMENT_MAP
GLVolumeWithIdAndZList to_render = volumes_to_render(this->volumes, type, view_matrix, filter_func); GLVolumeWithIdAndZList to_render = volumes_to_render(this->volumes, type, view_matrix, filter_func);
for (GLVolumeWithIdAndZ& volume : to_render) { for (GLVolumeWithIdAndZ& volume : to_render) {
volume.first->set_render_color(); volume.first->set_render_color();
@ -712,6 +733,11 @@ void GLVolumeCollection::render(GLVolumeCollection::ERenderType type, bool disab
#endif // ENABLE_SLOPE_RENDERING #endif // ENABLE_SLOPE_RENDERING
} }
#if ENABLE_ENVIRONMENT_MAP
if (use_environment_tex_id != -1 && use_environment_texture)
glsafe(::glBindTexture(GL_TEXTURE_2D, 0));
#endif // ENABLE_ENVIRONMENT_MAP
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0)); glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0));
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)); glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));

View File

@ -93,6 +93,11 @@ void AppConfig::set_defaults()
if (get("use_free_camera").empty()) if (get("use_free_camera").empty())
set("use_free_camera", "0"); set("use_free_camera", "0");
#if ENABLE_ENVIRONMENT_MAP
if (get("use_environment_map").empty())
set("use_environment_map", "0");
#endif // ENABLE_ENVIRONMENT_MAP
// Remove legacy window positions/sizes // Remove legacy window positions/sizes
erase("", "main_frame_maximized"); erase("", "main_frame_maximized");
erase("", "main_frame_pos"); erase("", "main_frame_pos");

View File

@ -1994,6 +1994,10 @@ void GLCanvas3D::render()
return; return;
} }
#if ENABLE_ENVIRONMENT_MAP
wxGetApp().plater()->init_environment_texture();
#endif // ENABLE_ENVIRONMENT_MAP
const Size& cnv_size = get_canvas_size(); const Size& cnv_size = get_canvas_size();
// Probably due to different order of events on Linux/GTK2, when one switched from 3D scene // Probably due to different order of events on Linux/GTK2, when one switched from 3D scene
// to preview, this was called before canvas had its final size. It reported zero width // to preview, this was called before canvas had its final size. It reported zero width

View File

@ -1583,6 +1583,9 @@ struct Plater::priv
Sidebar *sidebar; Sidebar *sidebar;
Bed3D bed; Bed3D bed;
Camera camera; Camera camera;
#if ENABLE_ENVIRONMENT_MAP
GLTexture environment_texture;
#endif // ENABLE_ENVIRONMENT_MAP
Mouse3DController mouse3d_controller; Mouse3DController mouse3d_controller;
View3D* view3D; View3D* view3D;
GLToolbar view_toolbar; GLToolbar view_toolbar;
@ -2078,7 +2081,6 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
}); });
wxGetApp().other_instance_message_handler()->init(this->q); wxGetApp().other_instance_message_handler()->init(this->q);
// collapse sidebar according to saved value // collapse sidebar according to saved value
bool is_collapsed = wxGetApp().app_config->get("collapsed_sidebar") == "1"; bool is_collapsed = wxGetApp().app_config->get("collapsed_sidebar") == "1";
sidebar->collapse(is_collapsed); sidebar->collapse(is_collapsed);
@ -5531,6 +5533,19 @@ Camera& Plater::get_camera()
return p->camera; return p->camera;
} }
#if ENABLE_ENVIRONMENT_MAP
void Plater::init_environment_texture()
{
if (p->environment_texture.get_id() == 0)
p->environment_texture.load_from_file(resources_dir() + "/icons/Pmetal_001.png", false, GLTexture::SingleThreaded, false);
}
unsigned int Plater::get_environment_texture_id() const
{
return p->environment_texture.get_id();
}
#endif // ENABLE_ENVIRONMENT_MAP
const Bed3D& Plater::get_bed() const const Bed3D& Plater::get_bed() const
{ {
return p->bed; return p->bed;

View File

@ -319,6 +319,11 @@ public:
const Camera& get_camera() const; const Camera& get_camera() const;
Camera& get_camera(); Camera& get_camera();
#if ENABLE_ENVIRONMENT_MAP
void init_environment_texture();
unsigned int get_environment_texture_id() const;
#endif // ENABLE_ENVIRONMENT_MAP
const Bed3D& get_bed() const; const Bed3D& get_bed() const;
Bed3D& get_bed(); Bed3D& get_bed();

View File

@ -180,10 +180,28 @@ void PreferencesDialog::build()
create_settings_mode_widget(); create_settings_mode_widget();
#if ENABLE_ENVIRONMENT_MAP
m_optgroup_render = std::make_shared<ConfigOptionsGroup>(this, _(L("Render")));
m_optgroup_render->label_width = 40;
m_optgroup_render->m_on_change = [this](t_config_option_key opt_key, boost::any value) {
m_values[opt_key] = boost::any_cast<bool>(value) ? "1" : "0";
};
def.label = L("Use environment map");
def.type = coBool;
def.tooltip = L("If enabled, renders object using the environment map.");
def.set_default_value(new ConfigOptionBool{ app_config->get("use_environment_map") == "1" });
option = Option(def, "use_environment_map");
m_optgroup_render->append_single_option_line(option);
#endif // ENABLE_ENVIRONMENT_MAP
auto sizer = new wxBoxSizer(wxVERTICAL); auto sizer = new wxBoxSizer(wxVERTICAL);
sizer->Add(m_optgroup_general->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10); sizer->Add(m_optgroup_general->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
sizer->Add(m_optgroup_camera->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10); sizer->Add(m_optgroup_camera->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
sizer->Add(m_optgroup_gui->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10); sizer->Add(m_optgroup_gui->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
#if ENABLE_ENVIRONMENT_MAP
sizer->Add(m_optgroup_render->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
#endif // ENABLE_ENVIRONMENT_MAP
SetFont(wxGetApp().normal_font()); SetFont(wxGetApp().normal_font());

View File

@ -20,6 +20,9 @@ class PreferencesDialog : public DPIDialog
std::shared_ptr<ConfigOptionsGroup> m_optgroup_general; std::shared_ptr<ConfigOptionsGroup> m_optgroup_general;
std::shared_ptr<ConfigOptionsGroup> m_optgroup_camera; std::shared_ptr<ConfigOptionsGroup> m_optgroup_camera;
std::shared_ptr<ConfigOptionsGroup> m_optgroup_gui; std::shared_ptr<ConfigOptionsGroup> m_optgroup_gui;
#if ENABLE_ENVIRONMENT_MAP
std::shared_ptr<ConfigOptionsGroup> m_optgroup_render;
#endif // ENABLE_ENVIRONMENT_MAP
wxSizer* m_icon_size_sizer; wxSizer* m_icon_size_sizer;
wxRadioBox* m_layout_mode_box; wxRadioBox* m_layout_mode_box;
bool isOSX {false}; bool isOSX {false};