ENABLE_SPLITTED_VERTEX_BUFFER - fixed bugs in export of toolpaths to obj files and in index buffer splitting

This commit is contained in:
enricoturri1966 2021-01-14 11:38:03 +01:00
parent bda43e4482
commit 8bdfb6bbce

View File

@ -793,13 +793,13 @@ void GCodeViewer::export_toolpaths_to_obj(const char* filename) const
}
fprintf(fp, "# G-Code Toolpaths Materials\n");
fprintf(fp, "# Generated by %s based on Slic3r\n", SLIC3R_BUILD_ID);
fprintf(fp, "# Generated by %s-%s based on Slic3r\n", SLIC3R_APP_NAME, SLIC3R_VERSION);
unsigned int colors_count = 1;
for (const Color& color : colors) {
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, "Kd %g %g %g\n", color[0], color[1], color[2]);
fprintf(fp, "Ks 0 0 0\n");
}
@ -813,7 +813,7 @@ void GCodeViewer::export_toolpaths_to_obj(const char* filename) const
}
fprintf(fp, "# G-Code Toolpaths\n");
fprintf(fp, "# Generated by %s based on Slic3r\n", SLIC3R_BUILD_ID);
fprintf(fp, "# Generated by %s-%s based on Slic3r\n", SLIC3R_APP_NAME, SLIC3R_VERSION);
fprintf(fp, "\nmtllib ./%s\n", mat_filename.filename().string().c_str());
#if ENABLE_SPLITTED_VERTEX_BUFFER
@ -821,8 +821,14 @@ void GCodeViewer::export_toolpaths_to_obj(const char* filename) const
std::vector<Vec3f> out_vertices;
std::vector<Vec3f> out_normals;
std::vector<size_t> vertices_offsets;
vertices_offsets.push_back(0);
struct VerticesOffset
{
unsigned int vbo;
size_t offset;
};
std::vector<VerticesOffset> vertices_offsets;
vertices_offsets.push_back({ t_buffer.vertices.vbos.front(), 0 });
// get vertices/normals data from vertex buffers on gpu
for (size_t i = 0; i < t_buffer.vertices.vbos.size(); ++i) {
@ -837,7 +843,9 @@ void GCodeViewer::export_toolpaths_to_obj(const char* filename) const
out_vertices.push_back({ vertices[base + 0], vertices[base + 1], vertices[base + 2] });
out_normals.push_back({ vertices[base + 3], vertices[base + 4], vertices[base + 5] });
}
vertices_offsets.push_back(vertices_offsets.back() + vertices_count);
if (i < t_buffer.vertices.vbos.size() - 1)
vertices_offsets.push_back({ t_buffer.vertices.vbos[i + 1], vertices_offsets.back().offset + vertices_count });
}
// save vertices to file
@ -863,16 +871,23 @@ void GCodeViewer::export_toolpaths_to_obj(const char* filename) const
continue;
const IBuffer& ibuffer = t_buffer.indices[render_path.index_buffer_id];
const size_t vertices_offset = vertices_offsets[render_path.index_buffer_id];
size_t vertices_offset = 0;
for (size_t j = 0; j < vertices_offsets.size(); ++j) {
const VerticesOffset& offset = vertices_offsets[j];
if (offset.vbo == ibuffer.vbo) {
vertices_offset = offset.offset;
break;
}
}
// get indices data from index buffer on gpu
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibuffer.ibo));
for (size_t j = 0; j < render_path.sizes.size(); ++j) {
const size_t triangles_count = render_path.sizes[j] / 3;
IndexBuffer indices(render_path.sizes[j]);
glsafe(::glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, static_cast<GLintptr>(render_path.offsets[j]),
static_cast<GLsizeiptr>(render_path.sizes[j] * sizeof(unsigned int)), static_cast<void*>(indices.data())));
const size_t triangles_count = render_path.sizes[j] / 3;
for (size_t k = 0; k < triangles_count; ++k) {
const size_t base = k * 3;
const size_t v1 = 1 + static_cast<size_t>(indices[base + 0]) + vertices_offset;
@ -1676,6 +1691,11 @@ void GCodeViewer::load_toolpaths(const GCodeProcessor::Result& gcode_result)
if (i_multibuffer.back().size() * sizeof(unsigned int) >= IBUFFER_THRESHOLD_BYTES - t_buffer.indices_per_segment_size_bytes()) {
i_multibuffer.push_back(IndexBuffer());
vbo_index_list.push_back(t_buffer.vertices.vbos[curr_vertex_buffer.first]);
if (t_buffer.render_primitive_type != TBuffer::ERenderPrimitiveType::Point) {
Path& last_path = t_buffer.paths.back();
--last_path.sub_paths.back().last.s_id;
last_path.add_sub_path(prev, static_cast<unsigned int>(i_multibuffer.size()) - 1, 0, i - 1);
}
}
// if adding the vertices for the current segment exceeds the threshold size of the current vertex buffer
@ -1689,6 +1709,7 @@ void GCodeViewer::load_toolpaths(const GCodeProcessor::Result& gcode_result)
if (t_buffer.render_primitive_type != TBuffer::ERenderPrimitiveType::Point) {
Path& last_path = t_buffer.paths.back();
--last_path.sub_paths.back().last.s_id;
last_path.add_sub_path(prev, static_cast<unsigned int>(i_multibuffer.size()) - 1, 0, i - 1);
}
}
@ -1728,10 +1749,6 @@ void GCodeViewer::load_toolpaths(const GCodeProcessor::Result& gcode_result)
size_t size_elements = i_buffer.size();
size_t size_bytes = size_elements * sizeof(unsigned int);
if (size_elements == 0) {
continue;
}
// stores index buffer informations into TBuffer
t_buffer.indices.push_back(IBuffer());
IBuffer& ibuf = t_buffer.indices.back();
@ -2777,6 +2794,7 @@ void GCodeViewer::refresh_render_paths(bool keep_sequential_current_first, bool
RenderPath key{ color, static_cast<unsigned int>(ibuffer_id), path_id };
if (render_path == nullptr || !RenderPathPropertyEqual()(*render_path, key))
render_path = const_cast<RenderPath*>(&(*buffer->render_paths.emplace(key).first));
unsigned int size_in_indices = 0;
switch (buffer->render_primitive_type)
{
@ -2791,7 +2809,6 @@ void GCodeViewer::refresh_render_paths(bool keep_sequential_current_first, bool
break;
}
}
render_path->sizes.push_back(size_in_indices);
unsigned int delta_1st = 0;