运行环境:Ubuntu 14.04、Qt 5.6.1
勘误!
由于本人对 opengl 不甚了解,所以,opengl 各版本的差异也不太懂,上次上这个老哥的博客http://blog.csdn.net/su_vast/article/details/52214642,用他的代码一直渲染不了,我感觉我的运气跟狗屎一样臭,试了不知道 多少遍,一直不行,真的跟吃了屎一样难受!直到今天在交叉编译的时候才意识到opengl 从 ver2.1 之后就不再维护 glBegin(), glEnd(),所以ver2.1之后就不能用来绘制图形了!所以又跑回来试了上面那位老哥的程序,哎呀,居然行了!!心中好多只草泥马奔腾而过!!
刚接触 opengl,而且对 qt 不熟悉,又有任务在身,只想以最快的速度完成,但是找了那么多天CSDN博客找到我心力憔悴,找的我连未来都失去了希望,都没找到能让我在机子上跑起来的,真的好能痛苦啊。所以在此,写下这篇博客,希望以后,有这个需求的童鞋能够第一时间跑起来看看运行效果,再去学习就有信心了!在此感谢 github,真的很感谢。废话不多说,上代码!
1、opengl ver2.1 以前版本渲染 NV12 (YUV420sp)
/* gl_widget.h */
#ifndef GL_WIDGET_H
#define GL_WIDGET_H
#include <QFile>
#include <QTimer>
#include <QGLWidget>
#include <QGLShader>
#include <QGLShaderProgram>
class Gl_widget: public QGLWidget
{
Q_OBJECT
public:
Gl_widget(int width, int height, const QString& path, QWidget *parent = 0);
~Gl_widget();
protected:
void paintGL();
void initializeGL();
void resizeGL(int width, int height);
protected slots:
void on_timeout();
private:
int video_width;
int video_height;
GLuint y_texture;
GLuint uv_texture;
unsigned char* y_data;
unsigned char* uv_data;
QFile file;
QTimer timer;
QGLShaderProgram program;
};
#endif
/* gl_widget.cpp */
#include <gl_widget.h>
#include <QtGui>
#include <QtOpenGL>
Gl_widget::Gl_widget(int width, int height, const QString& path, QWidget* parent)
:QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
, video_width(width)
, video_height(height)
, y_texture(0)
, uv_texture(0)
, y_data(0)
, uv_data(0)
, file(path)
, timer(this)
, program(this)
{
setFocusPolicy(Qt::StrongFocus);
y_data = new unsigned char[(width*height*3)>>1];
uv_data = y_data+(width*height);
file.open(QIODevice::ReadOnly);
}
Gl_widget::~Gl_widget()
{
file.close();
glDeleteTextures(1, &y_texture);
glDeleteTextures(1, &uv_texture);
uv_data = 0;
delete [] y_data;
y_data = 0;
}
void Gl_widget::initializeGL()
{
qDebug() << program.addShaderFromSourceCode(QGLShader::Fragment,
"uniform sampler2D y_texture;\n"
"uniform sampler2D uv_texture;\n"
"void main(void)\n"
"{\n"
" float y, u, v, red, green, blue;\n"<