prepare to draw on multiple planes

This commit is contained in:
2017-04-27 21:13:09 +01:00
parent e6332322b3
commit a6a020a389
4 changed files with 270 additions and 281 deletions

View File

@@ -1910,90 +1910,37 @@ public:
// auto plane_mvp = proj * camera * transform *
// glm::scale(glm::vec3(sz, 1));
auto plane_mvp = proj * camera * glm::translate(glm::vec3(0, 0, -1));
m_sampler.bind(0);
ui::ShaderManager::use(kShader::TextureAlpha);
ui::ShaderManager::u_int(kShaderUniform::Tex, 0);
ui::ShaderManager::u_mat4(kShaderUniform::MVP, plane_mvp);
auto blend = glIsEnabled(GL_BLEND);
glEnable(GL_BLEND);
for (auto i : m_canvas->m_order)
for (auto layer_index : m_canvas->m_order)
{
if (!(m_canvas->m_erase && m_canvas->m_show_tmp && m_canvas->m_current_layer_idx == i))
for (int plane_index = 0; plane_index < 4; plane_index++)
{
ui::ShaderManager::u_float(kShaderUniform::Alpha, m_canvas->m_layers[i].m_opacity);
m_canvas->m_layers[i].m_rtt.bindTexture();
NodeBorder::m_plane.draw_fill();
m_canvas->m_layers[i].m_rtt.unbindTexture();
}
if (m_canvas->m_show_tmp && m_canvas->m_current_layer_idx == i)
{
glEnable(GL_BLEND);
ui::ShaderManager::u_float(kShaderUniform::Alpha, m_canvas->m_current_stroke->m_brush.m_tip_opacity);
m_canvas->m_tmp.bindTexture();
NodeBorder::m_plane.draw_fill();
m_canvas->m_tmp.unbindTexture();
auto plane_mvp = proj * camera * glm::eulerAngleY(glm::radians(90.f * plane_index)) * glm::translate(glm::vec3(0, 0, -1));
ui::ShaderManager::u_mat4(kShaderUniform::MVP, plane_mvp);
if (!(m_canvas->m_erase && m_canvas->m_show_tmp && m_canvas->m_current_layer_idx == layer_index))
{
ui::ShaderManager::u_float(kShaderUniform::Alpha, m_canvas->m_layers[layer_index].m_opacity);
m_canvas->m_layers[layer_index].m_rtt[plane_index].bindTexture();
NodeBorder::m_plane.draw_fill();
m_canvas->m_layers[layer_index].m_rtt[plane_index].unbindTexture();
}
if (m_canvas->m_show_tmp && m_canvas->m_current_layer_idx == layer_index)
{
glEnable(GL_BLEND);
ui::ShaderManager::u_float(kShaderUniform::Alpha, m_canvas->m_current_stroke->m_brush.m_tip_opacity);
m_canvas->m_tmp[plane_index].bindTexture();
NodeBorder::m_plane.draw_fill();
m_canvas->m_tmp[plane_index].unbindTexture();
}
}
}
ui::ShaderManager::use(kShader::Color);
ui::ShaderManager::u_vec4(kShaderUniform::Col, { 1, 0, 0, 1 });
// auto loc = m_cur;
// auto clip_space = glm::vec2(loc.x, box.w - loc.y - 1.f) / box.zw() * 2.f - 1.f;
// auto inv = glm::inverse(proj * camera);
// auto wp = inv * glm::vec4(clip_space, 0.9, 1);
// wp = wp / wp.w;
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& 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(m_cur, { 0, 0, box.zw }, camera, proj, ray_origin, ray_dir);
glm::vec3 plane_origin{ 0, 0, -1 }, plane_dir{ 0, 0, 1 };
glm::vec3 hit;
if (intersect(ray_origin, ray_dir, plane_origin, plane_dir, hit))
{
glm::mat4 plane_camera = glm::lookAt(plane_origin, plane_dir, { 0, 1, 0 });
glm::vec4 plane_local = plane_camera * glm::vec4(hit, 1);
if (glm::abs(plane_local.x) < 0.5f && glm::abs(plane_local.y) < 0.5f)
{
LOG("hit %f %f %f", plane_local.x, plane_local.y, plane_local.z);
}
}
float fovy = glm::radians(m_canvas->m_cam_fov);
float fovx = fovy * box.z / box.w;
glm::vec2 fov = { fovx, fovy };
auto fov_t = glm::vec2(m_cur.x, box.w - m_cur.y - 1.f) / box.zw();
glm::vec2 fov_rot = glm::lerp(-fov*.5f, fov*.5f, fov_t);
glm::mat4 fov_rot_mat = glm::eulerAngleXY(-fov_rot.y, fov_rot.x) * camera;
if (method)
ui::ShaderManager::u_mat4(kShaderUniform::MVP, proj * camera * glm::translate(hit) * glm::scale(glm::vec3(.1)));
else
ui::ShaderManager::u_mat4(kShaderUniform::MVP, proj * camera * glm::translate(hit) * glm::scale(glm::vec3(.1)) * glm::transpose(fov_rot_mat));
//NodeBorder::m_plane.draw_fill();
blend ? glEnable(GL_BLEND) : glDisable(GL_BLEND);
m_sampler.unbind();
@@ -2015,9 +1962,6 @@ public:
KeyEvent* ke = static_cast<KeyEvent*>(e);
GestureEvent* ge = static_cast<GestureEvent*>(e);
auto loc = (me->m_pos - m_pos) * root()->m_zoom;
auto clip_space = glm::vec2(loc.x, m_size.y - loc.y - 1.f) / m_size * 2.f - 1.f;
//auto fb_space = glm::inverse(m_canvas->m_mvp) * glm::vec4(clip_space, 0, 1);
auto cur = glm::vec2(loc.x, m_size.y - loc.y - 1.f);// fb_space.xy();
switch (e->m_type)
{