您尚未登录。

楼主 #1 2021-01-07 14:28:25

拉轰的脚踏车
会员
注册时间: 2020-03-20
已发帖子: 288
积分: 222

试一试 Qt5 运行 OpenGL 程序

2021-01-07_142810.png

window.cpp

#include "window.h"
#include <QDebug>
#include <QString>

Window::~Window()
{
  makeCurrent();
  teardownGL();
}

/*******************************************************************************
 * OpenGL Events
 ******************************************************************************/

void Window::initializeGL()
{
  // Initialize OpenGL Backend
  initializeOpenGLFunctions();
  printContextInformation();

  // Set global information
  glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}

void Window::resizeGL(int width, int height)
{
  // Currently we are not handling width/height changes
  (void)width;
  (void)height;
}

void Window::paintGL()
{
  // Clear
  glClear(GL_COLOR_BUFFER_BIT);

  glColor4f(1, 0, 0, 1);
  glBegin(GL_LINES);
  glVertex4f(0, 0, 0, 1);
  glVertex4f(0, 1, 0, 1);

  glVertex4f(0, 0, 0, 1);
  glVertex4f(1, 0, 0, 1);

  glVertex4f(0, 0, 0, 1);
  glVertex4f(0, 0, 1, 1);
  glEnd();
}

void Window::teardownGL()
{
  // Currently we have no data to teardown
}

/*******************************************************************************
 * Private Helpers
 ******************************************************************************/

void Window::printContextInformation()
{
  QString glType;
  QString glVersion;
  QString glProfile;

  // Get Version Information
  glType = (context()->isOpenGLES()) ? "OpenGL ES" : "OpenGL";
  glVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));

  // Get Profile Information
#define CASE(c) case QSurfaceFormat::c: glProfile = #c; break
  switch (format().profile())
  {
    CASE(NoProfile);
    CASE(CoreProfile);
    CASE(CompatibilityProfile);
  }
#undef CASE

  // qPrintable() will print our QString w/o quotes around it.
  qDebug() << qPrintable(glType) << qPrintable(glVersion) << "(" << qPrintable(glProfile) << ")";
}

window.h

#ifndef WINDOW_H
#define WINDOW_H

#include <QOpenGLWindow>
#include <QOpenGLFunctions>

class Window : public QOpenGLWindow,
               protected QOpenGLFunctions
{
  Q_OBJECT

// OpenGL Events
public:
  ~Window();

  void initializeGL();
  void resizeGL(int width, int height);
  void paintGL();
  void teardownGL();

private:
  // Private Helpers
  void printContextInformation();
};

#endif // WINDOW_H

本站下载: qopenglwidget_simple_demo_2021010714.7z

离线

楼主 #2 2021-01-07 17:35:04

拉轰的脚踏车
会员
注册时间: 2020-03-20
已发帖子: 288
积分: 222

Re: 试一试 Qt5 运行 OpenGL 程序

qt_opengl_rotate.gif

#include "myopenglweight.h"
#include <QOpenGLFunctions>
#include <math.h>


MyOpenGLWeight::MyOpenGLWeight(QWidget *parent, Qt::WindowFlags f) :
    QOpenGLWidget(parent, f)
{

//    QSurfaceFormat format;
//    format.setDepthBufferSize(24);
//    format.setStencilBufferSize(8);
//    format.setVersion(3, 2);
//    format.setProfile(QSurfaceFormat::CoreProfile);
//    setFormat(format);

    rotationX = 0.0;
    rotationY = 0.0;
    rotationZ = 0.0;
    faceColor[0] = Qt::red;
    faceColor[1] = Qt::green;
    faceColor[2] = Qt::blue;
    faceColor[3] = Qt::yellow;


}

MyOpenGLWeight::~MyOpenGLWeight()
{

}

void MyOpenGLWeight::initializeGL()
{
    //QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
    glClearColor(0.2f, 0.1f, 0.5f, 1.0f);
    glEnable(GL_DEPTH_TEST);
}

