cube to equirect conversion using shader, save latlong to file
This commit is contained in:
@@ -370,7 +370,7 @@
|
||||
<icon width="100%" height="100%" icon="disk"/>
|
||||
</button-custom>-->
|
||||
|
||||
<button id="btn-switch" width="50" height="50" margin="0 0 5 0" text="Switch"/>
|
||||
<button id="btn-switch" width="50" height="50" margin="0 0 5 0" text="Save"/>
|
||||
<button id="btn-undo" width="50" height="50" margin="0 0 5 0" text="Undo"/>
|
||||
<button id="btn-close" width="50" height="50" margin="0 0 5 0" text="Clear"/>
|
||||
<button-custom id="btn-bucket" width="50" height="50" margin="0 0 5 0" thickness="1" border-color=".1" pad="2">
|
||||
|
||||
@@ -262,6 +262,35 @@ void App::initShaders()
|
||||
" frag = mix(c1, c2, alpha);\n"
|
||||
"}";
|
||||
|
||||
static const char* shader_equirect_v =
|
||||
SHADER_VERSION
|
||||
"#define PI 3.1415926535897932384626433832795\n"
|
||||
"#define TWO_PI 6.283185307179586476925286766559\n"
|
||||
"uniform mat4 mvp;\n"
|
||||
"in vec4 pos;\n"
|
||||
"in vec2 uvs;\n"
|
||||
"out vec2 uv;\n"
|
||||
"void main(){\n"
|
||||
" uv = (vec2(1.0) - uvs + vec2(0.25,0.0)) * vec2(TWO_PI, PI);\n"
|
||||
" gl_Position = mvp * vec4(pos.xyz, 1.0);\n"
|
||||
"}";
|
||||
static const char* shader_equirect_f =
|
||||
SHADER_VERSION
|
||||
"uniform samplerCube tex;\n"
|
||||
"in mediump vec2 uv;\n"
|
||||
"out mediump vec4 frag;\n"
|
||||
"void main(){\n"
|
||||
" float anglex = uv.x;\n"
|
||||
" float angley = uv.y;\n"
|
||||
" float sx = sin(anglex);\n"
|
||||
" float cx = cos(anglex);\n"
|
||||
" vec3 dir = vec3(0.0, 0.0, 0.0);\n"
|
||||
" dir.x = sin(angley) * cx;\n"
|
||||
" dir.y = cos(angley);\n"
|
||||
" dir.z = sin(angley) * sx;\n"
|
||||
" frag = texture(tex, dir);\n"
|
||||
"}";
|
||||
|
||||
LOG("initializing shaders");
|
||||
if (!ShaderManager::create(kShader::Texture, shader_v, shader_f))
|
||||
LOG("Failed to create shader Texture");
|
||||
@@ -287,6 +316,8 @@ void App::initShaders()
|
||||
LOG("Failed to create shader StrokeLayer");
|
||||
if (!ShaderManager::create(kShader::Checkerboard, shader_checkerboard_v, shader_checkerboard_f))
|
||||
LOG("Failed to create shader Checkerboard");
|
||||
if (!ShaderManager::create(kShader::Equirect, shader_equirect_v, shader_equirect_f))
|
||||
LOG("Failed to create shader Equirect");
|
||||
LOG("shaders initialized");
|
||||
}
|
||||
|
||||
@@ -444,7 +475,7 @@ void App::initLayout()
|
||||
//button->m_text->set_text(canvas->m_canvas->m_use_instanced ? "INST" : "NORM");
|
||||
}
|
||||
};
|
||||
button->m_text->set_text("NORM");
|
||||
//button->m_text->set_text("NORM");
|
||||
}
|
||||
if (auto* button = layout[main_id]->find<NodeButton>("btn-undo"))
|
||||
{
|
||||
|
||||
@@ -11,7 +11,7 @@ class App
|
||||
{
|
||||
public:
|
||||
static App I;
|
||||
std::string data_path;
|
||||
std::string data_path{ "." };
|
||||
Sampler sampler;
|
||||
Texture2D tex;
|
||||
LayoutManager layout;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -59,7 +59,10 @@ public:
|
||||
Sampler m_sampler_bg;
|
||||
glm::vec2 m_cam_rot;
|
||||
float m_cam_fov = 85;
|
||||
|
||||
|
||||
GLuint cube_id;
|
||||
RTT m_latlong;
|
||||
|
||||
std::vector<Layer::Snapshot> m_layers_snapshot;
|
||||
|
||||
bool create(int width, int height);
|
||||
|
||||
@@ -1968,6 +1968,13 @@ public:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//ui::ShaderManager::use(kShader::Equirect);
|
||||
//ui::ShaderManager::u_mat4(kShaderUniform::MVP, glm::scale(glm::vec3(.5, .5, 1)));
|
||||
//ui::ShaderManager::u_int(kShaderUniform::Tex, 0);
|
||||
//glBindTexture(GL_TEXTURE_CUBE_MAP, m_canvas->cube_id);
|
||||
//m_face_plane.draw_fill();
|
||||
//glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
||||
|
||||
blend ? glEnable(GL_BLEND) : glDisable(GL_BLEND);
|
||||
m_sampler.unbind();
|
||||
|
||||
@@ -16,6 +16,7 @@ public:
|
||||
|
||||
void destroy();
|
||||
bool create(int width, int height, int tex = -1);
|
||||
bool recreate() { return create(w, h); }
|
||||
void clear(glm::vec4 color = glm::vec4(0));
|
||||
void readTextureData(uint8_t* buffer);
|
||||
uint8_t* createBuffer();
|
||||
|
||||
@@ -30,6 +30,7 @@ enum class kShader : uint16_t
|
||||
StrokeErase = const_hash("stroke-erase"),
|
||||
StrokeLayer = const_hash("stroke-layer"),
|
||||
Checkerboard= const_hash("checkerboard"),
|
||||
Equirect = const_hash("equirect"),
|
||||
};
|
||||
|
||||
class Shader
|
||||
|
||||
@@ -97,6 +97,7 @@ void Sampler::set(GLint filter /*= GL_LINEAR*/, GLint wrap /*= GL_CLAMP_TO_EDGE*
|
||||
#if USE_SAMPLER
|
||||
glSamplerParameteri(id, GL_TEXTURE_WRAP_S, wrap);
|
||||
glSamplerParameteri(id, GL_TEXTURE_WRAP_T, wrap);
|
||||
glSamplerParameteri(id, GL_TEXTURE_WRAP_R, wrap);
|
||||
glSamplerParameteri(id, GL_TEXTURE_MIN_FILTER, filter);
|
||||
glSamplerParameteri(id, GL_TEXTURE_MAG_FILTER, filter);
|
||||
#endif // USE_SAMPLER
|
||||
|
||||
Reference in New Issue
Block a user