3DScene cutting plane moved to c++

This commit is contained in:
Enrico Turri 2018-05-18 11:05:48 +02:00
parent 6c6b8ccc42
commit 1e0a8de5b1
8 changed files with 196 additions and 58 deletions

View file

@ -1808,6 +1808,11 @@ BoundingBoxf3 _3DScene::get_max_bounding_box(wxGLCanvas* canvas)
return s_canvas_mgr.get_max_bounding_box(canvas);
}
void _3DScene::set_cutting_plane(wxGLCanvas* canvas, float z, const ExPolygons& polygons)
{
return s_canvas_mgr.set_cutting_plane(canvas, z, polygons);
}
unsigned int _3DScene::get_camera_type(wxGLCanvas* canvas)
{
return s_canvas_mgr.get_camera_type(canvas);
@ -1888,9 +1893,14 @@ void _3DScene::select_view(wxGLCanvas* canvas, const std::string& direction)
s_canvas_mgr.select_view(canvas, direction);
}
void _3DScene::render(wxGLCanvas* canvas)
void _3DScene::render_bed(wxGLCanvas* canvas)
{
s_canvas_mgr.render(canvas);
s_canvas_mgr.render_bed(canvas);
}
void _3DScene::render_cutting_plane(wxGLCanvas* canvas)
{
s_canvas_mgr.render_cutting_plane(canvas);
}
void _3DScene::register_on_viewport_changed_callback(wxGLCanvas* canvas, void* callback)

View file

@ -562,6 +562,8 @@ public:
static BoundingBoxf3 get_volumes_bounding_box(wxGLCanvas* canvas);
static BoundingBoxf3 get_max_bounding_box(wxGLCanvas* canvas);
static void set_cutting_plane(wxGLCanvas* canvas, float z, const ExPolygons& polygons);
static unsigned int get_camera_type(wxGLCanvas* canvas);
static void set_camera_type(wxGLCanvas* canvas, unsigned int type);
static std::string get_camera_type_as_string(wxGLCanvas* canvas);
@ -585,7 +587,8 @@ public:
static void zoom_to_volumes(wxGLCanvas* canvas);
static void select_view(wxGLCanvas* canvas, const std::string& direction);
static void render(wxGLCanvas* canvas);
static void render_bed(wxGLCanvas* canvas);
static void render_cutting_plane(wxGLCanvas* canvas);
static void register_on_viewport_changed_callback(wxGLCanvas* canvas, void* callback);

View file

@ -230,8 +230,7 @@ void GLCanvas3D::Bed::render()
::glColor4f(0.8f, 0.6f, 0.5f, 0.4f);
::glNormal3d(0.0f, 0.0f, 1.0f);
::glVertexPointer(3, GL_FLOAT, 0, (GLvoid*)m_triangles.get_data());
::glDrawArrays(GL_TRIANGLES, 0, triangles_vcount);
// ::glDisableClientState(GL_VERTEX_ARRAY);
::glDrawArrays(GL_TRIANGLES, 0, (GLsizei)triangles_vcount);
// we need depth test for grid, otherwise it would disappear when looking
// the object from below
@ -242,9 +241,8 @@ void GLCanvas3D::Bed::render()
::glLineWidth(3.0f);
::glColor4f(0.2f, 0.2f, 0.2f, 0.4f);
// ::glEnableClientState(GL_VERTEX_ARRAY);
::glVertexPointer(3, GL_FLOAT, 0, (GLvoid*)m_gridlines.get_data());
::glDrawArrays(GL_LINES, 0, gridlines_vcount);
::glDrawArrays(GL_LINES, 0, (GLsizei)gridlines_vcount);
::glDisableClientState(GL_VERTEX_ARRAY);
@ -299,6 +297,66 @@ void GLCanvas3D::Bed::_calc_gridlines(const ExPolygon& poly, const BoundingBox&
printf("Unable to create bed grid lines\n");
}
GLCanvas3D::CuttingPlane::CuttingPlane()
: m_z(-1.0f)
{
}
bool GLCanvas3D::CuttingPlane::set(float z, const ExPolygons& polygons)
{
m_z = z;
// grow slices in order to display them better
ExPolygons expolygons = offset_ex(polygons, scale_(0.1));
Lines lines = to_lines(expolygons);
return m_lines.set_from_lines(lines, m_z);
}
void GLCanvas3D::CuttingPlane::render_plane(const BoundingBoxf3& bb)
{
if (m_z >= 0.0f)
{
::glDisable(GL_CULL_FACE);
::glDisable(GL_LIGHTING);
::glEnable(GL_BLEND);
::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
float margin = 20.0f;
float min_x = bb.min.x - margin;
float max_x = bb.max.x + margin;
float min_y = bb.min.y - margin;
float max_y = bb.max.y + margin;
::glBegin(GL_QUADS);
::glColor4f(0.8f, 0.8f, 0.8f, 0.5f);
::glVertex3f(min_x, min_y, m_z);
::glVertex3f(max_x, min_y, m_z);
::glVertex3f(max_x, max_y, m_z);
::glVertex3f(min_x, max_y, m_z);
::glEnd();
::glEnable(GL_CULL_FACE);
::glDisable(GL_BLEND);
}
}
void GLCanvas3D::CuttingPlane::render_contour()
{
::glEnableClientState(GL_VERTEX_ARRAY);
if (m_z >= 0.0f)
{
unsigned int lines_vcount = m_lines.get_data_size() / 3;
::glLineWidth(2.0f);
::glColor3f(0.0f, 0.0f, 0.0f);
::glVertexPointer(3, GL_FLOAT, 0, (GLvoid*)m_lines.get_data());
::glDrawArrays(GL_LINES, 0, (GLsizei)lines_vcount);
}
::glDisableClientState(GL_VERTEX_ARRAY);
}
GLCanvas3D::GLCanvas3D(wxGLCanvas* canvas, wxGLContext* context)
: m_canvas(canvas)
, m_context(context)
@ -446,6 +504,11 @@ void GLCanvas3D::set_bed_origin(const Pointf& origin)
m_bed.set_origin(origin);
}
void GLCanvas3D::set_cutting_plane(float z, const ExPolygons& polygons)
{
m_cutting_plane.set(z, polygons);
}
GLCanvas3D::Camera::EType GLCanvas3D::get_camera_type() const
{
return m_camera.get_type();
@ -580,11 +643,17 @@ void GLCanvas3D::select_view(const std::string& direction)
}
}
void GLCanvas3D::render()
void GLCanvas3D::render_bed()
{
m_bed.render();
}
void GLCanvas3D::render_cutting_plane()
{
m_cutting_plane.render_plane(volumes_bounding_box());
m_cutting_plane.render_contour();
}
void GLCanvas3D::register_on_viewport_changed_callback(void* callback)
{
if (callback != nullptr)

View file

@ -3,6 +3,7 @@
#include "../../libslic3r/BoundingBox.hpp"
#include "../../libslic3r/Utils.hpp"
#include "../../libslic3r/ExPolygon.hpp"
class wxGLCanvas;
class wxGLContext;
@ -100,11 +101,26 @@ public:
void _calc_gridlines(const ExPolygon& poly, const BoundingBox& bed_bbox);
};
class CuttingPlane
{
float m_z;
GeometryBuffer m_lines;
public:
CuttingPlane();
bool set(float z, const ExPolygons& polygons);
void render_plane(const BoundingBoxf3& bb);
void render_contour();
};
private:
wxGLCanvas* m_canvas;
wxGLContext* m_context;
Camera m_camera;
Bed m_bed;
CuttingPlane m_cutting_plane;
GLVolumeCollection* m_volumes;
@ -140,6 +156,8 @@ public:
const Pointf& get_bed_origin() const;
void set_bed_origin(const Pointf& origin);
void set_cutting_plane(float z, const ExPolygons& polygons);
Camera::EType get_camera_type() const;
void set_camera_type(Camera::EType type);
std::string get_camera_type_as_string() const;
@ -167,7 +185,8 @@ public:
void zoom_to_volumes();
void select_view(const std::string& direction);
void render();
void render_bed();
void render_cutting_plane();
void register_on_viewport_changed_callback(void* callback);

