|
@@ -11,6 +11,7 @@ OpenGLVideoWidget::OpenGLVideoWidget(QWidget* parent)
|
|
|
, m_frameFormat(0)
|
|
, m_frameFormat(0)
|
|
|
, m_frameUpdated(false)
|
|
, m_frameUpdated(false)
|
|
|
, m_initialized(false)
|
|
, m_initialized(false)
|
|
|
|
|
+ , m_keepAspectRatio(true)
|
|
|
{
|
|
{
|
|
|
// 设置顶点坐标
|
|
// 设置顶点坐标
|
|
|
m_vertices[0] = -1.0f; m_vertices[1] = -1.0f;
|
|
m_vertices[0] = -1.0f; m_vertices[1] = -1.0f;
|
|
@@ -122,33 +123,36 @@ void OpenGLVideoWidget::resizeGL(int width, int height)
|
|
|
void OpenGLVideoWidget::paintGL()
|
|
void OpenGLVideoWidget::paintGL()
|
|
|
{
|
|
{
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
-
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
QMutexLocker locker(&m_mutex);
|
|
|
-
|
|
|
|
|
if (!m_frameData || m_frameWidth <= 0 || m_frameHeight <= 0 || !m_frameUpdated)
|
|
if (!m_frameData || m_frameWidth <= 0 || m_frameHeight <= 0 || !m_frameUpdated)
|
|
|
return;
|
|
return;
|
|
|
-
|
|
|
|
|
// 绑定纹理并更新数据
|
|
// 绑定纹理并更新数据
|
|
|
glBindTexture(GL_TEXTURE_2D, m_textureId);
|
|
glBindTexture(GL_TEXTURE_2D, m_textureId);
|
|
|
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_frameWidth, m_frameHeight,
|
|
|
|
|
- 0, GL_RGBA, GL_UNSIGNED_BYTE, m_frameData);
|
|
|
|
|
-
|
|
|
|
|
- // 使用着色器程序
|
|
|
|
|
|
|
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_frameWidth, m_frameHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_frameData);
|
|
|
m_program->bind();
|
|
m_program->bind();
|
|
|
-
|
|
|
|
|
- // 设置纹理单元
|
|
|
|
|
m_program->setUniformValue("texture", 0);
|
|
m_program->setUniformValue("texture", 0);
|
|
|
-
|
|
|
|
|
- // 设置顶点和纹理坐标
|
|
|
|
|
m_program->enableAttributeArray(0);
|
|
m_program->enableAttributeArray(0);
|
|
|
m_program->enableAttributeArray(1);
|
|
m_program->enableAttributeArray(1);
|
|
|
m_program->setAttributeArray(0, m_vertices, 2);
|
|
m_program->setAttributeArray(0, m_vertices, 2);
|
|
|
m_program->setAttributeArray(1, m_texCoords, 2);
|
|
m_program->setAttributeArray(1, m_texCoords, 2);
|
|
|
-
|
|
|
|
|
- // 绘制
|
|
|
|
|
|
|
+ // 保持比例
|
|
|
|
|
+ if (m_keepAspectRatio) {
|
|
|
|
|
+ QSize widgetSize = size();
|
|
|
|
|
+ double widgetRatio = double(widgetSize.width()) / widgetSize.height();
|
|
|
|
|
+ double videoRatio = double(m_frameWidth) / m_frameHeight;
|
|
|
|
|
+ int x = 0, y = 0, w = widgetSize.width(), h = widgetSize.height();
|
|
|
|
|
+ if (widgetRatio > videoRatio) {
|
|
|
|
|
+ w = int(h * videoRatio);
|
|
|
|
|
+ x = (widgetSize.width() - w) / 2;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ h = int(w / videoRatio);
|
|
|
|
|
+ y = (widgetSize.height() - h) / 2;
|
|
|
|
|
+ }
|
|
|
|
|
+ glViewport(x, y, w, h);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ glViewport(0, 0, width(), height());
|
|
|
|
|
+ }
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
-
|
|
|
|
|
- // 清理
|
|
|
|
|
m_program->disableAttributeArray(0);
|
|
m_program->disableAttributeArray(0);
|
|
|
m_program->disableAttributeArray(1);
|
|
m_program->disableAttributeArray(1);
|
|
|
m_program->release();
|
|
m_program->release();
|