change android icon, move code so .cpp and implement stroke preview using Stroke class for accurate preview.
This commit is contained in:
134
engine/canvas.h
134
engine/canvas.h
@@ -9,136 +9,28 @@ NS_START
|
||||
|
||||
class Canvas
|
||||
{
|
||||
Stroke* m_current_stroke = nullptr;
|
||||
Plane m_plane;
|
||||
BrushMesh m_mesh;
|
||||
int m_current_layer_idx = 0;
|
||||
public:
|
||||
int m_width;
|
||||
int m_height;
|
||||
bool m_use_instanced = false;
|
||||
std::vector<Layer> m_layers;
|
||||
std::vector<Stroke> m_strokes;
|
||||
std::unique_ptr<Layer> m_tmp;
|
||||
Stroke* m_current_stroke = nullptr;
|
||||
int m_current_layer_idx = 0;
|
||||
RTT m_fb;
|
||||
Sampler m_sampler;
|
||||
Plane m_plane;
|
||||
BrushMesh m_mesh;
|
||||
bool m_use_instanced = false;
|
||||
std::minstd_rand prng;
|
||||
bool create(int width, int height)
|
||||
{
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
m_tmp = std::make_unique<Layer>();
|
||||
m_tmp->create(width, height, "tmp");
|
||||
m_fb.create(width, height);
|
||||
m_sampler.create();
|
||||
m_plane.create<1>(1, 1);
|
||||
m_mesh.create();
|
||||
return true;
|
||||
}
|
||||
void resize(int width, int height)
|
||||
{
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
m_tmp->create(width, height, "tmp");
|
||||
m_fb.create(width, height);
|
||||
}
|
||||
void layer_add(std::string name)
|
||||
{
|
||||
m_layers.emplace_back();
|
||||
m_layers.back().create(m_width, m_height, name);
|
||||
}
|
||||
void stroke_start(glm::vec2 point, float pressure, const ui::Brush& brush)
|
||||
{
|
||||
prng.seed(0);
|
||||
m_strokes.emplace_back();
|
||||
m_strokes.back().start(point, pressure, brush);
|
||||
m_current_stroke = &m_strokes.back();
|
||||
}
|
||||
void stroke_update(glm::vec2 point, float pressure)
|
||||
{
|
||||
m_current_stroke->add_point(point, pressure);
|
||||
}
|
||||
void stroke_draw()
|
||||
{
|
||||
if (!(m_current_stroke && m_current_stroke->has_sample()))
|
||||
return;
|
||||
|
||||
m_fb.bindFramebuffer();
|
||||
|
||||
GLint vp[4];
|
||||
GLfloat cc[4];
|
||||
glGetIntegerv(GL_VIEWPORT, vp);
|
||||
glGetFloatv(GL_COLOR_CLEAR_VALUE, cc);
|
||||
|
||||
glViewport(0, 0, m_width, m_height);
|
||||
glEnable(GL_BLEND);
|
||||
|
||||
auto proj = glm::ortho(0.f, (float)m_width, (float)m_height, 0.f, -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);
|
||||
|
||||
if (m_use_instanced)
|
||||
{
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
ShaderManager::use("stroke");
|
||||
ShaderManager::u_vec4(kShaderUniform::Col, m_brush.m_tip_color);
|
||||
ShaderManager::u_int(kShaderUniform::Tex, 0);
|
||||
for (const auto& s : samples)
|
||||
{
|
||||
auto mvp = proj *
|
||||
glm::translate(glm::vec3(s.pos, 0)) *
|
||||
glm::scale(glm::vec3(s.size, s.size, 1)) *
|
||||
glm::eulerAngleZ(s.angle);
|
||||
|
||||
ShaderManager::u_mat4(kShaderUniform::MVP, mvp);
|
||||
ShaderManager::u_float(kShaderUniform::Alpha, s.flow);
|
||||
m_plane.draw_fill();
|
||||
}
|
||||
}
|
||||
|
||||
m_sampler.unbind();
|
||||
tex.unbind();
|
||||
|
||||
glDisable(GL_BLEND);
|
||||
|
||||
glViewport(vp[0], vp[1], vp[2], vp[3]);
|
||||
glClearColor(cc[0], cc[1], cc[2], cc[3]);
|
||||
|
||||
m_fb.unbindFramebuffer();
|
||||
}
|
||||
void stroke_end()
|
||||
{
|
||||
m_current_stroke = nullptr;
|
||||
}
|
||||
void clear()
|
||||
{
|
||||
m_fb.bindFramebuffer();
|
||||
|
||||
GLint vp[4];
|
||||
GLfloat cc[4];
|
||||
glGetIntegerv(GL_VIEWPORT, vp);
|
||||
glGetFloatv(GL_COLOR_CLEAR_VALUE, cc);
|
||||
|
||||
glClearColor(1, 1, 1, 1);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glViewport(0, 0, m_width, m_height);
|
||||
|
||||
//
|
||||
|
||||
glViewport(vp[0], vp[1], vp[2], vp[3]);
|
||||
glClearColor(cc[0], cc[1], cc[2], cc[3]);
|
||||
|
||||
m_fb.unbindFramebuffer();
|
||||
}
|
||||
bool create(int width, int height);
|
||||
void resize(int width, int height);
|
||||
void layer_add(std::string name);
|
||||
void stroke_start(glm::vec2 point, float pressure, const ui::Brush& brush);
|
||||
void stroke_update(glm::vec2 point, float pressure);
|
||||
void stroke_draw();
|
||||
void stroke_end();
|
||||
void clear();
|
||||
};
|
||||
|
||||
NS_END
|
||||
|
||||
Reference in New Issue
Block a user