ソースを参照

V0.7.2
1、使用OpenGL绘制成功了一个三角形

apple 4 週間 前
コミット
37cb77ced3

+ 18 - 3
CMakeLists.txt

@@ -139,12 +139,27 @@ find_package(Qt${QT_VERSION_MAJOR} COMPONENTS
     Core
     # SerialPort
     Network
-    # Multimedia
-    Sql
-    OpenGLWidgets
     REQUIRED
 )
 
+#Qt5
+if(QT_VERSION_MAJOR EQUAL 5)
+    find_package(Qt${QT_VERSION_MAJOR} COMPONENTS
+        OpenGL
+        REQUIRED
+    )
+
+#Qt6
+elseif(QT_VERSION_MAJOR EQUAL 6)
+    find_package(Qt${QT_VERSION_MAJOR} COMPONENTS
+        OpenGLWidgets
+        REQUIRED
+    )
+    
+endif(QT_VERSION_MAJOR EQUAL 5)
+
+
+
 include(${CMAKE_SOURCE_DIR}/External/Libraries/Libraries.cmake)
 # include(${CMAKE_SOURCE_DIR}/External_Ex/Library_EX.cmake)
 

+ 1 - 1
External

@@ -1 +1 @@
-Subproject commit f5f104d102e3c49bf0363470dc3416ad4329e21a
+Subproject commit 6f569e105d7a16ac86f465b58987b1f5bf6f34cf

+ 21 - 27
demo/VideoPlayerGL/CMakeLists.txt

@@ -2,8 +2,7 @@
 
 set(this_exe PlayerGL)
 
