【开源项目】QT OPENGL 与 shader 绘制展示视频代码实例 OPenGL直接显示YUV数据

本文使用QT中的QOpenGLFunctions(此类封装了OPenGL的方法,相当于QT版OPenGL)

其次重载 void paintGL();   void initializeGL();  void resizeGL(int width, int height);是基本操作


 * 三种GLSL变量说明
 * varying 顶点与片元共享
 * attribute 顶点使用,由bindAttributeLocation传入
 * uniform 程序传入,uniformLocation获取地址

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;
}

本文配套开源项目

下面是一个简单的QT OpenGL显示YUV图像的示例代码: ```c++ #include <QtOpenGL/QGLWidget> #include <QImage> #include <QKeyEvent> class YUVWidget : public QGLWidget { Q_OBJECT public: YUVWidget(QWidget *parent = nullptr); protected: void initializeGL(); void paintGL(); void resizeGL(int w, int h); void keyPressEvent(QKeyEvent *event); private: GLuint m_textureY; GLuint m_textureU; GLuint m_textureV; int m_width; int m_height; bool m_showY; bool m_showU; bool m_showV; }; YUVWidget::YUVWidget(QWidget *parent) : QGLWidget(parent) { m_textureY = 0; m_textureU = 0; m_textureV = 0; m_width = 0; m_height = 0; m_showY = true; m_showU = true; m_showV = true; } void YUVWidget::initializeGL() { glEnable(GL_TEXTURE_2D); glShadeModel(GL_FLAT); glGenTextures(1, &m_textureY); glGenTextures(1, &m_textureU); glGenTextures(1, &m_textureV); } void YUVWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (m_width > 0 && m_height > 0) { int halfWidth = m_width / 2; int halfHeight = m_height / 2; // 绑定纹理Y glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_textureY); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_width, m_height, GL_RED, GL_UNSIGNED_BYTE, yData); // 绑定纹理U glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m_textureU); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, halfWidth, halfHeight, GL_RED, GL_UNSIGNED_BYTE, uData); // 绑定纹理V glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, m_textureV); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, halfWidth, halfHeight, GL_RED, GL_UNSIGNED_BYTE, vData); // 绘制图像 glBegin(GL_QUADS); if (m_showY) { glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0); glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 0.0, 0.0); glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 0.0, 0.0); } if (m_showU) { glTexCoord2f(0.0, 0.0); glVertex3f(0.0, -1.0, 0.0); glTexCoord2f(1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 0.0, 0.0); glTexCoord2f(0.0, 1.0); glVertex3f(0.0, 0.0, 0.0); } if (m_showV) { glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, 0.0, 0.0); glTexCoord2f(1.0, 0.0); glVertex3f(0.0, 0.0, 0.0); glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0); glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0); } glEnd(); } } void YUVWidget::resizeGL(int w, int h) { glViewport(0, 0, w, h); } void YUVWidget::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_Y) { m_showY = !m_showY; update(); } else if (event->key() == Qt::Key_U) { m_showU = !m_showU; update(); } else if (event->key() == Qt::Key_V) { m_showV = !m_showV; update(); } } // 使用方法: // 1. 创建YUVWidget对象 // 2. 在需要更新图像时,设置yData、uData、vData为图像数据,调用update()更新视图 ``` 在上述代码中,我们使用了OpenGL绘制图像。我们需要创建三个纹理对象,分别用于存储Y、U、V分量的数据。在paintGL()函数中,我们首先绑定这三个纹理对象,并将图像数据传递给它们。然后,我们使用glBegin(GL_QUADS)和glVertex3f()函数来绘制图像。我们可以通过按下键盘上的Y、U、V键来切换是否显示对应的分量。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

༄yi笑奈何

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值