pull/3471/head
kallaballa 2 years ago
parent eb541b7a73
commit eb65ea00b2
  1. 4
      modules/v4d/include/opencv2/v4d/formhelper.hpp
  2. 3
      modules/v4d/include/opencv2/v4d/util.hpp
  3. 1
      modules/v4d/include/opencv2/v4d/v4d.hpp
  4. 2
      modules/v4d/samples/video-demo.cpp
  5. 22
      modules/v4d/src/detail/framebuffercontext.cpp
  6. 33
      modules/v4d/src/detail/nanoguicontext.cpp
  7. 46
      modules/v4d/src/detail/nanovgcontext.cpp
  8. 2
      modules/v4d/src/util.cpp
  9. 1
      modules/v4d/src/v4d.cpp

@ -6,9 +6,7 @@
#ifndef SRC_OPENCV_V4D_FORMHELPER_HPP_ #ifndef SRC_OPENCV_V4D_FORMHELPER_HPP_
#define SRC_OPENCV_V4D_FORMHELPER_HPP_ #define SRC_OPENCV_V4D_FORMHELPER_HPP_
#include "dialog.hpp" #include "dialog.hpp"
#ifndef OPENCV_V4D_USE_ES3
#include <glad/glad.h>
#endif
#include <nanogui/screen.h> #include <nanogui/screen.h>
#include <nanogui/formhelper.h> #include <nanogui/formhelper.h>

@ -163,12 +163,11 @@ CV_EXPORTS Source makeCaptureSource(const string& inputFilename);
#else #else
/*! /*!
* Creates a WebCam source object to use in conjunction with #V4D::setSource(). * 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 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) * @param height The frame height to capture (usually the initial height of the V4D object)
* @return A WebCam source object. * @return A WebCam source object.
*/ */
CV_EXPORTS Source makeCaptureSource(int width, int height, cv::Ptr<V4D> window); CV_EXPORTS Source makeCaptureSource(int width, int height);
#endif #endif
void resizePreserveAspectRatio(const cv::UMat& src, cv::UMat& output, const cv::Size& dstSize, const cv::Scalar& bgcolor = {0,0,0,255}); void resizePreserveAspectRatio(const cv::UMat& src, cv::UMat& output, const cv::Size& dstSize, const cv::Scalar& bgcolor = {0,0,0,255});

@ -21,6 +21,7 @@
#endif #endif
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "source.hpp" #include "source.hpp"
#include "sink.hpp" #include "sink.hpp"
#include "util.hpp" #include "util.hpp"

@ -236,7 +236,7 @@ int main() {
src.fps(), cv::Size(WIDTH, HEIGHT)); src.fps(), cv::Size(WIDTH, HEIGHT));
v4d->setSink(sink); v4d->setSink(sink);
#else #else
Source src = makeCaptureSource(WIDTH, HEIGHT, v4d); Source src = makeCaptureSource(WIDTH, HEIGHT);
v4d->setSource(src); v4d->setSource(src);
#endif #endif