-# 添加 OpenGL 和 Qt 配置选项
-set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DQT_OPENGL_LIB -DQT_WIDGETS_LIB")
+
 
 #包含源文件
 file(GLOB LOCAL_SRC
@@ -11,15 +10,11 @@ file(GLOB LOCAL_SRC
     ${CMAKE_CURRENT_SOURCE_DIR}/*.rc
     ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/*.ui
-    # ${CMAKE_CURRENT_SOURCE_DIR}/VideoPlayer/*.cpp
-    # ${CMAKE_CURRENT_SOURCE_DIR}/demo/*.cpp
-    ${CMAKE_CURRENT_SOURCE_DIR}/Player/*.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/GLWidget/*.cpp
+
 
     ${CMAKE_SOURCE_DIR}/External/module/Logs/*.cpp
     ${CMAKE_SOURCE_DIR}/External/module/ThreadPool/*.cpp
-    # ${CMAKE_SOURCE_DIR}/External/module/VideoPlayer/*.cpp
-
-    
     
 )
 
@@ -41,45 +36,44 @@ add_executable(${this_exe}
 target_include_directories(${this_exe} PRIVATE
 
     ${CMAKE_CURRENT_SOURCE_DIR}
-    ${CMAKE_CURRENT_SOURCE_DIR}/Player
-    # ${CMAKE_CURRENT_SOURCE_DIR}/VideoPlayer
-    # ${CMAKE_CURRENT_SOURCE_DIR}/demo
+
     ${CMAKE_SOURCE_DIR}/External/common
     ${CMAKE_SOURCE_DIR}/External/module
     ${CMAKE_SOURCE_DIR}/External/module/ThreadPool
     ${CMAKE_SOURCE_DIR}/External/module/RingQueue
-    # ${CMAKE_SOURCE_DIR}/External/module/VideoPlayer
-    
+
+    ${CMAKE_CURRENT_SOURCE_DIR}/GLWidget
     
     # ${CURL_INCLUDE_DIR}
     # ${FFMPEG_INCLUDE_DIR}
     ${spdlog_INCLUDE_DIR}
 )
 
-find_package(OpenGL REQUIRED)
 
 target_link_libraries(${this_exe} PRIVATE
-    Qt6::Widgets
-    Qt6::Core
-    Qt6::Network
-    Qt6::Gui
-    Qt6::OpenGL
-    Qt6::OpenGLWidgets
-    ${OPENGL_LIBRARIES}
+    Qt${QT_VERSION_MAJOR}::Widgets
+    Qt${QT_VERSION_MAJOR}::Core
+    Qt${QT_VERSION_MAJOR}::Network
+    # Qt${QT_VERSION_MAJOR}::Gui
+
 )
 
+if(QT_VERSION_MAJOR EQUAL 5)
+    target_link_libraries(${this_exe} PRIVATE
+        Qt5::OpenGL
+    )
+elseif(QT_VERSION_MAJOR EQUAL 6)
+    target_link_libraries(${this_exe} PRIVATE
+        Qt6::OpenGLWidgets
+    )
+endif(QT_VERSION_MAJOR EQUAL 5)
 
-# 确保正确的 Qt 配置
-set(QT_QPA_PLATFORM "cocoa" CACHE STRING "Qt Platform")
-set(QT_QPA_PLATFORM_PLUGIN_PATH "${CMAKE_PREFIX_PATH}/plugins/platforms" CACHE STRING "Qt Platform Plugin Path")
 
 target_link_libraries(${this_exe} PRIVATE 
-    # fmt::fmt
-    # spdlog::spdlog
     # ${CURL_LIBRARY}
     # ${FFMPEG_LIBRARY}
     ${spdlog_LIBRARY}
-    
+    opengl32
 )
 
 

+ 186 - 0
demo/VideoPlayerGL/GLWidget/PlayerGLWidget.cpp

@@ -0,0 +1,186 @@
+#include "PlayerGLWidget.h"
+#include "spdlog/spdlog.h"
+#include <gl/gl.h>
+
+
+
+
+PlayerGLWidget::PlayerGLWidget(QWidget *parent) : QOpenGLWidget(parent)
+{
+
+}
+
+PlayerGLWidget::~PlayerGLWidget()
+{
+
+}
+
+
+
+
+void PlayerGLWidget::initializeGL()
+{
+    /* 初始化OpenGL函数,OpenGL的函数是在运行时才确定函数指针的 */
+    initializeOpenGLFunctions();
+
+    /* -------------------------------------------------------------------------------------
+     * 顶点数组对象
+     * ------------------------------------------------------------------------------------ */
+    /* 创建一个顶点数组对象 */
+    GLuint VAO1;
+    /* 生成一个VAO,返回的ID存储在VAO1中 */
+    glGenVertexArrays(1, &VAO1);
+    /* 绑定VAO1到 GL_VERTEX_ARRAY 上,表示VAO1是一个顶点数组对象 */
+    glBindVertexArray(VAO1);
+
+    /* 创建一个三角形坐标数组,这里的z坐标都是0,表示平面2维 */
+    float vertices[] = {
+        -0.5f, -0.5f, 0.0f, // 左下角
+         0.5f, -0.5f, 0.0f, // 右下角
+         0.0f,  0.5f, 0.0f  // 顶部
+    };
+
+    /* -------------------------------------------------------------------------------------
+     * 创建顶点数组缓冲区
+     * (GLuint就是unsigned int) 
+     * ------------------------------------------------------------------------------------ */
+    GLuint VBO1;
+    /* 生成一个VBO,返回的ID存储在VBO1中 */
+    glGenBuffers(1, &VBO1);
+    /* OpenGL有很多缓冲对象类型,顶点缓冲对象的缓冲类型是GL_ARRAY_BUFFER
+     * 绑定VBO1到GL_ARRAY_BUFFER上,表示VBO1是一个顶点缓冲对象 */
+    glBindBuffer(GL_ARRAY_BUFFER, VBO1);
+    /* 将顶点数据复制到缓冲区的内存中 */
+    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
+
+    /* -------------------------------------------------------------------------------------
+     * 链接顶点属性
+     * ------------------------------------------------------------------------------------ */
+    /* 设置顶点属性指针,告诉OpenGL如何解析顶点数据
+     * 1、glVertexAttribPointer函数的第一个参数是顶点属性的位置值
+     * 2、第二个参数是每个顶点属性的大小,这里是3,因为我们有3个坐标分量。
+     * 3、第三个参数是数据类型,这里是GL_FLOAT,表示数据类型是float。
+     * 4、第四个参数是是否归一化,这里是GL_FALSE,表示不归一化。
+     * 5、第五个参数是步长,这里是0,表示紧凑存储。
+     * 6、第六个参数是偏移量,这里是0,表示从缓冲区的开头开始读取数据。
+     * 7、最后一个参数是启用顶点属性指针,告诉OpenGL我们要使用这个顶点属性。
+     */
+     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
+     /* 启用顶点属性指针 */
+     glEnableVertexAttribArray(0);
+
+
+    /* -------------------------------------------------------------------------------------
+     * 顶点着色器
+     * ------------------------------------------------------------------------------------ */
+    /* 顶点着色器的源代码
+     * GLSL看起来很像C语言。每个着色器都起始于一个版本声明。OpenGL 3.3以及和更高版本中,GLSL版本号和OpenGL的版本是匹配的(比如说GLSL 420版本对应于OpenGL 4.2)。
+     * 同样明确表示我们会使用核心模式。
+     * 1、#version 330 core 设置版本号和模式
+     * 2、使用in关键字,在顶点着色器中声明所有的输入顶点属性(Input Vertex Attribute)。现在我们只关心位置(Position)数据,所以我们只需要一个顶点属性。
+     *    GLSL有一个向量数据类型,它包含1到4个float分量,包含的数量可以从它的后缀数字看出来。由于每个顶点都有一个3D坐标,我们就创建一个vec3输入变量aPos。
+     *    我们同样也通过layout (location = 0)设定了输入变量的位置值(Location)
+     * 3、gl_Position是一个内建变量,它是一个vec4类型的变量,表示顶点在裁剪空间中的位置。我们将aPos赋值给gl_Position,OpenGL会自动将vec3转换为vec4。
+     */
+    const char* vertexShaderSource = R"(
+        #version 330 core
+        layout(location = 0) in vec3 aPos;
+        void main()
+        {
+            gl_Position = vec4(aPos, 1.0);
+        }
+    )";
+
+    /* -------------------------------------------------------------------------------------
+     * 编译着色器
+     * ------------------------------------------------------------------------------------ */
+     /* 创建一个着色器对象,参数是着色器类型
+      * GL_VERTEX_SHADER代表是顶点着色器,GL_FRAGMENT_SHADER代表是片段着色器
+      * glCreateShader函数返回一个着色器对象的ID,OpenGL会为我们分配一个唯一的ID
+      */
+     GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
+    /* 将着色器源代码附加到着色器对象上 */
+    glShaderSource(vertexShader, 1, &vertexShaderSource,nullptr);
+    /* 编译着色器 */
+    glCompileShader(vertexShader);
+    /* 检查编译错误 */
+    GLint success;
+    GLchar infoLog[512];
+    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
+    if (!success)
+    {
+        glGetShaderInfoLog(vertexShader, sizeof(infoLog), nullptr, infoLog);
+        SPDLOG_ERROR("着色器编译错误: {}", infoLog);
+    }
+
+    /* -------------------------------------------------------------------------------------
+     * 片段着色器
+     * ------------------------------------------------------------------------------------ */
+    /* 片段着色器的源代码 */
+    const char* fragmentShaderSource = R"(
+        #version 330 core
+        out vec4 FragColor;
+        void main()
+        {
+            FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
+        }
+    )";
+    /* 创建一个片段着色器对象 */
+    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
+    /* 将片段着色器源代码附加到片段着色器对象上 */
+    glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
+    /* 编译片段着色器 */
+    glCompileShader(fragmentShader);
+    /* 检查编译错误 */
+    success = 0;
+    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
+    if (!success)
+    {
+        glGetShaderInfoLog(fragmentShader, sizeof(infoLog), nullptr, infoLog);
+        SPDLOG_ERROR("片段着色器编译错误: {}", infoLog);
+    }
+
+    /* -------------------------------------------------------------------------------------
+     * 着色器程序对象
+     * ------------------------------------------------------------------------------------ */
+    /* 创建一个着色器程序对象 */
+    GLuint shaderProgram = glCreateProgram();
+    /* 将顶点着色器和片段着色器附加到着色器程序对象上 */
+    glAttachShader(shaderProgram, vertexShader);
+    glAttachShader(shaderProgram, fragmentShader);
+    /* 链接着色器程序对象 */
+    glLinkProgram(shaderProgram);
+    /* 检查链接错误 */
+    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
+    if(!success)
+    {
+        glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog);
+        SPDLOG_ERROR("着色器程序链接错误: {}", infoLog);
+    }
+    /* 删除着色器对象,因为它们已经链接到着色器程序对象上了 */
+    glDeleteShader(vertexShader);
+    glDeleteShader(fragmentShader);
+    /* 使用着色器程序对象 */
+    glUseProgram(shaderProgram);
+    
+}
+
+void PlayerGLWidget::resizeGL(int w, int h)
+{
+
+}
+
+void PlayerGLWidget::paintGL()
+{
+    /* glClearColor函数是一个状态设置函数,而glClear函数则是一个状态使用的函数,
+     * 它使用了当前的状态来获取应该清除为的颜色。 */
+    glClearColor(0.1f, 0.3f, 0.3f, 1.0f);
+    glClear(GL_COLOR_BUFFER_BIT);
+
+    /* 绘制三角形 */
+    /* glDrawArrays函数的第一个参数是绘制模式,这里是GL_TRIANGLES,表示绘制三角形。
+     * 第二个参数是起始索引,这里是0,表示从第一个顶点开始绘制。
+     * 第三个参数是顶点数量,这里是3,表示绘制3个顶点。
+     */
+     glDrawArrays(GL_TRIANGLES, 0, 3);
+}

