Trying to add history to the Fill tool

This commit is contained in:
2017-07-15 10:51:58 +01:00
parent 4666a3dffc
commit 4e765f1450
6 changed files with 112 additions and 17 deletions

View File

@@ -46,8 +46,37 @@ glm::mat4 ui::Canvas::m_plane_transform[6] = {
void ui::Canvas::clear(const glm::vec4& c/*={0,0,0,1}*/)
{
snap_history({ 0, 1, 2, 3, 4, 5 });
m_layers[m_current_layer_idx].clear(c);
}
void ui::Canvas::snap_history(const std::vector<int>& planes)
{
auto action = new ActionStroke;
for (auto i : planes)
{
if (!m_dirty_face[i])
continue; // no stroke on this face, skip it
m_layers[m_current_layer_idx].m_rtt[i].bindFramebuffer();
// save image before commit
glm::vec2 box_sz = m_dirty_box[i].zw() - m_dirty_box[i].xy();
action->m_image[i] = std::make_unique<uint8_t[]>(box_sz.x * box_sz.y * 4);
glReadPixels(m_dirty_box[i].x, m_dirty_box[i].y, box_sz.x, box_sz.y, GL_RGBA, GL_UNSIGNED_BYTE, action->m_image[i].get());
action->m_box[i] = m_dirty_box[i];
action->m_old_box[i] = m_layers[m_current_layer_idx].m_dirty_box[i];
action->m_old_dirty[i] = m_layers[m_current_layer_idx].m_dirty_face[i];
m_layers[m_current_layer_idx].m_rtt[i].unbindFramebuffer();
}
// save history
action->m_layer_idx = m_current_layer_idx;
action->m_canvas = this;
action->m_stroke = std::move(m_current_stroke);
action->clear_layer = true;
ActionManager::add(action);
}
void ui::Canvas::stroke_end()
{
if (!m_current_stroke)
@@ -268,7 +297,7 @@ bool ui::Canvas::point_trace(glm::vec2 loc, glm::vec3& ray_origin, glm::vec3& ra
return false;
}
bool ui::Canvas::point_trace_plane(glm::vec2 loc, glm::vec3& ray_origin, glm::vec3& ray_dir,
glm::vec3& hit_pos, glm::vec3& hit_normal, int plane_id)
glm::vec3& hit_pos, glm::vec3& hit_normal, glm::vec2& hit_fb_pos, int plane_id)
{
point_unproject(loc, { 0, 0, m_box.zw }, m_mv, m_proj, ray_origin, ray_dir);
glm::vec3 hit;
@@ -277,8 +306,11 @@ bool ui::Canvas::point_trace_plane(glm::vec2 loc, glm::vec3& ray_origin, glm::ve
m_plane_normal[plane_id], m_plane_tangent[plane_id], hit))
{
glm::mat4 plane_camera = glm::lookAt(m_plane_origin[plane_id], m_plane_normal[plane_id], m_plane_tangent[plane_id]);
glm::vec4 plane_local = plane_camera * glm::vec4(hit, 1);
hit_pos = hit;
hit_normal = m_plane_normal[plane_id];
hit_fb_pos.x = -(plane_local.x * 0.5f - 0.5f);
hit_fb_pos.y = (plane_local.y * 0.5f + 0.5f);
return true;
}
return false;
@@ -330,6 +362,7 @@ void ui::Canvas::stroke_commit()
for (int i = 0; i < 6; i++)
{
m_dirty_box[i] = glm::vec4(0, 0, m_width, m_height); // reset bounding box
if (!m_dirty_face[i])
continue; // no stroke on this face, skip it
@@ -1067,14 +1100,24 @@ void ui::Layer::clear(const glm::vec4& c)
glGetFloatv(GL_COLOR_CLEAR_VALUE, cc);
glClearColor(c.r, c.g, c.b, c.a);
bool erase = (c.a == 0.f);
for (int i = 0; i < 6; i++)
{
m_rtt[i].bindFramebuffer();
glClear(GL_COLOR_BUFFER_BIT);
m_rtt[i].unbindFramebuffer();
m_dirty_box[i] = glm::vec4(w, h, 0, 0); // reset bounding box
m_dirty_face[i] = false;
if (erase)
{
m_dirty_box[i] = glm::vec4(w, h, 0, 0); // reset bounding box
m_dirty_face[i] = false;
}
else
{
m_dirty_box[i] = glm::vec4(0, 0, w, h); // reset bounding box
m_dirty_face[i] = true;
}
}
// restore clear color state