PrusaSlicer-NonPlainar/xs/src/slic3r/GUI/GLGizmo.cpp

167 lines
4.0 KiB
C++
Raw Normal View History

2018-06-13 07:12:16 +00:00
#include "GLGizmo.hpp"
2018-06-13 07:26:58 +00:00
#include "../../libslic3r/Utils.hpp"
2018-06-13 13:44:04 +00:00
#include "../../libslic3r/BoundingBox.hpp"
#include <GL/glew.h>
2018-06-13 07:12:16 +00:00
#include <iostream>
namespace Slic3r {
namespace GUI {
GLGizmoBase::GLGizmoBase()
: m_state(Off)
{
}
GLGizmoBase::~GLGizmoBase()
{
}
2018-06-13 13:44:04 +00:00
bool GLGizmoBase::init()
{
return on_init();
}
2018-06-13 07:12:16 +00:00
GLGizmoBase::EState GLGizmoBase::get_state() const
{
return m_state;
}
2018-06-13 08:49:59 +00:00
void GLGizmoBase::set_state(GLGizmoBase::EState state)
2018-06-13 07:12:16 +00:00
{
2018-06-13 08:49:59 +00:00
m_state = state;
2018-06-13 07:12:16 +00:00
}
2018-06-13 08:49:59 +00:00
unsigned int GLGizmoBase::get_textures_id() const
2018-06-13 07:12:16 +00:00
{
2018-06-13 08:49:59 +00:00
return m_textures[m_state].get_id();
2018-06-13 07:12:16 +00:00
}
2018-06-13 08:49:59 +00:00
int GLGizmoBase::get_textures_size() const
2018-06-13 07:12:16 +00:00
{
return m_textures[Off].get_width();
}
2018-06-13 13:44:04 +00:00
void GLGizmoBase::render(const BoundingBoxf3& box) const
2018-06-13 07:12:16 +00:00
{
2018-06-13 13:44:04 +00:00
on_render(box);
2018-06-13 07:12:16 +00:00
}
GLGizmoRotate::GLGizmoRotate()
: GLGizmoBase()
, m_angle_x(0.0f)
, m_angle_y(0.0f)
, m_angle_z(0.0f)
{
}
bool GLGizmoRotate::on_init()
{
std::string path = resources_dir() + "/icons/overlay/";
std::string filename = path + "rotate_off.png";
if (!m_textures[Off].load_from_file(filename))
return false;
filename = path + "rotate_hover.png";
if (!m_textures[Hover].load_from_file(filename))
return false;
filename = path + "rotate_on.png";
if (!m_textures[On].load_from_file(filename))
return false;
return true;
}
2018-06-13 13:44:04 +00:00
void GLGizmoRotate::on_render(const BoundingBoxf3& box) const
{
std::cout << "GLGizmoRotate::render()" << std::endl;
}
const float GLGizmoScale::Offset = 5.0f;
const float GLGizmoScale::SquareHalfSize = 2.0f;
2018-06-13 07:12:16 +00:00
GLGizmoScale::GLGizmoScale()
: GLGizmoBase()
, m_scale_x(1.0f)
, m_scale_y(1.0f)
, m_scale_z(1.0f)
{
}
bool GLGizmoScale::on_init()
{
std::string path = resources_dir() + "/icons/overlay/";
std::string filename = path + "scale_off.png";
if (!m_textures[Off].load_from_file(filename))
return false;
filename = path + "scale_hover.png";
if (!m_textures[Hover].load_from_file(filename))
return false;
filename = path + "scale_on.png";
if (!m_textures[On].load_from_file(filename))
return false;
return true;
}
2018-06-13 13:44:04 +00:00
void GLGizmoScale::on_render(const BoundingBoxf3& box) const
{
::glDisable(GL_LIGHTING);
::glDisable(GL_DEPTH_TEST);
const Pointf3& size = box.size();
const Pointf3& center = box.center();
Pointf3 half_scaled_size = 0.5 * Pointf3((coordf_t)m_scale_x * size.x, (coordf_t)m_scale_y * size.y, (coordf_t)m_scale_z * size.z);
coordf_t min_x = center.x - half_scaled_size.x - (coordf_t)Offset;
coordf_t max_x = center.x + half_scaled_size.x + (coordf_t)Offset;
coordf_t min_y = center.y - half_scaled_size.y - (coordf_t)Offset;
coordf_t max_y = center.y + half_scaled_size.y + (coordf_t)Offset;
::glLineWidth(2.0f);
::glBegin(GL_LINE_LOOP);
// draw outline
::glColor3f(1.0f, 1.0f, 1.0f);
::glVertex3f((GLfloat)min_x, (GLfloat)min_y, 0.0f);
::glVertex3f((GLfloat)max_x, (GLfloat)min_y, 0.0f);
::glVertex3f((GLfloat)max_x, (GLfloat)max_y, 0.0f);
::glVertex3f((GLfloat)min_x, (GLfloat)max_y, 0.0f);
::glEnd();
// draw grabbers
::glColor3f(1.0f, 0.38f, 0.0f);
::glDisable(GL_CULL_FACE);
_render_square(Pointf3(min_x, min_y, 0.0));
_render_square(Pointf3(max_x, min_y, 0.0));
_render_square(Pointf3(max_x, max_y, 0.0));
_render_square(Pointf3(min_x, max_y, 0.0));
::glEnable(GL_CULL_FACE);
}
void GLGizmoScale::_render_square(const Pointf3& center) const
{
float min_x = (float)center.x - SquareHalfSize;
float max_x = (float)center.x + SquareHalfSize;
float min_y = (float)center.y - SquareHalfSize;
float max_y = (float)center.y + SquareHalfSize;
::glBegin(GL_TRIANGLES);
::glVertex3f((GLfloat)min_x, (GLfloat)min_y, 0.0f);
::glVertex3f((GLfloat)max_x, (GLfloat)min_y, 0.0f);
::glVertex3f((GLfloat)max_x, (GLfloat)max_y, 0.0f);
::glVertex3f((GLfloat)max_x, (GLfloat)max_y, 0.0f);
::glVertex3f((GLfloat)min_x, (GLfloat)max_y, 0.0f);
::glVertex3f((GLfloat)min_x, (GLfloat)min_y, 0.0f);
::glEnd();
}
2018-06-13 07:12:16 +00:00
} // namespace GUI
} // namespace Slic3r