+ 36 - 0
demo/VideoPlayerGL/GLWidget/PlayerGLWidget.h

@@ -0,0 +1,36 @@
+#ifndef PLAYEROPENGLWIDGET_H
+#define PLAYEROPENGLWIDGET_H
+
+
+#include <QOpenGLWidget>
+#include <QOpenGLFunctions_3_3_Core>
+
+
+/**
+ * @brief 
+ * 
+ */
+
+
+class PlayerGLWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core
+{
+    Q_OBJECT
+
+public:
+    explicit PlayerGLWidget(QWidget *parent = nullptr);
+    ~PlayerGLWidget();
+
+
+private:
+ 
+protected:
+    void initializeGL() override;
+    void resizeGL(int w, int h) override;
+    void paintGL() override;
+
+private:
+
+
+};
+
+#endif /* PLAYEROPENGLWIDGET_H */

+ 0 - 149
demo/VideoPlayerGL/Player/PlayerGLWidget(副本).cpp_

@@ -1,149 +0,0 @@
-#include "PlayerGLWidget.h"
-
-
-PlayerGLWidget::PlayerGLWidget(QWidget *parent) : QOpenGLWidget(parent)
-{
-
-}
-
-PlayerGLWidget::~PlayerGLWidget()
-{
-
-}
-
-/* 刷新一帧 */
-void PlayerGLWidget::updateFrame(Image_YUV420& image)
-{
-    yData = image.yData;
-    uData = image.uData;
-    vData = image.vData;
-    update();
-}
-
-
-void PlayerGLWidget::initShaders()
-{
-    // 顶点着色器
-    const char *vshader = R"(
-        #version 330 core
-        layout(location = 0) in vec4 vertexIn;
-        layout(location = 1) in vec2 textureIn;
-        out vec2 textureOut;
-        void main(void)
-        {
-            gl_Position = vertexIn;
-            textureOut = textureIn;
-        })";
-    // 片段着色器(YUV420P 转 RGB)
-    const char *fshader = R"(
-        #version 330 core
-        in vec2 textureOut;
-        out vec4 fragColor;
-        uniform sampler2D tex_y;
-        uniform sampler2D tex_u;
-        uniform sampler2D tex_v;
-        void main(void)
-        {
-            float y = texture(tex_y, textureOut).r;
-            float u = texture(tex_u, textureOut).r - 0.5;
-            float v = texture(tex_v, textureOut).r - 0.5;
-            float r = y + 1.403 * v;
-            float g = y - 0.344 * u - 0.714 * v;
-            float b = y + 1.770 * u;
-            fragColor = vec4(r, g, b, 1.0);
-        })";
-    // 创建着色器程序
-    program.addShaderFromSourceCode(QOpenGLShader::Vertex, vshader);
-    program.addShaderFromSourceCode(QOpenGLShader::Fragment, fshader);
-    program.link();
-}
-
-void PlayerGLWidget::initTextures()
-{
-    // 创建 YUV 纹理
-        textureY = new QOpenGLTexture(QOpenGLTexture::Target2D);
-        textureU = new QOpenGLTexture(QOpenGLTexture::Target2D);
-        textureV = new QOpenGLTexture(QOpenGLTexture::Target2D);
-
-        textureY->create();
-        textureU->create();
-        textureV->create();
-}
-
-// void PlayerGLWidget::initVertex()
-// {
-
-// }
-
-// void PlayerGLWidget::initFrameBuffer()
-// {
-
-// }
-
-void PlayerGLWidget::renderYUV420P()
-{
-    if (yData.isEmpty() || uData.isEmpty() || vData.isEmpty())
-            return;
-
-        // 绑定纹理
-        textureY->bind(0);
-        textureU->bind(1);
-        textureV->bind(2);
-
-        // 上传 YUV 数据到纹理
-        textureY->setData(QOpenGLTexture::Red, QOpenGLTexture::UInt8, yData.constData());
-        textureU->setData(QOpenGLTexture::Red, QOpenGLTexture::UInt8, uData.constData());
-        textureV->setData(QOpenGLTexture::Red, QOpenGLTexture::UInt8, vData.constData());
-
-        // 绑定着色器程序
-        program.bind();
-        program.setUniformValue("tex_y", 0);
-        program.setUniformValue("tex_u", 1);
-        program.setUniformValue("tex_v", 2);
-
-        // 绘制矩形
-        static const GLfloat vertices[] = {
-            -1.0f, -1.0f, 0.0f,
-             1.0f, -1.0f, 0.0f,
-            -1.0f,  1.0f, 0.0f,
-             1.0f,  1.0f, 0.0f
-        };
-
-        static const GLfloat texCoords[] = {
-            0.0f, 1.0f,
-            1.0f, 1.0f,
-            0.0f, 0.0f,
-            1.0f, 0.0f
-        };
-
-        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices);
-        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texCoords);
-        glEnableVertexAttribArray(0);
-        glEnableVertexAttribArray(1);
-
-        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
-
-        program.release();
-}
-
-void PlayerGLWidget::initializeGL()
-{
-    initializeOpenGLFunctions();
-    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
-    // 初始化着色器
-    initShaders();
-    initTextures();
-}
-
-void PlayerGLWidget::resizeGL(int w, int h)
-{
-    glViewport(0, 0, w, h);
-}
-
-void PlayerGLWidget::paintGL()
-{
-    glClear(GL_COLOR_BUFFER_BIT);
-
-    // 渲染 YUV 数据
-    renderYUV420P();
-}

