code cleanup and improve brush direction
This commit is contained in:
142
src/canvas.cpp
142
src/canvas.cpp
@@ -210,18 +210,18 @@ void Canvas::stroke_cancel()
|
||||
}
|
||||
void Canvas::stroke_draw_mix(const glm::vec2& bb_min, const glm::vec2& bb_sz)
|
||||
{
|
||||
gl_state gl;
|
||||
gl.save();
|
||||
|
||||
m_mixer.bindFramebuffer();
|
||||
|
||||
float zoom = m_node->root()->m_zoom;
|
||||
glViewport(0, 0, m_mixer.getWidth(), m_mixer.getHeight());
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glEnable(GL_SCISSOR_TEST);
|
||||
glDisable(GL_BLEND);
|
||||
|
||||
glScissor(bb_min.x, bb_min.y, bb_sz.x, bb_sz.y);
|
||||
|
||||
m_mixer.clear({ 1, 1, 1, 0 });
|
||||
m_sampler.bind(0);
|
||||
m_sampler_linear.bind(1);
|
||||
auto layer_index = m_current_layer_idx;
|
||||
for (int plane_index = 0; plane_index < 6; plane_index++)
|
||||
{
|
||||
@@ -237,36 +237,6 @@ void Canvas::stroke_draw_mix(const glm::vec2& bb_min, const glm::vec2& bb_sz)
|
||||
m_plane_transform[plane_index] *
|
||||
glm::translate(glm::vec3(0, 0, -1));
|
||||
|
||||
ShaderManager::use(kShader::TextureAlphaSep);
|
||||
ShaderManager::u_int(kShaderUniform::Tex, 0);
|
||||
ShaderManager::u_int(kShaderUniform::TexA, 1);
|
||||
ShaderManager::u_float(kShaderUniform::Alpha, m_layers[layer_index].m_opacity);
|
||||
ShaderManager::u_int(kShaderUniform::Highlight, false);
|
||||
ShaderManager::u_mat4(kShaderUniform::MVP, plane_mvp_z);
|
||||
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_layers[layer_index].m_rtt[plane_index].bindTexture();
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
m_layers[layer_index].m_rtt[plane_index].bindTexture();
|
||||
m_node->m_face_plane.draw_fill();
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
m_layers[layer_index].m_rtt[plane_index].unbindTexture();
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_layers[layer_index].m_rtt[plane_index].unbindTexture();
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
// glActiveTexture(GL_TEXTURE0);
|
||||
// m_tmp[plane_index].bindTexture();
|
||||
// glActiveTexture(GL_TEXTURE1);
|
||||
// m_tmp[plane_index].bindTexture();
|
||||
// m_node->m_face_plane.draw_fill();
|
||||
// glActiveTexture(GL_TEXTURE1);
|
||||
// m_tmp[plane_index].unbindTexture();
|
||||
// glActiveTexture(GL_TEXTURE0);
|
||||
// m_tmp[plane_index].unbindTexture();
|
||||
|
||||
m_sampler.bind(0);
|
||||
m_sampler.bind(1);
|
||||
m_sampler.bind(2);
|
||||
@@ -279,8 +249,8 @@ void Canvas::stroke_draw_mix(const glm::vec2& bb_min, const glm::vec2& bb_sz)
|
||||
//ShaderManager::u_int(kShaderUniform::TexStencil, 3);
|
||||
ShaderManager::u_float(kShaderUniform::StrokeAlpha, m_current_stroke->m_brush->m_tip_opacity);
|
||||
ShaderManager::u_float(kShaderUniform::Alpha, 1);
|
||||
ShaderManager::u_int(kShaderUniform::Lock, m_layers[layer_index].m_alpha_locked);
|
||||
ShaderManager::u_int(kShaderUniform::Mask, m_smask_active);
|
||||
ShaderManager::u_int(kShaderUniform::Lock, false/*m_layers[layer_index].m_alpha_locked*/);
|
||||
ShaderManager::u_int(kShaderUniform::Mask, false/*m_smask_active*/);
|
||||
ShaderManager::u_int(kShaderUniform::UseFragCoordUV2, false);
|
||||
ShaderManager::u_int(kShaderUniform::BlendMode, m_current_stroke->m_brush->m_blend_mode);
|
||||
ShaderManager::u_mat4(kShaderUniform::MVP, plane_mvp_z);
|
||||
@@ -290,15 +260,7 @@ void Canvas::stroke_draw_mix(const glm::vec2& bb_min, const glm::vec2& bb_sz)
|
||||
m_tmp[plane_index].bindTexture();
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
m_smask.m_rtt[plane_index].bindTexture();
|
||||
glActiveTexture(GL_TEXTURE3);
|
||||
if (m_current_stroke->m_brush->m_stencil_texture)
|
||||
m_current_stroke->m_brush->m_stencil_texture->bind();
|
||||
else
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
m_node->m_face_plane.draw_fill();
|
||||
if (m_current_stroke->m_brush->m_stencil_texture)
|
||||
m_current_stroke->m_brush->m_stencil_texture->unbind();
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
m_smask.m_rtt[plane_index].unbindTexture();
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
m_tmp[plane_index].unbindTexture();
|
||||
@@ -307,7 +269,8 @@ void Canvas::stroke_draw_mix(const glm::vec2& bb_min, const glm::vec2& bb_sz)
|
||||
}
|
||||
m_sampler.unbind();
|
||||
m_mixer.unbindFramebuffer();
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
|
||||
gl.restore();
|
||||
}
|
||||
void Canvas::stroke_draw()
|
||||
{
|
||||
@@ -386,7 +349,6 @@ void Canvas::stroke_draw()
|
||||
m_mixer_idle = false;
|
||||
}
|
||||
|
||||
static glm::vec2 UV2[4];
|
||||
glm::vec2 dx_mix(m_mixer_sample.size * 0.5f, 0), dy_mix(0, m_mixer_sample.size * 0.5f);
|
||||
glm::vec2 off_mix[4] = {
|
||||
-dx_mix - dy_mix, // A - bottom-left
|
||||
@@ -402,60 +364,43 @@ void Canvas::stroke_draw()
|
||||
+dx + dy, // C - top-right
|
||||
+dx - dy, // D - bottom-right
|
||||
};
|
||||
auto sz = glm::vec2(m_mixer.getWidth(), m_mixer.getHeight()) * m_mixer_scale;
|
||||
glm::vec2 bb_min(sz);
|
||||
glm::vec2 bb_max(0, 0);
|
||||
|
||||
glm::vec2 mixer_sz(m_mixer.getWidth(), m_mixer.getHeight());
|
||||
glm::vec2 mixer_bb_min(mixer_sz);
|
||||
glm::vec2 mixer_bb_max(0, 0);
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
auto p = (xy(m_mixer_sample.pos) + off_mix[j] * glm::orientate2(-s.angle) + glm::vec2(0, 1));
|
||||
UV2[j] = p / sz;
|
||||
bb_min = glm::max({ 0, 0 }, glm::min(bb_min, p));
|
||||
bb_max = glm::min(sz, glm::max(bb_max, p));
|
||||
auto p = (xy(m_mixer_sample.pos) + off_mix[j] * glm::orientate2(-s.angle) + glm::vec2(0, 1));
|
||||
mixer_bb_min = glm::max({ 0, 0 }, glm::min(mixer_bb_min, p));
|
||||
mixer_bb_max = glm::min(mixer_sz, glm::max(mixer_bb_max, p));
|
||||
|
||||
B[j].pos = glm::vec4(xy(s.pos) + off[j] * glm::orientate2(-s.angle), 1, 1);
|
||||
B[j].uvs2 = UV2[j];
|
||||
B[j].uvs2 = p / mixer_sz;
|
||||
}
|
||||
auto bb_sz = bb_max - bb_min;
|
||||
|
||||
if (m_brush->m_tip_mix > 0.f)
|
||||
{
|
||||
stroke_draw_mix(bb_min, bb_sz);
|
||||
|
||||
glViewport(0, 0, m_width, m_height);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
tex.bind();
|
||||
m_sampler_brush.bind(0);
|
||||
m_sampler_bg.bind(1);
|
||||
m_sampler_stencil.bind(2);
|
||||
m_sampler.bind(3);
|
||||
//m_sampler_linear.bind(5);
|
||||
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
if (m_brush->m_stencil_texture)
|
||||
m_brush->m_stencil_texture->bind();
|
||||
else
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glActiveTexture(GL_TEXTURE3);
|
||||
m_mixer.bindTexture();
|
||||
glDisable(GL_BLEND);
|
||||
stroke_draw_mix(mixer_bb_min, mixer_bb_max - mixer_bb_min);
|
||||
}
|
||||
|
||||
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(xyz(forward), m_plane_normal[i]);
|
||||
// TODO: use better threshold than 0.3
|
||||
// some trigonometric shit, tangent and stuff
|
||||
// if (dot < -0.3f)
|
||||
// continue;
|
||||
if (dot < -0.3f)
|
||||
continue;
|
||||
*/
|
||||
|
||||
int intersected = 0;
|
||||
int inside = 0;
|
||||
|
||||
// intersect P with the current face to clip diverging points from the plane
|
||||
auto P = poly_intersect(B, m_plane_shape[i]);
|
||||
|
||||
glm::mat4 plane_camera = glm::lookAt(m_plane_origin[i], m_plane_normal[i], m_plane_tangent[i]);
|
||||
for (int j = 0; j < P.size(); j++)
|
||||
{
|
||||
glm::vec3 ray_origin, ray_dir;
|
||||
@@ -481,12 +426,8 @@ void Canvas::stroke_draw()
|
||||
float hit_t;
|
||||
if (ray_intersect(ray_origin, ray_dir, m_plane_origin[i], m_plane_normal[i], m_plane_tangent[i], hit, hit_t))
|
||||
{
|
||||
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].uvs2 = xy(P[j].pos) / glm::vec2(App::I.width, App::I.height);
|
||||
P[j].pos.x = -(plane_local.x * 0.5f - 0.5f) * m_width;
|
||||
P[j].pos.y = (plane_local.y * 0.5f + 0.5f) * m_height;
|
||||
@@ -510,7 +451,7 @@ void Canvas::stroke_draw()
|
||||
}
|
||||
}
|
||||
|
||||
if (intersected < 3 || inside == 0)
|
||||
if (intersected < 3)
|
||||
continue;
|
||||
|
||||
m_dirty_face[i] = true;
|
||||
@@ -541,10 +482,8 @@ void Canvas::stroke_draw()
|
||||
tex_pos.x, tex_pos.y, tex_sz.x, tex_sz.y);
|
||||
}
|
||||
|
||||
m_dirty_box[i] = glm::vec4(
|
||||
glm::min(xy(m_dirty_box[i]), (glm::vec2)tex_pos),
|
||||
glm::max(zw(m_dirty_box[i]), (glm::vec2)(tex_pos + tex_sz))
|
||||
);
|
||||
m_dirty_box[i] = glm::vec4(glm::min(xy(m_dirty_box[i]), (glm::vec2)tex_pos),
|
||||
glm::max(zw(m_dirty_box[i]), (glm::vec2)(tex_pos + tex_sz)));
|
||||
|
||||
ShaderManager::use(kShader::Stroke);
|
||||
ShaderManager::u_mat4(kShaderUniform::MVP, ortho_proj);
|
||||
@@ -573,33 +512,6 @@ void Canvas::stroke_draw()
|
||||
}
|
||||
m_brush_shape.draw_fill();
|
||||
|
||||
/*
|
||||
// draw sample wireframe
|
||||
std::vector<vertex_t> lines;
|
||||
for (int vi = 0; vi < P.size(); vi += 3)
|
||||
{
|
||||
auto a = P[vi];
|
||||
auto b = P[(vi + 1) % P.size()];
|
||||
auto c = P[(vi + 2) % P.size()];
|
||||
a.pos.z = b.pos.z = c.pos.z = 0;
|
||||
|
||||
lines.push_back(a);
|
||||
lines.push_back(b);
|
||||
|
||||
lines.push_back(b);
|
||||
lines.push_back(c);
|
||||
|
||||
lines.push_back(c);
|
||||
lines.push_back(a);
|
||||
}
|
||||
|
||||
ShaderManager::use(kShader::Color);
|
||||
ShaderManager::u_vec4(kShaderUniform::Col, { s.col, 1 });
|
||||
ShaderManager::u_mat4(kShaderUniform::MVP, ortho_proj);
|
||||
m_brush_shape.update_vertices(lines.data(), lines.size());
|
||||
m_brush_shape.draw_stroke();
|
||||
*/
|
||||
|
||||
if (!ShaderManager::ext_framebuffer_fetch)
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
|
||||
Reference in New Issue
Block a user