QT OPENGL 與 shader 繪製展示視訊程式碼例項 OPenGL直接顯示YUV資料

༄yi笑奈何發表於2020-06-17

XvideoWidget.h

#pragma once

#include <QOpenGLWidget>
#include<QOpenGLFunctions>
#include<QGLShaderProgram>

class XvideoWidget : public QOpenGLWidget,protected QOpenGLFunctions
{
	Q_OBJECT

public:
	XvideoWidget(QWidget *parent);
	~XvideoWidget();

protected:
	//過載
	//重新整理顯示
	void paintGL();
	//初始化GL
	void initializeGL();
	//視窗尺寸變化
	void resizeGL(int width, int height);

private:
	//shader程式
	QGLShaderProgram program;

	//shader中yuv變數地址
	GLuint unis[3] = { 0 };
	//openg的 texture地址
	GLuint texs[3] = { 0 };

	//材質記憶體空間
	unsigned char *datas[3] = { 0 };

	int width = 352;
	int height = 288;


};

XvideoWidget.cpp

#include "XvideoWidget.h"
#include <QDebug>
#include <QTimer>
//自動加雙引號
#define GET_STR(x) #x
#define A_VER 3
#define T_VER 4

FILE *fp = NULL;

//頂點shader
const char *vString = GET_STR(
attribute vec4 vertexIn;   //頂點座標
attribute vec2 textureIn;  //材質座標
varying vec2 textureOut;   //頂點shader片元shader共享
void main(void)
{
	gl_Position = vertexIn;
	textureOut = textureIn;
}
);


//片元shader
const char *tString = GET_STR(
varying vec2 textureOut;
uniform sampler2D tex_y;
uniform sampler2D tex_u;
uniform sampler2D tex_v;
void main(void)
{
	vec3 rgb;
	vec3 yuv;
	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;
	//YUV轉RGB固定公式,利用矩陣轉換
	rgb = mat3(1.0, 1.0, 1.0,
		0.0, -0.39465, 2.03211,
		1.13983, -0.58060, 0.0) * yuv;
	gl_FragColor = vec4(rgb, 1.0);
}

);



//準備yuv資料
// ffmpeg -i v1080.mp4 -t 10 -s 240x128 -pix_fmt yuv420p  out240x128.yuv
XvideoWidget::XvideoWidget(QWidget *parent)
	: QOpenGLWidget(parent)
{
}

XvideoWidget::~XvideoWidget()
{
}

//初始化opengl
void XvideoWidget::initializeGL()
{
	qDebug() << "initializeGL";

	//初始化opengl (QOpenGLFunctions繼承)函式 
	initializeOpenGLFunctions();

	//program載入shader(頂點和片元)指令碼
	//片元(畫素)
	qDebug() << program.addShaderFromSourceCode(QGLShader::Fragment, tString);
	//頂點shader
	qDebug() << program.addShaderFromSourceCode(QGLShader::Vertex, vString);

	//設定頂點座標的變數
	program.bindAttributeLocation("vertexIn", A_VER);

	//設定材質座標
	program.bindAttributeLocation("textureIn", T_VER);

	//編譯shader
	qDebug() << "program.link() = " << program.link();

	qDebug() << "program.bind() = " << program.bind();

	//傳遞頂點和材質座標
	//頂點
	static const GLfloat ver[] = {
		-1.0f,-1.0f,
		1.0f,-1.0f,
		-1.0f, 1.0f,
		1.0f,1.0f
	};

	//材質
	static const GLfloat tex[] = {
		0.0f, 1.0f,
		1.0f, 1.0f,
		0.0f, 0.0f,
		1.0f, 0.0f
	};

	//頂點
	glVertexAttribPointer(A_VER, 2, GL_FLOAT, 0, 0, ver);
	glEnableVertexAttribArray(A_VER);

	//材質
	glVertexAttribPointer(T_VER, 2, GL_FLOAT, 0, 0, tex);
	glEnableVertexAttribArray(T_VER);


	//從shader獲取材質
	unis[0] = program.uniformLocation("tex_y");
	unis[1] = program.uniformLocation("tex_u");
	unis[2] = program.uniformLocation("tex_v");

	//建立材質,建立三個物件
	glGenTextures(3, texs);

	//Y 繫結材質
	glBindTexture(GL_TEXTURE_2D, texs[0]);
	//放大過濾,線性插值   GL_NEAREST(效率高,但馬賽克嚴重)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	//建立材質顯示卡空間
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, 0);

	//U
	glBindTexture(GL_TEXTURE_2D, texs[1]);
	//放大過濾,線性插值
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	//建立材質顯示卡空間
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width / 2, height / 2, 0, GL_RED, GL_UNSIGNED_BYTE, 0);

	//V
	glBindTexture(GL_TEXTURE_2D, texs[2]);
	//放大過濾,線性插值
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	//建立材質顯示卡空間
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width / 2, height / 2, 0, GL_RED, GL_UNSIGNED_BYTE, 0);

	///分配材質記憶體空間
	datas[0] = new unsigned char[width*height];		//Y
	datas[1] = new unsigned char[width*height / 4];	//U
	datas[2] = new unsigned char[width*height / 4];	//V

	fp = fopen("VIDEO2.yuv", "rb");
	if (!fp)
	{
		qDebug() << "VIDEO2.yuv file open failed!";
	}


	//啟動定時器
	QTimer *ti = new QTimer(this);
	connect(ti, SIGNAL(timeout()), this, SLOT(update()));
	ti->start(40);
}

//重新整理顯示
void XvideoWidget::paintGL()
{
	if (feof(fp))
	{
		fseek(fp, 0, SEEK_SET);
	}
	fread(datas[0], 1, width*height, fp);
	fread(datas[1], 1, width*height / 4, fp);
	fread(datas[2], 1, width*height / 4, fp);

	glActiveTexture(GL_TEXTURE0);//啟用第0層
	glBindTexture(GL_TEXTURE_2D, texs[0]); //0層繫結到Y材質
	//修改材質內容(複製記憶體內容)
	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RED, GL_UNSIGNED_BYTE, datas[0]);
	//與shader uni遍歷關聯
	glUniform1i(unis[0], 0);//0表示對應0層


	glActiveTexture(GL_TEXTURE0 + 1);
	glBindTexture(GL_TEXTURE_2D, texs[1]); //1層繫結到U材質
										   //修改材質內容(複製記憶體內容)
	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width / 2, height / 2, GL_RED, GL_UNSIGNED_BYTE, datas[1]);
	//與shader uni遍歷關聯
	glUniform1i(unis[1], 1);


	glActiveTexture(GL_TEXTURE0 + 2);
	glBindTexture(GL_TEXTURE_2D, texs[2]); //2層繫結到V材質
										   //修改材質內容(複製記憶體內容)
	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width / 2, height / 2, GL_RED, GL_UNSIGNED_BYTE, datas[2]);
	//與shader uni遍歷關聯
	glUniform1i(unis[2], 2);

	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
	qDebug() << "paintGL";
}


// 視窗尺寸變化
void XvideoWidget::resizeGL(int width, int height)
{
	qDebug() << "resizeGL " << width << ":" << height;
}

 

相關文章