+ 0 - 54
demo/VideoPlayerGL/Player/PlayerGLWidget(副本).h_

@@ -1,54 +0,0 @@
-#ifndef PLAYEROPENGLWIDGET_H
-#define PLAYEROPENGLWIDGET_H
-
-
-#include <QOpenGLWidget>
-#include <QOpenGLFunctions>
-#include <QOpenGLShaderProgram>
-#include <QOpenGLTexture>
-#include <QTimer>
-
-#include "PlayerGlobalInfo.h"
-
-
-class PlayerGLWidget : public QOpenGLWidget , public QOpenGLFunctions
-{
-    Q_OBJECT
-public:
-    explicit PlayerGLWidget(QWidget *parent = nullptr);
-    ~PlayerGLWidget();
-
-    /* 刷新一帧 */
-    void updateFrame(Image_YUV420& image);
-
-
-private:
-    void initShaders();
-    void initTextures();
-    // void initVertex();
-    // void initFrameBuffer();
-
-    void renderYUV420P();
-
-protected:
-    void initializeGL() override;
-    void resizeGL(int w, int h) override;
-    void paintGL() override;
-
-private:
-
-    QOpenGLShaderProgram program;
-    QOpenGLTexture *textureY = nullptr;
-    QOpenGLTexture *textureU = nullptr;
-    QOpenGLTexture *textureV = nullptr;
-
-    QByteArray yData;
-    QByteArray uData;
-    QByteArray vData;
-
-    int width = 0;
-    int height = 0;
-
-};
-
-#endif /* PLAYEROPENGLWIDGET_H */

