資源簡(jiǎn)介
yuv420在qt5下的使用opengl顯示方法,在ubuntu16下測(cè)試通過(guò)。

代碼片段和文件信息
#include?“cplaywidget.h“
#include?
#include?
#include?
CPlayWidget::CPlayWidget(QWidget?*parent):QOpenGLWidget(parent)
{
????textureUniformY?=?0;
????textureUniformU?=?0;
????textureUniformV?=?0;
????id_y?=?0;
????id_u?=?0;
????id_v?=?0;
????m_pBufYuv420p?=?NULL;
????m_pVSHader?=?NULL;
????m_pFSHader?=?NULL;
????m_pShaderProgram?=?NULL;
????m_pTextureY?=?NULL;
????m_pTextureU?=?NULL;
????m_pTextureV?=?NULL;
????m_pYuvFile?=?NULL;
????m_nVideoH?=?0;
????m_nVideoW?=?0;
}
CPlayWidget::~CPlayWidget()
{
}
void?CPlayWidget::PlayOneframe()
{//函數(shù)功能讀取一張yuv圖像數(shù)據(jù)進(jìn)行顯示每單擊一次,就顯示一張圖片
????if(NULL?==?m_pYuvFile)
????{
????????//打開(kāi)yuv視頻文件?注意修改文件路徑
???????//?m_pYuvFile?=?fopen(“F://OpenglYuvDemo//coastguard_cif.yuv“?“rb“);
?????????m_pYuvFile?=?fopen(“/home/root/test_cif.yuv“?“rb“);
?????????m_nVideoW?=?352;
?????????m_nVideoH?=?288;
????????//根據(jù)yuv視頻數(shù)據(jù)的分辨率設(shè)置寬高demo當(dāng)中是1080p,這個(gè)地方要注意跟實(shí)際數(shù)據(jù)分辨率對(duì)應(yīng)上
//????????m_nVideoW?=?1920;
//????????m_nVideoH?=?1080;
????}
????//申請(qǐng)內(nèi)存存一幀yuv圖像數(shù)據(jù)其大小為分辨率的1.5倍
????int?nLen?=?m_nVideoW*m_nVideoH*3/2;
????if(NULL?==?m_pBufYuv420p)
????{
????????m_pBufYuv420p?=?new?unsigned?char[nLen];
????????qDebug(“CPlayWidget::PlayOneframe?new?data?memory.?Len=%d?width=%d?height=%d\n“
???????????????nLen?m_nVideoW?m_nVideoH);
????}
????//將一幀yuv圖像讀到內(nèi)存中
????if(NULL?==?m_pYuvFile)
????{
????????qFatal(“read?yuv?file?err.may?be?path?is?wrong!\n“);
????????return;
????}
????fread(m_pBufYuv420p?1?nLen?m_pYuvFile);
????//刷新界面觸發(fā)paintGL接口
????update();
????return;
}
void?CPlayWidget::initializeGL()
{
????initializeOpenGLFunctions();
????glEnable(GL_DEPTH_TEST);
????//現(xiàn)代opengl渲染管線依賴著色器來(lái)處理傳入的數(shù)據(jù)
????//著色器:就是使用openGL著色語(yǔ)言(OpenGL?Shading?Language?GLSL)編寫(xiě)的一個(gè)小函數(shù)
????//???????GLSL是構(gòu)成所有OpenGL著色器的語(yǔ)言具體的GLSL語(yǔ)言的語(yǔ)法需要讀者查找相關(guān)資料
????//初始化頂點(diǎn)著色器?對(duì)象
????m_pVSHader?=?new?QOpenGLShader(QOpenGLShader::Vertex?this);
????//頂點(diǎn)著色器源碼
????const?char?*vsrc?=?“attribute?vec4?vertexIn;?\
????attribute?vec2?textureIn;?\
????varying?vec2?textureOut;??\
????void?main(void)???????????\
????{?????????????????????????\
????????gl_Position?=?vertexIn;?\
????????textureOut?=?textureIn;?\
????}“;
????//編譯頂點(diǎn)著色器程序
????bool?bCompile?=?m_pVSHader->compileSourceCode(vsrc);
????if(!bCompile)
????{
????}
????//初始化片段著色器?功能gpu中yuv轉(zhuǎn)換成rgb
????m_pFSHader?=?new?QOpenGLShader(QOpenGLShader::Fragment?this);
????//片段著色器源碼
????const?char?*fsrc?=?“varying?vec2?textureOut;?\
????uniform?sampler2D?tex_y;?\
????uniform?sampler2D?tex_u;?\
????uniform?sampler2D?tex_v;?\
????void?main(void)?\
????{?\
????????vec3?yuv;?\
????????vec3?rgb;?\
????????yuv.x?=?texture2D(tex_y?textureOut).r;?\
????????yuv.y?=?texture2D(tex_u?textureOut).r?-?0.5;?\
????????yuv.z?=?texture2D(tex_v?textureOut).r?-?0.5;?\
????????rgb?=?mat3(?1???????1?????????1?\
????????????????????0???????-0.39465??2.03211?\
????????????????????1.13983?-0.58060??0)?*?yuv;?\
????????gl_FragColor?=?vec4(rgb?1);?\
????}“;
????//將glsl源碼送入編譯器編譯著色器程序
????bCompile?=?m_pFSHader->compileS
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????1475??2018-09-08?17:07??cplaywidget.h
?????文件?????????172??2018-09-08?16:36??main.cpp
?????文件?????????873??2018-09-19?13:37??mainwindow.cpp
?????文件?????????499??2018-09-12?11:35??mainwindow.h
?????文件????????1751??2018-09-19?13:37??mainwindow.ui
?????文件?????????603??2018-09-14?14:14??slYuv.pro
?????文件???????46303??2018-09-19?14:27??slYuv.pro.user
?????文件????????8730??2018-09-19?14:26??cplaywidget.cpp
評(píng)論
共有 條評(píng)論