cube to equirect conversion using shader, save latlong to file

This commit is contained in:
2017-04-30 02:20:54 +01:00
parent 78a87f9cd3
commit f64e9e746c
9 changed files with 94 additions and 10 deletions

View File

@@ -346,6 +346,7 @@ void ui::Canvas::resize(int width, int height)
{
l.create(width, height, "");
}
m_latlong.create(width * 4, height * 2); // NOTE: w and h must be equal to make sense
}
bool ui::Canvas::create(int width, int height)
{
@@ -366,6 +367,7 @@ bool ui::Canvas::create(int width, int height)
{
l.create(width, height, "");
}
m_latlong.create(width * 4, height * 2); // NOTE: w and h must be equal to make sense
return true;
}
@@ -398,6 +400,7 @@ void ui::Canvas::clear_context()
m_tex[i].destroy();
m_tex2[i].destroy();
}
m_latlong.destroy();
};
void ui::Canvas::save(std::string data_path)
@@ -413,6 +416,18 @@ void ui::Canvas::save(std::string data_path)
glViewport(0, 0, m_width, m_height);
glDisable(GL_BLEND);
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 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
};
for (int i = 0; i < 6; i++)
{
m_tmp[i].bindFramebuffer();
@@ -421,7 +436,7 @@ void ui::Canvas::save(std::string data_path)
for (auto layer_index : m_order)
{
// copy to tmp2 for layer blending
glActiveTexture(GL_TEXTURE0);
glActiveTexture(GL_TEXTURE0); // TODO: maybe remove this line
m_tex2[i].bind();
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, m_width, m_height);
m_tex2[i].unbind();
@@ -443,17 +458,42 @@ void ui::Canvas::save(std::string data_path)
m_layers[layer_index].m_rtt[i].unbindTexture();
}
// 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();
}
auto data = std::make_unique<uint8_t[]>(m_tmp[0].bytes());
for (int i = 0; i < 6; i++)
// auto data = std::make_unique<uint8_t[]>(m_tmp[0].bytes());
// for (int i = 0; i < 6; 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());
// }
glViewport(0, 0, m_latlong.getWidth(), m_latlong.getHeight());
m_latlong.bindFramebuffer();
ui::ShaderManager::use(kShader::Equirect);
ui::ShaderManager::u_mat4(kShaderUniform::MVP, glm::ortho(-.5f, .5f, -.5f, .5f, -1.f, 1.f));
ui::ShaderManager::u_int(kShaderUniform::Tex, 0);
glBindTexture(GL_TEXTURE_CUBE_MAP, cube_id);
m_sampler.bind(0);
m_plane.draw_fill();
m_sampler.unbind();
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
m_latlong.unbindFramebuffer();
{
m_tmp[i].readTextureData(data.get());
auto latlong_data = std::make_unique<uint8_t[]>(m_latlong.bytes());
m_latlong.readTextureData(latlong_data.get());
static char name[128];
sprintf(name, "%s/Face%d.png", data_path.c_str(), i);
sprintf(name, "%s/latlong.png", data_path.c_str());
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());
int ret = stbi_write_png(name, m_latlong.getWidth(), m_latlong.getHeight(), 4, latlong_data.get(), m_latlong.stride());
}
// restore viewport and clear color states