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)
{

View File

@@ -66,11 +66,14 @@ public:
std::vector<Layer> m_layers;
std::vector<int> m_order;
glm::vec4 m_dirty_box[6];
bool m_dirty_face[6];
RTT m_tmp[6];
Texture2D m_tex[6];
Texture2D m_tex2[6];
glm::vec3 m_plane_origin[6];
glm::vec3 m_plane_normal[6];
static glm::vec3 m_plane_origin[6];
static glm::vec3 m_plane_normal[6];
static glm::vec3 m_plane_tangent[6];
static glm::mat4 m_plane_transform[6];
Sampler m_sampler;
Sampler m_sampler_bg;
glm::vec2 m_cam_rot;

View File

@@ -1867,6 +1867,7 @@ public:
std::unique_ptr<ui::Canvas> m_canvas;
ui::Brush m_brush;
Sampler m_sampler;
ui::Plane m_face_plane;
virtual Node* clone_instantiate() const override { return new NodeCanvas(); }
virtual void init() override
{
@@ -1876,6 +1877,7 @@ public:
m_canvas->layer_add("asd");
m_canvas->clear();
m_sampler.create();
m_face_plane.create<1>(1.99, 1.99);
}
virtual void draw() override
{
@@ -1892,8 +1894,6 @@ public:
glm::ivec4 c = (glm::ivec4)glm::vec4(box.x, (int)(vp[3] - box.y - box.w), box.z, box.w);
glViewport(c.x, c.y, c.z, c.w);
glm::vec2 sz = { m_canvas->m_width, m_canvas->m_height };
m_canvas->m_cam_rot = m_pan * 0.001f;
//glm::mat4 proj = glm::ortho(0.f, box.z, 0.f, box.w, -1000.f, 1000.f);
@@ -1920,23 +1920,24 @@ public:
glEnable(GL_BLEND);
for (auto layer_index : m_canvas->m_order)
{
for (int plane_index = 0; plane_index < 4; plane_index++)
for (int plane_index = 0; plane_index < 6; plane_index++)
{
auto plane_mvp = proj * camera * glm::eulerAngleY(glm::radians(90.f * plane_index)) * glm::translate(glm::vec3(0, 0, -1));
auto plane_mvp = proj * camera * m_canvas->m_plane_transform[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_face_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);
ui::ShaderManager::u_float(kShaderUniform::Alpha,
m_canvas->m_current_stroke->m_brush.m_tip_opacity * m_canvas->m_layers[layer_index].m_opacity);
m_canvas->m_tmp[plane_index].bindTexture();
NodeBorder::m_plane.draw_fill();
m_face_plane.draw_fill();
m_canvas->m_tmp[plane_index].unbindTexture();
}
}
@@ -1951,8 +1952,8 @@ public:
{
if (new_size.x > m_canvas->m_width)
{
m_canvas->resize((int)new_size.x, (int)new_size.y);
m_canvas->clear();
// m_canvas->resize((int)new_size.x, (int)new_size.y);
// m_canvas->clear();
}
}
virtual kEventResult handle_event(Event* e) override