Files
panopainter/engine/canvas.cpp

376 lines
13 KiB
C++

#include "pch.h"
#include "log.h"
#include "canvas.h"
glm::vec3 ui::Canvas::m_plane_origin[6] = {
{ 0, 0,-1}, // front
{ 1, 0, 0}, // right
{ 0, 0, 1}, // back
{-1, 0, 0}, // left
{ 0, 1, 0}, // top
{ 0,-1, 0}, // bottom
};
glm::vec3 ui::Canvas::m_plane_normal[6] = {
{ 0, 0, 1}, // front
{-1, 0, 0}, // right
{ 0, 0,-1}, // back
{ 1, 0, 0}, // left
{ 0,-1, 0}, // top
{ 0, 1, 0}, // bottom
};
glm::vec3 ui::Canvas::m_plane_tangent[6] = {
{0, 1, 0}, // front
{0, 1, 0}, // right
{0, 1, 0}, // back
{0, 1, 0}, // left
{0, 0,-1}, // top
{0, 0, 1}, // bottom
};
glm::mat4 ui::Canvas::m_plane_transform[6] = {
glm::lookAt(glm::vec3(), { 0, 0,-1}, {0, 1, 0}), // front
glm::lookAt(glm::vec3(), {-1, 0, 0}, {0, 1, 0}), // right
glm::lookAt(glm::vec3(), { 0, 0, 1}, {0, 1, 0}), // back
glm::lookAt(glm::vec3(), { 1, 0, 0}, {0, 1, 0}), // left
glm::lookAt(glm::vec3(), { 0, 1, 0}, {0, 0,-1}), // top
glm::lookAt(glm::vec3(), { 0,-1, 0}, {0, 0, 1}), // bottom
};
void ui::Canvas::clear(const glm::vec4& c/*={0,0,0,1}*/)
{
m_layers[m_current_layer_idx].clear(c);
}
void ui::Canvas::stroke_end()
{
stroke_commit();
m_current_stroke = nullptr;
m_show_tmp = false;
}
void ui::Canvas::stroke_draw()
{
if (!(m_current_stroke && m_current_stroke->has_sample()))
return;
m_dirty = true;
GLint vp[4];
GLfloat cc[4];
glGetIntegerv(GL_VIEWPORT, vp);
glGetFloatv(GL_COLOR_CLEAR_VALUE, cc);
glViewport(0, 0, m_width, m_height);
auto ortho_proj = glm::ortho(0.f, (float)m_width, 0.f, (float)m_height, -1.f, 1.f);
auto m_brush = m_current_stroke->m_brush;
auto samples = m_current_stroke->compute_samples();
auto& tex = TextureManager::get(m_brush.m_tex_id);
tex.bind();
m_sampler.bind(0);
m_sampler_bg.bind(1);
for (int i = 0; i < 6; i++)
{
m_tmp[i].bindFramebuffer();
glActiveTexture(GL_TEXTURE1);
m_tex[i].bind(); // bg, copy of framebuffer (copied before drawing)
if (m_use_instanced)
{
glEnable(GL_BLEND);
m_mesh.shader.use();
m_mesh.shader.u_vec4(kShaderUniform::Col, m_brush.m_tip_color);
m_mesh.shader.u_int(kShaderUniform::Tex, 0);
m_mesh.draw(samples, ortho_proj);
}
else
{
glDisable(GL_BLEND);
if (m_erase)
{
ShaderManager::use(ui::kShader::StrokeErase);
//ShaderManager::u_vec4(kShaderUniform::Col, m_brush.m_tip_color);
}
else
{
ShaderManager::use(ui::kShader::Stroke);
ShaderManager::u_vec4(kShaderUniform::Col, m_brush.m_tip_color);
}
ShaderManager::u_int(kShaderUniform::Tex, 0); // brush
ShaderManager::u_int(kShaderUniform::TexBG, 1); // bg
ShaderManager::u_vec2(kShaderUniform::Resolution, { m_width, m_height });
for (const auto& s : samples)
{
auto unproject = [](glm::vec2 loc, glm::vec4 vp, glm::mat4 camera, glm::mat4 proj, glm::vec3& out_origin, glm::vec3& out_dir) {
auto clip_space = glm::vec2(loc.x, vp.w - loc.y - 1.f) / vp.zw() * 2.f - 1.f;
auto inv = glm::inverse(proj * camera);
auto wp0 = inv * glm::vec4(clip_space, 0, 1);
auto wp1 = inv * glm::vec4(clip_space, .5, 1);
out_origin = (wp0 / wp0.w).xyz();
out_dir = glm::normalize((wp1 / wp1.w).xyz() - out_origin);
};
auto intersect = [](glm::vec3 ray_origin, glm::vec3 ray_dir, glm::vec3 plane_origin, glm::vec3 plane_normal, glm::vec3 plane_tangent, glm::vec3& out_hit) {
float den = glm::dot(ray_dir, plane_normal);
if (den == 0)
return false; // no intersection
float num = glm::dot(plane_origin - ray_origin, plane_normal);
float t = num / den;
if (t > 0)
out_hit = ray_origin + ray_dir * t;
else
// negative intersection
return false;
return true;
};
glm::vec3 ray_origin, ray_dir;
unproject(s.pos, { 0, 0, m_box.zw }, m_mv, m_proj, ray_origin, ray_dir);
glm::vec3 hit;
glm::vec2 fb_pos;
if (intersect(ray_origin, ray_dir, m_plane_origin[i], m_plane_normal[i], m_plane_tangent[i], hit))
{
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.f && glm::abs(plane_local.y) < 1.f)
{
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);
}
else
{
continue;
}
}
else
{
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++)
{
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()));
}
auto bb_sz = bb_max - bb_min;
glm::vec2 pad(1);
glm::ivec2 tex_pos = glm::clamp(glm::floor(bb_min) - pad , { 0, 0 }, { m_width, m_height });
glm::ivec2 tex_sz = glm::clamp(glm::ceil(bb_sz ) + pad*2.f, { 0, 0 }, (glm::vec2)(glm::ivec2(m_width, m_height) - tex_pos));
glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
tex_pos.x, tex_pos.y,
tex_pos.x, tex_pos.y,
tex_sz.x, tex_sz.y);
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_float(kShaderUniform::Alpha, s.flow);
//m_plane_brush.update_vertices(P);
m_plane_brush.draw_fill();
}
}
m_tex[i].unbind();
m_tmp[i].unbindFramebuffer();
}
glDisable(GL_BLEND);
glActiveTexture(GL_TEXTURE0);
m_sampler.unbind();
m_sampler_bg.unbind();
tex.unbind();
glViewport(vp[0], vp[1], vp[2], vp[3]);
glClearColor(cc[0], cc[1], cc[2], cc[3]);
}
void ui::Canvas::stroke_commit()
{
if (!m_dirty)
return;
m_dirty = false;
// save viewport and clear color states
GLint vp[4];
GLfloat cc[4];
glGetIntegerv(GL_VIEWPORT, vp);
glGetFloatv(GL_COLOR_CLEAR_VALUE, cc);
GLboolean blend = glIsEnabled(GL_BLEND);
// allocate action to add to history
auto action = new ActionStroke;
// prepare common states
glViewport(0, 0, m_width, m_height);
glDisable(GL_BLEND);
for (int i = 0; i < 6; i++)
{
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];
// copy to tmp2 for layer blending
glActiveTexture(GL_TEXTURE0);
m_tex2[i].bind();
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, m_width, m_height);
m_tex2[i].unbind();
m_tmp[i].bindTexture();
glActiveTexture(GL_TEXTURE1);
m_tex2[i].bind();
m_sampler.bind(0);
m_sampler_bg.bind(1);
if (m_erase)
{
ShaderManager::use(ui::kShader::Texture);
}
else
{
ShaderManager::use(ui::kShader::StrokeLayer);
ShaderManager::u_int(kShaderUniform::TexBG, 1);
ShaderManager::u_float(kShaderUniform::Alpha, m_current_stroke->m_brush.m_tip_opacity);
}
ShaderManager::u_int(kShaderUniform::Tex, 0);
ShaderManager::u_mat4(kShaderUniform::MVP, glm::ortho(-.5f, .5f, -.5f, .5f, -1.f, 1.f));
m_plane.draw_fill();
m_sampler.unbind();
m_sampler_bg.unbind();
m_tex2[i].unbind();
m_tmp[i].unbindTexture();
m_layers[m_current_layer_idx].m_rtt[i].unbindFramebuffer();
}
// restore viewport and clear color states
blend ? glEnable(GL_BLEND) : glDisable(GL_BLEND);
glViewport(vp[0], vp[1], vp[2], vp[3]);
glClearColor(cc[0], cc[1], cc[2], cc[3]);
glActiveTexture(GL_TEXTURE0);
// save history
action->m_layer_idx = m_current_layer_idx;
action->m_canvas = this;
action->m_stroke = std::move(m_current_stroke);
ActionManager::add(action);
}
void ui::Canvas::stroke_update(glm::vec2 point, float pressure)
{
m_current_stroke->add_point(point, pressure);
}
void ui::Canvas::stroke_start(glm::vec2 point, float pressure, const ui::Brush& brush)
{
m_current_stroke = std::make_unique<Stroke>();
m_current_stroke->m_camera = { m_cam_rot, m_cam_fov };
m_current_stroke->start(brush);
m_current_stroke->add_point(point, pressure);
for (int i = 0; i < 6; i++)
{
m_dirty_box[i] = glm::vec4(m_width, m_height, 0, 0); // reset bounding box
m_dirty_face[i] = false;
if (m_erase)
{
m_layers[m_current_layer_idx].m_rtt[i].bindFramebuffer();
m_tmp[i].bindTexture();
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, m_width, m_height);
m_tmp[i].unbindTexture();
m_layers[m_current_layer_idx].m_rtt[i].unbindFramebuffer();
}
else
{
m_tmp[i].bindFramebuffer();
m_tmp[i].clear({ 0, 0, 0, 0 });
m_tmp[i].unbindFramebuffer();
}
}
m_show_tmp = true;
}
void ui::Canvas::layer_add(std::string name)
{
int idx = (int)m_layers.size();
m_layers.emplace_back();
m_layers.back().create(m_width, m_height, name);
m_order.push_back(idx);
}
void ui::Canvas::layer_order(int idx, int pos)
{
std::swap(m_order[idx], m_order[pos]);
}
void ui::Canvas::resize(int width, int height)
{
m_width = width;
m_height = height;
for (int i = 0; i < 6; i++)
{
m_tmp[i].create(width, height);
m_tex[i].create(width, height);
m_tex2[i].create(width, height);
}
for (auto& l : m_layers)
{
l.create(width, height, "");
}
}
bool ui::Canvas::create(int width, int height)
{
m_width = width;
m_height = height;
for (int i = 0; i < 6; i++)
{
m_tmp[i].create(width, height);
m_tex[i].create(width, height);
m_tex2[i].create(width, height); // TODO: destroy before recreating
}
m_sampler.create();
m_sampler_bg.create();
m_plane.create<1>(1, 1);
m_plane_brush.create<1>(1, 1);
m_mesh.create();
return true;
}
void ui::Canvas::snapshot_save()
{
}
void ui::Canvas::snapshot_restore()
{
};