+ 0 - 137
demo/VideoPlayerGL/Player/PlayerGLWidget2.cpp_

@@ -1,137 +0,0 @@
-#include "PlayerGLWidget.h"
-
-
-PlayerGLWidget::PlayerGLWidget(QWidget *parent) : QOpenGLWidget(parent)
-{
-
-}
-
-PlayerGLWidget::~PlayerGLWidget()
-{
-
-}
-
-/* 刷新一帧 */
-void PlayerGLWidget::updateFrame(Image_YUV420& image)
-{
-    // yData = image.yData;
-    // uData = image.uData;
-    // vData = image.vData;
-    update();
-}
-
-/* 刷新一帧QImage */
-void PlayerGLWidget::updateFrame(Image_QImage& image)
-{
-    m_image = image;
-
-    texture->setData(m_image.image);
-    /* 设置纹理细节 */
-    texture->setLevelofDetailBias(-1);
-    update();
-}
-
-
-void PlayerGLWidget::initShaders()
-{
-    //纹理坐标
-    texCoords.append(QVector2D(0, 1)); //左上
-    texCoords.append(QVector2D(1, 1)); //右上
-    texCoords.append(QVector2D(0, 0)); //左下
-    texCoords.append(QVector2D(1, 0)); //右下
-    //顶点坐标
-    vertices.append(QVector3D(-1, -1, 1));//左下
-    vertices.append(QVector3D(1, -1, 1)); //右下
-    vertices.append(QVector3D(-1, 1, 1)); //左上
-    vertices.append(QVector3D(1, 1, 1));  //右上
-    QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
-    const char *vsrc =
-            "attribute vec4 vertex;\n"
-            "attribute vec2 texCoord;\n"
-            "varying vec2 texc;\n"
-            "void main(void)\n"
-            "{\n"
-            "    gl_Position = vertex;\n"
-            "    texc = texCoord;\n"
-            "}\n";
-    vshader->compileSourceCode(vsrc);//编译顶点着色器代码
- 
-    QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
-    const char *fsrc =
-            "uniform sampler2D texture;\n"
-            "varying vec2 texc;\n"
-            "void main(void)\n"
-            "{\n"
-            "    gl_FragColor = texture2D(texture,texc);\n"
-            "}\n";
-    fshader->compileSourceCode(fsrc); //编译纹理着色器代码
- 
-    program.addShader(vshader);//添加顶点着色器
-    program.addShader(fshader);//添加纹理碎片着色器
-    program.bindAttributeLocation("vertex", 0);//绑定顶点属性位置
-    program.bindAttributeLocation("texCoord", 1);//绑定纹理属性位置
-    // 链接着色器管道
-    if (!program.link())
-        close();
-    // 绑定着色器管道
-    if (!program.bind())
-        close();
-}
-
-void PlayerGLWidget::initTextures()
-{
-    // 加载 Avengers.jpg 图片
-    texture = new QOpenGLTexture(QOpenGLTexture::Target2D);
-    texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
-    texture->setMagnificationFilter(QOpenGLTexture::Linear);
-    //重复使用纹理坐标
-    //纹理坐标(1.1, 1.2)与(0.1, 0.2)相同
-    texture->setWrapMode(QOpenGLTexture::Repeat);
-    //设置纹理大小
-    texture->setSize(this->width(), this->height());
-    //分配储存空间
-    texture->allocateStorage();
-
-}
-
-
-void PlayerGLWidget::initializeGL()
-{
-    initializeOpenGLFunctions(); //初始化OPenGL功能函数
-    glClearColor(0, 0, 0, 0);    //设置背景为黑色
-    glEnable(GL_TEXTURE_2D);     //设置纹理2D功能可用
-    initTextures();              //初始化纹理设置
-    initShaders();               //初始化shaders
-
-}
-
-void PlayerGLWidget::resizeGL(int w, int h)
-{
-    // 计算窗口横纵比
-    qreal aspect = qreal(w) / qreal(h ? h : 1);
-    // 设置近平面值 3.0, 远平面值 7.0, 视场45度
-    const qreal zNear = 3.0, zFar = 7.0, fov = 45.0;
-    // 重设投影
-    projection.setToIdentity();
-    // 设置透视投影
-    projection.perspective(fov, static_cast<float>(aspect), zNear, zFar);
-
-}
-
-void PlayerGLWidget::paintGL()
-{
-    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除屏幕缓存和深度缓冲
-    QMatrix4x4 matrix;
-    matrix.translate(0.0, 0.0, -5.0);                   //矩阵变换
-    program.enableAttributeArray(0);
-    program.enableAttributeArray(1);
-    program.setAttributeArray(0, vertices.constData());
-    program.setAttributeArray(1, texCoords.constData());
-    program.setUniformValue("texture", 0); //将当前上下文中位置的统一变量设置为value
-    texture->bind();  //绑定纹理
-    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);//绘制纹理
-    texture->release(); //释放绑定的纹理
-    texture->destroy(); //消耗底层的纹理对象
-    texture->create();
-
-}

