adding color picking on iOS

This commit is contained in:
2017-08-02 19:51:59 +01:00
parent d01ec65cad
commit 235862c9d1
13 changed files with 202 additions and 15 deletions

View File

@@ -157,6 +157,7 @@ int t_count = 0;
- (void)viewDidAppear:(BOOL)animated
{
App::I.redraw = true;
[self resignFirstResponder];
[self registerForKeyboardNotifications];
}
@@ -235,6 +236,8 @@ int t_count = 0;
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
if (!App::I.redraw)
return;
App::I.clear();
App::I.update(0);
}

View File

@@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Save the pano image in the user gallery</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>

View File

@@ -353,6 +353,7 @@
</button-custom>
<button id="btn-undo" width="50" height="100%" margin="0 5 0 0" text="Undo"/>
<button id="btn-pen" width="50" height="100%" margin="0 0 0 0" text="Pen"/>
<button id="btn-pick" width="50" height="100%" margin="0 0 0 0" text="Pick"/>
<button id="btn-erase" width="60" height="100%" margin="0 0 0 0" text="Erase"/>
<button id="btn-line" width="50" height="100%" margin="0 0 0 0" text="Line"/>
<button id="btn-cam" width="70" height="100%" margin="0 0 0 0" text="Camera"/>

View File