View file

@ -232,6 +232,13 @@ BoundingBoxf3 GLCanvas3DManager::get_max_bounding_box(wxGLCanvas* canvas)
return (it != m_canvases.end()) ? it->second->max_bounding_box() : BoundingBoxf3();
}
void GLCanvas3DManager::set_cutting_plane(wxGLCanvas* canvas, float z, const ExPolygons& polygons)
{
CanvasesMap::iterator it = _get_canvas(canvas);
if (it != m_canvases.end())
it->second->set_cutting_plane(z, polygons);
}
unsigned int GLCanvas3DManager::get_camera_type(wxGLCanvas* canvas) const
{
CanvasesMap::const_iterator it = _get_canvas(canvas);
@ -343,11 +350,18 @@ void GLCanvas3DManager::select_view(wxGLCanvas* canvas, const std::string& direc
it->second->select_view(direction);
}
void GLCanvas3DManager::render(wxGLCanvas* canvas)
void GLCanvas3DManager::render_bed(wxGLCanvas* canvas)
{
CanvasesMap::iterator it = _get_canvas(canvas);
if (it != m_canvases.end())
it->second->render();
it->second->render_bed();
}
void GLCanvas3DManager::render_cutting_plane(wxGLCanvas* canvas)
{
CanvasesMap::iterator it = _get_canvas(canvas);
if (it != m_canvases.end())
it->second->render_cutting_plane();
}
void GLCanvas3DManager::register_on_viewport_changed_callback(wxGLCanvas* canvas, void* callback)

View file

@ -1,7 +1,7 @@
#ifndef slic3r_GLCanvas3DManager_hpp_
#define slic3r_GLCanvas3DManager_hpp_
#include "GLCanvas3D.hpp"
#include "../../slic3r/GUI/GLCanvas3D.hpp"
#include <map>
@ -72,6 +72,8 @@ public:
BoundingBoxf3 get_volumes_bounding_box(wxGLCanvas* canvas);
BoundingBoxf3 get_max_bounding_box(wxGLCanvas* canvas);
void set_cutting_plane(wxGLCanvas* canvas, float z, const ExPolygons& polygons);
unsigned int get_camera_type(wxGLCanvas* canvas) const;
void set_camera_type(wxGLCanvas* canvas, unsigned int type);
std::string get_camera_type_as_string(wxGLCanvas* canvas) const;
@ -95,7 +97,8 @@ public:
void zoom_to_volumes(wxGLCanvas* canvas);
void select_view(wxGLCanvas* canvas, const std::string& direction);
void render(wxGLCanvas* canvas);
void render_bed(wxGLCanvas* canvas);
void render_cutting_plane(wxGLCanvas* canvas);
void register_on_viewport_changed_callback(wxGLCanvas* canvas, void* callback);