2017-03-16 13:02:28 +00:00
|
|
|
#include <GL/glew.h>
|
|
|
|
|
2015-01-17 23:36:21 +00:00
|
|
|
#include "3DScene.hpp"
|
|
|
|
|
2018-11-13 16:45:44 +00:00
|
|
|
#include "libslic3r/ExtrusionEntity.hpp"
|
|
|
|
#include "libslic3r/ExtrusionEntityCollection.hpp"
|
|
|
|
#include "libslic3r/Geometry.hpp"
|
|
|
|
#include "libslic3r/GCode/PreviewData.hpp"
|
|
|
|
#include "libslic3r/Print.hpp"
|
|
|
|
#include "libslic3r/SLAPrint.hpp"
|
|
|
|
#include "libslic3r/Slicing.hpp"
|
2018-11-26 13:41:58 +00:00
|
|
|
#include "libslic3r/GCode/Analyzer.hpp"
|
2018-11-13 16:45:44 +00:00
|
|
|
#include "slic3r/GUI/PresetBundle.hpp"
|
2019-01-04 11:56:48 +00:00
|
|
|
#include "libslic3r/Format/STL.hpp"
|
2019-08-05 12:30:32 +00:00
|
|
|
#include "libslic3r/Utils.hpp"
|
2018-01-11 13:09:54 +00:00
|
|
|
|
2017-03-13 15:02:17 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <utility>
|
2017-03-15 15:33:25 +00:00
|
|
|
#include <assert.h>
|
2017-03-13 15:02:17 +00:00
|
|
|
|
|
|
|
#include <boost/log/trivial.hpp>
|
|
|
|
|
2019-01-04 11:56:48 +00:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
2019-08-20 12:58:27 +00:00
|
|
|
#include <boost/nowide/cstdio.hpp>
|
|
|
|
|
2017-03-13 15:02:17 +00:00
|
|
|
#include <tbb/parallel_for.h>
|
2017-03-23 10:10:53 +00:00
|
|
|
#include <tbb/spin_mutex.h>
|
2016-04-15 15:58:29 +00:00
|
|
|
|
2018-06-21 06:37:04 +00:00
|
|
|
#include <Eigen/Dense>
|
2018-01-16 13:59:06 +00:00
|
|
|
|
2018-02-26 15:23:44 +00:00
|
|
|
#include "GUI.hpp"
|
|
|
|
|
2019-01-31 08:37:27 +00:00
|
|
|
#ifdef HAS_GLSAFE
|
2019-03-27 09:26:55 +00:00
|
|
|
void glAssertRecentCallImpl(const char *file_name, unsigned int line, const char *function_name)
|
2019-01-31 08:37:27 +00:00
|
|
|
{
|
|
|
|
GLenum err = glGetError();
|
|
|
|
if (err == GL_NO_ERROR)
|
|
|
|
return;
|
|
|
|
const char *sErr = 0;
|
|
|
|
switch (err) {
|
|
|
|
case GL_INVALID_ENUM: sErr = "Invalid Enum"; break;
|
|
|
|
case GL_INVALID_VALUE: sErr = "Invalid Value"; break;
|
2019-03-21 10:02:10 +00:00
|
|
|
// be aware that GL_INVALID_OPERATION is generated if glGetError is executed between the execution of glBegin and the corresponding execution of glEnd
|
2019-01-31 08:37:27 +00:00
|
|
|
case GL_INVALID_OPERATION: sErr = "Invalid Operation"; break;
|
|
|
|
case GL_STACK_OVERFLOW: sErr = "Stack Overflow"; break;
|
|
|
|
case GL_STACK_UNDERFLOW: sErr = "Stack Underflow"; break;
|
|
|
|
case GL_OUT_OF_MEMORY: sErr = "Out Of Memory"; break;
|
|
|
|
default: sErr = "Unknown"; break;
|
|
|
|
}
|
2019-03-27 09:26:55 +00:00
|
|
|
BOOST_LOG_TRIVIAL(error) << "OpenGL error in " << file_name << ":" << line << ", function " << function_name << "() : " << (int)err << " - " << sErr;
|
2019-01-31 08:37:27 +00:00
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-17 23:36:21 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
|
2018-03-09 09:40:42 +00:00
|
|
|
void GLIndexedVertexArray::load_mesh_full_shading(const TriangleMesh &mesh)
|
|
|
|
{
|
|
|
|
assert(triangle_indices.empty() && vertices_and_normals_interleaved_size == 0);
|
|
|
|
assert(quad_indices.empty() && triangle_indices_size == 0);
|
|
|
|
assert(vertices_and_normals_interleaved.size() % 6 == 0 && quad_indices_size == vertices_and_normals_interleaved.size());
|
|
|
|
|
|
|
|
this->vertices_and_normals_interleaved.reserve(this->vertices_and_normals_interleaved.size() + 3 * 3 * 2 * mesh.facets_count());
|
|
|
|
|
|
|
|
unsigned int vertices_count = 0;
|
2018-10-08 13:17:36 +00:00
|
|
|
for (int i = 0; i < (int)mesh.stl.stats.number_of_facets; ++i) {
|
2018-03-09 09:40:42 +00:00
|
|
|
const stl_facet &facet = mesh.stl.facet_start[i];
|
|
|
|
for (int j = 0; j < 3; ++j)
|
2018-08-22 13:03:35 +00:00
|
|
|
this->push_geometry(facet.vertex[j](0), facet.vertex[j](1), facet.vertex[j](2), facet.normal(0), facet.normal(1), facet.normal(2));
|
2018-03-09 09:40:42 +00:00
|
|
|
|
|
|
|
this->push_triangle(vertices_count, vertices_count + 1, vertices_count + 2);
|
|
|
|
vertices_count += 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 12:30:32 +00:00
|
|
|
void GLIndexedVertexArray::finalize_geometry(bool opengl_initialized)
|
2017-03-16 13:02:28 +00:00
|
|
|
{
|
|
|
|
assert(this->vertices_and_normals_interleaved_VBO_id == 0);
|
|
|
|
assert(this->triangle_indices_VBO_id == 0);
|
|
|
|
assert(this->quad_indices_VBO_id == 0);
|
|
|
|
|
2019-08-05 12:30:32 +00:00
|
|
|
if (! opengl_initialized) {
|
|
|
|
// Shrink the data vectors to conserve memory in case the data cannot be transfered to the OpenGL driver yet.
|
|
|
|
this->shrink_to_fit();
|
|
|
|
return;
|
|
|
|
}
|
2017-03-16 13:02:28 +00:00
|
|
|
|
2019-08-05 12:30:32 +00:00
|
|
|
if (! this->vertices_and_normals_interleaved.empty()) {
|
2019-07-01 10:28:16 +00:00
|
|
|
glsafe(::glGenBuffers(1, &this->vertices_and_normals_interleaved_VBO_id));
|
|
|
|
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, this->vertices_and_normals_interleaved_VBO_id));
|
|
|
|
glsafe(::glBufferData(GL_ARRAY_BUFFER, this->vertices_and_normals_interleaved.size() * 4, this->vertices_and_normals_interleaved.data(), GL_STATIC_DRAW));
|
|
|
|
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0));
|
|
|
|
this->vertices_and_normals_interleaved.clear();
|
|
|
|
}
|
|
|
|
if (! this->triangle_indices.empty()) {
|
|
|
|
glsafe(::glGenBuffers(1, &this->triangle_indices_VBO_id));
|
|
|
|
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->triangle_indices_VBO_id));
|
|
|
|
glsafe(::glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->triangle_indices.size() * 4, this->triangle_indices.data(), GL_STATIC_DRAW));
|
2019-03-27 13:42:09 +00:00
|
|
|
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
|
2019-07-01 10:28:16 +00:00
|
|
|
this->triangle_indices.clear();
|
2017-03-16 13:02:28 +00:00
|
|
|
}
|
2019-07-01 10:28:16 +00:00
|
|
|
if (! this->quad_indices.empty()) {
|
|
|
|
glsafe(::glGenBuffers(1, &this->quad_indices_VBO_id));
|
|
|
|
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->quad_indices_VBO_id));
|
|
|
|
glsafe(::glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->quad_indices.size() * 4, this->quad_indices.data(), GL_STATIC_DRAW));
|
|
|
|
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
|
|
|
|
this->quad_indices.clear();
|
|
|
|
}
|
2017-03-16 13:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLIndexedVertexArray::release_geometry()
|
|
|
|
{
|
2018-11-16 17:28:50 +00:00
|
|
|
if (this->vertices_and_normals_interleaved_VBO_id) {
|
2019-03-27 13:42:09 +00:00
|
|
|
glsafe(::glDeleteBuffers(1, &this->vertices_and_normals_interleaved_VBO_id));
|
2018-11-16 17:28:50 +00:00
|
|
|
this->vertices_and_normals_interleaved_VBO_id = 0;
|
|
|
|
}
|
|
|
|
if (this->triangle_indices_VBO_id) {
|
2019-03-27 13:42:09 +00:00
|
|
|
glsafe(::glDeleteBuffers(1, &this->triangle_indices_VBO_id));
|
2018-11-16 17:28:50 +00:00
|
|
|
this->triangle_indices_VBO_id = 0;
|
|
|
|
}
|
|
|
|
if (this->quad_indices_VBO_id) {
|
2019-03-27 13:42:09 +00:00
|
|
|
glsafe(::glDeleteBuffers(1, &this->quad_indices_VBO_id));
|
2018-11-16 17:28:50 +00:00
|
|
|
this->quad_indices_VBO_id = 0;
|
|
|
|
}
|
2017-03-16 13:02:28 +00:00
|
|
|
this->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLIndexedVertexArray::render() const
|
|
|
|
{
|
2019-08-05 12:30:32 +00:00
|
|
|
assert(this->vertices_and_normals_interleaved_VBO_id != 0);
|
|
|
|
assert(this->triangle_indices_VBO_id != 0 || this->quad_indices_VBO_id != 0);
|
2019-07-02 10:55:55 +00:00
|
|
|
|
|
|
|
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, this->vertices_and_normals_interleaved_VBO_id));
|
|
|
|
glsafe(::glVertexPointer(3, GL_FLOAT, 6 * sizeof(float), (const void*)(3 * sizeof(float))));
|
|
|
|
glsafe(::glNormalPointer(GL_FLOAT, 6 * sizeof(float), nullptr));
|
|
|
|
|
2019-03-27 13:42:09 +00:00
|
|
|
glsafe(::glEnableClientState(GL_VERTEX_ARRAY));
|
|
|
|
glsafe(::glEnableClientState(GL_NORMAL_ARRAY));
|
2017-03-20 11:05:20 +00:00
|
|
|
|
2019-07-02 10:55:55 +00:00
|
|
|
// Render using the Vertex Buffer Objects.
|
|
|
|
if (this->triangle_indices_size > 0) {
|
|
|
|
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->triangle_indices_VBO_id));
|
|
|
|
glsafe(::glDrawElements(GL_TRIANGLES, GLsizei(this->triangle_indices_size), GL_UNSIGNED_INT, nullptr));
|
|
|
|
glsafe(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
|
|
|
|
}
|
|
|
|
if (this->quad_indices_size > 0) {
|
|
|
|
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->quad_indices_VBO_id));
|
|
|
|
glsafe(::glDrawElements(GL_QUADS, GLsizei(this->quad_indices_size), GL_UNSIGNED_INT, nullptr));
|
|
|
|
glsafe(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
|
|
|
|
}
|
2017-03-16 13:02:28 +00:00
|
|
|
|
2019-03-27 13:42:09 +00:00
|
|
|
glsafe(::glDisableClientState(GL_VERTEX_ARRAY));
|
|
|
|
glsafe(::glDisableClientState(GL_NORMAL_ARRAY));
|
2019-07-02 10:55:55 +00:00
|
|
|
|
|
|
|
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0));
|
2017-03-16 13:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLIndexedVertexArray::render(
|
2019-07-02 10:55:55 +00:00
|
|
|
const std::pair<size_t, size_t>& tverts_range,
|
|
|
|
const std::pair<size_t, size_t>& qverts_range) const
|
2017-03-16 13:02:28 +00:00
|
|
|
{
|
2019-08-05 12:30:32 +00:00
|
|
|
assert(this->vertices_and_normals_interleaved_VBO_id != 0);
|
|
|
|
assert(this->triangle_indices_VBO_id != 0 || this->quad_indices_VBO_id != 0);
|
2017-03-16 13:02:28 +00:00
|
|
|
|
2019-07-02 10:55:55 +00:00
|
|
|
// Render using the Vertex Buffer Objects.
|
|
|
|
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, this->vertices_and_normals_interleaved_VBO_id));
|
|
|
|
glsafe(::glVertexPointer(3, GL_FLOAT, 6 * sizeof(float), (const void*)(3 * sizeof(float))));
|
|
|
|
glsafe(::glNormalPointer(GL_FLOAT, 6 * sizeof(float), nullptr));
|
|
|
|
|
|
|
|
glsafe(::glEnableClientState(GL_VERTEX_ARRAY));
|
|
|
|
glsafe(::glEnableClientState(GL_NORMAL_ARRAY));
|
|
|
|
|
|
|
|
if (this->triangle_indices_size > 0) {
|
|
|
|
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->triangle_indices_VBO_id));
|
|
|
|
glsafe(::glDrawElements(GL_TRIANGLES, GLsizei(std::min(this->triangle_indices_size, tverts_range.second - tverts_range.first)), GL_UNSIGNED_INT, (const void*)(tverts_range.first * 4)));
|
|
|
|
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
|
|
|
|
}
|
|
|
|
if (this->quad_indices_size > 0) {
|
|
|
|
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->quad_indices_VBO_id));
|
|
|
|
glsafe(::glDrawElements(GL_QUADS, GLsizei(std::min(this->quad_indices_size, qverts_range.second - qverts_range.first)), GL_UNSIGNED_INT, (const void*)(qverts_range.first * 4)));
|
2019-03-27 13:42:09 +00:00
|
|
|
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
|
2017-03-16 13:02:28 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 13:42:09 +00:00
|
|
|
glsafe(::glDisableClientState(GL_VERTEX_ARRAY));
|
|
|
|
glsafe(::glDisableClientState(GL_NORMAL_ARRAY));
|
2019-07-02 10:55:55 +00:00
|
|
|
|
|
|
|
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0));
|
2017-03-13 15:02:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 09:40:42 +00:00
|
|
|
const float GLVolume::SELECTED_COLOR[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
|
2019-04-25 07:46:26 +00:00
|
|
|
const float GLVolume::HOVER_SELECT_COLOR[4] = { 0.4f, 0.9f, 0.1f, 1.0f };
|
2019-04-29 08:01:28 +00:00
|
|
|
const float GLVolume::HOVER_DESELECT_COLOR[4] = { 1.0f, 0.75f, 0.75f, 1.0f };
|
2018-03-20 12:01:50 +00:00
|
|
|
const float GLVolume::OUTSIDE_COLOR[4] = { 0.0f, 0.38f, 0.8f, 1.0f };
|
|
|
|
const float GLVolume::SELECTED_OUTSIDE_COLOR[4] = { 0.19f, 0.58f, 1.0f, 1.0f };
|
2018-11-07 11:11:34 +00:00
|
|
|
const float GLVolume::DISABLED_COLOR[4] = { 0.25f, 0.25f, 0.25f, 1.0f };
|
2019-04-08 16:09:31 +00:00
|
|
|
const float GLVolume::MODEL_COLOR[4][4] = {
|
|
|
|
{ 1.0f, 1.0f, 0.0f, 1.f },
|
|
|
|
{ 1.0f, 0.5f, 0.5f, 1.f },
|
|
|
|
{ 0.5f, 1.0f, 0.5f, 1.f },
|
|
|
|
{ 0.5f, 0.5f, 1.0f, 1.f }
|
|
|
|
};
|
2018-11-22 10:01:57 +00:00
|
|
|
const float GLVolume::SLA_SUPPORT_COLOR[4] = { 0.75f, 0.75f, 0.75f, 1.0f };
|
|
|
|
const float GLVolume::SLA_PAD_COLOR[4] = { 0.0f, 0.2f, 0.0f, 1.0f };
|
2018-03-09 09:40:42 +00:00
|
|
|
|
2018-06-21 06:37:04 +00:00
|
|
|
GLVolume::GLVolume(float r, float g, float b, float a)
|
2018-10-31 13:56:51 +00:00
|
|
|
: m_transformed_bounding_box_dirty(true)
|
2018-11-22 12:33:20 +00:00
|
|
|
, m_sla_shift_z(0.0)
|
2018-08-15 10:50:06 +00:00
|
|
|
, m_transformed_convex_hull_bounding_box_dirty(true)
|
2018-11-16 17:28:50 +00:00
|
|
|
// geometry_id == 0 -> invalid
|
|
|
|
, geometry_id(std::pair<size_t, size_t>(0, 0))
|
2018-06-21 06:37:04 +00:00
|
|
|
, extruder_id(0)
|
|
|
|
, selected(false)
|
2018-11-07 11:11:34 +00:00
|
|
|
, disabled(false)
|
2019-07-31 09:01:50 +00:00
|
|
|
, printable(true)
|
2018-06-21 06:37:04 +00:00
|
|
|
, is_active(true)
|
|
|
|
, zoom_to_volumes(true)
|
2018-07-26 11:12:09 +00:00
|
|
|
, shader_outside_printer_detection_enabled(false)
|
2018-06-21 06:37:04 +00:00
|
|
|
, is_outside(false)
|
2019-04-25 11:35:24 +00:00
|
|
|
, hover(HS_None)
|
2018-06-21 06:37:04 +00:00
|
|
|
, is_modifier(false)
|
|
|
|
, is_wipe_tower(false)
|
2018-07-27 06:49:58 +00:00
|
|
|
, is_extrusion_path(false)
|
2019-01-31 13:25:11 +00:00
|
|
|
, force_transparent(false)
|
2019-03-20 13:04:20 +00:00
|
|
|
, force_native_color(false)
|
2018-06-21 06:37:04 +00:00
|
|
|
, tverts_range(0, size_t(-1))
|
|
|
|
, qverts_range(0, size_t(-1))
|
|
|
|
{
|
|
|
|
color[0] = r;
|
|
|
|
color[1] = g;
|
|
|
|
color[2] = b;
|
|
|
|
color[3] = a;
|
|
|
|
set_render_color(r, g, b, a);
|
|
|
|
}
|
2018-03-09 09:40:42 +00:00
|
|
|
|
|
|
|
void GLVolume::set_render_color(float r, float g, float b, float a)
|
|
|
|
{
|
|
|
|
render_color[0] = r;
|
|
|
|
render_color[1] = g;
|
|
|
|
render_color[2] = b;
|
|
|
|
render_color[3] = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLVolume::set_render_color(const float* rgba, unsigned int size)
|
|
|
|
{
|
2019-01-04 11:56:48 +00:00
|
|
|
::memcpy((void*)render_color, (const void*)rgba, (size_t)(std::min((unsigned int)4, size) * sizeof(float)));
|
2018-03-09 09:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLVolume::set_render_color()
|
|
|
|
{
|
2019-03-20 13:04:20 +00:00
|
|
|
if (force_native_color)
|
2019-05-07 10:29:48 +00:00
|
|
|
{
|
|
|
|
if (is_outside && shader_outside_printer_detection_enabled)
|
|
|
|
set_render_color(OUTSIDE_COLOR, 4);
|
|
|
|
else
|
|
|
|
set_render_color(color, 4);
|
|
|
|
}
|
2019-03-20 13:04:20 +00:00
|
|
|
else {
|
2019-04-25 11:35:24 +00:00
|
|
|
if (hover == HS_Select)
|
2019-04-25 07:46:26 +00:00
|
|
|
set_render_color(HOVER_SELECT_COLOR, 4);
|
2019-04-25 11:35:24 +00:00
|
|
|
else if (hover == HS_Deselect)
|
2019-04-25 07:46:26 +00:00
|
|
|
set_render_color(HOVER_DESELECT_COLOR, 4);
|
|
|
|
else if (selected)
|
2019-03-20 13:04:20 +00:00
|
|
|
set_render_color(is_outside ? SELECTED_OUTSIDE_COLOR : SELECTED_COLOR, 4);
|
|
|
|
else if (disabled)
|
|
|
|
set_render_color(DISABLED_COLOR, 4);
|
|
|
|
else if (is_outside && shader_outside_printer_detection_enabled)
|
|
|
|
set_render_color(OUTSIDE_COLOR, 4);
|
|
|
|
else
|
|
|
|
set_render_color(color, 4);
|
|
|
|
}
|
2019-01-31 13:25:11 +00:00
|
|
|
|
2019-07-31 09:01:50 +00:00
|
|
|
if (!printable)
|
|
|
|
{
|
|
|
|
render_color[0] /= 4;
|
|
|
|
render_color[1] /= 4;
|
|
|
|
render_color[2] /= 4;
|
|
|
|
}
|
|
|
|
|
2019-01-31 13:25:11 +00:00
|
|
|
if (force_transparent)
|
|
|
|
render_color[3] = color[3];
|
2018-03-09 09:40:42 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 11:10:09 +00:00
|
|
|
void GLVolume::set_color_from_model_volume(const ModelVolume *model_volume)
|
|
|
|
{
|
|
|
|
if (model_volume->is_modifier()) {
|
|
|
|
color[0] = 0.2f;
|
|
|
|
color[1] = 1.0f;
|
|
|
|
color[2] = 0.2f;
|
|
|
|
}
|
|
|
|
else if (model_volume->is_support_blocker()) {
|
|
|
|
color[0] = 1.0f;
|
|
|
|
color[1] = 0.2f;
|
|
|
|
color[2] = 0.2f;
|
|
|
|
}
|
|
|
|
else if (model_volume->is_support_enforcer()) {
|
|
|
|
color[0] = 0.2f;
|
|
|
|
color[1] = 0.2f;
|
|
|
|
color[2] = 1.0f;
|
|
|
|
}
|
|
|
|
color[3] = model_volume->is_model_part() ? 1.f : 0.5f;
|
|
|
|
}
|
|
|
|
|
2018-11-22 12:33:20 +00:00
|
|
|
Transform3d GLVolume::world_matrix() const
|
|
|
|
{
|
|
|
|
Transform3d m = m_instance_transformation.get_matrix() * m_volume_transformation.get_matrix();
|
|
|
|
m.translation()(2) += m_sla_shift_z;
|
|
|
|
return m;
|
|
|
|
}
|
2018-06-21 06:37:04 +00:00
|
|
|
|
2019-04-02 11:47:49 +00:00
|
|
|
bool GLVolume::is_left_handed() const
|
|
|
|
{
|
|
|
|
const Vec3d &m1 = m_instance_transformation.get_mirror();
|
|
|
|
const Vec3d &m2 = m_volume_transformation.get_mirror();
|
|
|
|
return m1.x() * m1.y() * m1.z() * m2.x() * m2.y() * m2.z() < 0.;
|
|
|
|
}
|
|
|
|
|
2018-08-28 14:08:43 +00:00
|
|
|
const BoundingBoxf3& GLVolume::transformed_bounding_box() const
|
2018-06-21 06:37:04 +00:00
|
|
|
{
|
2019-07-01 11:26:06 +00:00
|
|
|
const BoundingBoxf3& box = bounding_box();
|
|
|
|
assert(box.defined || box.min(0) >= box.max(0) || box.min(1) >= box.max(1) || box.min(2) >= box.max(2));
|
2018-12-03 12:14:28 +00:00
|
|
|
|
2018-08-15 10:50:06 +00:00
|
|
|
if (m_transformed_bounding_box_dirty)
|
|
|
|
{
|
2019-07-01 11:26:06 +00:00
|
|
|
m_transformed_bounding_box = box.transformed(world_matrix());
|
2018-08-15 10:50:06 +00:00
|
|
|
m_transformed_bounding_box_dirty = false;
|
|
|
|
}
|
2018-06-21 06:37:04 +00:00
|
|
|
|
|
|
|
return m_transformed_bounding_box;
|
|
|
|
}
|
|
|
|
|
2018-08-28 14:08:43 +00:00
|
|
|
const BoundingBoxf3& GLVolume::transformed_convex_hull_bounding_box() const
|
2018-08-15 10:50:06 +00:00
|
|
|
{
|
2019-05-03 10:36:26 +00:00
|
|
|
if (m_transformed_convex_hull_bounding_box_dirty)
|
|
|
|
m_transformed_convex_hull_bounding_box = this->transformed_convex_hull_bounding_box(world_matrix());
|
2018-08-15 10:50:06 +00:00
|
|
|
return m_transformed_convex_hull_bounding_box;
|
|
|
|
}
|
|
|
|
|
2019-05-03 10:36:26 +00:00
|
|
|
BoundingBoxf3 GLVolume::transformed_convex_hull_bounding_box(const Transform3d &trafo) const
|
|
|
|
{
|
2019-06-11 15:08:47 +00:00
|
|
|
return (m_convex_hull && m_convex_hull->stl.stats.number_of_facets > 0) ?
|
2019-05-03 10:36:26 +00:00
|
|
|
m_convex_hull->transformed_bounding_box(trafo) :
|
2019-07-01 11:26:06 +00:00
|
|
|
bounding_box().transformed(trafo);
|
2019-05-03 10:36:26 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 11:26:06 +00:00
|
|
|
|
2017-03-13 15:02:17 +00:00
|
|
|
void GLVolume::set_range(double min_z, double max_z)
|
|
|
|
{
|
2019-07-02 10:55:55 +00:00
|
|
|
this->qverts_range.first = 0;
|
2017-03-20 11:05:20 +00:00
|
|
|
this->qverts_range.second = this->indexed_vertex_array.quad_indices_size;
|
2019-07-02 10:55:55 +00:00
|
|
|
this->tverts_range.first = 0;
|
2017-03-20 11:05:20 +00:00
|
|
|
this->tverts_range.second = this->indexed_vertex_array.triangle_indices_size;
|
2017-03-13 15:02:17 +00:00
|
|
|
if (! this->print_zs.empty()) {
|
|
|
|
// The Z layer range is specified.
|
|
|
|
// First test whether the Z span of this object is not out of (min_z, max_z) completely.
|
|
|
|
if (this->print_zs.front() > max_z || this->print_zs.back() < min_z) {
|
|
|
|
this->qverts_range.second = 0;
|
|
|
|
this->tverts_range.second = 0;
|
|
|
|
} else {
|
|
|
|
// Then find the lowest layer to be displayed.
|
|
|
|
size_t i = 0;
|
|
|
|
for (; i < this->print_zs.size() && this->print_zs[i] < min_z; ++ i);
|
|
|
|
if (i == this->print_zs.size()) {
|
|
|
|
// This shall not happen.
|
|
|
|
this->qverts_range.second = 0;
|
|
|
|
this->tverts_range.second = 0;
|
|
|
|
} else {
|
|
|
|
// Remember start of the layer.
|
|
|
|
this->qverts_range.first = this->offsets[i * 2];
|
|
|
|
this->tverts_range.first = this->offsets[i * 2 + 1];
|
|
|
|
// Some layers are above $min_z. Which?
|
|
|
|
for (; i < this->print_zs.size() && this->print_zs[i] <= max_z; ++ i);
|
|
|
|
if (i < this->print_zs.size()) {
|
|
|
|
this->qverts_range.second = this->offsets[i * 2];
|
|
|
|
this->tverts_range.second = this->offsets[i * 2 + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 13:02:28 +00:00
|
|
|
void GLVolume::render() const
|
|
|
|
{
|
2018-01-11 13:09:54 +00:00
|
|
|
if (!is_active)
|
|
|
|
return;
|
|
|
|
|
2019-04-02 11:47:49 +00:00
|
|
|
if (this->is_left_handed())
|
|
|
|
glFrontFace(GL_CW);
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glCullFace(GL_BACK));
|
|
|
|
glsafe(::glPushMatrix());
|
|
|
|
glsafe(::glMultMatrixd(world_matrix().data()));
|
2019-07-02 10:55:55 +00:00
|
|
|
|
|
|
|
this->indexed_vertex_array.render(this->tverts_range, this->qverts_range);
|
|
|
|
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glPopMatrix());
|
2019-04-02 11:47:49 +00:00
|
|
|
if (this->is_left_handed())
|
|
|
|
glFrontFace(GL_CCW);
|
2017-03-16 13:02:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 10:28:16 +00:00
|
|
|
void GLVolume::render(int color_id, int detection_id, int worldmatrix_id) const
|
2018-06-21 06:37:04 +00:00
|
|
|
{
|
|
|
|
if (color_id >= 0)
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glUniform4fv(color_id, 1, (const GLfloat*)render_color));
|
2018-06-21 06:37:04 +00:00
|
|
|
else
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glColor4fv(render_color));
|
2018-06-21 06:37:04 +00:00
|
|
|
|
|
|
|
if (detection_id != -1)
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glUniform1i(detection_id, shader_outside_printer_detection_enabled ? 1 : 0));
|
2018-06-21 06:37:04 +00:00
|
|
|
|
|
|
|
if (worldmatrix_id != -1)
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glUniformMatrix4fv(worldmatrix_id, 1, GL_FALSE, (const GLfloat*)world_matrix().cast<float>().data()));
|
2018-06-21 06:37:04 +00:00
|
|
|
|
2019-07-02 10:55:55 +00:00
|
|
|
render();
|
2018-06-21 06:37:04 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 10:29:48 +00:00
|
|
|
bool GLVolume::is_sla_support() const { return this->composite_id.volume_id == -int(slaposSupportTree); }
|
2019-09-24 13:15:49 +00:00
|
|
|
bool GLVolume::is_sla_pad() const { return this->composite_id.volume_id == -int(slaposPad); }
|
2019-05-07 10:29:48 +00:00
|
|
|
|
2018-10-08 14:05:55 +00:00
|
|
|
std::vector<int> GLVolumeCollection::load_object(
|
2019-08-05 12:30:32 +00:00
|
|
|
const ModelObject *model_object,
|
2018-10-08 14:05:55 +00:00
|
|
|
int obj_idx,
|
2019-08-05 12:30:32 +00:00
|
|
|
const std::vector<int> &instance_idxs,
|
|
|
|
const std::string &color_by,
|
|
|
|
bool opengl_initialized)
|
2018-11-16 17:28:50 +00:00
|
|
|
{
|
|
|
|
std::vector<int> volumes_idx;
|
2019-07-01 10:28:16 +00:00
|
|
|
for (int volume_idx = 0; volume_idx < int(model_object->volumes.size()); ++volume_idx)
|
2018-11-16 17:28:50 +00:00
|
|
|
for (int instance_idx : instance_idxs)
|
2019-08-05 12:30:32 +00:00
|
|
|
volumes_idx.emplace_back(this->GLVolumeCollection::load_object_volume(model_object, obj_idx, volume_idx, instance_idx, color_by, opengl_initialized));
|
2019-07-01 10:28:16 +00:00
|
|
|
return volumes_idx;
|
2018-11-16 17:28:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int GLVolumeCollection::load_object_volume(
|
2019-08-05 12:30:32 +00:00
|
|
|
const ModelObject *model_object,
|
|
|
|
int obj_idx,
|
|
|
|
int volume_idx,
|
|
|
|
int instance_idx,
|
|
|
|
const std::string &color_by,
|
|
|
|
bool opengl_initialized)
|
|
|
|
{
|
|
|
|
const ModelVolume *model_volume = model_object->volumes[volume_idx];
|
|
|
|
const int extruder_id = model_volume->extruder_id();
|
|
|
|
const ModelInstance *instance = model_object->instances[instance_idx];
|
|
|
|
const TriangleMesh &mesh = model_volume->mesh();
|
|
|
|
float color[4];
|
2019-04-08 16:09:31 +00:00
|
|
|
memcpy(color, GLVolume::MODEL_COLOR[((color_by == "volume") ? volume_idx : obj_idx) % 4], sizeof(float) * 3);
|
2019-07-01 10:28:16 +00:00
|
|
|
/* if (model_volume->is_support_blocker()) {
|
|
|
|
color[0] = 1.0f;
|
|
|
|
color[1] = 0.2f;
|
|
|
|
color[2] = 0.2f;
|
|
|
|
} else if (model_volume->is_support_enforcer()) {
|
|
|
|
color[0] = 0.2f;
|
|
|
|
color[1] = 0.2f;
|
|
|
|
color[2] = 1.0f;
|
|
|
|
}
|
|
|
|
color[3] = model_volume->is_model_part() ? 1.f : 0.5f; */
|
2018-12-21 08:56:11 +00:00
|
|
|
color[3] = model_volume->is_model_part() ? 1.f : 0.5f;
|
2018-11-16 17:28:50 +00:00
|
|
|
this->volumes.emplace_back(new GLVolume(color));
|
2019-07-01 10:28:16 +00:00
|
|
|
GLVolume& v = *this->volumes.back();
|
2018-12-05 11:10:09 +00:00
|
|
|
v.set_color_from_model_volume(model_volume);
|
2019-07-01 10:28:16 +00:00
|
|
|
v.indexed_vertex_array.load_mesh(mesh);
|
2019-08-05 12:30:32 +00:00
|
|
|
v.indexed_vertex_array.finalize_geometry(opengl_initialized);
|
2019-07-01 10:28:16 +00:00
|
|
|
v.composite_id = GLVolume::CompositeID(obj_idx, volume_idx, instance_idx);
|
2018-11-16 17:28:50 +00:00
|
|
|
if (model_volume->is_model_part())
|
|
|
|
{
|
2019-07-01 10:28:16 +00:00
|
|
|
// GLVolume will reference a convex hull from model_volume!
|
2019-06-11 15:08:47 +00:00
|
|
|
v.set_convex_hull(model_volume->get_convex_hull_shared_ptr());
|
2018-11-16 17:28:50 +00:00
|
|
|
if (extruder_id != -1)
|
|
|
|
v.extruder_id = extruder_id;
|
|
|
|
}
|
2019-07-01 10:28:16 +00:00
|
|
|
v.is_modifier = !model_volume->is_model_part();
|
2018-11-16 17:28:50 +00:00
|
|
|
v.shader_outside_printer_detection_enabled = model_volume->is_model_part();
|
|
|
|
v.set_instance_transformation(instance->get_transformation());
|
|
|
|
v.set_volume_transformation(model_volume->get_transformation());
|
|
|
|
|
2019-07-01 10:28:16 +00:00
|
|
|
return int(this->volumes.size() - 1);
|
2017-03-13 15:02:17 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 16:45:44 +00:00
|
|
|
// Load SLA auxiliary GLVolumes (for support trees or pad).
|
2018-11-16 17:28:50 +00:00
|
|
|
// This function produces volumes for multiple instances in a single shot,
|
|
|
|
// as some object specific mesh conversions may be expensive.
|
|
|
|
void GLVolumeCollection::load_object_auxiliary(
|
2019-08-05 12:30:32 +00:00
|
|
|
const SLAPrintObject *print_object,
|
2018-11-16 17:28:50 +00:00
|
|
|
int obj_idx,
|
|
|
|
// pairs of <instance_idx, print_instance_idx>
|
2019-07-01 10:28:16 +00:00
|
|
|
const std::vector<std::pair<size_t, size_t>>& instances,
|
2018-11-16 17:28:50 +00:00
|
|
|
SLAPrintObjectStep milestone,
|
2018-11-17 16:23:56 +00:00
|
|
|
// Timestamp of the last change of the milestone
|
2019-08-05 12:30:32 +00:00
|
|
|
size_t timestamp,
|
|
|
|
bool opengl_initialized)
|
2018-11-16 17:28:50 +00:00
|
|
|
{
|
|
|
|
assert(print_object->is_step_done(milestone));
|
2018-11-21 16:35:35 +00:00
|
|
|
Transform3d mesh_trafo_inv = print_object->trafo().inverse();
|
2018-11-16 17:28:50 +00:00
|
|
|
// Get the support mesh.
|
2018-11-21 16:35:35 +00:00
|
|
|
TriangleMesh mesh = print_object->get_mesh(milestone);
|
|
|
|
mesh.transform(mesh_trafo_inv);
|
2019-07-01 10:28:16 +00:00
|
|
|
// Convex hull is required for out of print bed detection.
|
|
|
|
TriangleMesh convex_hull = mesh.convex_hull_3d();
|
|
|
|
for (const std::pair<size_t, size_t>& instance_idx : instances) {
|
|
|
|
const ModelInstance& model_instance = *print_object->model_object()->instances[instance_idx.first];
|
2019-09-24 13:15:49 +00:00
|
|
|
this->volumes.emplace_back(new GLVolume((milestone == slaposPad) ? GLVolume::SLA_PAD_COLOR : GLVolume::SLA_SUPPORT_COLOR));
|
2019-07-01 10:28:16 +00:00
|
|
|
GLVolume& v = *this->volumes.back();
|
|
|
|
v.indexed_vertex_array.load_mesh(mesh);
|
2019-08-05 12:30:32 +00:00
|
|
|
v.indexed_vertex_array.finalize_geometry(opengl_initialized);
|
2019-07-01 10:28:16 +00:00
|
|
|
v.composite_id = GLVolume::CompositeID(obj_idx, -int(milestone), (int)instance_idx.first);
|
2018-11-17 16:23:56 +00:00
|
|
|
v.geometry_id = std::pair<size_t, size_t>(timestamp, model_instance.id().id);
|
2019-07-01 10:28:16 +00:00
|
|
|
// Create a copy of the convex hull mesh for each instance. Use a move operator on the last instance.
|
2019-06-11 15:08:47 +00:00
|
|
|
if (&instance_idx == &instances.back())
|
|
|
|
v.set_convex_hull(std::move(convex_hull));
|
|
|
|
else
|
|
|
|
v.set_convex_hull(convex_hull);
|
2019-07-01 10:28:16 +00:00
|
|
|
v.is_modifier = false;
|
2018-12-10 12:57:43 +00:00
|
|
|
v.shader_outside_printer_detection_enabled = (milestone == slaposSupportTree);
|
|
|
|
v.set_instance_transformation(model_instance.get_transformation());
|
2019-07-01 10:28:16 +00:00
|
|
|
// Leave the volume transformation at identity.
|
2018-11-16 17:28:50 +00:00
|
|
|
// v.set_volume_transformation(model_volume->get_transformation());
|
2018-11-13 16:45:44 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-17 14:53:40 +00:00
|
|
|
|
|
|
|
int GLVolumeCollection::load_wipe_tower_preview(
|
2019-08-05 12:30:32 +00:00
|
|
|
int obj_idx, float pos_x, float pos_y, float width, float depth, float height, float rotation_angle, bool size_unknown, float brim_width, bool opengl_initialized)
|
2017-05-17 14:53:40 +00:00
|
|
|
{
|
2018-07-27 13:56:27 +00:00
|
|
|
if (depth < 0.01f)
|
|
|
|
return int(this->volumes.size() - 1);
|
2018-05-03 09:09:13 +00:00
|
|
|
if (height == 0.0f)
|
|
|
|
height = 0.1f;
|
|
|
|
Point origin_of_rotation(0.f, 0.f);
|
2018-07-27 13:56:27 +00:00
|
|
|
TriangleMesh mesh;
|
|
|
|
float color[4] = { 0.5f, 0.5f, 0.0f, 1.f };
|
|
|
|
|
|
|
|
// In case we don't know precise dimensions of the wipe tower yet, we'll draw the box with different color with one side jagged:
|
|
|
|
if (size_unknown) {
|
2018-08-03 13:36:47 +00:00
|
|
|
color[0] = 0.9f;
|
|
|
|
color[1] = 0.6f;
|
2018-07-27 13:56:27 +00:00
|
|
|
|
|
|
|
depth = std::max(depth, 10.f); // Too narrow tower would interfere with the teeth. The estimate is not precise anyway.
|
|
|
|
float min_width = 30.f;
|
|
|
|
// We'll now create the box with jagged edge. y-coordinates of the pre-generated model are shifted so that the front
|
|
|
|
// edge has y=0 and centerline of the back edge has y=depth:
|
|
|
|
Pointf3s points;
|
2018-08-21 20:14:47 +00:00
|
|
|
std::vector<Vec3crd> facets;
|
2018-10-08 13:17:36 +00:00
|
|
|
float out_points_idx[][3] = { { 0, -depth, 0 }, { 0, 0, 0 }, { 38.453f, 0, 0 }, { 61.547f, 0, 0 }, { 100.0f, 0, 0 }, { 100.0f, -depth, 0 }, { 55.7735f, -10.0f, 0 }, { 44.2265f, 10.0f, 0 },
|
|
|
|
{ 38.453f, 0, 1 }, { 0, 0, 1 }, { 0, -depth, 1 }, { 100.0f, -depth, 1 }, { 100.0f, 0, 1 }, { 61.547f, 0, 1 }, { 55.7735f, -10.0f, 1 }, { 44.2265f, 10.0f, 1 } };
|
|
|
|
int out_facets_idx[][3] = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 5, 0 }, { 3, 5, 6 }, { 6, 2, 7 }, { 6, 0, 2 }, { 8, 9, 10 }, { 11, 12, 13 }, { 10, 11, 14 }, { 14, 11, 13 }, { 15, 8, 14 },
|
2018-07-27 13:56:27 +00:00
|
|
|
{8, 10, 14}, {3, 12, 4}, {3, 13, 12}, {6, 13, 3}, {6, 14, 13}, {7, 14, 6}, {7, 15, 14}, {2, 15, 7}, {2, 8, 15}, {1, 8, 2}, {1, 9, 8},
|
2019-07-01 10:28:16 +00:00
|
|
|
{0, 9, 1}, {0, 10, 9}, {5, 10, 0}, {5, 11, 10}, {4, 11, 5}, {4, 12, 11} };
|
|
|
|
for (int i = 0; i < 16; ++i)
|
|
|
|
points.push_back(Vec3d(out_points_idx[i][0] / (100.f / min_width), out_points_idx[i][1] + depth, out_points_idx[i][2]));
|
|
|
|
for (int i = 0; i < 28; ++i)
|
2018-08-21 20:14:47 +00:00
|
|
|
facets.push_back(Vec3crd(out_facets_idx[i][0], out_facets_idx[i][1], out_facets_idx[i][2]));
|
2018-07-27 13:56:27 +00:00
|
|
|
TriangleMesh tooth_mesh(points, facets);
|
|
|
|
|
|
|
|
// We have the mesh ready. It has one tooth and width of min_width. We will now append several of these together until we are close to
|
|
|
|
// the required width of the block. Than we can scale it precisely.
|
2019-07-01 10:28:16 +00:00
|
|
|
size_t n = std::max(1, int(width / min_width)); // How many shall be merged?
|
|
|
|
for (size_t i = 0; i < n; ++i) {
|
2018-07-27 13:56:27 +00:00
|
|
|
mesh.merge(tooth_mesh);
|
|
|
|
tooth_mesh.translate(min_width, 0.f, 0.f);
|
|
|
|
}
|
|
|
|
|
2019-07-01 10:28:16 +00:00
|
|
|
mesh.scale(Vec3d(width / (n * min_width), 1.f, height)); // Scaling to proper width
|
2018-07-27 13:56:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
mesh = make_cube(width, depth, height);
|
|
|
|
|
|
|
|
// We'll make another mesh to show the brim (fixed layer height):
|
2019-07-01 10:28:16 +00:00
|
|
|
TriangleMesh brim_mesh = make_cube(width + 2.f * brim_width, depth + 2.f * brim_width, 0.2f);
|
2018-07-27 13:56:27 +00:00
|
|
|
brim_mesh.translate(-brim_width, -brim_width, 0.f);
|
|
|
|
mesh.merge(brim_mesh);
|
|
|
|
|
2017-05-17 14:53:40 +00:00
|
|
|
this->volumes.emplace_back(new GLVolume(color));
|
2019-07-01 10:28:16 +00:00
|
|
|
GLVolume& v = *this->volumes.back();
|
|
|
|
v.indexed_vertex_array.load_mesh(mesh);
|
2019-08-05 12:30:32 +00:00
|
|
|
v.indexed_vertex_array.finalize_geometry(opengl_initialized);
|
2018-11-02 11:11:28 +00:00
|
|
|
v.set_volume_offset(Vec3d(pos_x, pos_y, 0.0));
|
2019-07-01 10:28:16 +00:00
|
|
|
v.set_volume_rotation(Vec3d(0., 0., (M_PI / 180.) * rotation_angle));
|
|
|
|
v.composite_id = GLVolume::CompositeID(obj_idx, 0, 0);
|
2019-04-26 15:28:31 +00:00
|
|
|
v.geometry_id.first = 0;
|
|
|
|
v.geometry_id.second = wipe_tower_instance_id().id;
|
2018-04-05 10:52:29 +00:00
|
|
|
v.is_wipe_tower = true;
|
2019-07-01 10:28:16 +00:00
|
|
|
v.shader_outside_printer_detection_enabled = !size_unknown;
|
2017-05-17 14:53:40 +00:00
|
|
|
return int(this->volumes.size() - 1);
|
|
|
|
}
|
|
|
|
|
2019-08-26 09:12:48 +00:00
|
|
|
GLVolume* GLVolumeCollection::new_toolpath_volume(const float *rgba, size_t reserve_vbo_floats)
|
|
|
|
{
|
|
|
|
GLVolume *out = new_nontoolpath_volume(rgba, reserve_vbo_floats);
|
|
|
|
out->is_extrusion_path = true;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLVolume* GLVolumeCollection::new_nontoolpath_volume(const float *rgba, size_t reserve_vbo_floats)
|
|
|
|
{
|
|
|
|
GLVolume *out = new GLVolume(rgba);
|
|
|
|
out->is_extrusion_path = false;
|
|
|
|
// Reserving number of vertices (3x position + 3x color)
|
|
|
|
out->indexed_vertex_array.reserve(reserve_vbo_floats / 6);
|
|
|
|
this->volumes.emplace_back(out);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2019-04-10 09:20:09 +00:00
|
|
|
GLVolumeWithIdAndZList volumes_to_render(const GLVolumePtrs& volumes, GLVolumeCollection::ERenderType type, const Transform3d& view_matrix, std::function<bool(const GLVolume&)> filter_func)
|
2018-12-21 08:56:11 +00:00
|
|
|
{
|
2019-04-10 09:20:09 +00:00
|
|
|
GLVolumeWithIdAndZList list;
|
2019-01-21 09:06:51 +00:00
|
|
|
list.reserve(volumes.size());
|
2018-12-21 08:56:11 +00:00
|
|
|
|
2019-04-10 09:20:09 +00:00
|
|
|
for (unsigned int i = 0; i < (unsigned int)volumes.size(); ++i)
|
2018-12-21 08:56:11 +00:00
|
|
|
{
|
2019-04-10 09:20:09 +00:00
|
|
|
GLVolume* volume = volumes[i];
|
2018-12-21 08:56:11 +00:00
|
|
|
bool is_transparent = (volume->render_color[3] < 1.0f);
|
2019-01-21 09:06:51 +00:00
|
|
|
if ((((type == GLVolumeCollection::Opaque) && !is_transparent) ||
|
|
|
|
((type == GLVolumeCollection::Transparent) && is_transparent) ||
|
|
|
|
(type == GLVolumeCollection::All)) &&
|
|
|
|
(! filter_func || filter_func(*volume)))
|
2019-04-10 09:20:09 +00:00
|
|
|
list.emplace_back(std::make_pair(volume, std::make_pair(i, 0.0)));
|
2018-12-21 08:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((type == GLVolumeCollection::Transparent) && (list.size() > 1))
|
|
|
|
{
|
2019-04-10 09:20:09 +00:00
|
|
|
for (GLVolumeWithIdAndZ& volume : list)
|
2018-12-21 08:56:11 +00:00
|
|
|
{
|
2019-07-01 11:26:06 +00:00
|
|
|
volume.second.second = volume.first->bounding_box().transformed(view_matrix * volume.first->world_matrix()).max(2);
|
2018-12-21 08:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(list.begin(), list.end(),
|
2019-04-10 09:20:09 +00:00
|
|
|
[](const GLVolumeWithIdAndZ& v1, const GLVolumeWithIdAndZ& v2) -> bool { return v1.second.second < v2.second.second; }
|
2018-12-21 08:56:11 +00:00
|
|
|
);
|
|
|
|
}
|
2019-04-11 06:36:00 +00:00
|
|
|
else if ((type == GLVolumeCollection::Opaque) && (list.size() > 1))
|
|
|
|
{
|
|
|
|
std::sort(list.begin(), list.end(),
|
|
|
|
[](const GLVolumeWithIdAndZ& v1, const GLVolumeWithIdAndZ& v2) -> bool { return v1.first->selected && !v2.first->selected; }
|
|
|
|
);
|
|
|
|
}
|
2018-12-21 08:56:11 +00:00
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2019-07-01 10:28:16 +00:00
|
|
|
void GLVolumeCollection::render(GLVolumeCollection::ERenderType type, bool disable_cullface, const Transform3d& view_matrix, std::function<bool(const GLVolume&)> filter_func) const
|
2017-03-20 11:05:20 +00:00
|
|
|
{
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glEnable(GL_BLEND));
|
|
|
|
glsafe(::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
|
2018-03-09 09:40:42 +00:00
|
|
|
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glCullFace(GL_BACK));
|
2018-12-21 08:56:11 +00:00
|
|
|
if (disable_cullface)
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glDisable(GL_CULL_FACE));
|
2018-12-21 08:56:11 +00:00
|
|
|
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glEnableClientState(GL_VERTEX_ARRAY));
|
|
|
|
glsafe(::glEnableClientState(GL_NORMAL_ARRAY));
|
2017-03-20 11:05:20 +00:00
|
|
|
|
|
|
|
GLint current_program_id;
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glGetIntegerv(GL_CURRENT_PROGRAM, ¤t_program_id));
|
2019-03-27 13:42:09 +00:00
|
|
|
GLint color_id = (current_program_id > 0) ? ::glGetUniformLocation(current_program_id, "uniform_color") : -1;
|
|
|
|
GLint z_range_id = (current_program_id > 0) ? ::glGetUniformLocation(current_program_id, "z_range") : -1;
|
2019-04-08 08:50:10 +00:00
|
|
|
GLint clipping_plane_id = (current_program_id > 0) ? ::glGetUniformLocation(current_program_id, "clipping_plane") : -1;
|
2019-03-27 13:42:09 +00:00
|
|
|
GLint print_box_min_id = (current_program_id > 0) ? ::glGetUniformLocation(current_program_id, "print_box.min") : -1;
|
|
|
|
GLint print_box_max_id = (current_program_id > 0) ? ::glGetUniformLocation(current_program_id, "print_box.max") : -1;
|
|
|
|
GLint print_box_detection_id = (current_program_id > 0) ? ::glGetUniformLocation(current_program_id, "print_box.volume_detection") : -1;
|
|
|
|
GLint print_box_worldmatrix_id = (current_program_id > 0) ? ::glGetUniformLocation(current_program_id, "print_box.volume_world_matrix") : -1;
|
|
|
|
glcheck();
|
2017-03-20 11:05:20 +00:00
|
|
|
|
2018-06-21 06:37:04 +00:00
|
|
|
if (print_box_min_id != -1)
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glUniform3fv(print_box_min_id, 1, (const GLfloat*)print_box_min));
|
2018-01-17 09:39:05 +00:00
|
|
|
|
2018-06-21 06:37:04 +00:00
|
|
|
if (print_box_max_id != -1)
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glUniform3fv(print_box_max_id, 1, (const GLfloat*)print_box_max));
|
2018-03-09 09:40:42 +00:00
|
|
|
|
2018-11-27 13:50:57 +00:00
|
|
|
if (z_range_id != -1)
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glUniform2fv(z_range_id, 1, (const GLfloat*)z_range));
|
2018-11-27 13:50:57 +00:00
|
|
|
|
2019-03-25 11:01:02 +00:00
|
|
|
if (clipping_plane_id != -1)
|
|
|
|
glsafe(::glUniform4fv(clipping_plane_id, 1, (const GLfloat*)clipping_plane));
|
|
|
|
|
2019-04-10 09:20:09 +00:00
|
|
|
GLVolumeWithIdAndZList to_render = volumes_to_render(this->volumes, type, view_matrix, filter_func);
|
|
|
|
for (GLVolumeWithIdAndZ& volume : to_render) {
|
2019-01-21 09:06:51 +00:00
|
|
|
volume.first->set_render_color();
|
2019-07-01 10:28:16 +00:00
|
|
|
volume.first->render(color_id, print_box_detection_id, print_box_worldmatrix_id);
|
2018-12-21 08:56:11 +00:00
|
|
|
}
|
2017-03-20 11:05:20 +00:00
|
|
|
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0));
|
|
|
|
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
|
2018-03-09 09:40:42 +00:00
|
|
|
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glDisableClientState(GL_VERTEX_ARRAY));
|
|
|
|
glsafe(::glDisableClientState(GL_NORMAL_ARRAY));
|
2018-03-09 09:40:42 +00:00
|
|
|
|
2018-12-21 08:56:11 +00:00
|
|
|
if (disable_cullface)
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glEnable(GL_CULL_FACE));
|
2018-12-21 08:56:11 +00:00
|
|
|
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glDisable(GL_BLEND));
|
2017-03-20 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 12:26:42 +00:00
|
|
|
bool GLVolumeCollection::check_outside_state(const DynamicPrintConfig* config, ModelInstance::EPrintVolumeState* out_state)
|
2018-03-09 09:40:42 +00:00
|
|
|
{
|
|
|
|
if (config == nullptr)
|
2018-04-24 07:00:33 +00:00
|
|
|
return false;
|
2018-03-09 09:40:42 +00:00
|
|
|
|
|
|
|
const ConfigOptionPoints* opt = dynamic_cast<const ConfigOptionPoints*>(config->option("bed_shape"));
|
|
|
|
if (opt == nullptr)
|
2018-04-24 07:00:33 +00:00
|
|
|
return false;
|
2018-03-09 09:40:42 +00:00
|
|
|
|
|
|
|
BoundingBox bed_box_2D = get_extents(Polygon::new_scale(opt->values));
|
2018-08-24 08:03:34 +00:00
|
|
|
BoundingBoxf3 print_volume(Vec3d(unscale<double>(bed_box_2D.min(0)), unscale<double>(bed_box_2D.min(1)), 0.0), Vec3d(unscale<double>(bed_box_2D.max(0)), unscale<double>(bed_box_2D.max(1)), config->opt_float("max_print_height")));
|
2018-03-20 08:31:42 +00:00
|
|
|
// Allow the objects to protrude below the print bed
|
2018-08-17 13:53:43 +00:00
|
|
|
print_volume.min(2) = -1e10;
|
2018-03-09 09:40:42 +00:00
|
|
|
|
2018-07-18 12:26:42 +00:00
|
|
|
ModelInstance::EPrintVolumeState state = ModelInstance::PVS_Inside;
|
2018-03-09 09:40:42 +00:00
|
|
|
|
2019-03-01 10:00:34 +00:00
|
|
|
bool contained_min_one = false;
|
|
|
|
|
2018-03-09 09:40:42 +00:00
|
|
|
for (GLVolume* volume : this->volumes)
|
|
|
|
{
|
2019-11-12 08:14:42 +00:00
|
|
|
if ((volume == nullptr) || volume->is_modifier || (volume->is_wipe_tower && !volume->shader_outside_printer_detection_enabled) || ((volume->composite_id.volume_id < 0) && !volume->shader_outside_printer_detection_enabled))
|
2018-12-10 12:57:43 +00:00
|
|
|
continue;
|
2018-07-18 12:26:42 +00:00
|
|
|
|
2018-12-10 12:57:43 +00:00
|
|
|
const BoundingBoxf3& bb = volume->transformed_convex_hull_bounding_box();
|
|
|
|
bool contained = print_volume.contains(bb);
|
2019-11-12 08:14:42 +00:00
|
|
|
|
|
|
|
volume->is_outside = !contained;
|
|
|
|
if (!volume->printable)
|
|
|
|
continue;
|
2018-07-18 12:26:42 +00:00
|
|
|
|
2019-03-01 10:00:34 +00:00
|
|
|
if (contained)
|
|
|
|
contained_min_one = true;
|
|
|
|
|
2018-12-10 12:57:43 +00:00
|
|
|
if ((state == ModelInstance::PVS_Inside) && volume->is_outside)
|
|
|
|
state = ModelInstance::PVS_Fully_Outside;
|
|
|
|
|
|
|
|
if ((state == ModelInstance::PVS_Fully_Outside) && volume->is_outside && print_volume.intersects(bb))
|
|
|
|
state = ModelInstance::PVS_Partly_Outside;
|
2018-04-24 07:00:33 +00:00
|
|
|
}
|
2018-03-09 09:40:42 +00:00
|
|
|
|
2018-07-18 12:26:42 +00:00
|
|
|
if (out_state != nullptr)
|
|
|
|
*out_state = state;
|
|
|
|
|
2019-11-12 08:14:42 +00:00
|
|
|
return contained_min_one;
|
2018-04-24 07:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLVolumeCollection::reset_outside_state()
|
|
|
|
{
|
|
|
|
for (GLVolume* volume : this->volumes)
|
|
|
|
{
|
|
|
|
if (volume != nullptr)
|
2018-03-09 09:40:42 +00:00
|
|
|
volume->is_outside = false;
|
|
|
|
}
|
2017-03-20 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 10:52:29 +00:00
|
|
|
void GLVolumeCollection::update_colors_by_extruder(const DynamicPrintConfig* config)
|
|
|
|
{
|
|
|
|
static const float inv_255 = 1.0f / 255.0f;
|
|
|
|
|
|
|
|
struct Color
|
|
|
|
{
|
|
|
|
std::string text;
|
|
|
|
unsigned char rgb[3];
|
|
|
|
|
|
|
|
Color()
|
|
|
|
: text("")
|
|
|
|
{
|
|
|
|
rgb[0] = 255;
|
|
|
|
rgb[1] = 255;
|
|
|
|
rgb[2] = 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(const std::string& text, unsigned char* rgb)
|
|
|
|
{
|
|
|
|
this->text = text;
|
|
|
|
::memcpy((void*)this->rgb, (const void*)rgb, 3 * sizeof(unsigned char));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (config == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ConfigOptionStrings* extruders_opt = dynamic_cast<const ConfigOptionStrings*>(config->option("extruder_colour"));
|
|
|
|
if (extruders_opt == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ConfigOptionStrings* filamemts_opt = dynamic_cast<const ConfigOptionStrings*>(config->option("filament_colour"));
|
|
|
|
if (filamemts_opt == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned int colors_count = std::max((unsigned int)extruders_opt->values.size(), (unsigned int)filamemts_opt->values.size());
|
|
|
|
if (colors_count == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::vector<Color> colors(colors_count);
|
|
|
|
|
|
|
|
unsigned char rgb[3];
|
|
|
|
for (unsigned int i = 0; i < colors_count; ++i)
|
|
|
|
{
|
|
|
|
const std::string& txt_color = config->opt_string("extruder_colour", i);
|
|
|
|
if (PresetBundle::parse_color(txt_color, rgb))
|
|
|
|
{
|
|
|
|
colors[i].set(txt_color, rgb);
|
2018-03-09 09:40:42 +00:00
|
|
|
}
|
2018-04-05 10:52:29 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
const std::string& txt_color = config->opt_string("filament_colour", i);
|
|
|
|
if (PresetBundle::parse_color(txt_color, rgb))
|
|
|
|
colors[i].set(txt_color, rgb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (GLVolume* volume : volumes)
|
|
|
|
{
|
2018-11-22 10:01:57 +00:00
|
|
|
if ((volume == nullptr) || volume->is_modifier || volume->is_wipe_tower || (volume->volume_idx() < 0))
|
2018-04-05 10:52:29 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
int extruder_id = volume->extruder_id - 1;
|
2018-10-08 13:17:36 +00:00
|
|
|
if ((extruder_id < 0) || ((int)colors.size() <= extruder_id))
|
2018-04-05 10:52:29 +00:00
|
|
|
extruder_id = 0;
|
2018-03-09 09:40:42 +00:00
|
|
|
|
2018-04-05 10:52:29 +00:00
|
|
|
const Color& color = colors[extruder_id];
|
|
|
|
if (!color.text.empty())
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
{
|
|
|
|
volume->color[i] = (float)color.rgb[i] * inv_255;
|
|
|
|
}
|
|
|
|
}
|
2018-03-09 09:40:42 +00:00
|
|
|
}
|
2017-03-20 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 08:14:47 +00:00
|
|
|
std::vector<double> GLVolumeCollection::get_current_print_zs(bool active_only) const
|
2018-02-22 07:59:47 +00:00
|
|
|
{
|
2018-04-04 09:00:25 +00:00
|
|
|
// Collect layer top positions of all volumes.
|
2018-02-22 07:59:47 +00:00
|
|
|
std::vector<double> print_zs;
|
|
|
|
for (GLVolume *vol : this->volumes)
|
2018-04-25 12:38:44 +00:00
|
|
|
{
|
2018-05-18 08:14:47 +00:00
|
|
|
if (!active_only || vol->is_active)
|
2018-04-25 12:38:44 +00:00
|
|
|
append(print_zs, vol->print_zs);
|
|
|
|
}
|
2018-02-22 07:59:47 +00:00
|
|
|
std::sort(print_zs.begin(), print_zs.end());
|
|
|
|
|
2018-04-04 09:00:25 +00:00
|
|
|
// Replace intervals of layers with similar top positions with their average value.
|
|
|
|
int n = int(print_zs.size());
|
|
|
|
int k = 0;
|
|
|
|
for (int i = 0; i < n;) {
|
|
|
|
int j = i + 1;
|
|
|
|
coordf_t zmax = print_zs[i] + EPSILON;
|
|
|
|
for (; j < n && print_zs[j] <= zmax; ++ j) ;
|
|
|
|
print_zs[k ++] = (j > i + 1) ? (0.5 * (print_zs[i] + print_zs[j - 1])) : print_zs[i];
|
|
|
|
i = j;
|
|
|
|
}
|
|
|
|
if (k < n)
|
|
|
|
print_zs.erase(print_zs.begin() + k, print_zs.end());
|
2018-02-22 07:59:47 +00:00
|
|
|
|
|
|
|
return print_zs;
|
|
|
|
}
|
|
|
|
|
2019-08-05 12:30:32 +00:00
|
|
|
size_t GLVolumeCollection::cpu_memory_used() const
|
|
|
|
{
|
|
|
|
size_t memsize = sizeof(*this) + this->volumes.capacity() * sizeof(GLVolume);
|
|
|
|
for (const GLVolume *volume : this->volumes)
|
|
|
|
memsize += volume->cpu_memory_used();
|
|
|
|
return memsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GLVolumeCollection::gpu_memory_used() const
|
|
|
|
{
|
|
|
|
size_t memsize = 0;
|
|
|
|
for (const GLVolume *volume : this->volumes)
|
|
|
|
memsize += volume->gpu_memory_used();
|
|
|
|
return memsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GLVolumeCollection::log_memory_info() const
|
|
|
|
{
|
|
|
|
return " (GLVolumeCollection RAM: " + format_memsize_MB(this->cpu_memory_used()) + " GPU: " + format_memsize_MB(this->gpu_memory_used()) + " Both: " + format_memsize_MB(this->gpu_memory_used()) + ")";
|
|
|
|
}
|
|
|
|
|
2019-08-20 09:33:58 +00:00
|
|
|
bool can_export_to_obj(const GLVolume& volume)
|
|
|
|
{
|
|
|
|
if (!volume.is_active || !volume.is_extrusion_path)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (volume.indexed_vertex_array.triangle_indices.empty() && (std::min(volume.indexed_vertex_array.triangle_indices_size, volume.tverts_range.second - volume.tverts_range.first) == 0))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (volume.indexed_vertex_array.quad_indices.empty() && (std::min(volume.indexed_vertex_array.quad_indices_size, volume.qverts_range.second - volume.qverts_range.first) == 0))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLVolumeCollection::has_toolpaths_to_export() const
|
|
|
|
{
|
|
|
|
for (const GLVolume* volume : this->volumes)
|
|
|
|
{
|
|
|
|
if (can_export_to_obj(*volume))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-20 07:01:09 +00:00
|
|
|
void GLVolumeCollection::export_toolpaths_to_obj(const char* filename) const
|
|
|
|
{
|
|
|
|
if (filename == nullptr)
|
|
|
|
return;
|
|
|
|
|
2019-08-20 09:33:58 +00:00
|
|
|
if (!has_toolpaths_to_export())
|
|
|
|
return;
|
|
|
|
|
2019-08-20 12:35:23 +00:00
|
|
|
// collect color information to generate materials
|
2019-08-27 14:05:38 +00:00
|
|
|
typedef std::array<float, 4> Color;
|
|
|
|
std::set<Color> colors;
|
2019-08-20 12:35:23 +00:00
|
|
|
for (const GLVolume* volume : this->volumes)
|
|
|
|
{
|
|
|
|
if (!can_export_to_obj(*volume))
|
|
|
|
continue;
|
|
|
|
|
2019-08-27 14:05:38 +00:00
|
|
|
Color color;
|
2019-08-20 12:35:23 +00:00
|
|
|
::memcpy((void*)color.data(), (const void*)volume->color, 4 * sizeof(float));
|
|
|
|
colors.insert(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
// save materials file
|
|
|
|
boost::filesystem::path mat_filename(filename);
|
|
|
|
mat_filename.replace_extension("mtl");
|
|
|
|
FILE* fp = boost::nowide::fopen(mat_filename.string().c_str(), "w");
|
|
|
|
if (fp == nullptr) {
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "GLVolumeCollection::export_toolpaths_to_obj: Couldn't open " << mat_filename.string().c_str() << " for writing";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fp, "# G-Code Toolpaths Materials\n");
|
|
|
|
fprintf(fp, "# Generated by %s based on Slic3r\n", SLIC3R_BUILD_ID);
|
|
|
|
|
|
|
|
unsigned int colors_count = 1;
|
2019-08-27 14:05:38 +00:00
|
|
|
for (const Color& color : colors)
|
|
|
|
{
|
2019-08-20 12:35:23 +00:00
|
|
|
fprintf(fp, "\nnewmtl material_%d\n", colors_count++);
|
|
|
|
fprintf(fp, "Ka 1 1 1\n");
|
|
|
|
fprintf(fp, "Kd %f %f %f\n", color[0], color[1], color[2]);
|
|
|
|
fprintf(fp, "Ks 0 0 0\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
// save geometry file
|
|
|
|
fp = boost::nowide::fopen(filename, "w");
|
2019-08-20 07:01:09 +00:00
|
|
|
if (fp == nullptr) {
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "GLVolumeCollection::export_toolpaths_to_obj: Couldn't open " << filename << " for writing";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fp, "# G-Code Toolpaths\n");
|
2019-08-20 12:35:23 +00:00
|
|
|
fprintf(fp, "# Generated by %s based on Slic3r\n", SLIC3R_BUILD_ID);
|
|
|
|
fprintf(fp, "\nmtllib ./%s\n", mat_filename.filename().string().c_str());
|
2019-08-20 07:01:09 +00:00
|
|
|
|
|
|
|
unsigned int vertices_count = 0;
|
2019-08-27 14:05:38 +00:00
|
|
|
unsigned int normals_count = 0;
|
2019-08-20 09:33:58 +00:00
|
|
|
unsigned int volumes_count = 0;
|
2019-08-20 07:01:09 +00:00
|
|
|
|
|
|
|
for (const GLVolume* volume : this->volumes)
|
|
|
|
{
|
2019-08-20 09:33:58 +00:00
|
|
|
if (!can_export_to_obj(*volume))
|
2019-08-20 07:01:09 +00:00
|
|
|
continue;
|
|
|
|
|
2019-08-27 14:05:38 +00:00
|
|
|
std::vector<float> src_vertices_and_normals_interleaved;
|
|
|
|
std::vector<int> src_triangle_indices;
|
|
|
|
std::vector<int> src_quad_indices;
|
2019-08-20 07:01:09 +00:00
|
|
|
|
|
|
|
if (!volume->indexed_vertex_array.vertices_and_normals_interleaved.empty())
|
2019-08-20 10:10:15 +00:00
|
|
|
// data are in CPU memory
|
2019-08-27 14:05:38 +00:00
|
|
|
src_vertices_and_normals_interleaved = volume->indexed_vertex_array.vertices_and_normals_interleaved;
|
2019-08-20 07:01:09 +00:00
|
|
|
else if ((volume->indexed_vertex_array.vertices_and_normals_interleaved_VBO_id != 0) && (volume->indexed_vertex_array.vertices_and_normals_interleaved_size != 0))
|
|
|
|
{
|
2019-08-20 10:10:15 +00:00
|
|
|
// data are in GPU memory
|
2019-08-27 14:05:38 +00:00
|
|
|
src_vertices_and_normals_interleaved = std::vector<float>(volume->indexed_vertex_array.vertices_and_normals_interleaved_size, 0.0f);
|
2019-08-20 07:01:09 +00:00
|
|
|
|
|
|
|
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, volume->indexed_vertex_array.vertices_and_normals_interleaved_VBO_id));
|
2019-08-27 14:05:38 +00:00
|
|
|
glsafe(::glGetBufferSubData(GL_ARRAY_BUFFER, 0, src_vertices_and_normals_interleaved.size() * sizeof(float), src_vertices_and_normals_interleaved.data()));
|
2019-08-20 07:01:09 +00:00
|
|
|
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!volume->indexed_vertex_array.triangle_indices.empty())
|
2019-08-20 10:10:15 +00:00
|
|
|
{
|
|
|
|
// data are in CPU memory
|
|
|
|
size_t size = std::min(volume->indexed_vertex_array.triangle_indices.size(), volume->tverts_range.second - volume->tverts_range.first);
|
|
|
|
if (size != 0)
|
|
|
|
{
|
|
|
|
std::vector<int>::const_iterator it_begin = volume->indexed_vertex_array.triangle_indices.begin() + volume->tverts_range.first;
|
|
|
|
std::vector<int>::const_iterator it_end = volume->indexed_vertex_array.triangle_indices.begin() + volume->tverts_range.first + size;
|
2019-08-27 14:05:38 +00:00
|
|
|
std::copy(it_begin, it_end, std::back_inserter(src_triangle_indices));
|
2019-08-20 10:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-20 07:01:09 +00:00
|
|
|
else if ((volume->indexed_vertex_array.triangle_indices_VBO_id != 0) && (volume->indexed_vertex_array.triangle_indices_size != 0))
|
|
|
|
{
|
2019-08-20 10:10:15 +00:00
|
|
|
// data are in GPU memory
|
2019-08-20 07:51:25 +00:00
|
|
|
size_t size = std::min(volume->indexed_vertex_array.triangle_indices_size, volume->tverts_range.second - volume->tverts_range.first);
|
|
|
|
if (size != 0)
|
|
|
|
{
|
2019-08-27 14:05:38 +00:00
|
|
|
src_triangle_indices = std::vector<int>(size, 0);
|
2019-08-20 07:01:09 +00:00
|
|
|
|
2019-08-20 07:51:25 +00:00
|
|
|
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, volume->indexed_vertex_array.triangle_indices_VBO_id));
|
2019-08-27 14:05:38 +00:00
|
|
|
glsafe(::glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, volume->tverts_range.first * sizeof(int), size * sizeof(int), src_triangle_indices.data()));
|
2019-08-20 07:51:25 +00:00
|
|
|
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
|
|
|
|
}
|
2019-08-20 07:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!volume->indexed_vertex_array.quad_indices.empty())
|
|
|
|
{
|
2019-08-20 10:10:15 +00:00
|
|
|
// data are in CPU memory
|
|
|
|
size_t size = std::min(volume->indexed_vertex_array.quad_indices.size(), volume->qverts_range.second - volume->qverts_range.first);
|
|
|
|
if (size != 0)
|
|
|
|
{
|
|
|
|
std::vector<int>::const_iterator it_begin = volume->indexed_vertex_array.quad_indices.begin() + volume->qverts_range.first;
|
|
|
|
std::vector<int>::const_iterator it_end = volume->indexed_vertex_array.quad_indices.begin() + volume->qverts_range.first + size;
|
2019-08-27 14:05:38 +00:00
|
|
|
std::copy(it_begin, it_end, std::back_inserter(src_quad_indices));
|
2019-08-20 10:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((volume->indexed_vertex_array.quad_indices_VBO_id != 0) && (volume->indexed_vertex_array.quad_indices_size != 0))
|
|
|
|
{
|
|
|
|
// data are in GPU memory
|
2019-08-20 07:51:25 +00:00
|
|
|
size_t size = std::min(volume->indexed_vertex_array.quad_indices_size, volume->qverts_range.second - volume->qverts_range.first);
|
|
|
|
if (size != 0)
|
|
|
|
{
|
2019-08-27 14:05:38 +00:00
|
|
|
src_quad_indices = std::vector<int>(size, 0);
|
2019-08-20 07:01:09 +00:00
|
|
|
|
2019-08-20 07:51:25 +00:00
|
|
|
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, volume->indexed_vertex_array.quad_indices_VBO_id));
|
2019-08-27 14:05:38 +00:00
|
|
|
glsafe(::glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, volume->qverts_range.first * sizeof(int), size * sizeof(int), src_quad_indices.data()));
|
2019-08-20 07:51:25 +00:00
|
|
|
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
|
|
|
|
}
|
2019-08-20 07:01:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 14:05:38 +00:00
|
|
|
if (src_triangle_indices.empty() && src_quad_indices.empty())
|
2019-08-20 07:01:09 +00:00
|
|
|
continue;
|
|
|
|
|
2019-08-27 14:05:38 +00:00
|
|
|
++volumes_count;
|
|
|
|
|
|
|
|
// reduce output size by keeping only used vertices and normals
|
|
|
|
|
|
|
|
struct Vector
|
|
|
|
{
|
|
|
|
std::array<coord_t, 3> vector;
|
|
|
|
|
|
|
|
explicit Vector(float* ptr)
|
|
|
|
{
|
|
|
|
vector[0] = scale_(*(ptr + 0));
|
|
|
|
vector[1] = scale_(*(ptr + 1));
|
|
|
|
vector[2] = scale_(*(ptr + 2));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
typedef std::vector<Vector> Vectors;
|
|
|
|
|
|
|
|
auto vector_less = [](const Vector& v1, const Vector& v2)->bool {
|
|
|
|
return v1.vector < v2.vector;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto vector_equal = [](const Vector& v1, const Vector& v2)->bool {
|
|
|
|
return (v1.vector[0] == v2.vector[0]) && (v1.vector[1] == v2.vector[1]) && (v1.vector[2] == v2.vector[2]);
|
|
|
|
};
|
|
|
|
|
|
|
|
// copy used vertices and normals data
|
|
|
|
Vectors dst_normals;
|
|
|
|
Vectors dst_vertices;
|
|
|
|
|
|
|
|
unsigned int src_triangle_indices_size = (unsigned int)src_triangle_indices.size();
|
|
|
|
for (unsigned int i = 0; i < src_triangle_indices_size; ++i)
|
|
|
|
{
|
|
|
|
float* src_ptr = src_vertices_and_normals_interleaved.data() + src_triangle_indices[i] * 6;
|
|
|
|
dst_normals.emplace_back(src_ptr + 0);
|
|
|
|
dst_vertices.emplace_back(src_ptr + 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int src_quad_indices_size = (unsigned int)src_quad_indices.size();
|
|
|
|
for (unsigned int i = 0; i < src_quad_indices_size; ++i)
|
|
|
|
{
|
|
|
|
float* src_ptr = src_vertices_and_normals_interleaved.data() + src_quad_indices[i] * 6;
|
|
|
|
dst_normals.emplace_back(src_ptr + 0);
|
|
|
|
dst_vertices.emplace_back(src_ptr + 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort vertices and normals
|
|
|
|
std::sort(dst_normals.begin(), dst_normals.end(), vector_less);
|
|
|
|
std::sort(dst_vertices.begin(), dst_vertices.end(), vector_less);
|
|
|
|
|
|
|
|
// remove duplicated vertices and normals
|
|
|
|
dst_normals.erase(std::unique(dst_normals.begin(), dst_normals.end(), vector_equal), dst_normals.end());
|
|
|
|
dst_vertices.erase(std::unique(dst_vertices.begin(), dst_vertices.end(), vector_equal), dst_vertices.end());
|
|
|
|
|
|
|
|
// reindex triangles and quads
|
|
|
|
struct IndicesPair
|
|
|
|
{
|
|
|
|
int vertex;
|
|
|
|
int normal;
|
|
|
|
IndicesPair(int vertex, int normal) : vertex(vertex), normal(normal) {}
|
|
|
|
};
|
|
|
|
typedef std::vector<IndicesPair> Indices;
|
|
|
|
|
|
|
|
unsigned int src_vertices_count = (unsigned int)src_vertices_and_normals_interleaved.size() / 6;
|
|
|
|
std::vector<int> src_dst_vertex_indices_map(src_vertices_count, -1);
|
|
|
|
std::vector<int> src_dst_normal_indices_map(src_vertices_count, -1);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < src_vertices_count; ++i)
|
|
|
|
{
|
|
|
|
float* src_ptr = src_vertices_and_normals_interleaved.data() + i * 6;
|
|
|
|
src_dst_normal_indices_map[i] = std::distance(dst_normals.begin(), std::lower_bound(dst_normals.begin(), dst_normals.end(), Vector(src_ptr + 0), vector_less));
|
|
|
|
src_dst_vertex_indices_map[i] = std::distance(dst_vertices.begin(), std::lower_bound(dst_vertices.begin(), dst_vertices.end(), Vector(src_ptr + 3), vector_less));
|
|
|
|
}
|
|
|
|
|
|
|
|
Indices dst_triangle_indices;
|
|
|
|
if (src_triangle_indices_size > 0)
|
|
|
|
dst_triangle_indices.reserve(src_triangle_indices_size);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < src_triangle_indices_size; ++i)
|
|
|
|
{
|
|
|
|
int id = src_triangle_indices[i];
|
|
|
|
dst_triangle_indices.emplace_back(src_dst_vertex_indices_map[id], src_dst_normal_indices_map[id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Indices dst_quad_indices;
|
|
|
|
if (src_quad_indices_size > 0)
|
|
|
|
dst_quad_indices.reserve(src_quad_indices_size);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < src_quad_indices_size; ++i)
|
|
|
|
{
|
|
|
|
int id = src_quad_indices[i];
|
|
|
|
dst_quad_indices.emplace_back(src_dst_vertex_indices_map[id], src_dst_normal_indices_map[id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// save to file
|
2019-08-20 09:33:58 +00:00
|
|
|
fprintf(fp, "\n# vertices volume %d\n", volumes_count);
|
2019-08-27 14:05:38 +00:00
|
|
|
for (const Vector& v : dst_vertices)
|
2019-08-20 07:01:09 +00:00
|
|
|
{
|
2019-08-28 09:06:30 +00:00
|
|
|
fprintf(fp, "v %g %g %g\n", unscale<float>(v.vector[0]), unscale<float>(v.vector[1]), unscale<float>(v.vector[2]));
|
2019-08-20 07:01:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 09:33:58 +00:00
|
|
|
fprintf(fp, "\n# normals volume %d\n", volumes_count);
|
2019-08-27 14:05:38 +00:00
|
|
|
for (const Vector& n : dst_normals)
|
2019-08-20 07:01:09 +00:00
|
|
|
{
|
2019-08-28 09:06:30 +00:00
|
|
|
fprintf(fp, "vn %g %g %g\n", unscale<float>(n.vector[0]), unscale<float>(n.vector[1]), unscale<float>(n.vector[2]));
|
2019-08-20 07:01:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 14:05:38 +00:00
|
|
|
Color color;
|
2019-08-20 12:35:23 +00:00
|
|
|
::memcpy((void*)color.data(), (const void*)volume->color, 4 * sizeof(float));
|
|
|
|
fprintf(fp, "\n# material volume %d\n", volumes_count);
|
2019-09-04 08:46:51 +00:00
|
|
|
fprintf(fp, "usemtl material_%lld\n", (long long)(1 + std::distance(colors.begin(), colors.find(color))));
|
2019-08-20 12:35:23 +00:00
|
|
|
|
2019-08-27 14:05:38 +00:00
|
|
|
int base_vertex_id = vertices_count + 1;
|
|
|
|
int base_normal_id = normals_count + 1;
|
|
|
|
|
|
|
|
if (!dst_triangle_indices.empty())
|
2019-08-20 07:01:09 +00:00
|
|
|
{
|
2019-08-27 14:05:38 +00:00
|
|
|
fprintf(fp, "\n# triangular facets volume %d\n", volumes_count);
|
|
|
|
for (unsigned int i = 0; i < (unsigned int)dst_triangle_indices.size(); i += 3)
|
|
|
|
{
|
|
|
|
fprintf(fp, "f %d//%d %d//%d %d//%d\n",
|
|
|
|
base_vertex_id + dst_triangle_indices[i + 0].vertex, base_normal_id + dst_triangle_indices[i + 0].normal,
|
|
|
|
base_vertex_id + dst_triangle_indices[i + 1].vertex, base_normal_id + dst_triangle_indices[i + 1].normal,
|
|
|
|
base_vertex_id + dst_triangle_indices[i + 2].vertex, base_normal_id + dst_triangle_indices[i + 2].normal);
|
|
|
|
}
|
2019-08-20 07:01:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 14:05:38 +00:00
|
|
|
if (!dst_quad_indices.empty())
|
2019-08-20 07:01:09 +00:00
|
|
|
{
|
2019-08-27 14:05:38 +00:00
|
|
|
fprintf(fp, "\n# quadrangular facets volume %d\n", volumes_count);
|
|
|
|
for (unsigned int i = 0; i < (unsigned int)src_quad_indices.size(); i += 4)
|
|
|
|
{
|
|
|
|
fprintf(fp, "f %d//%d %d//%d %d//%d %d//%d\n",
|
|
|
|
base_vertex_id + dst_quad_indices[i + 0].vertex, base_normal_id + dst_quad_indices[i + 0].normal,
|
|
|
|
base_vertex_id + dst_quad_indices[i + 1].vertex, base_normal_id + dst_quad_indices[i + 1].normal,
|
|
|
|
base_vertex_id + dst_quad_indices[i + 2].vertex, base_normal_id + dst_quad_indices[i + 2].normal,
|
|
|
|
base_vertex_id + dst_quad_indices[i + 3].vertex, base_normal_id + dst_quad_indices[i + 3].normal);
|
|
|
|
}
|
2019-08-20 07:01:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 14:05:38 +00:00
|
|
|
vertices_count += (unsigned int)dst_vertices.size();
|
|
|
|
normals_count += (unsigned int)dst_normals.size();
|
2019-08-20 07:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2015-01-25 14:21:45 +00:00
|
|
|
// caller is responsible for supplying NO lines with zero length
|
2017-03-15 15:33:25 +00:00
|
|
|
static void thick_lines_to_indexed_vertex_array(
|
2017-03-13 15:02:17 +00:00
|
|
|
const Lines &lines,
|
|
|
|
const std::vector<double> &widths,
|
|
|
|
const std::vector<double> &heights,
|
|
|
|
bool closed,
|
|
|
|
double top_z,
|
2017-03-15 15:33:25 +00:00
|
|
|
GLIndexedVertexArray &volume)
|
2015-01-17 23:36:21 +00:00
|
|
|
{
|
2016-04-15 15:58:29 +00:00
|
|
|
assert(! lines.empty());
|
|
|
|
if (lines.empty())
|
|
|
|
return;
|
2017-03-15 15:33:25 +00:00
|
|
|
|
|
|
|
#define LEFT 0
|
|
|
|
#define RIGHT 1
|
|
|
|
#define TOP 2
|
|
|
|
#define BOTTOM 3
|
|
|
|
|
|
|
|
// right, left, top, bottom
|
|
|
|
int idx_prev[4] = { -1, -1, -1, -1 };
|
|
|
|
double bottom_z_prev = 0.;
|
2019-07-01 06:33:40 +00:00
|
|
|
Vec2d b1_prev(Vec2d::Zero());
|
|
|
|
Vec2d v_prev(Vec2d::Zero());
|
2017-03-15 15:33:25 +00:00
|
|
|
int idx_initial[4] = { -1, -1, -1, -1 };
|
|
|
|
double width_initial = 0.;
|
2018-04-17 13:04:14 +00:00
|
|
|
double bottom_z_initial = 0.0;
|
2019-07-08 13:30:59 +00:00
|
|
|
double len_prev = 0.0;
|
2017-03-15 15:33:25 +00:00
|
|
|
|
2015-01-17 23:36:21 +00:00
|
|
|
// loop once more in case of closed loops
|
2017-03-15 15:33:25 +00:00
|
|
|
size_t lines_end = closed ? (lines.size() + 1) : lines.size();
|
|
|
|
for (size_t ii = 0; ii < lines_end; ++ ii) {
|
|
|
|
size_t i = (ii == lines.size()) ? 0 : ii;
|
2017-03-14 09:11:08 +00:00
|
|
|
const Line &line = lines[i];
|
|
|
|
double bottom_z = top_z - heights[i];
|
2018-04-17 13:04:14 +00:00
|
|
|
double middle_z = 0.5 * (top_z + bottom_z);
|
2017-03-15 15:33:25 +00:00
|
|
|
double width = widths[i];
|
2018-04-17 13:04:14 +00:00
|
|
|
|
|
|
|
bool is_first = (ii == 0);
|
|
|
|
bool is_last = (ii == lines_end - 1);
|
|
|
|
bool is_closing = closed && is_last;
|
|
|
|
|
2019-07-01 06:33:40 +00:00
|
|
|
Vec2d v = unscale(line.vector()).normalized();
|
2019-07-08 13:30:59 +00:00
|
|
|
double len = unscale<double>(line.length());
|
2018-04-17 13:04:14 +00:00
|
|
|
|
2018-08-21 19:05:24 +00:00
|
|
|
Vec2d a = unscale(line.a);
|
|
|
|
Vec2d b = unscale(line.b);
|
|
|
|
Vec2d a1 = a;
|
|
|
|
Vec2d a2 = a;
|
|
|
|
Vec2d b1 = b;
|
|
|
|
Vec2d b2 = b;
|
2017-03-15 15:33:25 +00:00
|
|
|
{
|
2018-04-17 13:04:14 +00:00
|
|
|
double dist = 0.5 * width; // scaled
|
2018-08-17 13:53:43 +00:00
|
|
|
double dx = dist * v(0);
|
|
|
|
double dy = dist * v(1);
|
2018-08-21 19:05:24 +00:00
|
|
|
a1 += Vec2d(+dy, -dx);
|
|
|
|
a2 += Vec2d(-dy, +dx);
|
|
|
|
b1 += Vec2d(+dy, -dx);
|
|
|
|
b2 += Vec2d(-dy, +dx);
|
2017-03-15 15:33:25 +00:00
|
|
|
}
|
|
|
|
|
2015-01-17 23:36:21 +00:00
|
|
|
// calculate new XY normals
|
2019-07-01 06:33:40 +00:00
|
|
|
Vec2d xy_right_normal = unscale(line.normal()).normalized();
|
2017-03-15 15:33:25 +00:00
|
|
|
|
2019-09-04 08:46:51 +00:00
|
|
|
int idx_a[4] = { 0, 0, 0, 0 }; // initialized to avoid warnings
|
|
|
|
int idx_b[4] = { 0, 0, 0, 0 }; // initialized to avoid warnings
|
2017-03-15 15:33:25 +00:00
|
|
|
int idx_last = int(volume.vertices_and_normals_interleaved.size() / 6);
|
|
|
|
|
|
|
|
bool bottom_z_different = bottom_z_prev != bottom_z;
|
|
|
|
bottom_z_prev = bottom_z;
|
|
|
|
|
2018-04-17 13:04:14 +00:00
|
|
|
if (!is_first && bottom_z_different)
|
|
|
|
{
|
|
|
|
// Found a change of the layer thickness -> Add a cap at the end of the previous segment.
|
|
|
|
volume.push_quad(idx_b[BOTTOM], idx_b[LEFT], idx_b[TOP], idx_b[RIGHT]);
|
|
|
|
}
|
|
|
|
|
2017-03-15 15:33:25 +00:00
|
|
|
// Share top / bottom vertices if possible.
|
2018-04-17 13:04:14 +00:00
|
|
|
if (is_first) {
|
|
|
|
idx_a[TOP] = idx_last++;
|
2018-08-17 13:53:43 +00:00
|
|
|
volume.push_geometry(a(0), a(1), top_z , 0., 0., 1.);
|
2017-03-15 15:33:25 +00:00
|
|
|
} else {
|
|
|
|
idx_a[TOP] = idx_prev[TOP];
|
|
|
|
}
|
2018-04-17 13:04:14 +00:00
|
|
|
|
|
|
|
if (is_first || bottom_z_different) {
|
2017-05-17 14:53:40 +00:00
|
|
|
// Start of the 1st line segment or a change of the layer thickness while maintaining the print_z.
|
2017-03-15 15:33:25 +00:00
|
|
|
idx_a[BOTTOM] = idx_last ++;
|
2018-08-17 13:53:43 +00:00
|
|
|
volume.push_geometry(a(0), a(1), bottom_z, 0., 0., -1.);
|
2017-05-17 14:53:40 +00:00
|
|
|
idx_a[LEFT ] = idx_last ++;
|
2019-07-01 06:33:40 +00:00
|
|
|
volume.push_geometry(a2(0), a2(1), middle_z, -xy_right_normal(0), -xy_right_normal(1), 0.0);
|
2017-05-17 14:53:40 +00:00
|
|
|
idx_a[RIGHT] = idx_last ++;
|
2019-07-01 06:33:40 +00:00
|
|
|
volume.push_geometry(a1(0), a1(1), middle_z, xy_right_normal(0), xy_right_normal(1), 0.0);
|
2018-04-17 13:04:14 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-03-15 15:33:25 +00:00
|
|
|
idx_a[BOTTOM] = idx_prev[BOTTOM];
|
|
|
|
}
|
|
|
|
|
2018-04-17 13:04:14 +00:00
|
|
|
if (is_first) {
|
2017-03-15 15:33:25 +00:00
|
|
|
// Start of the 1st line segment.
|
|
|
|
width_initial = width;
|
2018-04-17 13:04:14 +00:00
|
|
|
bottom_z_initial = bottom_z;
|
2017-03-15 15:33:25 +00:00
|
|
|
memcpy(idx_initial, idx_a, sizeof(int) * 4);
|
|
|
|
} else {
|
|
|
|
// Continuing a previous segment.
|
|
|
|
// Share left / right vertices if possible.
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
double v_dot = v_prev.dot(v);
|
2019-07-08 13:30:59 +00:00
|
|
|
// To reduce gpu memory usage, we try to reuse vertices
|
|
|
|
// To reduce the visual artifacts, due to averaged normals, we allow to reuse vertices only when any of two adjacent edges
|
|
|
|
// is longer than a fixed threshold.
|
|
|
|
// The following value is arbitrary, it comes from tests made on a bunch of models showing the visual artifacts
|
|
|
|
double len_threshold = 2.5;
|
|
|
|
|
|
|
|
// Generate new vertices if the angle between adjacent edges is greater than 45 degrees or thresholds conditions are met
|
|
|
|
bool sharp = (v_dot < 0.707) || (len_prev > len_threshold) || (len > len_threshold);
|
2017-03-15 15:33:25 +00:00
|
|
|
if (sharp) {
|
2018-04-17 13:04:14 +00:00
|
|
|
if (!bottom_z_different)
|
|
|
|
{
|
|
|
|
// Allocate new left / right points for the start of this segment as these points will receive their own normals to indicate a sharp turn.
|
|
|
|
idx_a[RIGHT] = idx_last++;
|
2019-07-01 06:33:40 +00:00
|
|
|
volume.push_geometry(a1(0), a1(1), middle_z, xy_right_normal(0), xy_right_normal(1), 0.0);
|
2018-04-17 13:04:14 +00:00
|
|
|
idx_a[LEFT] = idx_last++;
|
2019-07-01 06:33:40 +00:00
|
|
|
volume.push_geometry(a2(0), a2(1), middle_z, -xy_right_normal(0), -xy_right_normal(1), 0.0);
|
|
|
|
if (cross2(v_prev, v) > 0.) {
|
|
|
|
// Right turn. Fill in the right turn wedge.
|
|
|
|
volume.push_triangle(idx_prev[RIGHT], idx_a[RIGHT], idx_prev[TOP]);
|
|
|
|
volume.push_triangle(idx_prev[RIGHT], idx_prev[BOTTOM], idx_a[RIGHT]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Left turn. Fill in the left turn wedge.
|
|
|
|
volume.push_triangle(idx_prev[LEFT], idx_prev[TOP], idx_a[LEFT]);
|
|
|
|
volume.push_triangle(idx_prev[LEFT], idx_a[LEFT], idx_prev[BOTTOM]);
|
|
|
|
}
|
2018-04-17 13:04:14 +00:00
|
|
|
}
|
2017-03-15 15:33:25 +00:00
|
|
|
}
|
2019-07-01 06:33:40 +00:00
|
|
|
else
|
|
|
|
{
|
2018-04-17 13:04:14 +00:00
|
|
|
if (!bottom_z_different)
|
|
|
|
{
|
|
|
|
// The two successive segments are nearly collinear.
|
|
|
|
idx_a[LEFT ] = idx_prev[LEFT];
|
|
|
|
idx_a[RIGHT] = idx_prev[RIGHT];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_closing) {
|
|
|
|
if (!sharp) {
|
|
|
|
if (!bottom_z_different)
|
|
|
|
{
|
|
|
|
// Closing a loop with smooth transition. Unify the closing left / right vertices.
|
|
|
|
memcpy(volume.vertices_and_normals_interleaved.data() + idx_initial[LEFT ] * 6, volume.vertices_and_normals_interleaved.data() + idx_prev[LEFT ] * 6, sizeof(float) * 6);
|
|
|
|
memcpy(volume.vertices_and_normals_interleaved.data() + idx_initial[RIGHT] * 6, volume.vertices_and_normals_interleaved.data() + idx_prev[RIGHT] * 6, sizeof(float) * 6);
|
|
|
|
volume.vertices_and_normals_interleaved.erase(volume.vertices_and_normals_interleaved.end() - 12, volume.vertices_and_normals_interleaved.end());
|
|
|
|
// Replace the left / right vertex indices to point to the start of the loop.
|
|
|
|
for (size_t u = volume.quad_indices.size() - 16; u < volume.quad_indices.size(); ++ u) {
|
|
|
|
if (volume.quad_indices[u] == idx_prev[LEFT])
|
|
|
|
volume.quad_indices[u] = idx_initial[LEFT];
|
|
|
|
else if (volume.quad_indices[u] == idx_prev[RIGHT])
|
|
|
|
volume.quad_indices[u] = idx_initial[RIGHT];
|
|
|
|
}
|
2017-03-15 15:33:25 +00:00
|
|
|
}
|
2015-01-17 23:36:21 +00:00
|
|
|
}
|
2017-03-15 15:33:25 +00:00
|
|
|
// This is the last iteration, only required to solve the transition.
|
|
|
|
break;
|
2015-01-17 23:36:21 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-15 15:33:25 +00:00
|
|
|
|
|
|
|
// Only new allocate top / bottom vertices, if not closing a loop.
|
2018-04-17 13:04:14 +00:00
|
|
|
if (is_closing) {
|
2017-03-15 15:33:25 +00:00
|
|
|
idx_b[TOP] = idx_initial[TOP];
|
|
|
|
} else {
|
|
|
|
idx_b[TOP] = idx_last ++;
|
2018-08-17 13:53:43 +00:00
|
|
|
volume.push_geometry(b(0), b(1), top_z , 0., 0., 1.);
|
2017-03-15 15:33:25 +00:00
|
|
|
}
|
2018-04-17 13:04:14 +00:00
|
|
|
|
|
|
|
if (is_closing && (width == width_initial) && (bottom_z == bottom_z_initial)) {
|
2017-03-15 15:33:25 +00:00
|
|
|
idx_b[BOTTOM] = idx_initial[BOTTOM];
|
|
|
|
} else {
|
|
|
|
idx_b[BOTTOM] = idx_last ++;
|
2018-08-17 13:53:43 +00:00
|
|
|
volume.push_geometry(b(0), b(1), bottom_z, 0., 0., -1.);
|
2017-03-15 15:33:25 +00:00
|
|
|
}
|
|
|
|
// Generate new vertices for the end of this line segment.
|
|
|
|
idx_b[LEFT ] = idx_last ++;
|
2019-07-01 06:33:40 +00:00
|
|
|
volume.push_geometry(b2(0), b2(1), middle_z, -xy_right_normal(0), -xy_right_normal(1), 0.0);
|
2017-03-15 15:33:25 +00:00
|
|
|
idx_b[RIGHT ] = idx_last ++;
|
2019-07-01 06:33:40 +00:00
|
|
|
volume.push_geometry(b1(0), b1(1), middle_z, xy_right_normal(0), xy_right_normal(1), 0.0);
|
2017-03-15 15:33:25 +00:00
|
|
|
|
|
|
|
memcpy(idx_prev, idx_b, 4 * sizeof(int));
|
|
|
|
bottom_z_prev = bottom_z;
|
|
|
|
b1_prev = b1;
|
2018-04-17 13:04:14 +00:00
|
|
|
v_prev = v;
|
2019-07-08 13:30:59 +00:00
|
|
|
len_prev = len;
|
2018-04-17 13:04:14 +00:00
|
|
|
|
2018-09-07 08:30:13 +00:00
|
|
|
if (bottom_z_different && (closed || (!is_first && !is_last)))
|
2018-04-17 13:04:14 +00:00
|
|
|
{
|
|
|
|
// Found a change of the layer thickness -> Add a cap at the beginning of this segment.
|
|
|
|
volume.push_quad(idx_a[BOTTOM], idx_a[RIGHT], idx_a[TOP], idx_a[LEFT]);
|
|
|
|
}
|
2017-03-15 15:33:25 +00:00
|
|
|
|
2017-03-14 09:11:08 +00:00
|
|
|
if (! closed) {
|
2017-03-15 15:33:25 +00:00
|
|
|
// Terminate open paths with caps.
|
2018-09-07 08:30:13 +00:00
|
|
|
if (is_first)
|
2017-03-28 15:09:57 +00:00
|
|
|
volume.push_quad(idx_a[BOTTOM], idx_a[RIGHT], idx_a[TOP], idx_a[LEFT]);
|
2017-03-15 15:33:25 +00:00
|
|
|
// We don't use 'else' because both cases are true if we have only one line.
|
2018-09-07 08:30:13 +00:00
|
|
|
if (is_last)
|
2017-03-28 15:09:57 +00:00
|
|
|
volume.push_quad(idx_b[BOTTOM], idx_b[LEFT], idx_b[TOP], idx_b[RIGHT]);
|
2015-01-17 23:36:21 +00:00
|
|
|
}
|
2018-04-17 13:04:14 +00:00
|
|
|
|
2017-03-15 15:33:25 +00:00
|
|
|
// Add quads for a straight hollow tube-like segment.
|
2015-01-17 23:36:21 +00:00
|
|
|
// bottom-right face
|
2017-03-28 15:09:57 +00:00
|
|
|
volume.push_quad(idx_a[BOTTOM], idx_b[BOTTOM], idx_b[RIGHT], idx_a[RIGHT]);
|
2015-01-17 23:36:21 +00:00
|
|
|
// top-right face
|
2017-03-28 15:09:57 +00:00
|
|
|
volume.push_quad(idx_a[RIGHT], idx_b[RIGHT], idx_b[TOP], idx_a[TOP]);
|
2015-01-17 23:36:21 +00:00
|
|
|
// top-left face
|
2017-03-28 15:09:57 +00:00
|
|
|
volume.push_quad(idx_a[TOP], idx_b[TOP], idx_b[LEFT], idx_a[LEFT]);
|
2015-01-17 23:36:21 +00:00
|
|
|
// bottom-left face
|
2017-03-28 15:09:57 +00:00
|
|
|
volume.push_quad(idx_a[LEFT], idx_b[LEFT], idx_b[BOTTOM], idx_a[BOTTOM]);
|
2017-03-14 09:11:08 +00:00
|
|
|
}
|
2017-03-15 15:33:25 +00:00
|
|
|
|
|
|
|
#undef LEFT
|
|
|
|
#undef RIGHT
|
|
|
|
#undef TOP
|
|
|
|
#undef BOTTOM
|
2017-03-14 09:11:08 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 12:44:10 +00:00
|
|
|
// caller is responsible for supplying NO lines with zero length
|
|
|
|
static void thick_lines_to_indexed_vertex_array(const Lines3& lines,
|
|
|
|
const std::vector<double>& widths,
|
|
|
|
const std::vector<double>& heights,
|
|
|
|
bool closed,
|
|
|
|
GLIndexedVertexArray& volume)
|
|
|
|
{
|
|
|
|
assert(!lines.empty());
|
|
|
|
if (lines.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
#define LEFT 0
|
|
|
|
#define RIGHT 1
|
|
|
|
#define TOP 2
|
|
|
|
#define BOTTOM 3
|
|
|
|
|
|
|
|
// left, right, top, bottom
|
|
|
|
int idx_initial[4] = { -1, -1, -1, -1 };
|
|
|
|
int idx_prev[4] = { -1, -1, -1, -1 };
|
|
|
|
double z_prev = 0.0;
|
2019-07-08 13:30:59 +00:00
|
|
|
double len_prev = 0.0;
|
2019-07-01 06:33:40 +00:00
|
|
|
Vec3d n_right_prev = Vec3d::Zero();
|
|
|
|
Vec3d n_top_prev = Vec3d::Zero();
|
|
|
|
Vec3d unit_v_prev = Vec3d::Zero();
|
2018-01-08 12:44:10 +00:00
|
|
|
double width_initial = 0.0;
|
|
|
|
|
|
|
|
// new vertices around the line endpoints
|
|
|
|
// left, right, top, bottom
|
2018-08-21 15:43:05 +00:00
|
|
|
Vec3d a[4] = { Vec3d::Zero(), Vec3d::Zero(), Vec3d::Zero(), Vec3d::Zero() };
|
|
|
|
Vec3d b[4] = { Vec3d::Zero(), Vec3d::Zero(), Vec3d::Zero(), Vec3d::Zero() };
|
2018-01-08 12:44:10 +00:00
|
|
|
|
|
|
|
// loop once more in case of closed loops
|
|
|
|
size_t lines_end = closed ? (lines.size() + 1) : lines.size();
|
|
|
|
for (size_t ii = 0; ii < lines_end; ++ii)
|
|
|
|
{
|
|
|
|
size_t i = (ii == lines.size()) ? 0 : ii;
|
|
|
|
|
|
|
|
const Line3& line = lines[i];
|
|
|
|
double height = heights[i];
|
|
|
|
double width = widths[i];
|
|
|
|
|
2018-08-21 15:43:05 +00:00
|
|
|
Vec3d unit_v = unscale(line.vector()).normalized();
|
2019-07-08 13:30:59 +00:00
|
|
|
double len = unscale<double>(line.length());
|
2018-01-08 12:44:10 +00:00
|
|
|
|
2018-08-21 15:43:05 +00:00
|
|
|
Vec3d n_top = Vec3d::Zero();
|
|
|
|
Vec3d n_right = Vec3d::Zero();
|
2019-07-01 06:33:40 +00:00
|
|
|
|
2018-08-17 13:53:43 +00:00
|
|
|
if ((line.a(0) == line.b(0)) && (line.a(1) == line.b(1)))
|
2018-01-08 12:44:10 +00:00
|
|
|
{
|
|
|
|
// vertical segment
|
2019-07-01 06:33:40 +00:00
|
|
|
n_top = Vec3d::UnitY();
|
|
|
|
n_right = Vec3d::UnitX();
|
|
|
|
if (line.a(2) < line.b(2))
|
|
|
|
n_right = -n_right;
|
2018-01-08 12:44:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-07-01 06:33:40 +00:00
|
|
|
// horizontal segment
|
|
|
|
n_right = unit_v.cross(Vec3d::UnitZ()).normalized();
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
n_top = n_right.cross(unit_v).normalized();
|
2018-01-08 12:44:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 15:43:05 +00:00
|
|
|
Vec3d rl_displacement = 0.5 * width * n_right;
|
|
|
|
Vec3d tb_displacement = 0.5 * height * n_top;
|
|
|
|
Vec3d l_a = unscale(line.a);
|
|
|
|
Vec3d l_b = unscale(line.b);
|
2018-01-08 12:44:10 +00:00
|
|
|
|
|
|
|
a[RIGHT] = l_a + rl_displacement;
|
|
|
|
a[LEFT] = l_a - rl_displacement;
|
|
|
|
a[TOP] = l_a + tb_displacement;
|
|
|
|
a[BOTTOM] = l_a - tb_displacement;
|
|
|
|
b[RIGHT] = l_b + rl_displacement;
|
|
|
|
b[LEFT] = l_b - rl_displacement;
|
|
|
|
b[TOP] = l_b + tb_displacement;
|
|
|
|
b[BOTTOM] = l_b - tb_displacement;
|
|
|
|
|
2018-08-21 15:43:05 +00:00
|
|
|
Vec3d n_bottom = -n_top;
|
|
|
|
Vec3d n_left = -n_right;
|
2018-01-08 12:44:10 +00:00
|
|
|
|
|
|
|
int idx_a[4];
|
|
|
|
int idx_b[4];
|
|
|
|
int idx_last = int(volume.vertices_and_normals_interleaved.size() / 6);
|
|
|
|
|
2018-08-17 13:53:43 +00:00
|
|
|
bool z_different = (z_prev != l_a(2));
|
|
|
|
z_prev = l_b(2);
|
2018-01-08 12:44:10 +00:00
|
|
|
|
|
|
|
// Share top / bottom vertices if possible.
|
|
|
|
if (ii == 0)
|
|
|
|
{
|
|
|
|
idx_a[TOP] = idx_last++;
|
|
|
|
volume.push_geometry(a[TOP], n_top);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
idx_a[TOP] = idx_prev[TOP];
|
|
|
|
|
|
|
|
if ((ii == 0) || z_different)
|
|
|
|
{
|
|
|
|
// Start of the 1st line segment or a change of the layer thickness while maintaining the print_z.
|
|
|
|
idx_a[BOTTOM] = idx_last++;
|
|
|
|
volume.push_geometry(a[BOTTOM], n_bottom);
|
|
|
|
idx_a[LEFT] = idx_last++;
|
|
|
|
volume.push_geometry(a[LEFT], n_left);
|
|
|
|
idx_a[RIGHT] = idx_last++;
|
|
|
|
volume.push_geometry(a[RIGHT], n_right);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
idx_a[BOTTOM] = idx_prev[BOTTOM];
|
|
|
|
|
|
|
|
if (ii == 0)
|
|
|
|
{
|
|
|
|
// Start of the 1st line segment.
|
|
|
|
width_initial = width;
|
|
|
|
::memcpy(idx_initial, idx_a, sizeof(int) * 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Continuing a previous segment.
|
|
|
|
// Share left / right vertices if possible.
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
double v_dot = unit_v_prev.dot(unit_v);
|
|
|
|
bool is_right_turn = n_top_prev.dot(unit_v_prev.cross(unit_v)) > 0.0;
|
2018-01-08 12:44:10 +00:00
|
|
|
|
2019-07-08 13:30:59 +00:00
|
|
|
// To reduce gpu memory usage, we try to reuse vertices
|
|
|
|
// To reduce the visual artifacts, due to averaged normals, we allow to reuse vertices only when any of two adjacent edges
|
|
|
|
// is longer than a fixed threshold.
|
|
|
|
// The following value is arbitrary, it comes from tests made on a bunch of models showing the visual artifacts
|
|
|
|
double len_threshold = 2.5;
|
|
|
|
|
|
|
|
// Generate new vertices if the angle between adjacent edges is greater than 45 degrees or thresholds conditions are met
|
|
|
|
bool is_sharp = (v_dot < 0.707) || (len_prev > len_threshold) || (len > len_threshold);
|
2018-01-08 12:44:10 +00:00
|
|
|
if (is_sharp)
|
|
|
|
{
|
|
|
|
// Allocate new left / right points for the start of this segment as these points will receive their own normals to indicate a sharp turn.
|
|
|
|
idx_a[RIGHT] = idx_last++;
|
|
|
|
volume.push_geometry(a[RIGHT], n_right);
|
|
|
|
idx_a[LEFT] = idx_last++;
|
|
|
|
volume.push_geometry(a[LEFT], n_left);
|
|
|
|
|
2019-07-01 06:33:40 +00:00
|
|
|
if (is_right_turn)
|
|
|
|
{
|
|
|
|
// Right turn. Fill in the right turn wedge.
|
|
|
|
volume.push_triangle(idx_prev[RIGHT], idx_a[RIGHT], idx_prev[TOP]);
|
|
|
|
volume.push_triangle(idx_prev[RIGHT], idx_prev[BOTTOM], idx_a[RIGHT]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Left turn. Fill in the left turn wedge.
|
|
|
|
volume.push_triangle(idx_prev[LEFT], idx_prev[TOP], idx_a[LEFT]);
|
|
|
|
volume.push_triangle(idx_prev[LEFT], idx_a[LEFT], idx_prev[BOTTOM]);
|
|
|
|
}
|
2018-01-08 12:44:10 +00:00
|
|
|
}
|
2019-07-01 06:33:40 +00:00
|
|
|
else
|
2018-01-08 12:44:10 +00:00
|
|
|
{
|
2019-07-01 06:33:40 +00:00
|
|
|
// The two successive segments are nearly collinear.
|
2018-01-08 12:44:10 +00:00
|
|
|
idx_a[LEFT] = idx_prev[LEFT];
|
|
|
|
idx_a[RIGHT] = idx_prev[RIGHT];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ii == lines.size())
|
|
|
|
{
|
|
|
|
if (!is_sharp)
|
|
|
|
{
|
|
|
|
// Closing a loop with smooth transition. Unify the closing left / right vertices.
|
|
|
|
::memcpy(volume.vertices_and_normals_interleaved.data() + idx_initial[LEFT] * 6, volume.vertices_and_normals_interleaved.data() + idx_prev[LEFT] * 6, sizeof(float) * 6);
|
|
|
|
::memcpy(volume.vertices_and_normals_interleaved.data() + idx_initial[RIGHT] * 6, volume.vertices_and_normals_interleaved.data() + idx_prev[RIGHT] * 6, sizeof(float) * 6);
|
|
|
|
volume.vertices_and_normals_interleaved.erase(volume.vertices_and_normals_interleaved.end() - 12, volume.vertices_and_normals_interleaved.end());
|
|
|
|
// Replace the left / right vertex indices to point to the start of the loop.
|
|
|
|
for (size_t u = volume.quad_indices.size() - 16; u < volume.quad_indices.size(); ++u)
|
|
|
|
{
|
|
|
|
if (volume.quad_indices[u] == idx_prev[LEFT])
|
|
|
|
volume.quad_indices[u] = idx_initial[LEFT];
|
|
|
|
else if (volume.quad_indices[u] == idx_prev[RIGHT])
|
|
|
|
volume.quad_indices[u] = idx_initial[RIGHT];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the last iteration, only required to solve the transition.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only new allocate top / bottom vertices, if not closing a loop.
|
|
|
|
if (closed && (ii + 1 == lines.size()))
|
|
|
|
idx_b[TOP] = idx_initial[TOP];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
idx_b[TOP] = idx_last++;
|
|
|
|
volume.push_geometry(b[TOP], n_top);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (closed && (ii + 1 == lines.size()) && (width == width_initial))
|
|
|
|
idx_b[BOTTOM] = idx_initial[BOTTOM];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
idx_b[BOTTOM] = idx_last++;
|
|
|
|
volume.push_geometry(b[BOTTOM], n_bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate new vertices for the end of this line segment.
|
|
|
|
idx_b[LEFT] = idx_last++;
|
|
|
|
volume.push_geometry(b[LEFT], n_left);
|
|
|
|
idx_b[RIGHT] = idx_last++;
|
|
|
|
volume.push_geometry(b[RIGHT], n_right);
|
|
|
|
|
|
|
|
::memcpy(idx_prev, idx_b, 4 * sizeof(int));
|
|
|
|
n_right_prev = n_right;
|
|
|
|
n_top_prev = n_top;
|
|
|
|
unit_v_prev = unit_v;
|
2019-07-08 13:30:59 +00:00
|
|
|
len_prev = len;
|
2018-01-08 12:44:10 +00:00
|
|
|
|
|
|
|
if (!closed)
|
|
|
|
{
|
|
|
|
// Terminate open paths with caps.
|
|
|
|
if (i == 0)
|
|
|
|
volume.push_quad(idx_a[BOTTOM], idx_a[RIGHT], idx_a[TOP], idx_a[LEFT]);
|
|
|
|
|
|
|
|
// We don't use 'else' because both cases are true if we have only one line.
|
|
|
|
if (i + 1 == lines.size())
|
|
|
|
volume.push_quad(idx_b[BOTTOM], idx_b[LEFT], idx_b[TOP], idx_b[RIGHT]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add quads for a straight hollow tube-like segment.
|
|
|
|
// bottom-right face
|
|
|
|
volume.push_quad(idx_a[BOTTOM], idx_b[BOTTOM], idx_b[RIGHT], idx_a[RIGHT]);
|
|
|
|
// top-right face
|
|
|
|
volume.push_quad(idx_a[RIGHT], idx_b[RIGHT], idx_b[TOP], idx_a[TOP]);
|
|
|
|
// top-left face
|
|
|
|
volume.push_quad(idx_a[TOP], idx_b[TOP], idx_b[LEFT], idx_a[LEFT]);
|
|
|
|
// bottom-left face
|
|
|
|
volume.push_quad(idx_a[LEFT], idx_b[LEFT], idx_b[BOTTOM], idx_a[BOTTOM]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef LEFT
|
|
|
|
#undef RIGHT
|
|
|
|
#undef TOP
|
|
|
|
#undef BOTTOM
|
|
|
|
}
|
2018-01-08 15:05:01 +00:00
|
|
|
|
2018-08-21 20:14:47 +00:00
|
|
|
static void point_to_indexed_vertex_array(const Vec3crd& point,
|
2018-01-08 15:05:01 +00:00
|
|
|
double width,
|
|
|
|
double height,
|
|
|
|
GLIndexedVertexArray& volume)
|
|
|
|
{
|
|
|
|
// builds a double piramid, with vertices on the local axes, around the point
|
|
|
|
|
2018-08-21 15:43:05 +00:00
|
|
|
Vec3d center = unscale(point);
|
2018-01-08 15:05:01 +00:00
|
|
|
|
|
|
|
double scale_factor = 1.0;
|
|
|
|
double w = scale_factor * width;
|
|
|
|
double h = scale_factor * height;
|
|
|
|
|
|
|
|
// new vertices ids
|
|
|
|
int idx_last = int(volume.vertices_and_normals_interleaved.size() / 6);
|
|
|
|
int idxs[6];
|
|
|
|
for (int i = 0; i < 6; ++i)
|
|
|
|
{
|
|
|
|
idxs[i] = idx_last + i;
|
|
|
|
}
|
|
|
|
|
2018-08-21 15:43:05 +00:00
|
|
|
Vec3d displacement_x(w, 0.0, 0.0);
|
|
|
|
Vec3d displacement_y(0.0, w, 0.0);
|
|
|
|
Vec3d displacement_z(0.0, 0.0, h);
|
2018-01-08 15:05:01 +00:00
|
|
|
|
2018-08-21 15:43:05 +00:00
|
|
|
Vec3d unit_x(1.0, 0.0, 0.0);
|
|
|
|
Vec3d unit_y(0.0, 1.0, 0.0);
|
|
|
|
Vec3d unit_z(0.0, 0.0, 1.0);
|
2018-01-08 15:05:01 +00:00
|
|
|
|
|
|
|
// vertices
|
|
|
|
volume.push_geometry(center - displacement_x, -unit_x); // idxs[0]
|
|
|
|
volume.push_geometry(center + displacement_x, unit_x); // idxs[1]
|
|
|
|
volume.push_geometry(center - displacement_y, -unit_y); // idxs[2]
|
|
|
|
volume.push_geometry(center + displacement_y, unit_y); // idxs[3]
|
|
|
|
volume.push_geometry(center - displacement_z, -unit_z); // idxs[4]
|
|
|
|
volume.push_geometry(center + displacement_z, unit_z); // idxs[5]
|
|
|
|
|
|
|
|
// top piramid faces
|
|
|
|
volume.push_triangle(idxs[0], idxs[2], idxs[5]);
|
|
|
|
volume.push_triangle(idxs[2], idxs[1], idxs[5]);
|
|
|
|
volume.push_triangle(idxs[1], idxs[3], idxs[5]);
|
|
|
|
volume.push_triangle(idxs[3], idxs[0], idxs[5]);
|
|
|
|
|
|
|
|
// bottom piramid faces
|
|
|
|
volume.push_triangle(idxs[2], idxs[0], idxs[4]);
|
|
|
|
volume.push_triangle(idxs[1], idxs[2], idxs[4]);
|
|
|
|
volume.push_triangle(idxs[3], idxs[1], idxs[4]);
|
|
|
|
volume.push_triangle(idxs[0], idxs[3], idxs[4]);
|
|
|
|
}
|
2018-01-08 12:44:10 +00:00
|
|
|
|
2018-06-05 12:09:36 +00:00
|
|
|
void _3DScene::thick_lines_to_verts(
|
|
|
|
const Lines &lines,
|
2017-03-14 09:11:08 +00:00
|
|
|
const std::vector<double> &widths,
|
|
|
|
const std::vector<double> &heights,
|
|
|
|
bool closed,
|
|
|
|
double top_z,
|
|
|
|
GLVolume &volume)
|
|
|
|
{
|
2017-03-15 15:33:25 +00:00
|
|
|
thick_lines_to_indexed_vertex_array(lines, widths, heights, closed, top_z, volume.indexed_vertex_array);
|
2015-01-17 23:36:21 +00:00
|
|
|
}
|
|
|
|
|
2018-06-05 12:09:36 +00:00
|
|
|
void _3DScene::thick_lines_to_verts(const Lines3& lines,
|
2018-01-08 12:44:10 +00:00
|
|
|
const std::vector<double>& widths,
|
|
|
|
const std::vector<double>& heights,
|
|
|
|
bool closed,
|
|
|
|
GLVolume& volume)
|
|
|
|
{
|
|
|
|
thick_lines_to_indexed_vertex_array(lines, widths, heights, closed, volume.indexed_vertex_array);
|
|
|
|
}
|
2018-01-08 15:05:01 +00:00
|
|
|
|
2018-08-21 20:14:47 +00:00
|
|
|
static void thick_point_to_verts(const Vec3crd& point,
|
2018-01-08 15:05:01 +00:00
|
|
|
double width,
|
|
|
|
double height,
|
|
|
|
GLVolume& volume)
|
|
|
|
{
|
|
|
|
point_to_indexed_vertex_array(point, width, height, volume.indexed_vertex_array);
|
|
|
|
}
|
2018-01-10 12:43:00 +00:00
|
|
|
|
2019-09-30 14:25:26 +00:00
|
|
|
void _3DScene::extrusionentity_to_verts(const Polyline &polyline, float width, float height, float print_z, GLVolume& volume)
|
|
|
|
{
|
|
|
|
if (polyline.size() >= 2) {
|
|
|
|
size_t num_segments = polyline.size() - 1;
|
|
|
|
thick_lines_to_verts(polyline.lines(), std::vector<double>(num_segments, width), std::vector<double>(num_segments, height), false, print_z, volume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 12:43:00 +00:00
|
|
|
// Fill in the qverts and tverts with quads and triangles for the extrusion_path.
|
2018-06-05 08:56:55 +00:00
|
|
|
void _3DScene::extrusionentity_to_verts(const ExtrusionPath &extrusion_path, float print_z, GLVolume &volume)
|
2018-01-10 12:43:00 +00:00
|
|
|
{
|
2019-09-30 14:25:26 +00:00
|
|
|
extrusionentity_to_verts(extrusion_path.polyline, extrusion_path.width, extrusion_path.height, print_z, volume);
|
2018-01-10 12:43:00 +00:00
|
|
|
}
|
2018-01-08 12:44:10 +00:00
|
|
|
|
2017-03-13 15:02:17 +00:00
|
|
|
// Fill in the qverts and tverts with quads and triangles for the extrusion_path.
|
2018-06-05 08:56:55 +00:00
|
|
|
void _3DScene::extrusionentity_to_verts(const ExtrusionPath &extrusion_path, float print_z, const Point ©, GLVolume &volume)
|
2015-01-24 22:35:29 +00:00
|
|
|
{
|
2017-03-13 15:02:17 +00:00
|
|
|
Polyline polyline = extrusion_path.polyline;
|
|
|
|
polyline.remove_duplicate_points();
|
|
|
|
polyline.translate(copy);
|
|
|
|
Lines lines = polyline.lines();
|
|
|
|
std::vector<double> widths(lines.size(), extrusion_path.width);
|
|
|
|
std::vector<double> heights(lines.size(), extrusion_path.height);
|
|
|
|
thick_lines_to_verts(lines, widths, heights, false, print_z, volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in the qverts and tverts with quads and triangles for the extrusion_loop.
|
2018-06-05 08:56:55 +00:00
|
|
|
void _3DScene::extrusionentity_to_verts(const ExtrusionLoop &extrusion_loop, float print_z, const Point ©, GLVolume &volume)
|
2017-03-13 15:02:17 +00:00
|
|
|
{
|
|
|
|
Lines lines;
|
|
|
|
std::vector<double> widths;
|
|
|
|
std::vector<double> heights;
|
|
|
|
for (const ExtrusionPath &extrusion_path : extrusion_loop.paths) {
|
|
|
|
Polyline polyline = extrusion_path.polyline;
|
|
|
|
polyline.remove_duplicate_points();
|
|
|
|
polyline.translate(copy);
|
|
|
|
Lines lines_this = polyline.lines();
|
|
|
|
append(lines, lines_this);
|
|
|
|
widths.insert(widths.end(), lines_this.size(), extrusion_path.width);
|
|
|
|
heights.insert(heights.end(), lines_this.size(), extrusion_path.height);
|
|
|
|
}
|
|
|
|
thick_lines_to_verts(lines, widths, heights, true, print_z, volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in the qverts and tverts with quads and triangles for the extrusion_multi_path.
|
2018-06-05 08:56:55 +00:00
|
|
|
void _3DScene::extrusionentity_to_verts(const ExtrusionMultiPath &extrusion_multi_path, float print_z, const Point ©, GLVolume &volume)
|
2017-03-13 15:02:17 +00:00
|
|
|
{
|
|
|
|
Lines lines;
|
|
|
|
std::vector<double> widths;
|
|
|
|
std::vector<double> heights;
|
|
|
|
for (const ExtrusionPath &extrusion_path : extrusion_multi_path.paths) {
|
|
|
|
Polyline polyline = extrusion_path.polyline;
|
|
|
|
polyline.remove_duplicate_points();
|
|
|
|
polyline.translate(copy);
|
|
|
|
Lines lines_this = polyline.lines();
|
|
|
|
append(lines, lines_this);
|
|
|
|
widths.insert(widths.end(), lines_this.size(), extrusion_path.width);
|
|
|
|
heights.insert(heights.end(), lines_this.size(), extrusion_path.height);
|
|
|
|
}
|
|
|
|
thick_lines_to_verts(lines, widths, heights, false, print_z, volume);
|
|
|
|
}
|
|
|
|
|
2018-06-05 08:56:55 +00:00
|
|
|
void _3DScene::extrusionentity_to_verts(const ExtrusionEntityCollection &extrusion_entity_collection, float print_z, const Point ©, GLVolume &volume)
|
2017-03-13 15:02:17 +00:00
|
|
|
{
|
|
|
|
for (const ExtrusionEntity *extrusion_entity : extrusion_entity_collection.entities)
|
|
|
|
extrusionentity_to_verts(extrusion_entity, print_z, copy, volume);
|
|
|
|
}
|
|
|
|
|
2018-06-05 08:56:55 +00:00
|
|
|
void _3DScene::extrusionentity_to_verts(const ExtrusionEntity *extrusion_entity, float print_z, const Point ©, GLVolume &volume)
|
2017-03-13 15:02:17 +00:00
|
|
|
{
|
|
|
|
if (extrusion_entity != nullptr) {
|
|
|
|
auto *extrusion_path = dynamic_cast<const ExtrusionPath*>(extrusion_entity);
|
|
|
|
if (extrusion_path != nullptr)
|
|
|
|
extrusionentity_to_verts(*extrusion_path, print_z, copy, volume);
|
|
|
|
else {
|
|
|
|
auto *extrusion_loop = dynamic_cast<const ExtrusionLoop*>(extrusion_entity);
|
|
|
|
if (extrusion_loop != nullptr)
|
|
|
|
extrusionentity_to_verts(*extrusion_loop, print_z, copy, volume);
|
|
|
|
else {
|
|
|
|
auto *extrusion_multi_path = dynamic_cast<const ExtrusionMultiPath*>(extrusion_entity);
|
|
|
|
if (extrusion_multi_path != nullptr)
|
|
|
|
extrusionentity_to_verts(*extrusion_multi_path, print_z, copy, volume);
|
|
|
|
else {
|
|
|
|
auto *extrusion_entity_collection = dynamic_cast<const ExtrusionEntityCollection*>(extrusion_entity);
|
|
|
|
if (extrusion_entity_collection != nullptr)
|
|
|
|
extrusionentity_to_verts(*extrusion_entity_collection, print_z, copy, volume);
|
|
|
|
else {
|
2018-09-18 08:09:58 +00:00
|
|
|
throw std::runtime_error("Unexpected extrusion_entity type in to_verts()");
|
2017-03-13 15:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-24 22:35:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-05 08:56:55 +00:00
|
|
|
void _3DScene::polyline3_to_verts(const Polyline3& polyline, double width, double height, GLVolume& volume)
|
2018-01-08 12:44:10 +00:00
|
|
|
{
|
|
|
|
Lines3 lines = polyline.lines();
|
|
|
|
std::vector<double> widths(lines.size(), width);
|
|
|
|
std::vector<double> heights(lines.size(), height);
|
|
|
|
thick_lines_to_verts(lines, widths, heights, false, volume);
|
|
|
|
}
|
2018-01-08 15:05:01 +00:00
|
|
|
|
2018-08-21 20:14:47 +00:00
|
|
|
void _3DScene::point3_to_verts(const Vec3crd& point, double width, double height, GLVolume& volume)
|
2018-01-08 15:05:01 +00:00
|
|
|
{
|
2018-01-10 12:43:00 +00:00
|
|
|
thick_point_to_verts(point, width, height, volume);
|
2018-01-08 15:05:01 +00:00
|
|
|
}
|
2018-01-11 13:09:54 +00:00
|
|
|
|
2018-05-09 08:47:04 +00:00
|
|
|
GUI::GLCanvas3DManager _3DScene::s_canvas_mgr;
|
2018-03-09 09:40:42 +00:00
|
|
|
|
2018-12-19 13:44:37 +00:00
|
|
|
GLModel::GLModel()
|
2019-07-01 10:28:16 +00:00
|
|
|
: m_filename("")
|
2018-12-19 13:44:37 +00:00
|
|
|
{
|
2018-12-20 10:14:53 +00:00
|
|
|
m_volume.shader_outside_printer_detection_enabled = false;
|
2018-12-19 13:44:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLModel::~GLModel()
|
|
|
|
{
|
2019-01-04 11:56:48 +00:00
|
|
|
reset();
|
2018-12-19 13:44:37 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 10:14:53 +00:00
|
|
|
void GLModel::set_color(const float* color, unsigned int size)
|
2018-12-19 13:44:37 +00:00
|
|
|
{
|
2019-01-04 11:56:48 +00:00
|
|
|
::memcpy((void*)m_volume.color, (const void*)color, (size_t)(std::min((unsigned int)4, size) * sizeof(float)));
|
2018-12-19 13:44:37 +00:00
|
|
|
m_volume.set_render_color(color, size);
|
|
|
|
}
|
|
|
|
|
2018-12-20 10:14:53 +00:00
|
|
|
const Vec3d& GLModel::get_offset() const
|
|
|
|
{
|
|
|
|
return m_volume.get_volume_offset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLModel::set_offset(const Vec3d& offset)
|
|
|
|
{
|
|
|
|
m_volume.set_volume_offset(offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vec3d& GLModel::get_rotation() const
|
|
|
|
{
|
|
|
|
return m_volume.get_volume_rotation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLModel::set_rotation(const Vec3d& rotation)
|
|
|
|
{
|
|
|
|
m_volume.set_volume_rotation(rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vec3d& GLModel::get_scale() const
|
|
|
|
{
|
|
|
|
return m_volume.get_volume_scaling_factor();
|
|
|
|
}
|
|
|
|
|
2018-12-19 13:44:37 +00:00
|
|
|
void GLModel::set_scale(const Vec3d& scale)
|
|
|
|
{
|
|
|
|
m_volume.set_volume_scaling_factor(scale);
|
|
|
|
}
|
|
|
|
|
2019-01-04 11:56:48 +00:00
|
|
|
void GLModel::reset()
|
|
|
|
{
|
2019-07-09 06:24:23 +00:00
|
|
|
m_volume.indexed_vertex_array.release_geometry();
|
2019-01-04 11:56:48 +00:00
|
|
|
m_filename = "";
|
|
|
|
}
|
|
|
|
|
2018-12-19 13:44:37 +00:00
|
|
|
void GLModel::render() const
|
|
|
|
{
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glEnable(GL_BLEND));
|
|
|
|
glsafe(::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
|
2018-12-19 13:44:37 +00:00
|
|
|
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glCullFace(GL_BACK));
|
|
|
|
glsafe(::glEnableClientState(GL_VERTEX_ARRAY));
|
|
|
|
glsafe(::glEnableClientState(GL_NORMAL_ARRAY));
|
2018-12-19 13:44:37 +00:00
|
|
|
|
|
|
|
GLint current_program_id;
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glGetIntegerv(GL_CURRENT_PROGRAM, ¤t_program_id));
|
2019-03-27 13:42:09 +00:00
|
|
|
GLint color_id = (current_program_id > 0) ? ::glGetUniformLocation(current_program_id, "uniform_color") : -1;
|
|
|
|
glcheck();
|
2019-07-01 10:28:16 +00:00
|
|
|
|
|
|
|
m_volume.render(color_id, -1, -1);
|
2018-12-19 13:44:37 +00:00
|
|
|
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0));
|
|
|
|
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
|
2018-12-19 13:44:37 +00:00
|
|
|
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glDisableClientState(GL_VERTEX_ARRAY));
|
|
|
|
glsafe(::glDisableClientState(GL_NORMAL_ARRAY));
|
2018-12-19 13:44:37 +00:00
|
|
|
|
2019-01-31 08:37:27 +00:00
|
|
|
glsafe(::glDisable(GL_BLEND));
|
2018-12-19 13:44:37 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 10:28:16 +00:00
|
|
|
bool GLArrow::on_init()
|
2018-12-19 13:44:37 +00:00
|
|
|
{
|
|
|
|
Pointf3s vertices;
|
|
|
|
std::vector<Vec3crd> triangles;
|
|
|
|
|
2018-12-20 10:14:53 +00:00
|
|
|
// bottom face
|
2018-12-19 13:44:37 +00:00
|
|
|
vertices.emplace_back(0.5, 0.0, -0.1);
|
|
|
|
vertices.emplace_back(0.5, 2.0, -0.1);
|
|
|
|
vertices.emplace_back(1.0, 2.0, -0.1);
|
|
|
|
vertices.emplace_back(0.0, 3.0, -0.1);
|
|
|
|
vertices.emplace_back(-1.0, 2.0, -0.1);
|
|
|
|
vertices.emplace_back(-0.5, 2.0, -0.1);
|
|
|
|
vertices.emplace_back(-0.5, 0.0, -0.1);
|
|
|
|
|
2018-12-20 10:14:53 +00:00
|
|
|
// top face
|
2018-12-19 13:44:37 +00:00
|
|
|
vertices.emplace_back(0.5, 0.0, 0.1);
|
|
|
|
vertices.emplace_back(0.5, 2.0, 0.1);
|
|
|
|
vertices.emplace_back(1.0, 2.0, 0.1);
|
|
|
|
vertices.emplace_back(0.0, 3.0, 0.1);
|
|
|
|
vertices.emplace_back(-1.0, 2.0, 0.1);
|
|
|
|
vertices.emplace_back(-0.5, 2.0, 0.1);
|
|
|
|
vertices.emplace_back(-0.5, 0.0, 0.1);
|
|
|
|
|
|
|
|
// bottom face
|
|
|
|
triangles.emplace_back(0, 6, 1);
|
|
|
|
triangles.emplace_back(6, 5, 1);
|
|
|
|
triangles.emplace_back(5, 4, 3);
|
|
|
|
triangles.emplace_back(5, 3, 1);
|
|
|
|
triangles.emplace_back(1, 3, 2);
|
|
|
|
|
|
|
|
// top face
|
|
|
|
triangles.emplace_back(7, 8, 13);
|
|
|
|
triangles.emplace_back(13, 8, 12);
|
|
|
|
triangles.emplace_back(12, 10, 11);
|
|
|
|
triangles.emplace_back(8, 10, 12);
|
|
|
|
triangles.emplace_back(8, 9, 10);
|
|
|
|
|
|
|
|
// side face
|
|
|
|
triangles.emplace_back(0, 1, 8);
|
|
|
|
triangles.emplace_back(8, 7, 0);
|
|
|
|
triangles.emplace_back(1, 2, 9);
|
|
|
|
triangles.emplace_back(9, 8, 1);
|
|
|
|
triangles.emplace_back(2, 3, 10);
|
|
|
|
triangles.emplace_back(10, 9, 2);
|
|
|
|
triangles.emplace_back(3, 4, 11);
|
|
|
|
triangles.emplace_back(11, 10, 3);
|
|
|
|
triangles.emplace_back(4, 5, 12);
|
|
|
|
triangles.emplace_back(12, 11, 4);
|
|
|
|
triangles.emplace_back(5, 6, 13);
|
|
|
|
triangles.emplace_back(13, 12, 5);
|
|
|
|
triangles.emplace_back(6, 0, 7);
|
|
|
|
triangles.emplace_back(7, 13, 6);
|
|
|
|
|
2019-07-01 10:28:16 +00:00
|
|
|
m_volume.indexed_vertex_array.load_mesh(TriangleMesh(vertices, triangles));
|
2019-08-05 12:30:32 +00:00
|
|
|
m_volume.indexed_vertex_array.finalize_geometry(true);
|
2018-12-19 13:44:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-12-20 10:14:53 +00:00
|
|
|
|
|
|
|
GLCurvedArrow::GLCurvedArrow(unsigned int resolution)
|
|
|
|
: GLModel()
|
|
|
|
, m_resolution(resolution)
|
|
|
|
{
|
|
|
|
if (m_resolution == 0)
|
|
|
|
m_resolution = 1;
|
|
|
|
}
|
|
|
|
|
2019-07-01 10:28:16 +00:00
|
|
|
bool GLCurvedArrow::on_init()
|
2018-12-20 10:14:53 +00:00
|
|
|
{
|
|
|
|
Pointf3s vertices;
|
|
|
|
std::vector<Vec3crd> triangles;
|
|
|
|
|
|
|
|
double ext_radius = 2.5;
|
|
|
|
double int_radius = 1.5;
|
|
|
|
double step = 0.5 * (double)PI / (double)m_resolution;
|
|
|
|
|
|
|
|
unsigned int vertices_per_level = 4 + 2 * m_resolution;
|
|
|
|
|
|
|
|
// bottom face
|
|
|
|
vertices.emplace_back(0.0, 1.5, -0.1);
|
|
|
|
vertices.emplace_back(0.0, 1.0, -0.1);
|
|
|
|
vertices.emplace_back(-1.0, 2.0, -0.1);
|
|
|
|
vertices.emplace_back(0.0, 3.0, -0.1);
|
|
|
|
vertices.emplace_back(0.0, 2.5, -0.1);
|
|
|
|
|
|
|
|
for (unsigned int i = 1; i <= m_resolution; ++i)
|
|
|
|
{
|
|
|
|
double angle = (double)i * step;
|
|
|
|
double x = ext_radius * ::sin(angle);
|
|
|
|
double y = ext_radius * ::cos(angle);
|
|
|
|
|
|
|
|
vertices.emplace_back(x, y, -0.1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < m_resolution; ++i)
|
|
|
|
{
|
|
|
|
double angle = (double)i * step;
|
|
|
|
double x = int_radius * ::cos(angle);
|
|
|
|
double y = int_radius * ::sin(angle);
|
|
|
|
|
|
|
|
vertices.emplace_back(x, y, -0.1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// top face
|
|
|
|
vertices.emplace_back(0.0, 1.5, 0.1);
|
|
|
|
vertices.emplace_back(0.0, 1.0, 0.1);
|
|
|
|
vertices.emplace_back(-1.0, 2.0, 0.1);
|
|
|
|
vertices.emplace_back(0.0, 3.0, 0.1);
|
|
|
|
vertices.emplace_back(0.0, 2.5, 0.1);
|
|
|
|
|
|
|
|
for (unsigned int i = 1; i <= m_resolution; ++i)
|
|
|
|
{
|
|
|
|
double angle = (double)i * step;
|
|
|
|
double x = ext_radius * ::sin(angle);
|
|
|
|
double y = ext_radius * ::cos(angle);
|
|
|
|
|
|
|
|
vertices.emplace_back(x, y, 0.1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < m_resolution; ++i)
|
|
|
|
{
|
|
|
|
double angle = (double)i * step;
|
|
|
|
double x = int_radius * ::cos(angle);
|
|
|
|
double y = int_radius * ::sin(angle);
|
|
|
|
|
|
|
|
vertices.emplace_back(x, y, 0.1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// bottom face
|
|
|
|
triangles.emplace_back(0, 1, 2);
|
|
|
|
triangles.emplace_back(0, 2, 4);
|
|
|
|
triangles.emplace_back(4, 2, 3);
|
|
|
|
|
|
|
|
int first_id = 4;
|
|
|
|
int last_id = (int)vertices_per_level;
|
|
|
|
triangles.emplace_back(last_id, 0, first_id);
|
|
|
|
triangles.emplace_back(last_id, first_id, first_id + 1);
|
|
|
|
for (unsigned int i = 1; i < m_resolution; ++i)
|
|
|
|
{
|
|
|
|
triangles.emplace_back(last_id - i, last_id - i + 1, first_id + i);
|
|
|
|
triangles.emplace_back(last_id - i, first_id + i, first_id + i + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// top face
|
|
|
|
last_id += 1;
|
|
|
|
triangles.emplace_back(last_id + 0, last_id + 2, last_id + 1);
|
|
|
|
triangles.emplace_back(last_id + 0, last_id + 4, last_id + 2);
|
|
|
|
triangles.emplace_back(last_id + 4, last_id + 3, last_id + 2);
|
|
|
|
|
|
|
|
first_id = last_id + 4;
|
|
|
|
last_id = last_id + 4 + 2 * (int)m_resolution;
|
|
|
|
triangles.emplace_back(last_id, first_id, (int)vertices_per_level + 1);
|
|
|
|
triangles.emplace_back(last_id, first_id + 1, first_id);
|
|
|
|
for (unsigned int i = 1; i < m_resolution; ++i)
|
|
|
|
{
|
|
|
|
triangles.emplace_back(last_id - i, first_id + i, last_id - i + 1);
|
|
|
|
triangles.emplace_back(last_id - i, first_id + i + 1, first_id + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// side face
|
2018-12-22 09:02:42 +00:00
|
|
|
for (unsigned int i = 0; i < 4 + 2 * (unsigned int)m_resolution; ++i)
|
2018-12-20 10:14:53 +00:00
|
|
|
{
|
|
|
|
triangles.emplace_back(i, vertices_per_level + 2 + i, i + 1);
|
|
|
|
triangles.emplace_back(i, vertices_per_level + 1 + i, vertices_per_level + 2 + i);
|
|
|
|
}
|
|
|
|
triangles.emplace_back(vertices_per_level, vertices_per_level + 1, 0);
|
|
|
|
triangles.emplace_back(vertices_per_level, 2 * vertices_per_level + 1, vertices_per_level + 1);
|
|
|
|
|
2019-07-01 10:28:16 +00:00
|
|
|
m_volume.indexed_vertex_array.load_mesh(TriangleMesh(vertices, triangles));
|
2019-08-05 12:30:32 +00:00
|
|
|
m_volume.indexed_vertex_array.finalize_geometry(true);
|
2018-12-20 10:14:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-12-19 13:44:37 +00:00
|
|
|
|
2019-07-01 10:28:16 +00:00
|
|
|
bool GLBed::on_init_from_file(const std::string& filename)
|
2019-01-04 11:56:48 +00:00
|
|
|
{
|
|
|
|
reset();
|
|
|
|
|
|
|
|
if (!boost::filesystem::exists(filename))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!boost::algorithm::iends_with(filename, ".stl"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Model model;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
model = Model::read_from_file(filename);
|
|
|
|
}
|
2019-01-24 18:08:58 +00:00
|
|
|
catch (std::exception & /* ex */)
|
2019-01-04 11:56:48 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_filename = filename;
|
|
|
|
|
2019-07-25 06:43:21 +00:00
|
|
|
m_volume.indexed_vertex_array.load_mesh(model.mesh());
|
2019-08-05 12:30:32 +00:00
|
|
|
m_volume.indexed_vertex_array.finalize_geometry(true);
|
2019-01-04 11:56:48 +00:00
|
|
|
|
2019-02-08 15:45:03 +00:00
|
|
|
float color[4] = { 0.235f, 0.235f, 0.235f, 1.0f };
|
2019-01-04 11:56:48 +00:00
|
|
|
set_color(color, 4);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-04 08:14:09 +00:00
|
|
|
std::string _3DScene::get_gl_info(bool format_as_html, bool extensions)
|
2018-03-09 09:40:42 +00:00
|
|
|
{
|
2019-05-23 11:49:57 +00:00
|
|
|
return Slic3r::GUI::GLCanvas3DManager::get_gl_info().to_string(format_as_html, extensions);
|
2018-03-09 09:40:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 10:49:00 +00:00
|
|
|
bool _3DScene::add_canvas(wxGLCanvas* canvas, GUI::Bed3D& bed, GUI::Camera& camera, GUI::GLToolbar& view_toolbar)
|
2018-01-16 13:59:06 +00:00
|
|
|
{
|
2019-03-07 10:49:00 +00:00
|
|
|
return s_canvas_mgr.add(canvas, bed, camera, view_toolbar);
|
2018-01-16 13:59:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-09 08:47:04 +00:00
|
|
|
bool _3DScene::remove_canvas(wxGLCanvas* canvas)
|
|
|
|
{
|
|
|
|
return s_canvas_mgr.remove(canvas);
|
2017-03-16 13:02:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-09 08:47:04 +00:00
|
|
|
void _3DScene::remove_all_canvases()
|
2017-05-24 13:20:20 +00:00
|
|
|
{
|
2018-05-09 08:47:04 +00:00
|
|
|
s_canvas_mgr.remove_all();
|
2017-05-24 13:20:20 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 11:15:28 +00:00
|
|
|
bool _3DScene::init(wxGLCanvas* canvas)
|
2017-05-24 13:20:20 +00:00
|
|
|
{
|
2018-06-04 11:15:28 +00:00
|
|
|
return s_canvas_mgr.init(canvas);
|
2017-05-24 13:20:20 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 18:02:25 +00:00
|
|
|
void _3DScene::destroy()
|
|
|
|
{
|
|
|
|
s_canvas_mgr.destroy();
|
|
|
|
}
|
|
|
|
|
2018-10-11 08:24:19 +00:00
|
|
|
GUI::GLCanvas3D* _3DScene::get_canvas(wxGLCanvas* canvas)
|
|
|
|
{
|
|
|
|
return s_canvas_mgr.get_canvas(canvas);
|
|
|
|
}
|
2018-03-09 09:40:42 +00:00
|
|
|
|
2018-03-22 12:37:01 +00:00
|
|
|
} // namespace Slic3r
|