void MyOpenGLWeight::resizeGL(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    GLfloat x = GLfloat(w) / h;
    glFrustum(-x, +x, -1, +1, 5.0, 30.0);
    glMatrixMode(GL_MODELVIEW);

}

void MyOpenGLWeight::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    draw();

}

void MyOpenGLWeight::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        startPoint = event->pos();
    }
    mouseMoveEvent(event);

}

void MyOpenGLWeight::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton)
    {
        QPoint movePoint = event->pos() - startPoint;
        {
            rotationX += movePoint.y();
            rotationY += movePoint.x();
        }
        update();
        startPoint = event->pos();
     }

}

GLfloat ctrlpoints[4][3] = {
        { -4.0, -4.0, 0.0}, { -2.0, 4.0, 0.0},
        {2.0, -4.0, 0.0}, {4.0, 4.0, 0.0}};

void MyOpenGLWeight::draw()
{
    static const GLfloat P1[3] = {0.0, -1.0, 2.0};
    static const GLfloat P2[3] = {1.0, -1.0, -2.0};
    static const GLfloat P3[3] = {0.5, 1.0, 1.5};
    static const GLfloat P4[3] = {-1.0, 0.0, -1.5};

    static const GLfloat * const coords[4][3] = {
        {P1, P2, P3},
        {P1, P3, P4},
        {P1, P4, P2},
        {P2, P4, P3},
    };

    glLoadIdentity();
    glTranslatef(0.0, 0.0, -10);
    glRotatef(rotationX, 1.0, 0.0, 0.0);
    glRotatef(rotationY, 0.0, 1.0, 0.0);
    glRotatef(rotationZ, 0.0, 0.0, 1.0);

    glBegin(GL_LINES);
    float s = 3;
    glColor3f(255.0, 0, 0);
    glVertex3f( 0, 0, 0 );
    glVertex3f( s, 0, 0 );
    glColor3f(0, 255.0, 0);
    glVertex3f( 0, 0, 0 );
    glVertex3f( 0, s, 0 );
    glColor3f(0, 0, 255.0);
    glVertex3f( 0, 0, 0 );
    glVertex3f( 0, 0, s );
    glEnd();

    glBegin(GL_LINE_STRIP);
        for (int i = 0; i <= 30; i++)
           glEvalCoord1f((GLfloat) i/30.0);
     glEnd();
     /* The following code displays the control points as dots. */
     glPointSize(5.0);
     glColor3f(1.0, 1.0, 0.0);
     glBegin(GL_POINTS);
        for (int i = 0; i < 4; i++)
           glVertex3fv(&ctrlpoints[i][0]);
     glEnd();



//    for(int i = 0; i < 4; ++i)
//    {
//        glBegin(GL_TRIANGLES);
//        glColor3f(faceColor[i].redF(), faceColor[i].greenF(), faceColor[i].blueF());
//        for(int j = 0; j < 4; ++j)
//            glVertex3f(coords[i][j][0], coords[i][j][1], coords[i][j][2]);
//        glEnd();
//    }

}

https://github.com/PhoebeTsou/TestGL.git

本站下载: TestGL.7z

离线

楼主 #3 2021-01-09 10:08:49

拉轰的脚踏车
会员
注册时间: 2020-03-20
已发帖子: 288
积分: 222

Re: 试一试 Qt5 运行 OpenGL 程序

2021-01-09_100453.png

Qt5 用 OpenGL 把 YUV 转 RGB显示到 Widget的故事: https://github.com/MasterAler/SampleYUVRenderer

YUV测试文件这里下载: http://trace.eas.asu.edu/yuv/

解压、改名test.yuv复制到 bin 目录即可运行。


也可以本站下载

源码: SampleYUVRenderer_git.7z

YUV: akiyo_cif.7z

离线

楼主 #4 2021-01-09 14:14:30

拉轰的脚踏车
会员
注册时间: 2020-03-20
已发帖子: 288
积分: 222

Re: 试一试 Qt5 运行 OpenGL 程序

顺手推荐一个在线显示YUV/RGB格式文件的网站: https://rawpixels.net/

离线

楼主 #5 2021-01-09 14:23:30

