我正在尝试实现一个高效的视频播放器,所以我从Qt的纹理示例开始。
我有:
void HUD::initializeGL()
{
initializeOpenGLFunctions();
makeObject();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
#define PROGRAM_VERTEX_ATTRIBUTE 0
#define PROGRAM_TEXCOORD_ATTRIBUTE 1
QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
const char *vsrc =
"attribute highp vec4 vertex;\n"
"attribute mediump vec4 texCoord;\n"
"varying mediump vec4 texc;\n"
"uniform mediump mat4 matrix;\n"
"void main(void)\n"
"{\n"
" gl_Position = matrix * vertex;\n"
" texc = texCoord;\n"
"}\n";
vshader->compileSourceCode(vsrc);
QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
const char *fsrc =
"uniform sampler2D texture;\n"
"varying mediump vec4 texc;\n"
"void main(void)\n"
"{\n"
" gl_FragColor = texture2D(texture, texc.st);\n"
"}\n";
fshader->compileSourceCode(fsrc);
program = new QOpenGLShaderProgram;
program->addShader(vshader);
program->addShader(fshader);
program->bindAttributeLocation("vertex", PROGRAM_VERTEX_ATTRIBUTE);
program->bindAttributeLocation("texCoord", PROGRAM_TEXCOORD_ATTRIBUTE);
program->link();
program->bind();
program->setUniformValue("texture", 0);
}
void HUD::paintGL()
{
clock_t begin = clock();
glClearColor(clearColor.redF(), clearColor.greenF(), clearColor.blueF(), clearColor.alphaF());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
static int ctt = 0;
QMatrix4x4 m;
m.ortho(-0.2f, +0.2f, +0.2f, -0.2f, 4.0f, 15.0f);
m.translate(0.0f, 0.0f, -5.0f);
program->setUniformValue("matrix", m);
program->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE);
program->enableAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE);
program->setAttributeBuffer(PROGRAM_VERTEX_ATTRIBUTE, GL_FLOAT, 0, 3, 5 * sizeof(GLfloat));
program->setAttributeBuffer(PROGRAM_TEXCOORD_ATTRIBUTE, GL_FLOAT, 3 * sizeof(GLfloat), 2, 5 * sizeof(GLfloat));
//???//
//How to bind the QImage into a texture efficiently???
//???//
textures->bind();// textures is a QOpenGLTexture
glDrawArrays(GL_TRIANGLE_FAN, 0 * 4, 4);
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
qDebug() << elapsed_secs;
}
该示例最初使用 QOpenGLTexture 从单个QImage读取并将其绑定到paintGL()函数绘制的四边形。
但是,如何更新纹理内容以便以30 Hz的频率显示视频?
答案 0 :(得分:2)
你应该看看Qt video widget example这是一个基本的视频播放器。您还可以将视频流式传输到可以显示在QGraphicsView中的QGraphicsVideoItem,它可以包含其他图形项,如果您为其提供QOpenGLWidget视口,则可以呈现OpenGL内容。
如果你想自己动手,你可能想要使用Qt视频功能并实现一个基于QAbstractVideoSurface的类来接收来自媒体播放器的视频帧。看一下QVideoFrame的handleType()。如果是QAbstractVideoBuffer :: GLTextureHandle或QAbstractVideoBuffer :: EGLImageHandle,您可以使用带有handle()作为纹理ID的四边形着色器将其绘制到屏幕上。如果没有将数据写入QOpenGLTexture。您可以在线程中上传纹理以防止阻塞GUI线程。
答案 1 :(得分:0)
似乎QOpenGLTexture类不允许更新图像数据,请参阅QOpenGLTexture::allocateStorage。
在这种情况下,您应该考虑分配自己的OpenGL纹理并更新它。这可以使用以下代码完成:
// Keep the ID somewhere
GLuint textureId;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// First frame
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, <w>, <h>, 0, GL_RGB, GL_UNSIGNED_BYTE, frame.bits());
// Subsequent frames
glTextSubImage2D(GL_TEXTURE_2D, 0, 0, 0, <w>, <h>, GL_RGB, GL_UNSIGNED_BYTE, frame.bits());
// Destruction
glDeleteTextures(1, &textureId);
要在上传之前准备像素数据,您可以执行以下操作:
QImage prepareFrame(const QImage& frame) {
if(frame.format() != QImage::Format_RGB888) {
return frame.convertToFormat(QImage::Format_RGB888);
}
return frame;
}