implemented internal framebuffer resizing

pull/3471/head
kallaballa 2 years ago
parent fe3e98f8cf
commit 52bd3b8499
  1. 20
      modules/v4d/include/opencv2/v4d/v4d.hpp
  2. 2
      modules/v4d/samples/beauty-demo.cpp
  3. 29
      modules/v4d/samples/cube-demo.cpp
  4. 50
      modules/v4d/samples/example_v4d_optflow-demo.html
  5. 50
      modules/v4d/samples/example_v4d_shader-demo.html
  6. 3
      modules/v4d/samples/font-demo.cpp
  7. 2
      modules/v4d/samples/font_with_gui.cpp
  8. 3
      modules/v4d/samples/nanovg-demo.cpp
  9. 4
      modules/v4d/samples/optflow-demo.cpp
  10. 2
      modules/v4d/samples/pedestrian-demo.cpp
  11. 33
      modules/v4d/samples/shader-demo.cpp
  12. 7
      modules/v4d/samples/video-demo.cpp
  13. 8
      modules/v4d/samples/video_editing.cpp
  14. 9
      modules/v4d/src/detail/framebuffercontext.cpp
  15. 21
      modules/v4d/src/detail/glcontext.cpp
  16. 21
      modules/v4d/src/detail/nanovgcontext.cpp
  17. 97
      modules/v4d/src/v4d.cpp