+ 0 - 55
demo/VideoPlayerGL/Player/PlayerGLWidget2.h_

@@ -1,55 +0,0 @@
-#ifndef PLAYEROPENGLWIDGET_H
-#define PLAYEROPENGLWIDGET_H
-
-
-#include <QOpenGLWidget>
-#include <QOpenGLFunctions>
-#include <QOpenGLShaderProgram>
-#include <QOpenGLTexture>
-#include <QTimer>
-
-#include "PlayerGlobalInfo.h"
-
-/**
- * @brief 可以播放,但是CPU占用率比纯CPU绘制还高
- * 
- */
-
-
-class PlayerGLWidget : public QOpenGLWidget , public QOpenGLFunctions
-{
-    Q_OBJECT
-public:
-    explicit PlayerGLWidget(QWidget *parent = nullptr);
-    ~PlayerGLWidget();
-
-    /* 刷新一帧 */
-    void updateFrame(Image_YUV420& image);
-    /* 刷新一帧QImage */
-    void updateFrame(Image_QImage& image);
-
-
-private:
-    void initShaders();
-    void initTextures();
-
-protected:
-    void initializeGL() override;
-    void resizeGL(int w, int h) override;
-    void paintGL() override;
-
-    // void setImage(const QImage &image);
-
-
-private:
-
-    QVector<QVector3D> vertices;
-    QVector<QVector2D> texCoords;
-    QOpenGLShaderProgram program;
-    QOpenGLTexture *texture;
-    QMatrix4x4 projection;
-
-    Image_QImage m_image;
-};
-
-#endif /* PLAYEROPENGLWIDGET_H */

