fix all six planes for rendering and drawing

This commit is contained in:
2017-04-28 00:06:24 +01:00
parent a6a020a389
commit 5753002ca9
3 changed files with 79 additions and 30 deletions

View File

@@ -2,6 +2,39 @@
#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);
@@ -26,7 +59,7 @@ void ui::Canvas::stroke_draw()
glViewport(0, 0, m_width, m_height);
auto proj = glm::ortho(0.f, (float)m_width, 0.f, (float)m_height, -1.f, 1.f);
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();
@@ -48,7 +81,7 @@ void ui::Canvas::stroke_draw()
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, proj);
m_mesh.draw(samples, ortho_proj);
}
else
{
@@ -76,7 +109,7 @@ void ui::Canvas::stroke_draw()
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) {
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
@@ -88,28 +121,37 @@ void ui::Canvas::stroke_draw()
// 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 plane_origin{ 0, 0, -1 }, plane_dir{ 0, 0, 1 };
glm::vec3 hit;
glm::vec2 fb_pos;
if (intersect(ray_origin, ray_dir, plane_origin, plane_dir, hit))
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(plane_origin, plane_dir, { 0, 1, 0 });
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) < 0.5f && glm::abs(plane_local.y) < 0.5f)
if (glm::abs(plane_local.x) < 1.f && glm::abs(plane_local.y) < 1.f)
{
fb_pos.x = -(plane_local.x - 0.5f) * m_width;
fb_pos.y = (plane_local.y + 0.5f) * m_height;
LOG("draw %f %f", fb_pos.x, fb_pos.y);
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 = proj *
glm::translate(glm::vec3(fb_pos, 0)) *
glm::scale(glm::vec3(s.size, s.size, 1)) *
glm::eulerAngleZ(s.angle);
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
@@ -118,7 +160,7 @@ void ui::Canvas::stroke_draw()
mvp * glm::vec4(glm::vec2(+.5f, -.5f), 0, 1.f), // D - bottom-right
};
auto mvp_inv = glm::inverse(proj);
auto mvp_inv = glm::inverse(ortho_proj);
glm::vec4 P2[4]{
mvp_inv * P[0],
mvp_inv * P[1],
@@ -146,9 +188,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, glm::mat4());
ShaderManager::u_mat4(kShaderUniform::MVP, mvp);
ShaderManager::u_float(kShaderUniform::Alpha, s.flow);
m_plane_brush.update_vertices(P);
//m_plane_brush.update_vertices(P);
m_plane_brush.draw_fill();
}
}
@@ -190,6 +232,9 @@ void ui::Canvas::stroke_commit()
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
@@ -228,7 +273,6 @@ void ui::Canvas::stroke_commit()
m_tex2[i].unbind();
m_tmp[i].unbindTexture();
m_layers[m_current_layer_idx].m_rtt[i].unbindFramebuffer();
}
@@ -258,6 +302,7 @@ void ui::Canvas::stroke_start(glm::vec2 point, float pressure, const ui::Brush&
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)
{