@ -52,10 +52,6 @@ void FrameBufferContext::init() {
if (debug_) if (debug_)
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE); glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
if (offscreen_)
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
else
glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE);
glfwSetTime(0); glfwSetTime(0);
#ifdef __APPLE__ #ifdef __APPLE__
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@ -82,7 +78,8 @@ void FrameBufferContext::init() {
glfwWindowHint(GLFW_STENCIL_BITS, 8); glfwWindowHint(GLFW_STENCIL_BITS, 8);
glfwWindowHint(GLFW_DEPTH_BITS, 24); glfwWindowHint(GLFW_DEPTH_BITS, 24);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); 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, glfwWindow_ = glfwCreateWindow(frameBufferSize_.width, frameBufferSize_.height, title_.c_str(), nullptr,
sharedWindow_); sharedWindow_);
@ -92,6 +89,8 @@ void FrameBufferContext::init() {
} }
this->makeCurrent(); this->makeCurrent();
glfwSwapInterval(0); glfwSwapInterval(0);
if(!offscreen_)
this->setVisible(true);
#ifndef OPENCV_V4D_USE_ES3 #ifndef OPENCV_V4D_USE_ES3
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
throw std::runtime_error("Could not initialize GLAD!"); throw std::runtime_error("Could not initialize GLAD!");
@ -410,12 +409,20 @@ void FrameBufferContext::execute(std::function<void(cv::UMat&)> fn) {
cv::Point2f FrameBufferContext::toWindowCoord(const cv::Point2f& pt) { cv::Point2f FrameBufferContext::toWindowCoord(const cv::Point2f& pt) {
double bs = 1.0 / blitScale(); 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) { cv::Vec2f FrameBufferContext::toWindowCoord(const cv::Vec2f& pt) {
double bs = 1.0 / blitScale(); 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() { cv::ogl::Texture2D& FrameBufferContext::getTexture2D() {
@ -470,7 +477,6 @@ void FrameBufferContext::begin(GLenum framebufferTarget) {
this->makeCurrent(); this->makeCurrent();
glGetIntegerv( GL_VIEWPORT, viewport_ ); glGetIntegerv( GL_VIEWPORT, viewport_ );
glGetError(); glGetError();
GL_CHECK(glBindFramebuffer(framebufferTarget, frameBufferID_)); GL_CHECK(glBindFramebuffer(framebufferTarget, frameBufferID_));
GL_CHECK(glBindTexture(GL_TEXTURE_2D, textureID_)); GL_CHECK(glBindTexture(GL_TEXTURE_2D, textureID_));
GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, renderBufferID_)); GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, renderBufferID_));

@ -2,6 +2,7 @@
// It is subject to the license terms in the LICENSE file found in the top-level directory // 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. // of this distribution and at http://opencv.org/license.html.
// Copyright Amir Hassan (kallaballa) <amir@viel-zu.org> // Copyright Amir Hassan (kallaballa) <amir@viel-zu.org>
#include "opencv2/v4d/v4d.hpp" #include "opencv2/v4d/v4d.hpp"
#include "nanoguicontext.hpp" #include "nanoguicontext.hpp"
@ -15,7 +16,14 @@ NanoguiContext::NanoguiContext(V4D& v4d, FrameBufferContext& fbContext) :
} }
void NanoguiContext::init() { 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); 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_ = new nanogui::Screen();
screen_->initialize(nguiFbContext_.getGLFWWindow(), false); screen_->initialize(nguiFbContext_.getGLFWWindow(), false);
fbCtx().setWindowSize(fbCtx().size()); fbCtx().setWindowSize(fbCtx().size());
@ -37,11 +45,32 @@ void NanoguiContext::render() {
// FrameBufferContext::GLScope glGlScope(fbCtx()); // FrameBufferContext::GLScope glGlScope(fbCtx());
// FrameBufferContext::FrameBufferScope fbScope(fbCtx(), fb_); // FrameBufferContext::FrameBufferScope fbScope(fbCtx(), fb_);
// preFB_.copyTo(fb_); // preFB_.copyTo(fb_);
// } // } glClear(GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
#endif #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().draw_widgets();
// screen().nvg_flush();
// //FIXME make nvgCancelFrame possible
// nvgEndFrame(context_);
// nvgRestore(context_);
} }
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
// { // {

@ -5,17 +5,6 @@
#include "nanovgcontext.hpp" #include "nanovgcontext.hpp"
#include "opencv2/v4d/v4d.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 cv {
namespace v4d { namespace v4d {
@ -27,30 +16,19 @@ NanoVGContext::NanoVGContext(V4D& v4d, FrameBufferContext& fbContext) :
} }
void NanoVGContext::init() { 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); FrameBufferContext::GLScope glScope(fbCtx(), GL_DRAW_FRAMEBUFFER);
glClear(GL_STENCIL_BUFFER_BIT);
screen_ = new nanogui::Screen(); screen_ = new nanogui::Screen();
screen_->initialize(fbCtx().getGLFWWindow(), false); screen_->initialize(fbCtx().getGLFWWindow(), false);
fbCtx().setWindowSize(fbCtx().size()); fbCtx().setWindowSize(fbCtx().size());
context_ = screen_->nvg_context(); 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_) if (!context_)
throw std::runtime_error("Could not initialize NanoVG!"); throw std::runtime_error("Could not initialize NanoVG!");
} }
@ -70,7 +48,16 @@ void NanoVGContext::render(std::function<void(const cv::Size&)> fn) {
// } // }
#endif #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()); 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); NanoVGContext::Scope nvgScope(*this);
cv::v4d::nvg::detail::NVG::initializeContext(context_); cv::v4d::nvg::detail::NVG::initializeContext(context_);
fn(fbCtx().size()); fn(fbCtx().size());
@ -96,6 +83,7 @@ void NanoVGContext::begin() {
float r = fbCtx().getXPixelRatio(); float r = fbCtx().getXPixelRatio();
nvgSave(context_); nvgSave(context_);
// glClear(GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
nvgBeginFrame(context_, w, h, r); nvgBeginFrame(context_, w, h, r);
//FIXME mirroring with text somehow doesn't work //FIXME mirroring with text somehow doesn't work
// nvgTranslate(context_, 0, h); // nvgTranslate(context_, 0, h);

@ -415,7 +415,7 @@ EM_JS(void,copyVideoFrame,(int p), {
} }
}); });
Source makeCaptureSource(int width, int height, cv::Ptr<V4D> window) { Source makeCaptureSource(int width, int height) {
using namespace std; using namespace std;
return Source([=](cv::UMat& frame) { return Source([=](cv::UMat& frame) {

@ -497,6 +497,7 @@ bool V4D::isVisible() {
void V4D::setVisible(bool v) { void V4D::setVisible(bool v) {
fbCtx().setVisible(v); fbCtx().setVisible(v);
nguiCtx().screen().perform_layout();
} }
bool V4D::isOffscreen() { bool V4D::isOffscreen() {

Loading…
Cancel
Save