+ 5 - 13
demo/VideoPlayerGL/main.cpp

@@ -1,11 +1,10 @@
 #include "widget.h"
 
 #include <QApplication>
-#include <QSurfaceFormat>
 #include "Logs/loginit.h"
 #include "spdlog/spdlog.h"
-#include "FmtLog/fmtlog.h"
-#include <QProcessEnvironment>
+#include <QDebug>
+#include <qwidget.h>
 
 // extern "C" {
 // #include <libavcodec/avcodec.h>
@@ -15,22 +14,15 @@
 int main(int argc, char *argv[])
 {
     QApplication a(argc, argv);
-    init_log();
-
-    // 设置 QT_QPA_PLATFORM 环境变量
-    qputenv("QT_QPA_PLATFORM", "cocoa");
 
-    QSurfaceFormat format;
-    format.setDepthBufferSize(24);
-    format.setStencilBufferSize(8);
-    format.setVersion(3, 3);
-    format.setProfile(QSurfaceFormat::CoreProfile);
-    QSurfaceFormat::setDefaultFormat(format);
+    init_log();
 
     SPDLOG_INFO("********** VideoPlayer **********");
 
+
     Widget w;
     w.show();
 
+
     return a.exec();
 }

+ 16 - 15
demo/VideoPlayerGL/widget.cpp

