From a94d9c4bb136f434eb24d37805f9e2c8a971a96e Mon Sep 17 00:00:00 2001 From: Jared Bruni Date: Thu, 23 Jan 2020 22:29:53 -0800 Subject: [PATCH] got video working with gl --- src/display_window.cpp | 6 ++-- src/gl_display.cpp | 74 +++++++++++++++++++++++++++++------------- src/gl_display.h | 8 +++-- 3 files changed, 59 insertions(+), 29 deletions(-) diff --git a/src/display_window.cpp b/src/display_window.cpp index e489b28..6d137e5 100644 --- a/src/display_window.cpp +++ b/src/display_window.cpp @@ -30,13 +30,13 @@ void DisplayWindow::createControls() { img_label->setGeometry(0,0,640, 480); } void DisplayWindow::displayImage(const QImage &img) { - gl_display->setNewFrame(img); - QRect src(QPoint(0, 0), size()); + gl_display->setNewFrame(img.copy()); + /*QRect src(QPoint(0, 0), size()); QPixmap p = QPixmap::fromImage(img).scaled(size(),Qt::KeepAspectRatio, Qt::FastTransformation); QRect dst(QPoint(0,0),p.size()); dst.moveCenter(src.center()); img_label->setGeometry(dst); - img_label->setPixmap(p); + img_label->setPixmap(p); */ } void DisplayWindow::paintEvent(QPaintEvent *) { diff --git a/src/gl_display.cpp b/src/gl_display.cpp index d78fe1a..1e8a5a1 100644 --- a/src/gl_display.cpp +++ b/src/gl_display.cpp @@ -13,7 +13,7 @@ glDisplayWindow::glDisplayWindow(QWindow *parent) : QWindow(parent) , m_animating(false) , m_context(0) - , m_device(0) +, m_device(0), updated(false) { setSurfaceType(QWindow::OpenGLSurface); } @@ -27,6 +27,8 @@ void glDisplayWindow::render(QPainter *painter) { } void glDisplayWindow::initialize() { + resize(1280, 720); + glEnable(GL_TEXTURE_2D); glClearDepth(1.0f); glClearColor(0, 0, 0, 0); glMatrixMode(GL_PROJECTION); @@ -35,35 +37,61 @@ void glDisplayWindow::initialize() { glEnable (GL_DEPTH_TEST); } -void glDisplayWindow::setNewFrame(const QImage &new_one) { - frame_copy = new_one; +void glDisplayWindow::setNewFrame(QImage image) { + frame = image; + updated = true; } -unsigned int texID; +void glDisplayWindow::updateTexture(QImage image) { + static int lazy = 0; + QImage new_one = image.mirrored(); + if(lazy == 0) { + glEnable(GL_TEXTURE_2D); + glGenTextures(1, &tex); + glBindTexture(GL_TEXTURE_2D, tex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, new_one.width(), new_one.height(), 0, GL_BGR, GL_UNSIGNED_BYTE, new_one.bits()); + lazy = 1; + } else { + glBindTexture(GL_TEXTURE_2D, tex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, new_one.width(), new_one.height(), GL_BGR, GL_UNSIGNED_BYTE, new_one.bits()); + } +} void glDisplayWindow::render() { + if (!m_device) m_device = new QOpenGLPaintDevice; - if(frame_copy.width()>100 && isVisible()) { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - float aspect=(float)width()/(float)height(); - - if(width() <= height()) - glOrtho ( -5.0, 5.0, -5.0/aspect, 5.0/aspect, -5.0, 5.0); - else - glOrtho (-5.0*aspect, 5.0*aspect, -5.0, 5.0, -5.0, 5.0); - if(frame_copy.width() > 25 && frame_copy.height() > 25) { - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glDrawPixels(frame_copy.width(), frame_copy.height(), GL_RGB,GL_UNSIGNED_BYTE,(unsigned char*)frame_copy.bits()); - glFlush(); - } + + if(updated == true && frame.width()>100 && frame.height()>100) { + updateTexture(frame); + updated = false; } - m_device->setPaintFlipped(true); - m_device->setSize(size() * devicePixelRatio()); - m_device->setDevicePixelRatio(devicePixelRatio()); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0, 1280, 0, 720, -1, 1); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glClear(GL_COLOR_BUFFER_BIT); + glBindTexture(GL_TEXTURE_2D, tex); + glEnable(GL_TEXTURE_2D); + glBegin(GL_QUADS); + glTexCoord2i(0, 0); glVertex2i(0, 0); + glTexCoord2i(0, 1); glVertex2i(0, 720); + glTexCoord2i(1, 1); glVertex2i(1280, 720); + glTexCoord2i(1, 0); glVertex2i(1280, 0); + glEnd(); + glDisable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, 0); + //m_device->setSize(size() * devicePixelRatio()); + //m_device->setDevicePixelRatio(devicePixelRatio()); QPainter painter(m_device); render(&painter); } diff --git a/src/gl_display.h b/src/gl_display.h index aaf2889..6d7d036 100644 --- a/src/gl_display.h +++ b/src/gl_display.h @@ -24,8 +24,8 @@ public: virtual void initialize(); void setAnimating(bool animating); - void setNewFrame(const QImage &image); - + void setNewFrame(QImage image); + void updateTexture(QImage image); public slots: void renderLater(); void renderNow(); @@ -37,9 +37,11 @@ protected: private: bool m_animating; - QImage frame_copy; QOpenGLContext *m_context; QOpenGLPaintDevice *m_device; + GLuint tex; + QImage frame; + bool updated; }; #endif