refactor export equirectangular

This commit is contained in:
2019-08-15 19:12:50 +02:00
parent e959fb4d91
commit 542e5a9c19
12 changed files with 223 additions and 206 deletions

View File

@@ -76,7 +76,7 @@ void Canvas::pick_update(int plane)
{
std::array<bool, 6> faces{ false };
faces[plane] = true;
draw_merge(faces);
draw_merge(true, faces);
int i = plane;
m_layers_merge.m_rtt[i].bindFramebuffer();
@@ -915,8 +915,10 @@ void Canvas::stroke_commit()
ActionManager::add(action);
}
void Canvas::draw_merge(std::array<bool, 6> faces /*= SIXPLETTE(false)*/)
void Canvas::draw_merge(bool draw_checkerboard, std::array<bool, 6> faces /*= SIXPLETTE(false)*/)
{
assert(App::I->is_render_thread());
glViewport(0, 0, m_width, m_height);
auto ortho = glm::ortho<float>(-0.5f, 0.5f, -0.5f, 0.5f, -1.f, 1.f);
const auto& b = m_current_stroke->m_brush;
@@ -939,6 +941,7 @@ void Canvas::draw_merge(std::array<bool, 6> faces /*= SIXPLETTE(false)*/)
continue;
m_layers_merge.m_rtt[plane_index].bindFramebuffer();
m_layers_merge.m_rtt[plane_index].clear({ 1, 1, 1, 0 });
if (use_blend)
{
@@ -947,10 +950,13 @@ void Canvas::draw_merge(std::array<bool, 6> faces /*= SIXPLETTE(false)*/)
}
else
{
ShaderManager::use(kShader::Checkerboard);
ShaderManager::u_int(kShaderUniform::Colorize, false);
ShaderManager::u_mat4(kShaderUniform::MVP, ortho);
m_plane.draw_fill();
if (draw_checkerboard)
{
ShaderManager::use(kShader::Checkerboard);
ShaderManager::u_int(kShaderUniform::Colorize, false);
ShaderManager::u_mat4(kShaderUniform::MVP, ortho);
m_plane.draw_fill();
}
glEnable(GL_BLEND);
}
@@ -1125,10 +1131,13 @@ void Canvas::draw_merge(std::array<bool, 6> faces /*= SIXPLETTE(false)*/)
glEnable(GL_BLEND);
//draw the grid
ShaderManager::use(kShader::Checkerboard);
ShaderManager::u_int(kShaderUniform::Colorize, false);
ShaderManager::u_mat4(kShaderUniform::MVP, ortho);
m_plane.draw_fill();
if (draw_checkerboard)
{
ShaderManager::use(kShader::Checkerboard);
ShaderManager::u_int(kShaderUniform::Colorize, false);
ShaderManager::u_mat4(kShaderUniform::MVP, ortho);
m_plane.draw_fill();
}
// draw the layers
m_sampler.bind(0);
@@ -1720,203 +1729,29 @@ void Canvas::export_equirectangular(std::string file_path, std::function<void()>
void Canvas::export_equirectangular_thread(std::string file_path)
{
std::shared_ptr<NodeProgressBar> pb;
if (App::I->layout.m_loaded)
{
pb = std::make_shared<NodeProgressBar>();
pb->m_manager = &App::I->layout;
pb->init();
pb->create();
pb->loaded();
pb->m_progress->SetWidthP(0);
pb->m_title->set_text("Export Pano Image");
App::I->layout[App::I->main_id]->add_child(pb);
}
RTT m_latlong;
m_latlong.create(m_width * 4, m_height * 2); // NOTE: w and h must be equal to make sense
GLuint cube_id;
static int faces[]{
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, // front
GL_TEXTURE_CUBE_MAP_NEGATIVE_X, // right
GL_TEXTURE_CUBE_MAP_POSITIVE_Z, // back
GL_TEXTURE_CUBE_MAP_POSITIVE_X, // left
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, // top
GL_TEXTURE_CUBE_MAP_POSITIVE_Y, // bottom
};
App::I->render_task([&]
{
glGenTextures(1, &cube_id);
glBindTexture(GL_TEXTURE_CUBE_MAP, cube_id);
for (GLuint i = 0; i < 6; i++)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA8,
m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
}
});
int progress = 0;
int total = 6 + 2;
Texture2D face;
face.create(m_width, m_height);
for (int i = 0; i < 6; i++)
{
App::I->render_task([&]
{
// prepare common states
glViewport(0, 0, m_width, m_height);
glDisable(GL_BLEND);
ShaderManager::use(kShader::TextureBlend);
ShaderManager::u_int(kShaderUniform::Tex, 0);
ShaderManager::u_mat4(kShaderUniform::MVP, glm::ortho(-.5f, .5f, -.5f, .5f, -1.f, 1.f));
m_tmp[i].bindFramebuffer();
// clear transparent not to mess with blending modes
m_tmp[i].clear({ 1, 1, 1, 0 });
if (!ShaderManager::ext_framebuffer_fetch)
{
ShaderManager::u_int(kShaderUniform::TexBG, 2);
glActiveTexture(GL_TEXTURE2);
face.bind();
m_sampler_nearest.bind(2);
}
m_sampler_nearest.bind(0); // nearest
for (int layer_index = 0; layer_index < m_layers.size(); layer_index++)
{
if (!m_layers[layer_index]->m_visible ||
m_layers[layer_index]->m_opacity == 0.f ||
!m_layers[layer_index]->m_dirty_face[i])
continue;
if (!ShaderManager::ext_framebuffer_fetch)
{
glActiveTexture(GL_TEXTURE2);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, m_width, m_height);
}
ShaderManager::u_int(kShaderUniform::BlendMode, m_layers[layer_index]->m_blend_mode);
ShaderManager::u_float(kShaderUniform::Alpha, m_layers[layer_index]->m_opacity);
glActiveTexture(GL_TEXTURE0);
m_layers[layer_index]->m_rtt[i].bindTexture();
m_plane.draw_fill();
m_layers[layer_index]->m_rtt[i].unbindTexture();
}
if (!ShaderManager::ext_framebuffer_fetch)
{
glActiveTexture(GL_TEXTURE2);
face.unbind();
}
// now blend with the background
glEnable(GL_BLEND);
ShaderManager::use(kShader::Texture);
ShaderManager::u_int(kShaderUniform::Tex, 0);
ShaderManager::u_mat4(kShaderUniform::MVP, glm::ortho(-.5f, .5f, -.5f, .5f, -1.f, 1.f));
m_sampler.bind(0); // linear
glActiveTexture(GL_TEXTURE0);
face.bind();
// copy the framebuffer before clearing to white
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, m_width, m_height);
m_tmp[i].clear({ 1, 1, 1, 0 });
m_plane.draw_fill();
face.unbind();
// copy result to cubemap
glBindTexture(GL_TEXTURE_CUBE_MAP, cube_id);
glCopyTexImage2D(faces[i], 0, GL_RGBA8, 0, 0, m_width, m_height, 0);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
m_tmp[i].unbindFramebuffer();
});
progress++;
float p = (float)progress / total * 100.f;
LOG("progress: %f", p);
if (App::I->layout.m_loaded)
{
pb->m_progress->SetWidthP(p);
}
}
face.destroy();
//auto data = std::make_unique<uint8_t[]>(m_tmp[0].bytes());
//for (int i = 0; i < 1; i++)
//{
// m_tmp[i].readTextureData(data.get());
// static char name[128];
// sprintf(name, "%s/Face%d.png", data_path.c_str(), i);
// LOG("writing %s", name);
// int ret = stbi_write_png(name, m_tmp[i].getWidth(), m_tmp[i].getHeight(), 4, data.get(), m_tmp[i].stride());
//}
Image data;
App::I->render_task([&]
{
glDisable(GL_BLEND);
glViewport(0, 0, m_latlong.getWidth(), m_latlong.getHeight());
glActiveTexture(GL_TEXTURE0);
m_latlong.bindFramebuffer();
ShaderManager::use(kShader::Equirect);
ShaderManager::u_mat4(kShaderUniform::MVP, glm::ortho(-.5f, .5f, -.5f, .5f, -1.f, 1.f));
ShaderManager::u_int(kShaderUniform::Tex, 0);
glBindTexture(GL_TEXTURE_CUBE_MAP, cube_id);
m_sampler.bind(0);
m_plane.draw_fill();
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
m_latlong.unbindFramebuffer();
draw_merge(false);
Texture2D equirect = m_layers_merge.gen_equirect();
data = equirect.get_image();
});
auto latlong_data = std::make_unique<uint8_t[]>(m_latlong.bytes());
m_latlong.readTextureData(latlong_data.get());
progress++;
LOG("progress: %f", (float)progress / total * 100.f);
if (App::I->layout.m_loaded)
{
pb->m_progress->SetWidthP((float)progress / total * 100.f);
}
LOG("writing %s", file_path.c_str());
if (file_path.substr(file_path.size() - 4) == ".jpg")
{
stbi_write_jpg(file_path.c_str(), m_latlong.getWidth(), m_latlong.getHeight(), 4, latlong_data.get(), 100);
data.save_jpg(file_path, 100);
inject_xmp(file_path);
}
else if (file_path.substr(file_path.size() - 4) == ".png")
{
stbi_write_png(file_path.c_str(), m_latlong.getWidth(), m_latlong.getHeight(), 4, latlong_data.get(), 0);
data.save_png(file_path);
}
progress++;
LOG("progress: %f", (float)progress / total * 100.f);
if (App::I->layout.m_loaded)
{
pb->m_progress->SetWidthP((float)progress / total * 100.f);
}
//int ret = stbi_write_png(name, m_latlong.getWidth(), m_latlong.getHeight(), 4, latlong_data.get(), m_latlong.stride());
#ifdef __IOS__
save_image_library(file_path);
#endif
App::I->render_task_async([id=cube_id]
{
glDeleteTextures(1, &id);
});
m_latlong.destroy();
if (App::I->layout.m_loaded)
{
pb->destroy();
}
}
void Canvas::inject_xmp(std::string jpg_path)
@@ -1990,7 +1825,7 @@ void Canvas::export_depth_thread(std::string file_name)
App::I->render_task([&]
{
draw_merge();
draw_merge(false);
rtt.bindFramebuffer();
rtt.clear({ 0, 0, 0, 1 });