@@ -2,12 +2,8 @@
 #include "./ui_widget.h"
 
 #include <QTimer>
-#include <QFileDialog>
-
 #include "spdlog/spdlog.h"
-// #include "fmtlog.h"
 
-// #include "VideoPlayer1.h"
 
 
 Widget::Widget(QWidget *parent)
@@ -16,18 +12,15 @@ Widget::Widget(QWidget *parent)
 {
     ui->setupUi(this);
 
-    m_videoPlayer = std::make_shared<PlayerGLWidget>(ui->widget_display);
-    m_videoPlayer->resize(1280, 720);
-
-    // m_videoPlayer1 = std::make_shared<VideoPlayer1>();
-    // m_videoPlayer1->setParent(ui->widget_display);
-    SPDLOG_INFO("***** Qt Library *****");
+    m_playerGLWidget = new PlayerGLWidget(ui->widget_display);
+    m_playerGLWidget->setGeometry(0, 0, ui->widget_display->width(), ui->widget_display->height());
+    m_playerGLWidget->setStyleSheet(R"(border-radius:10px;)");
 
-    /* 显示预览图片 */
-    QString imagePath = QApplication::applicationDirPath() + "/0.jpg";
-    QImage image(imagePath);
-    image = image.scaled(1280, 720);
-    ui->label->setPixmap(QPixmap::fromImage(image));
+    /* 设置背景颜色 */
+    this->setAutoFillBackground(true);
+    QPalette palette = m_playerGLWidget->palette();
+    palette.setColor(QPalette::Window, Qt::black); // 设置背景颜色为黑色
+    this->setPalette(palette);
     
 }
 
@@ -37,6 +30,14 @@ Widget::~Widget()
     delete ui;
 }
 
+void Widget::resizeEvent(QResizeEvent *event) 
+{
+    if (m_playerGLWidget) {
+        m_playerGLWidget->setGeometry(0, 0, ui->widget_display->width(), ui->widget_display->height());
+    }
+    QWidget::resizeEvent(event);
+}
+
 
 
 

+ 5 - 6
demo/VideoPlayerGL/widget.h

@@ -2,9 +2,8 @@
 #define WIDGET_H
 
 #include <QWidget>
-// #include "VideoPlayer.h"
-// #include "VideoPlayer1.h"
-#include "Player/PlayerGLWidget.h"
+#include <memory>
+#include "PlayerGLWidget.h"
 
 QT_BEGIN_NAMESPACE
 namespace Ui { class Widget; }
@@ -20,13 +19,13 @@ public:
 
 private slots:
 
+protected:
+    void resizeEvent(QResizeEvent *event) override;
     
 private:
     Ui::Widget *ui;
 
-    bool m_isPlay = false;
-    // std::shared_ptr<VideoPlayer1> m_videoPlayer1 = nullptr;
-    std::shared_ptr<PlayerGLWidget> m_videoPlayer = nullptr;
+    PlayerGLWidget *m_playerGLWidget = nullptr;
 
 };
 #endif // WIDGET_H

+ 15 - 19
demo/VideoPlayerGL/widget.ui

@@ -14,25 +14,21 @@
    <string>Widget</string>
   </property>
   <layout class="QVBoxLayout" name="verticalLayout">
-   <item>
-    <widget class="QLabel" name="label">
-     <property name="minimumSize">
-      <size>
-       <width>1280</width>
-       <height>720</height>
-      </size>
-     </property>
-     <property name="maximumSize">
-      <size>
-       <width>1280</width>
-       <height>720</height>
-      </size>
-     </property>
-     <property name="text">
-      <string/>
-     </property>
-    </widget>
-   </item>
+   <property name="spacing">
+    <number>0</number>
+   </property>
+   <property name="leftMargin">
+    <number>20</number>
+   </property>
+   <property name="topMargin">
+    <number>20</number>
+   </property>
+   <property name="rightMargin">
+    <number>20</number>
+   </property>
+   <property name="bottomMargin">
+    <number>20</number>
+   </property>
    <item>
     <widget class="QWidget" name="widget_display" native="true"/>
    </item>

+ 0 - 0
demo/VideoPlayerGL/解码路线图.xmind