拉轰的脚踏车
会员
注册时间: 2020-03-20
已发帖子: 288
积分: 222

Re: 试一试 Qt5 运行 OpenGL 程序

ffmpeg 转码 MOV/MP4 文件到 yuv, 尺寸 1920x1080:

ffmpeg -i tiky.mov tiky.yuv

如果只需生成一帧, 用这个命令

ffmpeg -i tiky.mov -vframes 1 tiky.yuv


2021-01-09_142302.png










拉轰的脚踏车 说:

顺手推荐一个在线显示YUV/RGB格式文件的网站: https://rawpixels.net/

2021-01-09_150843.png









视频素材来源:

TKM32F499配4.0寸IPS屏在RGB888模式下截屏功能
http://whycan.com/t_5830.html
(出处:哇酷开发者社区【好钜润半导体(TIKY)】)

最近编辑记录 拉轰的脚踏车 (2021-01-09 15:09:48)

离线

楼主 #6 2021-01-09 16:53:08

拉轰的脚踏车
会员
注册时间: 2020-03-20
已发帖子: 288
积分: 222

Re: 试一试 Qt5 运行 OpenGL 程序

qt_opengl_zoom.gif

https://github.com/james-yoo/CodeBase

#include "myopenglweight.h"
#include <QOpenGLFunctions>
#include <math.h>


MyOpenGLWeight::MyOpenGLWeight(QWidget *parent, Qt::WindowFlags f) :
    QOpenGLWidget(parent, f)
{

    zoomScale = 1.0f;
//    QSurfaceFormat format;
//    format.setDepthBufferSize(24);
//    format.setStencilBufferSize(8);
//    format.setVersion(3, 2);
//    format.setProfile(QSurfaceFormat::CoreProfile);
//    setFormat(format);

    rotationX = 0.0;
    rotationY = 0.0;
    rotationZ = 0.0;
    faceColor[0] = Qt::red;
    faceColor[1] = Qt::green;
    faceColor[2] = Qt::blue;
    faceColor[3] = Qt::yellow;


}

MyOpenGLWeight::~MyOpenGLWeight()
{

}

void MyOpenGLWeight::initializeGL()
{
    //QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
    glClearColor(0.2f, 0.1f, 0.5f, 1.0f);
    glEnable(GL_DEPTH_TEST);
}

void MyOpenGLWeight::resizeGL(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    GLfloat x = GLfloat(w) / h;
    glFrustum(-x, +x, -1, +1, 5.0, 30.0);
    glMatrixMode(GL_MODELVIEW);

}

void MyOpenGLWeight::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    draw();
}

void MyOpenGLWeight::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        startPoint = event->pos();
    }
    mouseMoveEvent(event);

}

void MyOpenGLWeight::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton)
    {
        QPoint movePoint = event->pos() - startPoint;
        {
            rotationX += movePoint.y();
            rotationY += movePoint.x();
        }
        update();
        startPoint = event->pos();
     }

}

void MyOpenGLWeight::wheelEvent(QWheelEvent *event)
{
    QPoint numDegrees = event->angleDelta();
    if (numDegrees.y() < 0)  zoomScale = zoomScale/1.1;
    if (numDegrees.y() > 0)  zoomScale = zoomScale*1.1;

    paintGL(); // call paintGL()
    //update();
}

GLfloat ctrlpoints[4][3] = {
        { -4.0, -4.0, 0.0}, { -2.0, 4.0, 0.0},
        {2.0, -4.0, 0.0}, {4.0, 4.0, 0.0}};

