From f16adb8fcc45f43e1813a5d83fffa37a9ced7b1e Mon Sep 17 00:00:00 2001 From: omigamedev Date: Wed, 9 May 2018 12:04:29 +0200 Subject: [PATCH] fix framebuffer read --- PanoPainter/GameViewController.m | 35 +++++++++++++++++++++++++++++--- engine/app.cpp | 9 +++++++- engine/canvas.cpp | 4 ++-- engine/pch.h | 1 + engine/rtt.cpp | 18 ++++++++++------ engine/rtt.h | 3 ++- 6 files changed, 57 insertions(+), 13 deletions(-) diff --git a/PanoPainter/GameViewController.m b/PanoPainter/GameViewController.m index 0388b45..d969d94 100644 --- a/PanoPainter/GameViewController.m +++ b/PanoPainter/GameViewController.m @@ -188,6 +188,7 @@ NSThread* lock_thread; t_count = 0; } +std::set ignored_touch; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; @@ -198,6 +199,10 @@ NSThread* lock_thread; kEventSource source = touch.type == UITouchType::UITouchTypeStylus ? kEventSource::Stylus : kEventSource::Touch; if (source == kEventSource::Stylus) pen_down = true; + + if (touch.majorRadius > 25.f) + ignored_touch.insert(touch); + NSLog(@"touch down size %f", touch.majorRadius); [self async_lock]; App::I.mouse_down(0, touchLocation.x * scale, touchLocation.y * scale, touch.force, source); [self async_unlock]; @@ -209,16 +214,34 @@ NSThread* lock_thread; NSUInteger n = [[event allTouches] count]; float scale = self.view.contentScaleFactor; + std::vector valid_touches; + for (int i = 0; i < n; i++) + { + auto t = [[[event allTouches] allObjects] objectAtIndex:i]; + auto it = ignored_touch.find(t); + if (it == ignored_touch.end()) + { + valid_touches.push_back(t); + } + else + { + NSLog(@"ignored moved"); + } + } + n = valid_touches.size(); + if (n == 0) + return; + UITouch* t0 = nullptr; UITouch* t1 = nullptr; - t0 = [[[event allTouches] allObjects] objectAtIndex:0]; + t0 = valid_touches[0]; CGPoint tl0 = [t0 locationInView:self.view]; glm::vec2 p0 = glm::vec2(tl0.x * scale, tl0.y * scale); glm::vec2 p1; if (n > 1) { - t1 = [[[event allTouches] allObjects] objectAtIndex:1]; + t1 = valid_touches[1]; CGPoint tl1 = [t1 locationInView:self.view]; p1 = glm::vec2(tl1.x * scale, tl1.y * scale); } @@ -244,7 +267,7 @@ NSThread* lock_thread; } else if (n == 1) { - UITouch *touch = [[event allTouches] anyObject]; + UITouch *touch = valid_touches[0]; CGPoint touchLocation = [touch locationInView:self.view]; //auto p = glm::vec2(touchLocation.x * scale, touchLocation.y * scale); float force = touch.type == UITouchType::UITouchTypeStylus ? touch.force : 1.0f; @@ -267,6 +290,12 @@ NSThread* lock_thread; UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self.view]; float scale = self.view.contentScaleFactor; + + if (ignored_touch.count(touch)) + { + ignored_touch.erase(touch); + NSLog(@"remove ignored"); + } kEventSource source = touch.type == UITouchType::UITouchTypeStylus ? kEventSource::Stylus : kEventSource::Touch; pen_down = false; diff --git a/engine/app.cpp b/engine/app.cpp index 881f9c3..1914108 100644 --- a/engine/app.cpp +++ b/engine/app.cpp @@ -359,7 +359,14 @@ void App::update(float dt) #else glBindFramebuffer(GL_FRAMEBUFFER, 0); #endif - glReadBuffer(GL_BACK); + + GLint dfbo, rfbo; + glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &rfbo); + glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &dfbo); + if (dfbo != rfbo) + LOG("DIFFERENT FB"); + + glReadBuffer(GL_FRONT); glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data); { std::lock_guard lock(rec_mutex); diff --git a/engine/canvas.cpp b/engine/canvas.cpp index afb3bc4..c8c0605 100644 --- a/engine/canvas.cpp +++ b/engine/canvas.cpp @@ -1761,7 +1761,7 @@ ui::Layer::Snapshot ui::Layer::snapshot(std::string data_path) static int counter = 0; LOG("errno = %d", errno); glBindTexture(GL_TEXTURE_2D, 0); - glBindFramebuffer(GL_FRAMEBUFFER, 0); + //glBindFramebuffer(GL_FRAMEBUFFER, 0); for (int i = 0; i < 6; i++) { snap.m_dirty_box[i] = m_dirty_box[i]; @@ -1777,7 +1777,7 @@ ui::Layer::Snapshot ui::Layer::snapshot(std::string data_path) glm::vec2 box_sz = zw(m_dirty_box[i]) - xy(m_dirty_box[i]); glReadPixels(m_dirty_box[i].x, m_dirty_box[i].y, box_sz.x, box_sz.y, GL_RGBA, GL_UNSIGNED_BYTE, snap.image[i].get()); m_rtt[i].unbindFramebuffer(); - glReadBuffer(GL_NONE); + //glReadBuffer(GL_NONE); LOG("snapshot face %d - %d bytes (%dx%d)", i, (int)box_sz.x * (int)box_sz.y * 4, (int)box_sz.x, (int)box_sz.y); static char name[128]; diff --git a/engine/pch.h b/engine/pch.h index b8a12bd..f78f358 100644 --- a/engine/pch.h +++ b/engine/pch.h @@ -83,6 +83,7 @@ #ifdef __cplusplus #include +#include #include #include #include diff --git a/engine/rtt.cpp b/engine/rtt.cpp index 8d103a9..c53e807 100644 --- a/engine/rtt.cpp +++ b/engine/rtt.cpp @@ -74,6 +74,7 @@ bool RTT::create(int width, int height, int tex/* = -1*/, GLint internal_format) // glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height); // glBindRenderbuffer(GL_RENDERBUFFER, 0); + GLint oldFboID; glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldFboID); // Create a framebuffer object @@ -106,7 +107,8 @@ bool RTT::create(int width, int height, int tex/* = -1*/, GLint internal_format) // Switch back to window-system-provided framebuffer glBindFramebuffer(GL_FRAMEBUFFER, oldFboID); - oldFboID = 0; + oldRFboID = 0; + oldDFboID = 0; return status == GL_FRAMEBUFFER_COMPLETE; } @@ -123,14 +125,18 @@ void RTT::bindFramebuffer() } #endif // _DEBUG bound = true; - glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldFboID); - glBindFramebuffer(GL_FRAMEBUFFER, fboID); + glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldDFboID); + glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &oldRFboID); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboID); + glBindFramebuffer(GL_READ_FRAMEBUFFER, fboID); } void RTT::unbindFramebuffer() { - glBindFramebuffer(GL_FRAMEBUFFER, oldFboID); - oldFboID = 0; + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, oldDFboID); + glBindFramebuffer(GL_READ_FRAMEBUFFER, oldRFboID); + oldRFboID = 0; + oldDFboID = 0; bound = false; } @@ -147,7 +153,7 @@ void RTT::readTextureData(uint8_t* buffer) //glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer); glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer); unbindFramebuffer(); - glReadBuffer(GL_NONE); + //glReadBuffer(GL_NONE); } uint8_t* RTT::createBuffer() diff --git a/engine/rtt.h b/engine/rtt.h index b73b0c6..d706cc1 100644 --- a/engine/rtt.h +++ b/engine/rtt.h @@ -3,7 +3,8 @@ class RTT { bool bound = false; - GLint oldFboID = 0; + GLint oldRFboID = 0; + GLint oldDFboID = 0; GLuint fboID = 0; GLuint rboID = 0; GLuint texID = 0;