export depth

This commit is contained in:
2019-04-25 23:31:03 +02:00
parent 2da84ec63d
commit db208bd7cd
5 changed files with 131 additions and 1 deletions

View File

@@ -0,0 +1,26 @@
[[vertex]]
uniform mat4 mvp;
in vec4 pos;
in vec2 uvs;
out vec2 uv;
void main()
{
uv = uvs;
gl_Position = mvp * vec4(pos.xyz, 1.0);
}
[[fragment]]
uniform sampler2D tex;
uniform vec4 col;
in mediump vec2 uv;
out mediump vec4 frag;
void main()
{
frag = vec4(col.rgb, texture(tex, uv).a > 0.01 ? 1.0 : 0.0);
}

View File

@@ -471,7 +471,7 @@ void App::dialog_export_depth()
if (canvas)
{
// TODO: use picker
canvas->m_canvas->export_layers(doc_name, [this] {
canvas->m_canvas->export_depth(doc_name, [this] {
#if defined(__IOS__)
message_box("Export 3D View + Depth", "Image and depth exported to Files/PanoPainter");
#elif defined(__OSX__)

View File

@@ -29,6 +29,8 @@ void App::initShaders()
LOG("Failed to create shader TextureAlpha");
if (!ShaderManager::load(kShader::TextureAlphaSep, "data/shaders/texture-alpha-sep.glsl"))
LOG("Failed to create shader TextureAlphaSep");
if (!ShaderManager::load(kShader::TextureColorize, "data/shaders/texture-colorize.glsl"))
LOG("Failed to create shader TextureColorize");
if (!ShaderManager::load(kShader::TextureBlend, "data/shaders/texture-blend.glsl"))
LOG("Failed to create shader TextureBlend");
if (!ShaderManager::load(kShader::StrokePreview, "data/shaders/stroke-preview.glsl"))

View File

@@ -1032,6 +1032,21 @@ void Canvas::draw_merge(std::array<bool, 6> faces /*= SIXPLETTE(false)*/)
m_layers[layer_index]->m_rtt[plane_index].unbindTexture();
glActiveTexture(GL_TEXTURE0);
m_layers[layer_index]->m_rtt[plane_index].unbindTexture();
/*
m_sampler.bind(0);
m_sampler_linear.bind(1);
ShaderManager::use(kShader::TextureColorize);
ShaderManager::u_int(kShaderUniform::Tex, 0);
ShaderManager::u_vec4(kShaderUniform::Col, { glm::vec3((float)layer_index / (float)m_order.size()), 1.f });
ShaderManager::u_mat4(kShaderUniform::MVP, ortho);
glActiveTexture(GL_TEXTURE0);
m_layers[layer_index]->m_rtt[plane_index].bindTexture();
m_plane.draw_fill();
m_layers[layer_index]->m_rtt[plane_index].unbindTexture();
*/
}
if (use_blend)
@@ -1828,6 +1843,92 @@ void Canvas::export_depth_thread(std::string file_name)
gl_state gl;
gl.save();
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
RTT rtt;
rtt.create(1024, 1024);
rtt.bindFramebuffer();
rtt.clear({ 0, 0, 0, 1 });
glViewport(0, 0, rtt.getWidth(), rtt.getHeight());
glm::mat4 proj = glm::perspective(glm::radians(m_cam_fov), (float)rtt.getWidth() / (float)rtt.getHeight(), 0.1f, 100.f);
glm::mat4 camera = m_cam_rot;
for (int plane_index = 0; plane_index < 6; plane_index++)
{
auto plane_mvp_z = proj * camera *
m_plane_transform[plane_index] *
glm::translate(glm::vec3(0, 0, -1)) *
glm::scale(glm::vec3(2));
m_sampler.bind(0);
m_sampler_linear.bind(1);
ShaderManager::use(kShader::TextureAlphaSep);
ShaderManager::u_int(kShaderUniform::Tex, 0);
ShaderManager::u_int(kShaderUniform::TexA, 1);
ShaderManager::u_float(kShaderUniform::Alpha, 1.f);
ShaderManager::u_int(kShaderUniform::Highlight, false);
ShaderManager::u_mat4(kShaderUniform::MVP, plane_mvp_z);
glActiveTexture(GL_TEXTURE0);
m_layers_merge.m_rtt[plane_index].bindTexture();
glActiveTexture(GL_TEXTURE1);
m_layers_merge.m_rtt[plane_index].bindTexture();
m_plane.draw_fill();
glActiveTexture(GL_TEXTURE1);
m_layers_merge.m_rtt[plane_index].unbindTexture();
glActiveTexture(GL_TEXTURE0);
m_layers_merge.m_rtt[plane_index].unbindTexture();
}
uint8_t* rgba_data = rtt.readTextureData();
stbi_flip_vertically_on_write(true);
std::string path_rgba = App::I.work_path + "/" + file_name + ".png";
stbi_write_jpg(path_rgba.c_str(), rtt.getWidth(), rtt.getHeight(), 4, rgba_data, 100);
delete rgba_data;
rtt.clear({ 0, 0, 0, 1 });
for (size_t i = 0; i < m_order.size(); i++)
{
auto layer_index = m_order[i];
for (int plane_index = 0; plane_index < 6; plane_index++)
{
if ((!m_layers[layer_index]->m_visible ||
m_layers[layer_index]->m_opacity == .0f ||
!m_layers[layer_index]->m_dirty_face[plane_index]))
continue;
auto plane_mvp_z = proj * camera *
m_plane_transform[plane_index] *
glm::translate(glm::vec3(0, 0, -1)) *
glm::scale(glm::vec3(2));
m_sampler.bind(0);
m_sampler_linear.bind(1);
ShaderManager::use(kShader::TextureColorize);
ShaderManager::u_int(kShaderUniform::Tex, 0);
ShaderManager::u_vec4(kShaderUniform::Col, { glm::vec3((float)(i + 1) / (float)(m_order.size() + 1)), 1.f });
ShaderManager::u_mat4(kShaderUniform::MVP, plane_mvp_z);
glActiveTexture(GL_TEXTURE0);
m_layers[layer_index]->m_rtt[plane_index].bindTexture();
m_plane.draw_fill();
m_layers[layer_index]->m_rtt[plane_index].unbindTexture();
}
}
uint8_t* depth_data = rtt.readTextureData();
std::string path_depth = App::I.work_path + "/" + file_name + "_depth.png";
stbi_write_jpg(path_depth.c_str(), rtt.getWidth(), rtt.getHeight(), 4, depth_data, 100);
delete depth_data;
stbi_flip_vertically_on_write(false);
rtt.unbindFramebuffer();
rtt.destroy();
gl.restore();
App::I.async_end();
}

View File

@@ -57,6 +57,7 @@ enum class kShader : uint16_t
ColorTri = const_hash("color-tri"),
ColorHue = const_hash("color-hue"),
Texture = const_hash("texture"),
TextureColorize = const_hash("texture-colorize"),
TextureAlpha= const_hash("texture-alpha"),
TextureAlphaSep= const_hash("texture-alpha-sep"),
TextureBlend= const_hash("texture-blend"),