void MyOpenGLWeight::draw()
{
    static const GLfloat P1[3] = {0.0, -1.0, 2.0};
    static const GLfloat P2[3] = {1.0, -1.0, -2.0};
    static const GLfloat P3[3] = {0.5, 1.0, 1.5};
    static const GLfloat P4[3] = {-1.0, 0.0, -1.5};

    static const GLfloat * const coords[4][3] = {
        {P1, P2, P3},
        {P1, P3, P4},
        {P1, P4, P2},
        {P2, P4, P3},
    };

    int range = 1000;
    glLoadIdentity();
    int height = 320;
    int width = 240;
    //glScalef(zoomScale, zoomScale, zoomScale);
        //glOrtho(-0.5*range*zoomScale, +0.5*range*zoomScale, -0.5*height/width*range*zoomScale, +0.5*height/width*range*zoomScale, -5*range, +5*range);

    glTranslatef(0.0, 0.0, -10);
    glRotatef(rotationX, 1.0, 0.0, 0.0);
    glRotatef(rotationY, 0.0, 1.0, 0.0);
    glRotatef(rotationZ, 0.0, 0.0, 1.0);

    glBegin(GL_LINES);
    glVertex3f(-0.6f, -0.3f, 0.2f);
    glVertex3f(0.6f, 0.3f, 0.3f);

    glVertex3f(0.6f, 0.3f, 0.3f);
    glVertex3f(0.1f, 0.1f, -0.2f);

    glVertex3f(0.1f, 0.1f, -0.2f);
    glVertex3f(-0.6f, -0.3f, 0.2f);
    glEnd();

    glBegin(GL_LINES);
    float s = 3;
    glColor3f(255.0, 0, 0);
    glVertex3f( 0, 0, 0 );
    glVertex3f( s, 0, 0 );
    glColor3f(0, 255.0, 0);
    glVertex3f( 0, 0, 0 );
    glVertex3f( 0, s, 0 );
    glColor3f(0, 0, 255.0);
    glVertex3f( 0, 0, 0 );
    glVertex3f( 0, 0, s );
    glEnd();

    glBegin(GL_LINE_STRIP);
        for (int i = 0; i <= 30; i++)
           glEvalCoord1f((GLfloat) i/30.0);
     glEnd();
     /* The following code displays the control points as dots. */
     glPointSize(5.0);
     glColor3f(1.0, 1.0, 0.0);
     glBegin(GL_POINTS);
        for (int i = 0; i < 4; i++)
           glVertex3fv(&ctrlpoints[i][0]);
     glEnd();



//    for(int i = 0; i < 4; ++i)
//    {
//        glBegin(GL_TRIANGLES);
//        glColor3f(faceColor[i].redF(), faceColor[i].greenF(), faceColor[i].blueF());
//        for(int j = 0; j < 4; ++j)
//            glVertex3f(coords[i][j][0], coords[i][j][1], coords[i][j][2]);
//        glEnd();
//    }


}

本站下载: qt_opengl_zoom.7z



https://docs.microsoft.com/en-us/windows/win32/opengl/the-program-ported-to-win32

最近编辑记录 拉轰的脚踏车 (2021-01-09 17:09:16)

离线

#7 2024-07-30 21:32:18

memory
会员
注册时间: 2021-08-11
已发帖子: 483
积分: 459

Re: 试一试 Qt5 运行 OpenGL 程序

https://blog.csdn.net/Fox_Alex/article/details/80163942

QOpenGLTest1.zip

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QGLWidget(parent)
{
}

Widget::~Widget()
{
}

void Widget::initializeGL()
{
    //设置widget的坐标和尺寸
    setGeometry(300, 150, 500, 500);
    //设置清除时颜色
    glClearColor(0.0, 0.0, 0.0, 0);
}
void Widget::resizeGL(int w, int h)
{
    //视口变换
    glViewport(0,0,(GLsizei)w,(GLsizei)h);
    //投影变换
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(40.0,(GLdouble)w/(GLdouble)h,0.1,10000.0);
    //视图变换
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.0,0.0,15.0,0.0,0.0,0.0,0.0,1.0,0.0);
}
void Widget::paintGL()
{
    //清屏
    glClear(GL_COLOR_BUFFER_BIT);
    //绘制七彩三角形
    glBegin(GL_TRIANGLES);
    glColor3f(1.0,0.0,0.0);
    glVertex3f(-2,0,0);
    glColor3f(0.0,1.0,0.0);
    glVertex3f(2,0,0);
    glColor3f(0.0,0.0,1.0);
    glVertex3f(0,4,0);
    glEnd();
    glFlush();
}

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn