fix brush projection to eliminate seams, still problems with big brushes and need to implement erase

This commit is contained in:
2017-08-13 16:59:58 +01:00
parent 060e08a891
commit 90ee185dcd
7 changed files with 123 additions and 90 deletions

View File

@@ -213,9 +213,17 @@ void ui::Canvas::stroke_draw()
m_sampler.bind(0);
m_sampler_bg.bind(1);
m_sampler_mask.bind(2);
for (int i = 0; i < 6; i++)
{
// check if plane is even visible
glm::vec4 forward = m_mv * glm::vec4(0, 0, 1, 1);
float dot = glm::dot(forward.xyz(), m_plane_normal[i]);
// TODO: use better threshold than 0.3
// some trigonometric shit, tangent and stuff
// if (dot < -0.3f)
// continue;
m_tmp[i].bindFramebuffer();
glActiveTexture(GL_TEXTURE1);
@@ -232,82 +240,59 @@ void ui::Canvas::stroke_draw()
else
{
glDisable(GL_BLEND);
// if (0 && m_state == kCanvasMode::Erase)
// {
// ShaderManager::use(ui::kShader::StrokeErase);
// //ShaderManager::u_vec4(kShaderUniform::Col, m_brush.m_tip_color);
// }
// else if(m_layers[m_current_layer_idx].m_alpha_locked)
// {
// ShaderManager::use(kShader::StrokeLock);
// ShaderManager::u_vec4(kShaderUniform::Col, m_brush.m_tip_color);
// ShaderManager::u_int(kShaderUniform::TexMask, 2); // alpha mask
// glActiveTexture(GL_TEXTURE2);
// m_layers[m_current_layer_idx].m_rtt[i].bindTexture();
// glActiveTexture(GL_TEXTURE1);
// }
// else
{
ShaderManager::use(ui::kShader::Stroke);
ShaderManager::u_vec4(kShaderUniform::Col, m_brush.m_tip_color);
}
ShaderManager::use(ui::kShader::Stroke);
ShaderManager::u_int(kShaderUniform::Tex, 0); // brush
ShaderManager::u_int(kShaderUniform::TexBG, 1); // bg
ShaderManager::u_vec4(kShaderUniform::Col, m_brush.m_tip_color);
ShaderManager::u_vec2(kShaderUniform::Resolution, { m_width, m_height });
for (const auto& s : samples)
{
glm::vec3 ray_origin, ray_dir;
point_unproject(s.pos, { 0, 0, m_box.zw }, m_mv, m_proj, ray_origin, ray_dir);
glm::vec3 hit;
glm::vec2 fb_pos;
if (ray_intersect(ray_origin, ray_dir, m_plane_origin[i], m_plane_normal[i], m_plane_tangent[i], hit))
glm::vec2 dx(s.size, 0), dy(0, s.size);
glm::vec2 off[4] = {
- dx - dy, // A - bottom-left
- dx + dy, // B - top-left
+ dx + dy, // C - top-right
+ dx - dy, // D - bottom-right
};
static glm::vec4 P[4];
int intersected = 0;
int inside = 0;
for (int j = 0; j < 4; j++)
{
glm::mat4 plane_camera = glm::lookAt(m_plane_origin[i], m_plane_normal[i], m_plane_tangent[i]);
glm::vec4 plane_local = plane_camera * glm::vec4(hit, 1);
if (glm::abs(plane_local.x) < 1.5f && glm::abs(plane_local.y) < 1.5f)
glm::vec3 ray_origin, ray_dir;
point_unproject(s.pos + off[j], { 0, 0, m_box.zw }, m_mv, m_proj, ray_origin, ray_dir);
glm::vec3 hit;
if (ray_intersect(ray_origin, ray_dir, m_plane_origin[i], m_plane_normal[i], m_plane_tangent[i], hit))
{
fb_pos.x = -(plane_local.x * 0.5f - 0.5f) * m_width;
fb_pos.y = (plane_local.y * 0.5f + 0.5f) * m_height;
//LOG("draw %f %f", fb_pos.x, fb_pos.y);
glm::mat4 plane_camera = glm::lookAt(m_plane_origin[i], m_plane_normal[i], m_plane_tangent[i]);
glm::vec4 plane_local = plane_camera * glm::vec4(hit, 1);
if (glm::abs(plane_local.x) < 1.5f && glm::abs(plane_local.y) < 1.5f)
{
inside++;
}
P[j].x = -(plane_local.x * 0.5f - 0.5f) * m_width;
P[j].y = (plane_local.y * 0.5f + 0.5f) * m_height;
intersected++;
}
else
{
continue;
// if (i==0)
// LOG("no intersection with plane %d", i);
break;
}
}
else
{
if (intersected < 4 || inside == 0)
continue;
}
m_dirty_face[i] = true;
auto mvp = ortho_proj *
glm::translate(glm::vec3(fb_pos, 0)) *
glm::scale(glm::vec3(s.size, s.size, 1)) *
glm::eulerAngleZ(s.angle);
glm::vec4 P[4] {
mvp * glm::vec4(glm::vec2(-.5f, -.5f), 0, 1.f), // A - bottom-left
mvp * glm::vec4(glm::vec2(-.5f, +.5f), 0, 1.f), // B - top-left
mvp * glm::vec4(glm::vec2(+.5f, +.5f), 0, 1.f), // C - top-right
mvp * glm::vec4(glm::vec2(+.5f, -.5f), 0, 1.f), // D - bottom-right
};
auto mvp_inv = glm::inverse(ortho_proj);
glm::vec4 P2[4]{
mvp_inv * P[0],
mvp_inv * P[1],
mvp_inv * P[2],
mvp_inv * P[3],
};
glm::vec2 bb_min(m_width, m_height);
glm::vec2 bb_max(0, 0);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
bb_min = glm::max({ 0, 0 }, glm::min(bb_min, P2[i].xy()));
bb_max = glm::min({ m_width, m_height }, glm::max(bb_max, P2[i].xy()));
bb_min = glm::max({ 0, 0 }, glm::min(bb_min, P[j].xy()));
bb_max = glm::min({ m_width, m_height }, glm::max(bb_max, P[j].xy()));
}
auto bb_sz = bb_max - bb_min;
@@ -324,9 +309,9 @@ void ui::Canvas::stroke_draw()
m_dirty_box[i].xy = glm::min(m_dirty_box[i].xy(), (glm::vec2)tex_pos);
m_dirty_box[i].zw = glm::max(m_dirty_box[i].zw(), (glm::vec2)(tex_pos + tex_sz));
ShaderManager::u_mat4(kShaderUniform::MVP, mvp);
ShaderManager::u_mat4(kShaderUniform::MVP, ortho_proj);
ShaderManager::u_float(kShaderUniform::Alpha, s.flow);
//m_plane_brush.update_vertices(P);
m_plane_brush.update_vertices(P);
m_plane_brush.draw_fill();
}
}