implement basic paint canvas

This commit is contained in:
2017-03-26 14:23:15 +01:00
parent 744cd8bfb5
commit a385addae5
11 changed files with 210 additions and 126 deletions

View File

@@ -47,6 +47,8 @@ void App::initShaders()
"void main(){"
" frag = vec4(uv.xy, 0.0, 1.0);"
"}";
// TEXTURE ATLAS
static const char* shader_atlas_v =
SHADER_VERSION
"uniform mat4 mvp;"
@@ -67,6 +69,8 @@ void App::initShaders()
"void main(){"
" frag = texture(tex, uv);"
"}";
// SOLID COLOR
static const char* shader_color_v =
SHADER_VERSION
"uniform mat4 mvp;"
@@ -81,6 +85,8 @@ void App::initShaders()
"void main(){"
" frag = col;"
"}";
// COLOR QUAD
static const char* shader_color_quad_v =
SHADER_VERSION
"uniform mat4 mvp;"
@@ -100,6 +106,8 @@ void App::initShaders()
" vec4 gradient_x = mix(col, vec4(1.0, 1.0, 1.0, 1.0), uv.x);"
" frag = mix(gradient_x, vec4(0.0, 0.0, 0.0, 1.0), uv.y);"
"}";
// HUE
static const char* shader_color_hue_v =
SHADER_VERSION
"uniform mat4 mvp;"
@@ -123,6 +131,8 @@ void App::initShaders()
"void main(){"
" frag = vec4(hsv2rgb(vec3(uv.y, 1.0, 1.0)), 1.0);"
"}";
// FONT
static const char* shader_font_v =
SHADER_VERSION
"uniform mat4 mvp;"
@@ -144,6 +154,29 @@ void App::initShaders()
" frag = vec4(col.rgb, a);"
"}";
// STROKE
static const char* shader_stroke_v =
SHADER_VERSION
"uniform mat4 mvp;"
"in vec4 pos;"
"in vec2 uvs;"
"out vec3 uv;"
"void main(){"
" uv = vec3(uvs, pos.w);"
" gl_Position = mvp * vec4(pos.xyz, 1.0);"
"}";
static const char* shader_stroke_f =
SHADER_VERSION
"uniform sampler2D tex;"
"uniform vec4 col;"
"uniform float alpha;"
"in vec3 uv;"
"out vec4 frag;"
"void main(){"
" float a = (1.0 - texture(tex, uv.xy).r) * alpha;"
" frag = vec4(col.rgb, a);"
"}";
LOG("initializing shaders");
if (!ShaderManager::create(kShader::Texture, shader_v, shader_f))
LOG("Failed to create shader Texture");
@@ -159,6 +192,8 @@ void App::initShaders()
LOG("Failed to create shader Font");
if (!ShaderManager::create(kShader::Atlas, shader_atlas_v, shader_atlas_f))
LOG("Failed to create shader Atlas");
if (!ShaderManager::create(kShader::Stroke, shader_stroke_v, shader_stroke_f))
LOG("Failed to create shader Stroke");
LOG("shaders initialized");
}
@@ -193,6 +228,7 @@ void App::initLayout()
LOG("initializing layout components");
sidebar = layout[main_id]->find<NodeBorder>("sidebar");
panels = layout[main_id]->find<Node>("panels");
canvas = layout[main_id]->find<NodeCanvas>("paint-canvas");
//brushes = layout[main_id]->find<NodePanelBrush>("panel-brush");
//layers = layout[main_id]->find<NodePanelLayer>("panel-layer");
@@ -225,20 +261,23 @@ void App::initLayout()
brushes->on_brush_changed = [this](Node* target, int index) {
auto tid = brushes->get_texture_id(index);
stroke->m_canvas->m_tex_id = tid;
stroke->m_canvas->m_brush.m_tex_id = tid;
stroke->m_canvas->draw_stroke();
canvas->m_brush = stroke->m_canvas->m_brush;
if (on_brush_select)
on_brush_select(index);
};
color->on_color_changed = [this](Node* target, glm::vec4 color) {
stroke->m_canvas->m_tip_color = color;
stroke->m_canvas->m_brush.m_tip_color = color;
stroke->m_canvas->draw_stroke();
canvas->m_brush = stroke->m_canvas->m_brush;
if (on_color_change)
on_color_change(color);
};
stroke->on_stroke_change = [this](Node*target) {
canvas->m_brush = stroke->m_canvas->m_brush;
if (on_stroke_change)
on_stroke_change();
};
@@ -306,7 +345,10 @@ void App::initLayout()
if (auto* button = layout[main_id]->find<NodeButton>("btn-close"))
{
button->on_click = [](Node*) { exit(0); };
button->on_click = [this](Node*) {
//exit(0);
canvas->m_canvas->clear();
};
}
if (auto* button = layout[main_id]->find<NodeButton>("btn-popup"))
{