adding color picking on iOS
This commit is contained in:
@@ -2,6 +2,11 @@
|
||||
#include "log.h"
|
||||
#include "canvas.h"
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <Foundation/Foundation.h>
|
||||
#endif
|
||||
|
||||
|
||||
ui::Canvas* ui::Canvas::I;
|
||||
std::vector<CanvasMode*> ui::Canvas::modes[] = {
|
||||
{ new CanvasModePen, new CanvasModeBasicCamera },
|
||||
@@ -44,6 +49,91 @@ glm::mat4 ui::Canvas::m_plane_transform[6] = {
|
||||
glm::lookAt(glm::vec3(), { 0,-1, 0}, {0, 0, 1}), // bottom
|
||||
};
|
||||
|
||||
void ui::Canvas::pick_start()
|
||||
{
|
||||
for (int i = 0; i < 6; i++)
|
||||
m_pick_ready[i] = false;
|
||||
}
|
||||
void ui::Canvas::pick_update(int plane)
|
||||
{
|
||||
// check if already updated
|
||||
if (m_pick_ready[plane])
|
||||
return;
|
||||
|
||||
// save viewport and clear color states
|
||||
GLint vp[4];
|
||||
GLfloat cc[4];
|
||||
glGetIntegerv(GL_VIEWPORT, vp);
|
||||
glGetFloatv(GL_COLOR_CLEAR_VALUE, cc);
|
||||
GLboolean blend = glIsEnabled(GL_BLEND);
|
||||
|
||||
// prepare common states
|
||||
glViewport(0, 0, m_width, m_height);
|
||||
glDisable(GL_BLEND);
|
||||
|
||||
//for (int i = 0; i < 6; i++)
|
||||
{
|
||||
int i = plane;
|
||||
m_tmp[i].bindFramebuffer();
|
||||
m_tmp[i].clear({ 1, 1, 1, 1 });
|
||||
|
||||
for (auto layer_index : m_order)
|
||||
{
|
||||
// copy to tmp2 for layer blending
|
||||
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();
|
||||
|
||||
m_layers[layer_index].m_rtt[i].bindTexture();
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
m_tex2[i].bind();
|
||||
m_sampler.bind(0);
|
||||
m_sampler_bg.bind(1);
|
||||
ShaderManager::use(ui::kShader::StrokeLayer);
|
||||
ShaderManager::u_int(kShaderUniform::TexBG, 1);
|
||||
ShaderManager::u_float(kShaderUniform::Alpha, m_layers[layer_index].m_opacity);
|
||||
ShaderManager::u_int(kShaderUniform::Tex, 0);
|
||||
ShaderManager::u_mat4(kShaderUniform::MVP, glm::ortho(-.5f, .5f, -.5f, .5f, -1.f, 1.f));
|
||||
m_plane.draw_fill();
|
||||
m_sampler.unbind();
|
||||
m_sampler_bg.unbind();
|
||||
m_tex2[i].unbind();
|
||||
m_layers[layer_index].m_rtt[i].unbindTexture();
|
||||
}
|
||||
if (!m_pick_data[plane])
|
||||
m_pick_data[plane] = std::make_unique<glm::u8vec4[]>(m_width*m_height);
|
||||
glReadPixels(0, 0, m_width, m_height, GL_RGBA, GL_UNSIGNED_BYTE, m_pick_data[plane].get());
|
||||
LOG("read plane %d", plane);
|
||||
m_tmp[i].unbindFramebuffer();
|
||||
}
|
||||
|
||||
// restore viewport and clear color states
|
||||
blend ? glEnable(GL_BLEND) : glDisable(GL_BLEND);
|
||||
glViewport(vp[0], vp[1], vp[2], vp[3]);
|
||||
glClearColor(cc[0], cc[1], cc[2], cc[3]);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
|
||||
|
||||
m_pick_ready[plane] = true;
|
||||
}
|
||||
glm::vec4 ui::Canvas::pick_get(glm::vec2 canvas_loc)
|
||||
{
|
||||
glm::vec3 ray_origin;
|
||||
glm::vec3 ray_dir;
|
||||
glm::vec3 hit_pos;
|
||||
glm::vec3 hit_normal;
|
||||
int plane_id;
|
||||
if (point_trace(canvas_loc, ray_origin, ray_dir, hit_pos, hit_normal, plane_id))
|
||||
{
|
||||
pick_update(plane_id);
|
||||
glm::vec2 fbpos = (hit_pos.xy() * 0.5f + 0.5f) * glm::vec2(m_width, m_height);
|
||||
LOG("pick plane %d x %d y %d", plane_id, (int)fbpos.x, (int)fbpos.y);
|
||||
int i = fbpos.x + fbpos.y * m_width;
|
||||
return glm::vec4(m_pick_data[plane_id][i]) / 255.f;
|
||||
}
|
||||
return {0,0,0,1};
|
||||
}
|
||||
void ui::Canvas::clear(const glm::vec4& c/*={0,0,0,1}*/)
|
||||
{
|
||||
snap_history({ 0, 1, 2, 3, 4, 5 });
|
||||
@@ -558,6 +648,9 @@ void ui::Canvas::clear_context()
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef __IOS__
|
||||
UIImage *image;
|
||||
#endif
|
||||
void ui::Canvas::export_equirectangular(std::string data_path)
|
||||
{
|
||||
// save viewport and clear color states
|
||||
@@ -657,6 +750,10 @@ void ui::Canvas::export_equirectangular(std::string data_path)
|
||||
params.m_quality = 100;
|
||||
bool saved = jpge::compress_image_to_jpeg_file(name, m_latlong.getWidth(), m_latlong.getHeight(), 4, latlong_data.get(), params);
|
||||
//int ret = stbi_write_png(name, m_latlong.getWidth(), m_latlong.getHeight(), 4, latlong_data.get(), m_latlong.stride());
|
||||
#ifdef __IOS__
|
||||
image = [UIImage imageWithContentsOfFile:[NSString stringWithUTF8String:name]];
|
||||
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
|
||||
#endif
|
||||
}
|
||||
|
||||
glDeleteTextures(1, &cube_id);
|
||||
|
||||
Reference in New Issue
Block a user