@ -118,7 +118,6 @@ class NVG;
class CV_EXPORTS V4D {
friend class NanoVGContext;
const cv::Size initialSize_;
cv::Size frameBufferSize_;
cv::Rect viewport_;
float scale_;
cv::Vec2f mousePos_;
@ -171,8 +170,7 @@ public:
* @param samples MSAA samples.
* @param debug Create a debug OpenGL context.
*/
CV_EXPORTS static cv::Ptr<V4D> make(const cv::Size& initialSize,
const cv::Size& frameBufferSize, bool offscreen, const string& title, int major = 3,
CV_EXPORTS static cv::Ptr<V4D> make(const cv::Size& initialSize, bool offscreen, const string& title, int major = 3,
int minor = 2, bool compat = false, int samples = 0, bool debug = false);
/*!
* Default destructor
@ -307,7 +305,7 @@ public:
* Get the current viewport.
* @return The current viewport.
*/
CV_EXPORTS cv::Rect getViewport();
CV_EXPORTS cv::Rect& viewport();
/*!
* Set the window size.
* @param sz The new window size.
@ -404,19 +402,7 @@ public:
CV_EXPORTS bool display();
CV_EXPORTS void printSystemInfo();
private:
/*!
* Creates a V4D object which is the central object to perform visualizations with.
* @param initialSize The initial size of the heavy-weight window.
* @param frameBufferSize The initial size of the framebuffer backing the window (needs to be equal or greate then initial size).
* @param offscreen Don't create a window and rather render offscreen.
* @param title The window title.
* @param major The OpenGL major version to request.
* @param minor The OpenGL minor version to request.
* @param compat Request a compatibility context.
* @param samples MSAA samples.
* @param debug Create a debug OpenGL context.
*/
CV_EXPORTS V4D(const cv::Size& initialSize, const cv::Size& frameBufferSize, bool offscreen,
V4D(const cv::Size& initialSize, bool offscreen,
const string& title, int major = 3, int minor = 2, bool compat = false, int samples = 0, bool debug = false);
void setDefaultKeyboardEventCallback();
void setKeyboardEventCallback(

@ -48,7 +48,7 @@ bool side_by_side = false;
bool stretch = false;
#endif
static cv::Ptr<cv::v4d::V4D> v4d = cv::v4d::V4D::make(cv::Size(WIDTH, HEIGHT), cv::Size(WIDTH, HEIGHT), OFFSCREEN, "Beauty Demo");
static cv::Ptr<cv::v4d::V4D> v4d = cv::v4d::V4D::make(cv::Size(WIDTH, HEIGHT), OFFSCREEN, "Beauty Demo");
static cv::Ptr<cv::face::Facemark> facemark = cv::face::createFacemarkLBF(); //Face landmark detection
#ifdef USE_TRACKER
static cv::Ptr<cv::Tracker> tracker = cv::TrackerKCF::create(); //Instead of continues face detection we can use a tracker

@ -5,9 +5,6 @@
#include <opencv2/v4d/v4d.hpp>
//adapted from https://gitlab.com/wikibooks-opengl/modern-tutorials/-/blob/master/tut05_cube/cube.cpp
#include <cstdio>
#include <cstdlib>
#include <cmath>
constexpr long unsigned int WIDTH = 1920;
constexpr long unsigned int HEIGHT = 1080;
@ -30,7 +27,7 @@ unsigned int shader_program;
unsigned int vao;
unsigned int uniform_transform;
static cv::Ptr<cv::v4d::V4D> v4d = cv::v4d::V4D::make(cv::Size(WIDTH, HEIGHT), cv::Size(WIDTH, HEIGHT),
static cv::Ptr<cv::v4d::V4D> v4d = cv::v4d::V4D::make(cv::Size(WIDTH, HEIGHT),
OFFSCREEN, "Cube Demo");
static GLuint load_shader() {
@ -167,8 +164,11 @@ static void render_scene() {
-sin(angle), 0.0,cos(angle), 0.0,
0.0, 0.0, 0.0, 1.0);
cv::Matx44f rotZMat(cos(angle), -sin(angle), 0.0, 0.0, sin(angle), cos(angle), 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0);
cv::Matx44f rotZMat(
cos(angle), -sin(angle), 0.0, 0.0,
sin(angle), cos(angle), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
cv::Matx44f transform = scaleMat * rotXMat * rotYMat * rotZMat;
@ -205,19 +205,18 @@ static void glow_effect(const cv::UMat& src, cv::UMat& dst, const int ksize) {
static bool iteration() {
using namespace cv::v4d;
v4d->gl(init_scene);
//Render using OpenGL
v4d->gl(render_scene);
////If we have OpenCL and maybe even CL-GL sharing then this is faster than the glow shader. Without OpenCL this is very slow.
//#ifndef __EMSCRIPTEN__
// //Aquire the frame buffer for use by OpenCL
// v4d->fb([&](cv::UMat& frameBuffer) {
// //Glow effect (OpenCL)
// glow_effect(frameBuffer, frameBuffer, GLOW_KERNEL_SIZE);
// });
//#endif
//To slow for WASM
#ifndef __EMSCRIPTEN__
//Aquire the frame buffer for use by OpenCL
v4d->fb([&](cv::UMat& frameBuffer) {
//Glow effect (OpenCL)
glow_effect(frameBuffer, frameBuffer, GLOW_KERNEL_SIZE);
});
#endif
updateFps(v4d, true);

@ -109,10 +109,6 @@
</span>
</span>
<canvas id="cameraCanvas" width="1920" height="1080" style="display: none;"></canvas>
<canvas id="canvas1" width="1920" height="1080" style="display: none;"></canvas>
<canvas id="canvas2" width="1920" height="1080" style="display: none;"></canvas>
<canvas id="canvas3" width="1920" height="1080" style="display: none;"></canvas>
<canvas id="canvas4" width="1920" height="1080" style="display: none;"></canvas>
<video id="video" width="1920" height="1080" autoplay style="display: none;"></video>
<div class="emscripten" id="status">Downloading...</div>
@ -128,10 +124,6 @@
<script type='text/javascript'>
var statusElement = document.getElementById('status');
var progressElement = document.getElementById('progress');
var canvas1 = document.getElementById('canvas1');
var canvas2 = document.getElementById('canvas2');
var canvas3 = document.getElementById('canvas3');
var canvas4 = document.getElementById('canvas4');
function fixCanvasSize() {
Module.canvas.style.width = (1920 / window.devicePixelRatio)+ "px";
@ -169,48 +161,6 @@
},
canvas: (function() {
var canvas = document.getElementById('canvas');
canvas.addEventListener("resize", function(e) {
console.log(e);
var w = canvas.width;
var h = canvas.height;
canvas1.width = w;
canvas1.height = h;
canvas2.width = w;
canvas2.height = h;
canvas3.width = w;
canvas3.height = h;
canvas4.width = w;
canvas4.height = h;
}, false);
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
// application robust, you may want to override this behavior before shipping!
// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);
return canvas;
})(),
canvas1: (function() {
var canvas = document.getElementById('canvas1');
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
// application robust, you may want to override this behavior before shipping!
// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);
return canvas;
})(),
canvas2: (function() {
var canvas = document.getElementById('canvas2');
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
// application robust, you may want to override this behavior before shipping!
// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);
return canvas;
})(),
canvas3: (function() {
var canvas = document.getElementById('canvas3');
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
// application robust, you may want to override this behavior before shipping!

@ -109,10 +109,6 @@
</span>
</span>
<canvas id="cameraCanvas" width="1920" height="1080" style="display: none;"></canvas>
<canvas id="canvas1" width="1920" height="1080" style="display: none;"></canvas>
<canvas id="canvas2" width="1920" height="1080" style="display: none;"></canvas>
<canvas id="canvas3" width="1920" height="1080" style="display: none;"></canvas>
<canvas id="canvas4" width="1920" height="1080" style="display: none;"></canvas>
<video id="video" width="1920" height="1080" autoplay style="display: none;"></video>
<div class="emscripten" id="status">Downloading...</div>
@ -128,10 +124,6 @@
<script type='text/javascript'>
var statusElement = document.getElementById('status');
var progressElement = document.getElementById('progress');
var canvas1 = document.getElementById('canvas1');
var canvas2 = document.getElementById('canvas2');
var canvas3 = document.getElementById('canvas3');
var canvas4 = document.getElementById('canvas4');
function fixCanvasSize() {
Module.canvas.style.width = (1920 / window.devicePixelRatio)+ "px";
@ -169,48 +161,6 @@
},
canvas: (function() {
var canvas = document.getElementById('canvas');
canvas.addEventListener("resize", function(e) {
console.log(e);
var w = canvas.width;
var h = canvas.height;
canvas1.width = w;
canvas1.height = h;
canvas2.width = w;
canvas2.height = h;
canvas3.width = w;
canvas3.height = h;
canvas4.width = w;
canvas4.height = h;
}, false);
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
// application robust, you may want to override this behavior before shipping!
// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);
return canvas;
})(),
canvas1: (function() {
var canvas = document.getElementById('canvas1');
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
// application robust, you may want to override this behavior before shipping!
// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);
return canvas;
})(),
canvas2: (function() {
var canvas = document.getElementById('canvas2');
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
// application robust, you may want to override this behavior before shipping!
// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);
return canvas;
})(),
canvas3: (function() {
var canvas = document.getElementById('canvas3');
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
// application robust, you may want to override this behavior before shipping!

@ -45,7 +45,7 @@ using std::string;
using std::vector;
using std::istringstream;
static cv::Ptr<cv::v4d::V4D> v4d = cv::v4d::V4D::make(cv::Size(WIDTH, HEIGHT), cv::Size(WIDTH, HEIGHT), OFFSCREEN, "Font Demo");
static cv::Ptr<cv::v4d::V4D> v4d = cv::v4d::V4D::make(cv::Size(WIDTH, HEIGHT), OFFSCREEN, "Font Demo");
vector<string> lines;
static bool update_stars = true;
static bool update_perspective = true;
@ -216,7 +216,6 @@ int main() {
v4d->printSystemInfo();
//The text to display
string txt = cv::getBuildInformation();
//Save the text to a vector

@ -26,7 +26,7 @@ int main() {
});
v4d->run([&]() {
v4d->clear();
v4d->clear();
//Render the text at the center of the screen
v4d->nvg([&](const Size& sz) {
using namespace cv::v4d::nvg;

@ -15,7 +15,7 @@ constexpr const char *OUTPUT_FILENAME = "nanovg-demo.mkv";
using std::cerr;
using std::endl;
static cv::Ptr<cv::v4d::V4D> v4d = cv::v4d::V4D::make(cv::Size(WIDTH, HEIGHT), cv::Size(WIDTH, HEIGHT), OFFSCREEN, "NanoVG Demo");
static cv::Ptr<cv::v4d::V4D> v4d = cv::v4d::V4D::make(cv::Size(WIDTH, HEIGHT), OFFSCREEN, "NanoVG Demo");
static void draw_color_wheel(float x, float y, float w, float h, float hue) {
//color wheel drawing code taken from https://github.com/memononen/nanovg/blob/master/example/demo.c
@ -198,7 +198,6 @@ int main() {
v4d->setSource(src);
#endif
v4d->run(iteration);
return 0;

@ -47,9 +47,9 @@ constexpr const char* OUTPUT_FILENAME = "optflow-demo.mkv";
#endif
constexpr bool OFFSCREEN = false;
static cv::Ptr<cv::v4d::V4D> v4d = cv::v4d::V4D::make(cv::Size(WIDTH, HEIGHT), cv::Size(WIDTH, HEIGHT), OFFSCREEN, "Sparse Optical Flow Demo");
static cv::Ptr<cv::v4d::V4D> v4d = cv::v4d::V4D::make(cv::Size(WIDTH, HEIGHT), OFFSCREEN, "Sparse Optical Flow Demo");
#ifndef __EMSCRIPTEN__
static cv::Ptr<cv::v4d::V4D> v4d2 = cv::v4d::V4D::make(cv::Size(240, 360), cv::Size(240,360), false, "Display Settings");
static cv::Ptr<cv::v4d::V4D> v4d2 = cv::v4d::V4D::make(cv::Size(240, 360), false, "Display Settings");
#endif
/** Visualization parameters **/

@ -29,7 +29,7 @@ using std::endl;
using std::vector;
using std::string;
static cv::Ptr<cv::v4d::V4D> v4d = cv::v4d::V4D::make(cv::Size(WIDTH, HEIGHT), cv::Size(WIDTH, HEIGHT), OFFSCREEN, "Pedestrian Demo");
static cv::Ptr<cv::v4d::V4D> v4d = cv::v4d::V4D::make(cv::Size(WIDTH, HEIGHT), OFFSCREEN, "Pedestrian Demo");
static cv::HOGDescriptor hog;
//adapted from cv::dnn_objdetect::InferBbox

@ -11,14 +11,14 @@ using std::endl;
constexpr long unsigned int WIDTH = 1920;
constexpr long unsigned int HEIGHT = 1080;
constexpr bool OFFSCREEN = false;
const unsigned long DIAG = hypot(double(WIDTH), double(HEIGHT));
#ifndef __EMSCRIPTEN__
constexpr double FPS = 60;
constexpr const char* OUTPUT_FILENAME = "shader-demo.mkv";
#endif
const unsigned long DIAG = hypot(double(WIDTH), double(HEIGHT));
static cv::Ptr<cv::v4d::V4D> v4d = cv::v4d::V4D::make(cv::Size(WIDTH, HEIGHT),
cv::Size(WIDTH, HEIGHT), OFFSCREEN, "Shader Demo");
static cv::Ptr<cv::v4d::V4D> v4d = cv::v4d::V4D::make(cv::Size(WIDTH, HEIGHT), OFFSCREEN, "Shader Demo");
int glow_kernel_size = std::max(int(DIAG / 200 % 2 == 0 ? DIAG / 200 + 1 : DIAG / 200), 1);
@ -85,21 +85,6 @@ static void load_buffer_data() {
glBindVertexArray(0);
}
//workaround: required with emscripten on every iteration before rendering
static void rebind_buffers() {
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*) 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
//mandelbrot shader code adapted from my own project: https://github.com/kallaballa/FractalDive#after
static void load_shader() {
#ifndef OPENCV_V4D_USE_ES3
@ -168,7 +153,7 @@ static void load_shader() {
outColor = vec4(base_color[0] * iterations * cb, base_color[1] * iterations * cb, base_color[2] * iterations * cb, base_color[3]);
} else {
//outColor = vec4(0,0,0,0);
outColor = vec4(0,0,0,0);
}
}
@ -191,6 +176,8 @@ static float easeInOutQuint(float x) {
}
static void init_scene(const cv::Size& sz) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
load_shader();
load_buffer_data();
@ -325,8 +312,8 @@ static void setup_gui(cv::Ptr<cv::v4d::V4D> v4dMain) {
}
static bool iteration() {
v4d->capture();
// return false;
if(!v4d->capture())
return false;
//Render using OpenGL
v4d->gl(render_scene);
@ -378,8 +365,8 @@ int main() {
FPS, cv::Size(WIDTH, HEIGHT));
v4d->setSink(sink);
#else
// Source src = makeCaptureSource(WIDTH, HEIGHT);
// v4d->setSource(src);
Source src = makeCaptureSource(WIDTH, HEIGHT);
v4d->setSource(src);
#endif
v4d->run(iteration);

@ -25,8 +25,8 @@ unsigned int shader_program;
unsigned int vao;
unsigned int uniform_transform;
static cv::Ptr<cv::v4d::V4D> v4d = cv::v4d::V4D::make(cv::Size(WIDTH, HEIGHT), cv::Size(WIDTH, HEIGHT),
OFFSCREEN, "Cube Demo");
static cv::Ptr<cv::v4d::V4D> v4d = cv::v4d::V4D::make(cv::Size(WIDTH, HEIGHT),
OFFSCREEN, "Video Demo");
static GLuint load_shader() {
#ifndef OPENCV_V4D_USE_ES3
@ -189,7 +189,8 @@ static void glow_effect(const cv::UMat& src, cv::UMat& dst, const int ksize) {
static bool iteration() {
using namespace cv::v4d;
v4d->capture();
if(!v4d->capture())
return false;
//Render using OpenGL
v4d->gl(render_scene);

@ -1,10 +1,10 @@
#include <opencv2/v4d/v4d.hpp>
#ifndef __EMSCRIPTEN__
int main(int argc, char** argv) {
#else
int main() {
#endif
//In case of emscripten
CV_UNUSED(argc);
CV_UNUSED(argv);
using namespace cv;
using namespace cv::v4d;

@ -25,7 +25,6 @@ FrameBufferContext::FrameBufferContext(const cv::Size& frameBufferSize, bool off
if(parent_ != nullptr)
textureID_ = parent_->textureID_;
#else
CV_UNUSED(sharedTexture);
isShared_ = false;
#endif
glfwSetErrorCallback(cv::v4d::glfw_error_callback);
@ -285,6 +284,9 @@ cv::Size FrameBufferContext::getSize() {
}
void FrameBufferContext::execute(std::function<void(cv::UMat&)> fn) {
if(frameBuffer_.empty())
frameBuffer_.create(getSize(), CV_8UC4);
cv::resize(frameBuffer_,frameBuffer_, getSize());
#ifndef __EMSCRIPTEN__
CLExecScope_t clExecScope(getCLExecContext());
#endif
@ -336,7 +338,6 @@ void FrameBufferContext::begin() {
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID_, 0));
assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
GL_CHECK(glViewport(0, 0, frameBufferSize_.width, frameBufferSize_.height));
}
void FrameBufferContext::end() {
@ -369,8 +370,6 @@ void FrameBufferContext::acquireFromGL(cv::UMat& m) {
if (clglSharing_) {
GL_CHECK(fromGLTexture2D(getTexture2D(), m));
} else {
if (m.empty())
m.create(getSize(), CV_8UC4);
download(m);
GL_CHECK(glFlush());
GL_CHECK(glFinish());
@ -385,8 +384,6 @@ void FrameBufferContext::releaseToGL(cv::UMat& m) {
if (clglSharing_) {
GL_CHECK(toGLTexture2D(m, getTexture2D()));
} else {
if (m.empty())
m.create(getSize(), CV_8UC4);
upload(m);
GL_CHECK(glFlush());
GL_CHECK(glFinish());

@ -13,25 +13,18 @@ GLContext::GLContext(FrameBufferContext& fbContext) :
}
void GLContext::render(std::function<void(const cv::Size&)> fn) {
// cv::UMat tmp;
#ifdef __EMSCRIPTEN__
{
#ifndef __EMSCRIPTEN__
CLExecScope_t scope(mainFbContext_.getCLExecContext());
#endif
FrameBufferContext::GLScope mainGlScope(mainFbContext_);
FrameBufferContext::FrameBufferScope fbScope(mainFbContext_, fb_);
fb_.copyTo(preFB_);
}
// cvtColor(preFB_, tmp, cv::COLOR_BGRA2GRAY);
// cerr << "nz1: " << cv::countNonZero(tmp) << endl;
{
#ifndef __EMSCRIPTEN__
CLExecScope_t scope(glFbContext_.getCLExecContext());
#endif
FrameBufferContext::GLScope glGlScope(glFbContext_);
FrameBufferContext::FrameBufferScope fbScope(glFbContext_, fb_);
preFB_.copyTo(fb_);
}
#endif
{
#ifndef __EMSCRIPTEN__
CLExecScope_t scope(glFbContext_.getCLExecContext());
@ -39,24 +32,18 @@ void GLContext::render(std::function<void(const cv::Size&)> fn) {
FrameBufferContext::GLScope glScope(glFbContext_);
fn(glFbContext_.getSize());
}
#ifdef __EMSCRIPTEN__
{
#ifndef __EMSCRIPTEN__
CLExecScope_t scope(glFbContext_.getCLExecContext());
#endif
FrameBufferContext::GLScope glScope(glFbContext_);
FrameBufferContext::FrameBufferScope fbScope(glFbContext_, fb_);
fb_.copyTo(postFB_);
}
// cvtColor(postFB_, tmp, cv::COLOR_BGRA2GRAY);
// cerr << "nz2: " << cv::countNonZero(tmp) << endl;
{
#ifndef __EMSCRIPTEN__
CLExecScope_t scope(mainFbContext_.getCLExecContext());
#endif
FrameBufferContext::GLScope mainGlScope(mainFbContext_);
FrameBufferContext::FrameBufferScope fbScope(mainFbContext_, fb_);
postFB_.copyTo(fb_);
}
#endif
}
FrameBufferContext& GLContext::fbCtx() {

@ -10,32 +10,24 @@ namespace v4d {
namespace detail {
NanoVGContext::NanoVGContext(FrameBufferContext& fbContext) :
mainFbContext_(fbContext), nvgFbContext_(fbContext) {
#ifndef __EMSCRIPTEN__
CLExecScope_t scope(nvgFbContext_.getCLExecContext());
#endif
FrameBufferContext::GLScope nvgGlScope(nvgFbContext_);
screen_ = new nanogui::Screen();
screen_->initialize(nvgFbContext_.getGLFWWindow(), false);
context_ = screen_->nvg_context();
}
void NanoVGContext::render(std::function<void(const cv::Size&)> fn) {
#ifdef __EMSCRIPTEN__
{
#ifndef __EMSCRIPTEN__
CLExecScope_t scope(mainFbContext_.getCLExecContext());
#endif
FrameBufferContext::GLScope mainGlScope(mainFbContext_);
FrameBufferContext::FrameBufferScope fbScope(mainFbContext_, fb_);
fb_.copyTo(preFB_);
}
{
#ifndef __EMSCRIPTEN__
CLExecScope_t scope(nvgFbContext_.getCLExecContext());
#endif
FrameBufferContext::GLScope nvgGlScope(nvgFbContext_);
FrameBufferContext::FrameBufferScope fbScope(nvgFbContext_, fb_);
preFB_.copyTo(fb_);
}
#endif
{
#ifndef __EMSCRIPTEN__
CLExecScope_t scope(nvgFbContext_.getCLExecContext());
@ -45,23 +37,18 @@ void NanoVGContext::render(std::function<void(const cv::Size&)> fn) {
cv::v4d::nvg::detail::NVG::initializeContext(context_);
fn(nvgFbContext_.getSize());
}
#ifdef __EMSCRIPTEN__
{
#ifndef __EMSCRIPTEN__
CLExecScope_t scope(nvgFbContext_.getCLExecContext());
#endif
FrameBufferContext::GLScope nvgGlScope(nvgFbContext_);
FrameBufferContext::FrameBufferScope fbScope(nvgFbContext_, fb_);
fb_.copyTo(postFB_);
}
{
#ifndef __EMSCRIPTEN__
CLExecScope_t scope(mainFbContext_.getCLExecContext());
#endif
FrameBufferContext::GLScope mainGlScope(mainFbContext_);
FrameBufferContext::FrameBufferScope fbScope(mainFbContext_, fb_);
postFB_.copyTo(fb_);
}
#endif
}
void NanoVGContext::begin() {

@ -65,26 +65,28 @@ void resizeKeepAspectRatio(const cv::UMat& src, cv::UMat& output, const cv::Size
}
cv::Ptr<V4D> V4D::make(const cv::Size& size, const string& title, bool debug) {
cv::Ptr<V4D> v4d = new V4D(size, size, false, title, 4, 6, true, 0, debug);
cv::Ptr<V4D> v4d = new V4D(size, false, title, 4, 6, true, 0, debug);
v4d->setVisible(true);
return v4d;
}
cv::Ptr<V4D> V4D::make(const cv::Size& initialSize, const cv::Size& frameBufferSize,
bool offscreen, const string& title, int major, int minor, bool compat, int samples, bool debug) {
return new V4D(initialSize, frameBufferSize, offscreen, title, major, minor, compat, samples, debug);
cv::Ptr<V4D> V4D::make(const cv::Size& initialSize, bool offscreen, const string& title, int major,
int minor, bool compat, int samples, bool debug) {
return new V4D(initialSize, offscreen, title, major, minor, compat, samples, debug);
}
V4D::V4D(const cv::Size& size, const cv::Size& frameBufferSize, bool offscreen,
const string& title, int major, int minor, bool compat, int samples, bool debug) :
initialSize_(size), frameBufferSize_(frameBufferSize), viewport_(0, 0,
frameBufferSize.width, frameBufferSize.height), scale_(1), mousePos_(0, 0), stretch_(false), offscreen_(offscreen) {
assert(
frameBufferSize_.width >= initialSize_.width
&& frameBufferSize_.height >= initialSize_.height);
mainFbContext_ = new detail::FrameBufferContext(this->getFrameBufferSize(), offscreen, title, major, minor, compat, samples, debug, nullptr, nullptr);
V4D::V4D(const cv::Size& size, bool offscreen, const string& title, int major, int minor,
bool compat, int samples, bool debug) :
initialSize_(size), viewport_(0, 0, size.width, size.height), scale_(1), mousePos_(0, 0), stretch_(
false), offscreen_(offscreen) {
mainFbContext_ = new detail::FrameBufferContext(size, offscreen, title,
major, minor, compat, samples, debug, nullptr, nullptr);
if(!initializeGUI())
clvaContext_ = new detail::CLVAContext(*mainFbContext_);
glContext_ = new detail::GLContext(*mainFbContext_);
nvgContext_ = new detail::NanoVGContext(*mainFbContext_);
if (!initializeGUI())
assert(false);
}
@ -108,7 +110,6 @@ V4D::~V4D() {
bool V4D::initializeGUI() {
try {
FrameBufferContext::GLScope mainGlScope(*mainFbContext_);
screen_ = new nanogui::Screen();
screen().initialize(getGLFWWindow(), false);
form_ = new FormHelper(&screen());
@ -170,39 +171,42 @@ bool V4D::initializeGUI() {
}
);
glfwSetWindowSizeCallback(getGLFWWindow(), [](GLFWwindow* glfwWin, int width, int height) {
V4D* v4d = reinterpret_cast<V4D*>(glfwGetWindowUserPointer(glfwWin));
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
int w = mode->width;
int h = mode->height;
// v4d->screen().resize_callback_event(width, height);
if (width <= w && height <= h) {
cerr << "winsize: " << width << ":" << height << endl;
v4d->nvgCtx().fbCtx().teardown();
v4d->glCtx().fbCtx().teardown();
v4d->fbCtx().teardown();
v4d->fbCtx().setup(cv::Size(width, height));
v4d->glCtx().fbCtx().setup(cv::Size(width, height));
v4d->nvgCtx().fbCtx().setup(cv::Size(width, height));
}
});
// glfwSetWindowSizeCallback(getGLFWWindow(), [](GLFWwindow* glfwWin, int width, int height) {
// V4D* v4d = reinterpret_cast<V4D*>(glfwGetWindowUserPointer(glfwWin));
// GLFWmonitor* monitor = glfwGetPrimaryMonitor();
// const GLFWvidmode* mode = glfwGetVideoMode(monitor);
// int w = mode->width;
// int h = mode->height;
//
//// v4d->screen().resize_callback_event(width, height);
// if (width <= w && height <= h) {
// cerr << "winsize: " << width << ":" << height << endl;
// v4d->nvgCtx().fbCtx().teardown();
// v4d->glCtx().fbCtx().teardown();
// v4d->fbCtx().teardown();
// v4d->fbCtx().setup(cv::Size(width, height));
// v4d->glCtx().fbCtx().setup(cv::Size(width, height));
// v4d->nvgCtx().fbCtx().setup(cv::Size(width, height));
// }
// });
glfwSetFramebufferSizeCallback(getGLFWWindow(),
[](GLFWwindow* glfwWin, int width, int height) {
V4D* v4d = reinterpret_cast<V4D*>(glfwGetWindowUserPointer(glfwWin));
cerr << "fbsize: " << width << ":" << height << endl;
v4d->screen().resize_callback_event(width, height);
// v4d->nvgCtx().fbCtx().teardown();
// v4d->glCtx().fbCtx().teardown();
// v4d->fbCtx().teardown();
// v4d->fbCtx().setup(cv::Size(width, height));
// v4d->glCtx().fbCtx().setup(cv::Size(width, height));
// v4d->nvgCtx().fbCtx().setup(cv::Size(width, height));
cv::Rect& vp = v4d->viewport();
vp.x = 0;
vp.y = 0;
vp.width = width;
vp.height = height;
#ifndef __EMSCRIPTEN__
v4d->nvgCtx().fbCtx().teardown();
v4d->glCtx().fbCtx().teardown();
v4d->fbCtx().teardown();
v4d->fbCtx().setup(cv::Size(width, height));
v4d->glCtx().fbCtx().setup(cv::Size(width, height));
v4d->nvgCtx().fbCtx().setup(cv::Size(width, height));
#endif
});
clvaContext_ = new detail::CLVAContext(*mainFbContext_);
glContext_ = new detail::GLContext(*mainFbContext_);
nvgContext_ = new detail::NanoVGContext(*mainFbContext_);
} catch (std::exception& ex) {
cerr << "V4D initialization failed: " << ex.what() << endl;
return false;
@ -517,7 +521,7 @@ float V4D::getScale() {
return scale_;
}
cv::Rect V4D::getViewport() {
cv::Rect& V4D::viewport() {
return viewport_;
}
@ -529,7 +533,7 @@ cv::Size V4D::getNativeFrameBufferSize() {
}
cv::Size V4D::getFrameBufferSize() {
return frameBufferSize_;
return fbCtx().getSize();
}
cv::Size V4D::getWindowSize() {
@ -630,11 +634,12 @@ bool V4D::display() {
bool result = true;
if (!offscreen_) {
fbCtx().makeCurrent();
GL_CHECK(glViewport(0, 0, getFrameBufferSize().width, getFrameBufferSize().height));
screen().draw_contents();
#ifndef __EMSCRIPTEN__
mainFbContext_->blitFrameBufferToScreen(getViewport(), getWindowSize(), isStretching());
mainFbContext_->blitFrameBufferToScreen(viewport(), getWindowSize(), isStretching());
#else
mainFbContext_->blitFrameBufferToScreen(getViewport(), getInitialSize(), isStretching());
mainFbContext_->blitFrameBufferToScreen(viewport(), getInitialSize(), isStretching());
#endif
screen().draw_widgets();
glfwSwapBuffers(getGLFWWindow());

Loading…
Cancel
Save