@@ -278,7 +278,7 @@
AD4C08D21E89BD0F0051D85F /* bezier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = bezier.h; sourceTree = "<group>"; };
AD4C08D31E89BD0F0051D85F /* brush.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = brush.cpp; sourceTree = "<group>"; };
AD4C08D41E89BD0F0051D85F /* brush.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = brush.h; sourceTree = "<group>"; };
AD4C08D51E89BD0F0051D85F /* canvas.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = canvas.cpp; sourceTree = "<group>"; };
AD4C08D51E89BD0F0051D85F /* canvas.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.objcpp; fileEncoding = 4; path = canvas.cpp; sourceTree = "<group>"; };
AD4C08D61E89BD0F0051D85F /* canvas.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = canvas.h; sourceTree = "<group>"; };
AD4C08D71E89BD0F0051D85F /* rtt.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rtt.cpp; sourceTree = "<group>"; };
AD4C08D81E89BD0F0051D85F /* rtt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rtt.h; sourceTree = "<group>"; };

View File

@@ -88,7 +88,8 @@ void App::init()
LOG("Screen Resolution: %dx%d", (int)width, (int)height);
zoom = ceilf(width / 2000.f);
//zoom = ceilf(width / 2000.f);
//zoom = 2;
#ifdef __IOS__
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
@@ -119,6 +120,9 @@ void App::update(float dt)
// update offscreen stuff
if (canvas && canvas->m_canvas)
canvas->m_canvas->stroke_draw();
if (!redraw)
return;
//glClearColor(.1f, .1f, .1f, 1.f);
//glViewport(0, 0, (GLsizei)width, (GLsizei)height);
@@ -129,6 +133,13 @@ void App::update(float dt)
#endif
if (auto* main = layout[main_id])
main->update(width, height, zoom);
static glm::vec4 color_button_normal{.1, .1, .1, 1};
static glm::vec4 color_button_hlight{ 1, .0, .0, 1};
CanvasModePen* mode = (CanvasModePen*)canvas->m_canvas->modes[(int)Canvas::kCanvasMode::Draw][0];
layout[main_id]->find<NodeButton>("btn-pick")->set_color(
mode->m_picking ? color_button_hlight : color_button_normal);
auto observer = [this](Node* n)
{
@@ -145,7 +156,9 @@ void App::update(float dt)
if (auto* main = layout[main_id])
main->watch(observer);
//msgbox->watch(observer);
glDisable(GL_SCISSOR_TEST);
glDisable(GL_SCISSOR_TEST);
redraw = false;
}

View File

@@ -49,10 +49,13 @@ public:
float width;
float height;
bool keys[256];
bool redraw = true;
glm::vec2 gesture_p0;
glm::vec2 gesture_p1;
#ifdef __ANDROID__
float zoom = 3.0;
#elif __IOS__
float zoom = 2.0;
#else
float zoom = 1.0;
#endif // __ANDROID__

View File

@@ -5,6 +5,7 @@ using namespace ui;
void App::resize(float w, float h)
{
redraw = true;
width = w;
height = h;
if (auto* main = layout[main_id])
@@ -13,6 +14,7 @@ void App::resize(float w, float h)
void App::showKeyboard()
{
redraw = true;
#ifdef __IOS__
[ios_view becomeFirstResponder];
#endif
@@ -20,6 +22,7 @@ void App::showKeyboard()
void App::hideKeyboard()
{
redraw = true;
#ifdef __IOS__
[ios_view resignFirstResponder];
#endif
@@ -27,6 +30,7 @@ void App::hideKeyboard()
bool App::mouse_down(int button, float x, float y, float pressure)
{
redraw = true;
MouseEvent e;
e.m_type = button ? kEventType::MouseDownR : kEventType::MouseDownL;
e.m_pos = { x / zoom, y / zoom };
@@ -37,6 +41,7 @@ bool App::mouse_down(int button, float x, float y, float pressure)
}
bool App::mouse_move(float x, float y, float pressure)
{
redraw = true;
MouseEvent e;
e.m_type = kEventType::MouseMove;
e.m_pos = { x / zoom, y / zoom };
@@ -48,6 +53,7 @@ bool App::mouse_move(float x, float y, float pressure)
}
bool App::mouse_up(int button, float x, float y)
{
redraw = true;
MouseEvent e;
e.m_type = button ? kEventType::MouseUpR : kEventType::MouseUpL;
e.m_pos = { x / zoom, y / zoom };
@@ -57,6 +63,7 @@ bool App::mouse_up(int button, float x, float y)
}
bool App::mouse_scroll(float x, float y, float delta)
{
redraw = true;
MouseEvent e;
e.m_type = kEventType::MouseScroll;
e.m_pos = { x / zoom, y / zoom };
@@ -67,6 +74,7 @@ bool App::mouse_scroll(float x, float y, float delta)
}
bool App::mouse_cancel(int button)
{
redraw = true;
MouseEvent e;
e.m_type = kEventType::MouseCancel;
auto ret = layout[main_id]->on_event(&e);
@@ -75,6 +83,7 @@ bool App::mouse_cancel(int button)
}
bool App::gesture_start(const glm::vec2& p0, const glm::vec2& p1)
{
redraw = true;
GestureEvent e;
glm::vec2 p = glm::lerp(p0, p1, 0.5f);
e.m_type = kEventType::GestureStart;
@@ -88,6 +97,7 @@ bool App::gesture_start(const glm::vec2& p0, const glm::vec2& p1)
}
bool App::gesture_move(const glm::vec2& p0, const glm::vec2& p1)
{
redraw = true;
GestureEvent e;
glm::vec2 p = glm::lerp(p0, p1, 0.5f);
e.m_type = kEventType::GestureMove;
@@ -101,6 +111,7 @@ bool App::gesture_move(const glm::vec2& p0, const glm::vec2& p1)
}
bool App::gesture_end()
{
redraw = true;
GestureEvent e;
e.m_type = kEventType::GestureEnd;
auto ret = layout[main_id]->on_event(&e);
@@ -109,6 +120,7 @@ bool App::gesture_end()
}
bool App::key_down(kKey key)
{
redraw = true;
keys[(int)key] = true;
KeyEvent e;
e.m_type = kEventType::KeyDown;
@@ -119,6 +131,7 @@ bool App::key_down(kKey key)
}
bool App::key_up(kKey key)
{
redraw = true;
keys[(int)key] = false;
KeyEvent e;
e.m_type = kEventType::KeyUp;
@@ -129,6 +142,7 @@ bool App::key_up(kKey key)
}
bool App::key_char(char key)
{
redraw = true;
KeyEvent e;
e.m_type = kEventType::KeyChar;
e.m_char = key;

View File

@@ -127,6 +127,16 @@ void App::initLayout()
layout[main_id]->find<NodeButton>("btn-pen")->set_color(color_button_hlight);
Canvas::set_mode(Canvas::kCanvasMode::Draw);
}
if (auto* button = layout[main_id]->find<NodeButton>("btn-pick"))
{
button->on_click = [this](Node*) {
CanvasModePen* mode = (CanvasModePen*)canvas->m_canvas->modes[(int)Canvas::kCanvasMode::Draw][0];
if (mode)
{
mode->m_picking = true;
}
};
}
if (auto* button = layout[main_id]->find<NodeButton>("btn-erase"))
{
button->on_click = [this](Node*) {

View File

@@ -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);

View File

@@ -65,6 +65,8 @@ public:
RTT m_tmp[6];
Texture2D m_tex[6];
Texture2D m_tex2[6];
bool m_pick_ready[6];
std::unique_ptr<glm::u8vec4[]> m_pick_data[6];
static glm::vec3 m_plane_origin[6];
static glm::vec3 m_plane_normal[6];
static glm::vec3 m_plane_tangent[6];
@@ -104,6 +106,9 @@ public:
void stroke_cancel();
void stroke_commit();
void clear(const glm::vec4& color = { 1, 1, 1, 0 });
void pick_start();
void pick_update(int plane);
glm::vec4 pick_get(glm::vec2 canvas_loc);
void snapshot_save(std::string data_path);
void snapshot_restore();
void snap_history(const std::vector<int>& planes);

View File

@@ -72,9 +72,10 @@ void CanvasModePen::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
switch (me->m_type)
{
case kEventType::MouseDownL:
if (App::I.keys[(int)kKey::KeyAlt])
if (App::I.keys[(int)kKey::KeyAlt] || m_picking)
{
m_picking = true;
canvas->pick_start();
}
else
{
@@ -94,14 +95,46 @@ void CanvasModePen::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
{
m_picking = false;
node->mouse_release();
int x = me->m_pos.x;
int y = App::I.height - me->m_pos.y - 1;
float pix[3];
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, pix);
glm::vec4 pix = canvas->pick_get(loc);
auto hsv = convert_rgb2hsv(glm::vec3(pix[0], pix[1], pix[2]));
App::I.color->m_hue->set_value(hsv.x);
App::I.color->m_quad->set_value(1.f - hsv.y, 1.f - hsv.z);
/*
glm::vec3 ray_origin;
glm::vec3 ray_dir;
glm::vec3 hit_pos;
glm::vec3 hit_normal;
int plane_id;
if (canvas->point_trace(loc, ray_origin, ray_dir, hit_pos, hit_normal, plane_id))
{
canvas->m_layers[canvas->m_current_layer_idx].m_rtt[plane_id].bindFramebuffer();
glm::vec2 fbpos = (hit_pos.xy() * 0.5f + 0.5f) * glm::vec2(canvas->m_width, canvas->m_height);
int x = fbpos.x;
int y = fbpos.y;
glm::u8vec4 sample;
glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &sample);
glm::vec3 pix = glm::vec3(sample) / 255.f;
auto hsv = convert_rgb2hsv(glm::vec3(pix[0], pix[1], pix[2]));
App::I.color->m_hue->set_value(hsv.x);
App::I.color->m_quad->set_value(1.f - hsv.y, 1.f - hsv.z);
canvas->m_layers[canvas->m_current_layer_idx].m_rtt[plane_id].unbindFramebuffer();
}
*/
/*
glm::vec2 fbpos = me->m_pos * node->root()->m_zoom;
int x = fbpos.x;
int y = App::I.height - fbpos.y - 1;
glm::i8vec4 sample;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glReadBuffer(GL_FRONT);
glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &sample);
glm::vec3 pix = glm::vec3(sample) / 255.f;
auto hsv = convert_rgb2hsv(glm::vec3(pix[0], pix[1], pix[2]));
App::I.color->m_hue->set_value(hsv.x);
App::I.color->m_quad->set_value(1.f - hsv.y, 1.f - hsv.z);
*/
}
break;
case kEventType::MouseMove:
@@ -109,11 +142,7 @@ void CanvasModePen::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
canvas->stroke_update(loc, me->m_pressure);
if (m_picking)
{
int x = me->m_pos.x;
int y = App::I.height - me->m_pos.y - 1;
float pix[3];
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, pix);
glm::vec4 pix = canvas->pick_get(loc);
auto hsv = convert_rgb2hsv(glm::vec3(pix[0], pix[1], pix[2]));
App::I.color->m_hue->set_value(hsv.x);
App::I.color->m_quad->set_value(1.f - hsv.y, 1.f - hsv.z);

View File

@@ -35,13 +35,13 @@ public:
class CanvasModePen : public CanvasMode
{
bool m_dragging = false;
bool m_picking = false;
glm::vec2 m_pan_start;
float m_camera_fov;
float m_zoom_canvas = 1.f;
float m_zoom_start;
public:
virtual void on_MouseEvent(MouseEvent* me, glm::vec2& loc) override;
bool m_picking = false;
};
class CanvasModeLine : public CanvasMode

View File

@@ -10,6 +10,11 @@
#define TARGET_OS_IOS 1
#define __IOS__ 1
#include <CoreFoundation/CoreFoundation.h>
#ifdef __OBJC__
#include <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>
#endif
#include <OpenGLES/ES3/gl.h>
#include <OpenGLES/ES3/glext.h>
#define SHADER_VERSION "#version 300 es\n"
@@ -17,6 +22,11 @@
#define TARGET_OS_IOS 1
#define __IOS__ 1
#include <CoreFoundation/CoreFoundation.h>
#ifdef __OBJC__
#include <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>
#endif
#include <OpenGLES/ES3/gl.h>
#include <OpenGLES/ES3/glext.h>
#define SHADER_VERSION "#version 300 es\n"