no message

master
a7458969 2020-05-25 23:52:40 +08:00
parent 2bc06e7dc3
commit d3aff4e773
3 changed files with 152 additions and 56 deletions

View File

@ -34,14 +34,11 @@ void main(void) \
// rgb片段着色器源码 // rgb片段着色器源码
const char *fsrcrgb = "in vec3 ourColor; \ const char *fsrcrgb = "in vec3 ourColor; \
in vec2 TexCoord; \ in vec2 TexCoord; \
out vec4 color; \ uniform sampler2D rgbdata; \
uniform sampler2D ourTexture1; \
// Texture samplers \
uniform sampler2D ourTexture2; \
uniform float mixValue; \ uniform float mixValue; \
void main() \ void main() \
{ \ { \
color = texture(ourTexture1, TexCoord); \ gl_FragColor = texture(rgbdata, TexCoord); \
}"; }";
void CPlayWidget::OnUpdateFrame() { void CPlayWidget::OnUpdateFrame() {
@ -68,6 +65,7 @@ CPlayWidget::CPlayWidget(QWidget *parent):QOpenGLWidget(parent) {
id_y = 0; id_y = 0;
id_u = 0; id_u = 0;
id_v = 0; id_v = 0;
m_pTextureRGB = nullptr;
m_pBufYuv420p = NULL; m_pBufYuv420p = NULL;
m_pVSHader = NULL; m_pVSHader = NULL;
m_pFSHader = NULL; m_pFSHader = NULL;
@ -81,7 +79,7 @@ CPlayWidget::CPlayWidget(QWidget *parent):QOpenGLWidget(parent) {
mType = TYPE_YUV420P; mType = TYPE_YUV420P;
connect(&this->tm,SIGNAL(timeout()),this,SLOT(OnUpdateFrame())); connect(&this->tm,SIGNAL(timeout()),this,SLOT(OnUpdateFrame()));
tm.start(1000); //tm.start(1000);
} }
CPlayWidget::~CPlayWidget() { CPlayWidget::~CPlayWidget() {
} }
@ -91,8 +89,6 @@ void CPlayWidget::PlayOneFrame() {//函数功能读取一张yuv图像数据进
//打开yuv视频文件 注意修改文件路径 //打开yuv视频文件 注意修改文件路径
// m_pYuvFile = fopen("F://OpenglYuvDemo//1920_1080.yuv", "rb"); // m_pYuvFile = fopen("F://OpenglYuvDemo//1920_1080.yuv", "rb");
m_pYuvFile = fopen("F://md_sample_sp420_1080p.yuv", "rb"); m_pYuvFile = fopen("F://md_sample_sp420_1080p.yuv", "rb");
m_nVideoW = 1920;
m_nVideoH = 1080;
//根据yuv视频数据的分辨率设置宽高,demo当中是1080p这个地方要注意跟实际数据分辨率对应上 //根据yuv视频数据的分辨率设置宽高,demo当中是1080p这个地方要注意跟实际数据分辨率对应上
// m_nVideoW = 1920; // m_nVideoW = 1920;
// m_nVideoH = 1080; // m_nVideoH = 1080;
@ -122,6 +118,25 @@ int CPlayWidget::SetDataType(CPlayWidget::IMG_TYPE type)
this->mType = type; this->mType = type;
} }
int CPlayWidget::OnCameraData(uint8_t *dat, uint32_t size)
{
memcpy(this->m_pBufRgb32,dat,size);
qDebug()<<"camera receive data"<<size<<"\r\n";
update();
}
int CPlayWidget::SetImgSize(uint32_t width, uint32_t height)
{
m_nVideoH = height;
m_nVideoW = width;
if(mType == TYPE_RGB32){
m_pBufRgb32 = new uint8_t[width * height *4];
}
if(mType == TYPE_YUV420P){
m_pBufYuv420p = new uint8_t[width * height *3/2];
}
}
/* /*
@ -157,8 +172,12 @@ void CPlayWidget::initializeGL()
} }
//初始化片段着色器 功能gpu中yuv转换成rgb //初始化片段着色器 功能gpu中yuv转换成rgb
m_pFSHader = new QOpenGLShader(QOpenGLShader::Fragment, this); m_pFSHader = new QOpenGLShader(QOpenGLShader::Fragment, this);
if(mType == TYPE_RGB32){
bCompile = m_pFSHader->compileSourceCode(fsrcrgb);
}
if(mType == TYPE_YUV420P){
bCompile = m_pFSHader->compileSourceCode(fsrcyuv); bCompile = m_pFSHader->compileSourceCode(fsrcyuv);
}
if(!bCompile) if(!bCompile)
{ {
// todo 设置错误状态 // todo 设置错误状态
@ -180,6 +199,38 @@ void CPlayWidget::initializeGL()
m_pShaderProgram->link(); m_pShaderProgram->link();
//激活所有链接 //激活所有链接
m_pShaderProgram->bind(); m_pShaderProgram->bind();
if(this->mType == TYPE_YUV420P){
initShaderYuv();
}
if(this->mType == TYPE_RGB32){
initShaderRgb();
}
glClearColor(0.3,0.3,0.3,0.0);//设置背景色
}
void CPlayWidget::resizeGL(int w, int h)
{
if(h == 0)// 防止被零除
{
h = 1;// 将高设为1
}
//设置视口
glViewport(0,0, w,h);
}
void CPlayWidget::paintGL()
{
if(mType == TYPE_YUV420P)
loadYuvTexture();
if(mType == TYPE_RGB32){
loadRgbTexture();
}
//使用顶点数组方式绘制图形
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
return;
}
void CPlayWidget::initShaderYuv()
{
//读取着色器中的数据变量tex_y, tex_u, tex_v的位置,这些变量的声明可以在 //读取着色器中的数据变量tex_y, tex_u, tex_v的位置,这些变量的声明可以在
//片段着色器源码中可以看到 //片段着色器源码中可以看到
textureUniformY = m_pShaderProgram->uniformLocation("tex_y"); textureUniformY = m_pShaderProgram->uniformLocation("tex_y");
@ -220,23 +271,40 @@ void CPlayWidget::initializeGL()
id_u = m_pTextureU->textureId(); id_u = m_pTextureU->textureId();
//获取返回v分量的纹理索引值 //获取返回v分量的纹理索引值
id_v = m_pTextureV->textureId(); id_v = m_pTextureV->textureId();
glClearColor(0.3,0.3,0.3,0.0);//设置背景色
} }
void CPlayWidget::resizeGL(int w, int h)
void CPlayWidget::initShaderRgb()
{ {
if(h == 0)// 防止被零除 //读取着色器中的数据变量tex_y, tex_u, tex_v的位置,这些变量的声明可以在
{ //片段着色器源码中可以看到
h = 1;// 将高设为1 textureUniformRGB = m_pShaderProgram->uniformLocation("rgbdata");
} // 顶点矩阵
//设置视口 static const GLfloat vertexVertices[] = {
glViewport(0,0, w,h); -1.0f, -1.0f,
} 1.0f, -1.0f,
void CPlayWidget::paintGL() -1.0f, 1.0f,
{ 1.0f, 1.0f,
loadYuvTexture(); };
//使用顶点数组方式绘制图形 //纹理矩阵
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); static const GLfloat textureVertices[] = {
return; 0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
};
//设置属性ATTRIB_VERTEX的顶点矩阵值以及格式
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, vertexVertices);
//设置属性ATTRIB_TEXTURE的纹理矩阵值以及格式
glVertexAttribPointer(ATTRIB_TEXTURE, 2, GL_FLOAT, 0, 0, textureVertices);
//启用ATTRIB_VERTEX属性的数据,默认是关闭的
glEnableVertexAttribArray(ATTRIB_VERTEX);
//启用ATTRIB_TEXTURE属性的数据,默认是关闭的
glEnableVertexAttribArray(ATTRIB_TEXTURE);
//分别创建y,u,v纹理对象
m_pTextureRGB = new QOpenGLTexture(QOpenGLTexture::Target2D);
m_pTextureRGB->create();
//获取返回y分量的纹理索引值
id_rgb = m_pTextureRGB->textureId();
} }
int CPlayWidget::loadYuvTexture() int CPlayWidget::loadYuvTexture()
@ -288,3 +356,19 @@ void CPlayWidget::resizeGL(int w, int h)
glUniform1i(textureUniformV, 2); glUniform1i(textureUniformV, 2);
return 0; return 0;
} }
int CPlayWidget::loadRgbTexture()
{
//加载rgb数据纹理
//激活纹理单元GL_TEXTURE0
glActiveTexture(GL_TEXTURE0);
//使用来自y数据生成纹理
glBindTexture(GL_TEXTURE_2D, id_rgb);
//使用内存中m_pBufYuv420p数据创建真正的y数据纹理
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_nVideoW, m_nVideoH, 0, GL_RGB, GL_UNSIGNED_BYTE, m_pBufRgb32);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glUniform1i(textureUniformRGB, 0);
}

View File

@ -11,7 +11,7 @@
#define ATTRIB_VERTEX 3 #define ATTRIB_VERTEX 3
#define ATTRIB_TEXTURE 4 #define ATTRIB_TEXTURE 4
class CPlayWidget:public QOpenGLWidget,protected QOpenGLFunctions class CPlayWidget:public QOpenGLWidget,protected QOpenGLFunctions,public Camera::CameraObserver
{ {
Q_OBJECT Q_OBJECT
public slots: public slots:
@ -26,6 +26,8 @@ public:
~CPlayWidget(); ~CPlayWidget();
void PlayOneFrame(); void PlayOneFrame();
int SetDataType(IMG_TYPE); int SetDataType(IMG_TYPE);
int OnCameraData(uint8_t *dat, uint32_t size) override;
int SetImgSize(uint32_t width,uint32_t );
protected: protected:
QTimer tm; QTimer tm;
void initializeGL() Q_DECL_OVERRIDE; void initializeGL() Q_DECL_OVERRIDE;
@ -36,14 +38,18 @@ private:
GLuint textureUniformY; //y纹理数据位置 GLuint textureUniformY; //y纹理数据位置
GLuint textureUniformU; //u纹理数据位置 GLuint textureUniformU; //u纹理数据位置
GLuint textureUniformV; //v纹理数据位置 GLuint textureUniformV; //v纹理数据位置
GLuint textureUniformRGB; //RGB纹理位置
GLuint textureUnifromRGB; //rgb32 的纹理位置 GLuint textureUnifromRGB; //rgb32 的纹理位置
GLuint id_rgb;
GLuint id_y; GLuint id_y;
GLuint id_u; GLuint id_u;
GLuint id_v; //v纹理对象ID GLuint id_v; //v纹理对象ID
QOpenGLTexture* m_pTextureRGB; //RGB 纹理是一整块的
QOpenGLTexture* m_pTextureY; //y纹理对象 QOpenGLTexture* m_pTextureY; //y纹理对象
QOpenGLTexture* m_pTextureU; //u纹理对象 QOpenGLTexture* m_pTextureU; //u纹理对象
QOpenGLTexture* m_pTextureV; //v纹理对象 QOpenGLTexture* m_pTextureV; //v纹理对象
@ -53,8 +59,13 @@ private:
int m_nVideoW; //视频分辨率宽 int m_nVideoW; //视频分辨率宽
int m_nVideoH; //视频分辨率高 int m_nVideoH; //视频分辨率高
unsigned char *m_pBufYuv420p; unsigned char *m_pBufYuv420p;
unsigned char* m_pBufRgb32;
FILE* m_pYuvFile; FILE* m_pYuvFile;
void initShaderYuv();
void initShaderRgb();
int loadYuvTexture(); int loadYuvTexture();
int loadRgbTexture(); int loadRgbTexture();
}; };

View File

@ -12,7 +12,8 @@ int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);
CPlayWidget gPlayer(nullptr); CPlayWidget gPlayer(nullptr);
gPlayer.SetDataType(CPlayWidget::IMG_TYPE::TYPE_RGB32);
gPlayer.SetImgSize(1920,1080);
gPlayer.show(); gPlayer.show();
Camera *gCam = Camera::GetInstance(); Camera *gCam = Camera::GetInstance();
@ -20,7 +21,7 @@ int main(int argc, char *argv[])
for(auto itr = names.begin();itr != names.end();itr++){ for(auto itr = names.begin();itr != names.end();itr++){
qDebug()<<QString::fromStdWString(*itr); qDebug()<<QString::fromStdWString(*itr);
} }
gCam->SetCallBack(CameraDataCallback); gCam->SetObserver(&gPlayer);
if(gCam->Open(*names.begin()) ){ if(gCam->Open(*names.begin()) ){
qDebug("open success"); qDebug("open success");
}else{ }else{