clunky but fixable multi wondow support

pull/3471/head
kallaballa 2 years ago
parent 9ad15c64d3
commit f0711a82b0
  1. 65
      src/common/viz2d.cpp
  2. 14
      src/common/viz2d.hpp
  3. 37
      src/optflow/optflow-demo.cpp

@ -20,6 +20,22 @@ void error_callback(int error, const char *description) {
}
}
template <typename T> void find_widgets(nanogui::Widget* parent, std::vector<T>& widgets) {
T w;
for(auto* child: parent->children()) {
find_widgets(child, widgets);
if((w = dynamic_cast<T>(child)) != nullptr) {
widgets.push_back(w);
}
}
}
bool contains_absolute(nanogui::Widget* w, const nanogui::Vector2i &p) {
nanogui::Vector2i d = p - w->absolute_position();
return d.x() >= 0 && d.y() >= 0 &&
d.x() < w->size().x() && d.y() < w->size().y();
}
cv::Scalar color_convert(const cv::Scalar& src, cv::ColorConversionCodes code) {
cv::Mat tmpIn(1,1,CV_8UC3);
cv::Mat tmpOut(1,1,CV_8UC3);
@ -134,9 +150,9 @@ bool Viz2DWindow::mouse_drag_event(const nanogui::Vector2i &p, const nanogui::Ve
}
Viz2D::Viz2D(const cv::Size &size, const cv::Size& frameBufferSize, bool offscreen, const string &title, int major, int minor, int samples, bool debug) :
initialSize_(size), frameBufferSize_(frameBufferSize), viewport_(0, 0, frameBufferSize.width, frameBufferSize.height), scale_(1), cursor_(0,0), offscreen_(offscreen), stretch_(false), title_(title), major_(major), minor_(minor), samples_(samples), debug_(debug) {
initialSize_(size), frameBufferSize_(frameBufferSize), viewport_(0, 0, frameBufferSize.width, frameBufferSize.height), scale_(1), mousePos_(0,0), offscreen_(offscreen), stretch_(false), title_(title), major_(major), minor_(minor), samples_(samples), debug_(debug), vaCaptureDeviceIndex_(0), vaWriterDeviceIndex_(0) {
assert(frameBufferSize_.width >= initialSize_.width && frameBufferSize_.height >= initialSize_.height);
initialize();
initializeWindowing();
}
Viz2D::~Viz2D() {
@ -153,7 +169,7 @@ Viz2D::~Viz2D() {
delete clglContext_;
}
void Viz2D::initialize() {
void Viz2D::initializeWindowing() {
assert(glfwInit() == GLFW_TRUE);
glfwSetErrorCallback(kb::viz2d::error_callback);
@ -163,7 +179,7 @@ void Viz2D::initialize() {
if (offscreen_)
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwSetTime(0);
// glfwSetTime(0);
#ifdef __APPLE__
glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
@ -208,12 +224,12 @@ void Viz2D::initialize() {
glfwSetCursorPosCallback(getGLFWWindow(), [](GLFWwindow *glfwWin, double x, double y) {
Viz2D* v2d = reinterpret_cast<Viz2D*>(glfwGetWindowUserPointer(glfwWin));
v2d->screen().cursor_pos_callback_event(x, y);
auto cursor = v2d->getCursor();
auto cursor = v2d->getMousePosition();
auto diff = cursor - cv::Vec2f(x, y);
if(v2d->isMouseDrag()) {
v2d->pan(diff[0], -diff[1]);
}
v2d->setCursor(x, y);
v2d->setMousePosition(x, y);
}
);
glfwSetMouseButtonCallback(getGLFWWindow(), [](GLFWwindow *glfwWin, int button, int action, int modifiers) {
@ -241,7 +257,16 @@ void Viz2D::initialize() {
);
glfwSetScrollCallback(getGLFWWindow(), [](GLFWwindow *glfwWin, double x, double y) {
Viz2D* v2d = reinterpret_cast<Viz2D*>(glfwGetWindowUserPointer(glfwWin));
v2d->screen().scroll_callback_event(x, y);
std::vector<nanogui::Widget*> widgets;
find_widgets(&v2d->screen(), widgets);
for(auto* w : widgets) {
auto mousePos = nanogui::Vector2i(v2d->getMousePosition()[0] / v2d->getXPixelRatio(), v2d->getMousePosition()[1] / v2d->getYPixelRatio());
if(contains_absolute(w, mousePos)) {
v2d->screen().scroll_callback_event(x, y);
return;
}
}
v2d->zoom(y < 0 ? 1.1 : 0.9);
}
);
@ -313,8 +338,8 @@ void Viz2D::setVideoFrameSize(const cv::Size& sz) {
}
void Viz2D::opengl(std::function<void(const cv::Size&)> fn) {
detail::CLExecScope_t scope(clglContext_->getCLExecContext());
detail::CLGLContext::GLScope glScope(*clglContext_);
detail::CLExecScope_t scope(clgl().getCLExecContext());
detail::CLGLContext::GLScope glScope(clgl());
fn(getFrameBufferSize());
}
@ -338,7 +363,7 @@ void Viz2D::write() {
});
}
void Viz2D::makeGLFWContextCurrent() {
void Viz2D::makeCurrent() {
glfwMakeContextCurrent(getGLFWWindow());
}
@ -425,7 +450,7 @@ void Viz2D::zoom(float factor) {
} else if(scale_ > 1) {
scale_ = 1;
viewport_.width = origW;
viewport_.height = origW;
viewport_.height = origH;
if(factor > 1) {
viewport_.x += log10(((viewport_.x * (1.0 - factor)) / viewport_.width) * 9 + 1.0) * viewport_.width;
viewport_.y += log10(((viewport_.y * (1.0 - factor)) / viewport_.height) * 9 + 1.0) * viewport_.height;
@ -433,6 +458,7 @@ void Viz2D::zoom(float factor) {
viewport_.x += log10(((-viewport_.x * (1.0 - factor)) / viewport_.width) * 9 + 1.0) * viewport_.width;
viewport_.y += log10(((-viewport_.y * (1.0 - factor)) / viewport_.height) * 9 + 1.0) * viewport_.height;
}
return;
}
cv::Vec2f offset;
@ -445,7 +471,7 @@ void Viz2D::zoom(float factor) {
float delta_y;
if(factor < 1.0) {
offset = cv::Vec2f(viewport_.x, viewport_.y) - cv::Vec2f(cursor_[0], origH - cursor_[1]);
offset = cv::Vec2f(viewport_.x, viewport_.y) - cv::Vec2f(mousePos_[0], origH - mousePos_[1]);
delta_x = offset[0] / oldW;
delta_y = offset[1] / oldH;
} else {
@ -468,12 +494,18 @@ void Viz2D::zoom(float factor) {
}
}
cv::Vec2f Viz2D::getCursor() {
return cursor_;
cv::Vec2f Viz2D::getPosition() {
int x, y;
glfwGetWindowPos(getGLFWWindow(), &x, &y);
return {float(x), float(y)};
}
cv::Vec2f Viz2D::getMousePosition() {
return mousePos_;
}
void Viz2D::setCursor(int x, int y) {
cursor_ = {float(x), float(y)};
void Viz2D::setMousePosition(int x, int y) {
mousePos_ = {float(x), float(y)};
}
float Viz2D::getScale() {
@ -643,6 +675,7 @@ void Viz2D::setAccelerated(bool a) {
bool Viz2D::display() {
bool result = true;
if (!offscreen_) {
makeCurrent();
glfwPollEvents();
screen().draw_contents();
clglContext_->blitFrameBufferToScreen(getViewport(), getWindowSize(), isStretching());

@ -60,7 +60,7 @@ class Viz2D: public nanogui::Screen {
cv::Size frameBufferSize_;
cv::Rect viewport_;
float scale_;
cv::Vec2f cursor_;
cv::Vec2f mousePos_;
bool offscreen_;
bool stretch_;
string title_;
@ -85,7 +85,8 @@ class Viz2D: public nanogui::Screen {
public:
Viz2D(const cv::Size &initialSize, const cv::Size& frameBufferSize, bool offscreen, const string &title, int major = 4, int minor = 6, int samples = 0, bool debug = false);
virtual ~Viz2D();
void initialize();
void initializeWindowing();
void makeCurrent();
cv::ogl::Texture2D& texture();
void opengl(std::function<void(const cv::Size&)> fn);
@ -103,8 +104,8 @@ public:
bool isMouseDrag();
void pan(int x, int y);
void zoom(float factor);
cv::Vec2f getCursor();
void setCursor(int x, int y);
cv::Vec2f getPosition();
cv::Vec2f getMousePosition();
float getScale();
cv::Rect getViewport();
void setWindowSize(const cv::Size& sz);
@ -149,14 +150,13 @@ public:
return var;
}
private:
virtual bool keyboard_event(int key, int scancode, int action, int modifiers);
virtual bool keyboard_event(int key, int scancode, int action, int modifiers) override;
void setMousePosition(int x, int y);
CLGLContext& clgl();
CLVAContext& clva();
NanoVGContext& nvg();
nanogui::Screen& screen();
void makeGLFWContextCurrent();
GLFWwindow* getGLFWWindow();
NVGcontext* getNVGcontext();
};

@ -256,8 +256,11 @@ void composite_layers(cv::UMat& background, const cv::UMat& foreground, const cv
kb::viz2d::Viz2DWindow* effectWindow;
kb::viz2d::Viz2DWindow* settingsWindow;
kb::viz2d::Viz2DWindow* menuWindow;
void setup_gui(cv::Ptr<kb::viz2d::Viz2D> v2d) {
void setup_gui(cv::Ptr<kb::viz2d::Viz2D> v2d, cv::Ptr<kb::viz2d::Viz2D> v2dMenu) {
v2d->makeCurrent();
effectWindow = new kb::viz2d::Viz2DWindow(v2d, 5, 30, "Effects");
v2d->form()->set_window(effectWindow);
@ -304,7 +307,7 @@ void setup_gui(cv::Ptr<kb::viz2d::Viz2D> v2d) {
}
use_bloom = b;
});
settingsWindow = new kb::viz2d::Viz2DWindow(v2d, 240, 30, "Settings");
settingsWindow = new kb::viz2d::Viz2DWindow(v2d, 220, 320, "Settings");
v2d->form()->set_window(settingsWindow);
v2d->makeGroup("Hardware Acceleration");
@ -314,15 +317,26 @@ void setup_gui(cv::Ptr<kb::viz2d::Viz2D> v2d) {
v2d->makeFormVariable("Threshold", scene_change_thresh, 0.1f, 1.0f, true, "", "Peak threshold. Lowering it makes detection more sensitive");
v2d->makeFormVariable("Threshold Diff", scene_change_thresh_diff, 0.1f, 1.0f, true, "", "Difference of peak thresholds. Lowering it makes detection more sensitive");
v2d->makeGroup("Display");
v2d->makeFormVariable("Show FPS", show_fps, "Enable or disable the On-screen FPS display");
v2d->makeFormVariable("Stetch", stretch, "Stretch the frame buffer to the window size")->set_callback([=](const bool& s) {
v2dMenu->makeCurrent();
menuWindow = new kb::viz2d::Viz2DWindow(v2dMenu, 0, 0, "Display");
v2dMenu->form()->set_window(menuWindow);
v2dMenu->makeGroup("Display");
v2dMenu->makeFormVariable("Show FPS", show_fps, "Enable or disable the On-screen FPS display");
v2dMenu->makeFormVariable("Stetch", stretch, "Stretch the frame buffer to the window size")->set_callback([=](const bool& s) {
v2d->setStretching(s);
});
v2d->form()->add_button("Fullscreen", [=]() {
v2dMenu->form()->add_button("Fullscreen", [=]() {
v2d->setFullscreen(!v2d->isFullscreen());
});
v2dMenu->form()->add_button("Offscreen", [=]() {
v2d->setOffscreen(!v2d->isOffscreen());
});
menuWindow->center();
v2d->makeCurrent();
}
int main(int argc, char **argv) {
@ -332,13 +346,18 @@ int main(int argc, char **argv) {
exit(1);
}
cv::Ptr<Viz2D> v2d = new Viz2D(cv::Size(WIDTH, HEIGHT), cv::Size(WIDTH, HEIGHT), OFFSCREEN, "Sparse Optical Flow Demo");
cv::Ptr<Viz2D> v2dMenu = new Viz2D(cv::Size(240, 360), cv::Size(240,360), false, "Display Settings");
v2d->makeCurrent();
print_system_info();
if(!v2d->isOffscreen()) {
setup_gui(v2d);
setup_gui(v2d, v2dMenu);
v2d->setVisible(true);
v2dMenu->setVisible(true);
}
v2d->makeCurrent();
auto capture = v2d->makeVACapture(argv[1], VA_HW_DEVICE_INDEX);
if (!capture.isOpened()) {
@ -359,6 +378,7 @@ int main(int argc, char **argv) {
vector<cv::Point2f> detectedPoints;
while (true) {
v2d->makeCurrent();
if(v2d->isAccelerated() != use_acceleration)
v2d->setAccelerated(use_acceleration);
@ -402,6 +422,9 @@ int main(int argc, char **argv) {
//If onscreen rendering is enabled it displays the framebuffer in the native window. Returns false if the window was closed.
if(!v2d->display())
break;
if(!v2dMenu->display())
break;
}
return 0;

Loading…
Cancel
Save