diff --git a/modules/v4d/include/opencv2/v4d/formhelper.hpp b/modules/v4d/include/opencv2/v4d/formhelper.hpp index 67e63bceb..3e1719be8 100644 --- a/modules/v4d/include/opencv2/v4d/formhelper.hpp +++ b/modules/v4d/include/opencv2/v4d/formhelper.hpp @@ -6,9 +6,7 @@ #ifndef SRC_OPENCV_V4D_FORMHELPER_HPP_ #define SRC_OPENCV_V4D_FORMHELPER_HPP_ #include "dialog.hpp" -#ifndef OPENCV_V4D_USE_ES3 -#include -#endif + #include #include diff --git a/modules/v4d/include/opencv2/v4d/util.hpp b/modules/v4d/include/opencv2/v4d/util.hpp index af1531d25..7c7dd2feb 100644 --- a/modules/v4d/include/opencv2/v4d/util.hpp +++ b/modules/v4d/include/opencv2/v4d/util.hpp @@ -163,12 +163,11 @@ CV_EXPORTS Source makeCaptureSource(const string& inputFilename); #else /*! * Creates a WebCam source object to use in conjunction with #V4D::setSource(). - * In the background it uses emscripten's file system implementation to transfer frames from the camera to the source object * @param width The frame width to capture (usually the initial width of the V4D object) * @param height The frame height to capture (usually the initial height of the V4D object) * @return A WebCam source object. */ -CV_EXPORTS Source makeCaptureSource(int width, int height, cv::Ptr window); +CV_EXPORTS Source makeCaptureSource(int width, int height); #endif void resizePreserveAspectRatio(const cv::UMat& src, cv::UMat& output, const cv::Size& dstSize, const cv::Scalar& bgcolor = {0,0,0,255}); diff --git a/modules/v4d/include/opencv2/v4d/v4d.hpp b/modules/v4d/include/opencv2/v4d/v4d.hpp index 13285fb95..72b78c405 100644 --- a/modules/v4d/include/opencv2/v4d/v4d.hpp +++ b/modules/v4d/include/opencv2/v4d/v4d.hpp @@ -21,6 +21,7 @@ #endif #include + #include "source.hpp" #include "sink.hpp" #include "util.hpp" diff --git a/modules/v4d/samples/video-demo.cpp b/modules/v4d/samples/video-demo.cpp index 189311f11..8222e4c76 100644 --- a/modules/v4d/samples/video-demo.cpp +++ b/modules/v4d/samples/video-demo.cpp @@ -236,7 +236,7 @@ int main() { src.fps(), cv::Size(WIDTH, HEIGHT)); v4d->setSink(sink); #else - Source src = makeCaptureSource(WIDTH, HEIGHT, v4d); + Source src = makeCaptureSource(WIDTH, HEIGHT); v4d->setSource(src); #endif diff --git a/modules/v4d/src/detail/framebuffercontext.cpp b/modules/v4d/src/detail/framebuffercontext.cpp index eefbcd641..eed34ac58 100644 --- a/modules/v4d/src/detail/framebuffercontext.cpp +++ b/modules/v4d/src/detail/framebuffercontext.cpp @@ -52,10 +52,6 @@ void FrameBufferContext::init() { if (debug_) glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE); - if (offscreen_) - glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); - else - glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE); glfwSetTime(0); #ifdef __APPLE__ glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); @@ -82,7 +78,8 @@ void FrameBufferContext::init() { glfwWindowHint(GLFW_STENCIL_BITS, 8); glfwWindowHint(GLFW_DEPTH_BITS, 24); glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); - glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_FALSE); + glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); + glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE); glfwWindow_ = glfwCreateWindow(frameBufferSize_.width, frameBufferSize_.height, title_.c_str(), nullptr, sharedWindow_); @@ -92,6 +89,8 @@ void FrameBufferContext::init() { } this->makeCurrent(); glfwSwapInterval(0); + if(!offscreen_) + this->setVisible(true); #ifndef OPENCV_V4D_USE_ES3 if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) throw std::runtime_error("Could not initialize GLAD!"); @@ -410,12 +409,20 @@ void FrameBufferContext::execute(std::function fn) { cv::Point2f FrameBufferContext::toWindowCoord(const cv::Point2f& pt) { double bs = 1.0 / blitScale(); - return cv::Point2f((pt.x * bs) - blitOffsetX() * bs, (pt.y * bs) - blitOffsetY() * bs); +#ifdef __EMSCRIPTEN__ + return cv::Point2f(((pt.x * bs) - blitOffsetX()) * getXPixelRatio(), ((pt.y * bs) - blitOffsetY()) * getYPixelRatio()); +#else + return cv::Point2f(((pt.x * bs) - blitOffsetX()), ((pt.y * bs) - blitOffsetY())); +#endif } cv::Vec2f FrameBufferContext::toWindowCoord(const cv::Vec2f& pt) { double bs = 1.0 / blitScale(); - return cv::Vec2f((pt[0] * bs) - (blitOffsetX()), (pt[1] * bs) - (blitOffsetY())); +#ifdef __EMSCRIPTEN__ + return cv::Vec2f(((pt[0] * bs) - blitOffsetX()) * getXPixelRatio(), ((pt[1] * bs) - blitOffsetY()) * getYPixelRatio()); +#else + return cv::Vec2f(((pt[0] * bs) - blitOffsetX()), ((pt[1] * bs) - blitOffsetY())); +#endif } cv::ogl::Texture2D& FrameBufferContext::getTexture2D() { @@ -470,7 +477,6 @@ void FrameBufferContext::begin(GLenum framebufferTarget) { this->makeCurrent(); glGetIntegerv( GL_VIEWPORT, viewport_ ); glGetError(); - GL_CHECK(glBindFramebuffer(framebufferTarget, frameBufferID_)); GL_CHECK(glBindTexture(GL_TEXTURE_2D, textureID_)); GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, renderBufferID_)); diff --git a/modules/v4d/src/detail/nanoguicontext.cpp b/modules/v4d/src/detail/nanoguicontext.cpp index d8fdadeec..036d32913 100644 --- a/modules/v4d/src/detail/nanoguicontext.cpp +++ b/modules/v4d/src/detail/nanoguicontext.cpp @@ -2,6 +2,7 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. // Copyright Amir Hassan (kallaballa) + #include "opencv2/v4d/v4d.hpp" #include "nanoguicontext.hpp" @@ -15,7 +16,14 @@ NanoguiContext::NanoguiContext(V4D& v4d, FrameBufferContext& fbContext) : } void NanoguiContext::init() { +// GL_CHECK(glEnable(GL_DEPTH_TEST)); +// GL_CHECK(glDepthFunc(GL_LESS)); +// GL_CHECK(glEnable(GL_STENCIL_TEST)); +// GL_CHECK(glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP)); +// GL_CHECK(glStencilFunc(GL_ALWAYS, 0, 0xffffffff)); FrameBufferContext::GLScope glScope(fbCtx(), GL_DRAW_FRAMEBUFFER); + glClear(GL_STENCIL_BUFFER_BIT); +// glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); screen_ = new nanogui::Screen(); screen_->initialize(nguiFbContext_.getGLFWWindow(), false); fbCtx().setWindowSize(fbCtx().size()); @@ -37,11 +45,32 @@ void NanoguiContext::render() { // FrameBufferContext::GLScope glGlScope(fbCtx()); // FrameBufferContext::FrameBufferScope fbScope(fbCtx(), fb_); // preFB_.copyTo(fb_); -// } +// } glClear(GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); #endif { - FrameBufferContext::GLScope glScope(fbCtx()); + +// GL_CHECK(glEnable(GL_DEPTH_TEST)); +// GL_CHECK(glDepthFunc(GL_LESS)); +// GL_CHECK(glEnable(GL_STENCIL_TEST)); +// GL_CHECK(glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP)); +// GL_CHECK(glStencilFunc(GL_ALWAYS, 0, 0xffffffff)); + FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER); + glClear(GL_STENCIL_BUFFER_BIT); +// glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + +// float w = fbCtx().size().width; +// float h = fbCtx().size().height; +// float r = fbCtx().getXPixelRatio(); + +// nvgSave(context_); +// nvgBeginFrame(context_, w, h, r); + +// screen().draw_setup(); screen().draw_widgets(); +// screen().nvg_flush(); +// //FIXME make nvgCancelFrame possible +// nvgEndFrame(context_); +// nvgRestore(context_); } #ifdef __EMSCRIPTEN__ // { diff --git a/modules/v4d/src/detail/nanovgcontext.cpp b/modules/v4d/src/detail/nanovgcontext.cpp index fe396f014..5266d102b 100644 --- a/modules/v4d/src/detail/nanovgcontext.cpp +++ b/modules/v4d/src/detail/nanovgcontext.cpp @@ -5,17 +5,6 @@ #include "nanovgcontext.hpp" #include "opencv2/v4d/v4d.hpp" -#include "nanovg.h" - -#ifdef OPENCV_V4D_USE_ES3 -# define NANOVG_GLES3_IMPLEMENTATION 1 -# define NANOVG_GLES3 1 -#else -# define NANOVG_GL3 1 -# define NANOVG_GL3_IMPLEMENTATION 1 -#endif -#define NANOVG_GL_USE_UNIFORMBUFFER 1 -#include "nanovg_gl.h" namespace cv { namespace v4d { @@ -27,30 +16,19 @@ NanoVGContext::NanoVGContext(V4D& v4d, FrameBufferContext& fbContext) : } void NanoVGContext::init() { +// GL_CHECK(glEnable(GL_DEPTH_TEST)); +// GL_CHECK(glDepthFunc(GL_LESS)); +// GL_CHECK(glEnable(GL_STENCIL_TEST)); +// GL_CHECK(glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP)); +// GL_CHECK(glStencilFunc(GL_ALWAYS, 0, 0xffffffff)); +// GL_CHECK(glStencilMask(0x00)); FrameBufferContext::GLScope glScope(fbCtx(), GL_DRAW_FRAMEBUFFER); + glClear(GL_STENCIL_BUFFER_BIT); screen_ = new nanogui::Screen(); screen_->initialize(fbCtx().getGLFWWindow(), false); fbCtx().setWindowSize(fbCtx().size()); context_ = screen_->nvg_context(); -// FrameBufferContext::GLScope glScope(fbCtx()); -// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); -// glEnable(GL_BLEND); -// glEnable(GL_STENCIL_TEST); -// glEnable(GL_DEPTH_TEST); -// glDisable(GL_SCISSOR_TEST); -// glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); -// glStencilMask(0xffffffff); -// glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); -// glStencilFunc(GL_ALWAYS, 0, 0xffffffff); -// glStencilMask(0xFF); -// -// int flags = NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG; -//#ifdef OPENCV_V4D_USE_ES3 || __EMSCRIPTEN__ -// context_ = nvgCreateGLES3(flags); -//#else -// context_ = nvgCreateGL3(flags); -//#endif if (!context_) throw std::runtime_error("Could not initialize NanoVG!"); } @@ -70,7 +48,16 @@ void NanoVGContext::render(std::function fn) { // } #endif { +// GL_CHECK(glEnable(GL_DEPTH_TEST)); +// GL_CHECK(glDepthFunc(GL_LESS)); +// GL_CHECK(glEnable(GL_STENCIL_TEST)); +// GL_CHECK(glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP)); +// GL_CHECK(glStencilFunc(GL_ALWAYS, 0, 0xffffffff)); +// GL_CHECK(glStencilMask(0x00)); FrameBufferContext::GLScope glScope(fbCtx()); + glClear(GL_STENCIL_BUFFER_BIT); +// glClearColor(0.0f, 0.0f, 0.0f, 1.0f); +// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); NanoVGContext::Scope nvgScope(*this); cv::v4d::nvg::detail::NVG::initializeContext(context_); fn(fbCtx().size()); @@ -96,6 +83,7 @@ void NanoVGContext::begin() { float r = fbCtx().getXPixelRatio(); nvgSave(context_); +// glClear(GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); nvgBeginFrame(context_, w, h, r); //FIXME mirroring with text somehow doesn't work // nvgTranslate(context_, 0, h); diff --git a/modules/v4d/src/util.cpp b/modules/v4d/src/util.cpp index c1ab8f087..6acec691b 100644 --- a/modules/v4d/src/util.cpp +++ b/modules/v4d/src/util.cpp @@ -415,7 +415,7 @@ EM_JS(void,copyVideoFrame,(int p), { } }); -Source makeCaptureSource(int width, int height, cv::Ptr window) { +Source makeCaptureSource(int width, int height) { using namespace std; return Source([=](cv::UMat& frame) { diff --git a/modules/v4d/src/v4d.cpp b/modules/v4d/src/v4d.cpp index 56e946548..b9c667e6d 100644 --- a/modules/v4d/src/v4d.cpp +++ b/modules/v4d/src/v4d.cpp @@ -497,6 +497,7 @@ bool V4D::isVisible() { void V4D::setVisible(bool v) { fbCtx().setVisible(v); + nguiCtx().screen().perform_layout(); } bool V4D::isOffscreen() {