2020-05-20 12:11:22 +00:00
|
|
|
#include "libslic3r/libslic3r.h"
|
|
|
|
#include "GLShadersManager.hpp"
|
2020-05-21 08:15:00 +00:00
|
|
|
#include "3DScene.hpp"
|
2020-05-27 14:19:40 +00:00
|
|
|
#include "GUI_App.hpp"
|
2020-05-20 12:11:22 +00:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <algorithm>
|
2020-12-10 12:20:18 +00:00
|
|
|
#include <string_view>
|
|
|
|
using namespace std::literals;
|
2020-05-20 12:11:22 +00:00
|
|
|
|
2020-05-21 08:15:00 +00:00
|
|
|
#include <GL/glew.h>
|
|
|
|
|
2020-05-20 12:11:22 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
std::pair<bool, std::string> GLShadersManager::init()
|
|
|
|
{
|
|
|
|
std::string error;
|
|
|
|
|
2020-12-10 12:20:18 +00:00
|
|
|
auto append_shader = [this, &error](const std::string& name, const GLShaderProgram::ShaderFilenames& filenames,
|
|
|
|
const std::initializer_list<std::string_view> &defines = {}) {
|
2020-05-20 12:11:22 +00:00
|
|
|
m_shaders.push_back(std::make_unique<GLShaderProgram>());
|
2020-12-10 12:20:18 +00:00
|
|
|
if (!m_shaders.back()->init_from_files(name, filenames, defines)) {
|
2020-05-20 12:11:22 +00:00
|
|
|
error += name + "\n";
|
|
|
|
// if any error happens while initializating the shader, we remove it from the list
|
|
|
|
m_shaders.pop_back();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(m_shaders.empty());
|
|
|
|
|
|
|
|
bool valid = true;
|
|
|
|
|
2020-05-27 14:19:40 +00:00
|
|
|
// used to render bed axes and model, selection hints, gcode sequential view marker model, preview shells
|
2020-05-20 12:11:22 +00:00
|
|
|
valid &= append_shader("gouraud_light", { "gouraud_light.vs", "gouraud_light.fs" });
|
|
|
|
// used to render printbed
|
|
|
|
valid &= append_shader("printbed", { "printbed.vs", "printbed.fs" });
|
2020-05-22 11:21:43 +00:00
|
|
|
// used to render options in gcode preview
|
|
|
|
valid &= append_shader("options_110", { "options_110.vs", "options_110.fs" });
|
2020-08-19 09:25:12 +00:00
|
|
|
if (GUI::wxGetApp().is_glsl_version_greater_or_equal_to(1, 20))
|
2020-08-25 06:12:28 +00:00
|
|
|
valid &= append_shader("options_120", { "options_120.vs", "options_120.fs" });
|
2020-08-26 13:29:33 +00:00
|
|
|
// used to render extrusion and travel paths as lines in gcode preview
|
2020-08-25 06:12:28 +00:00
|
|
|
valid &= append_shader("toolpaths_lines", { "toolpaths_lines.vs", "toolpaths_lines.fs" });
|
2020-05-20 12:11:22 +00:00
|
|
|
// used to render objects in 3d editor
|
2020-12-10 12:20:18 +00:00
|
|
|
valid &= append_shader("gouraud", { "gouraud.vs", "gouraud.fs" }
|
|
|
|
#if ENABLE_ENVIRONMENT_MAP
|
|
|
|
, { "ENABLE_ENVIRONMENT_MAP"sv }
|
|
|
|
#endif
|
|
|
|
);
|
2020-05-20 12:11:22 +00:00
|
|
|
// used to render variable layers heights in 3d editor
|
|
|
|
valid &= append_shader("variable_layer_height", { "variable_layer_height.vs", "variable_layer_height.fs" });
|
|
|
|
|
|
|
|
return { valid, error };
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLShadersManager::shutdown()
|
|
|
|
{
|
2020-12-10 12:20:18 +00:00
|
|
|
m_shaders.clear();
|
2020-05-20 12:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLShaderProgram* GLShadersManager::get_shader(const std::string& shader_name)
|
|
|
|
{
|
2020-12-10 12:20:18 +00:00
|
|
|
auto it = std::find_if(m_shaders.begin(), m_shaders.end(), [&shader_name](std::unique_ptr<GLShaderProgram>& p) { return p->get_name() == shader_name; });
|
2020-05-20 12:11:22 +00:00
|
|
|
return (it != m_shaders.end()) ? it->get() : nullptr;
|
|
|
|
}
|
|
|
|
|
2020-05-21 08:15:00 +00:00
|
|
|
GLShaderProgram* GLShadersManager::get_current_shader()
|
|
|
|
{
|
|
|
|
GLint id = 0;
|
|
|
|
glsafe(::glGetIntegerv(GL_CURRENT_PROGRAM, &id));
|
|
|
|
if (id == 0)
|
2020-05-21 08:27:41 +00:00
|
|
|
return nullptr;
|
2020-05-21 08:15:00 +00:00
|
|
|
|
2020-07-08 11:33:50 +00:00
|
|
|
auto it = std::find_if(m_shaders.begin(), m_shaders.end(), [id](std::unique_ptr<GLShaderProgram>& p) { return static_cast<GLint>(p->get_id()) == id; });
|
2020-05-21 08:15:00 +00:00
|
|
|
return (it != m_shaders.end()) ? it->get() : nullptr;
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:11:22 +00:00
|
|
|
} // namespace Slic3r
|
|
|
|
|