123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- #include "PlayerGLWidget.h"
- PlayerGLWidget::PlayerGLWidget(QWidget *parent) : QOpenGLWidget(parent)
- {
- }
- PlayerGLWidget::~PlayerGLWidget()
- {
- }
- void PlayerGLWidget::updateFrame(Image_YUV420& image)
- {
-
-
-
- update();
- }
- void PlayerGLWidget::updateFrame(Image_QImage& image)
- {
- m_image = image;
- texture->setData(m_image.image);
-
- texture->setLevelofDetailBias(-1);
- update();
- }
- void PlayerGLWidget::initShaders()
- {
-
- texCoords.append(QVector2D(0, 1));
- texCoords.append(QVector2D(1, 1));
- texCoords.append(QVector2D(0, 0));
- texCoords.append(QVector2D(1, 0));
-
- vertices.append(QVector3D(-1, -1, 1));
- vertices.append(QVector3D(1, -1, 1));
- vertices.append(QVector3D(-1, 1, 1));
- vertices.append(QVector3D(1, 1, 1));
- QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
- const char *vsrc =
- "attribute vec4 vertex;\n"
- "attribute vec2 texCoord;\n"
- "varying vec2 texc;\n"
- "void main(void)\n"
- "{\n"
- " gl_Position = vertex;\n"
- " texc = texCoord;\n"
- "}\n";
- vshader->compileSourceCode(vsrc);
-
- QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
- const char *fsrc =
- "uniform sampler2D texture;\n"
- "varying vec2 texc;\n"
- "void main(void)\n"
- "{\n"
- " gl_FragColor = texture2D(texture,texc);\n"
- "}\n";
- fshader->compileSourceCode(fsrc);
-
- program.addShader(vshader);
- program.addShader(fshader);
- program.bindAttributeLocation("vertex", 0);
- program.bindAttributeLocation("texCoord", 1);
-
- if (!program.link())
- close();
-
- if (!program.bind())
- close();
- }
- void PlayerGLWidget::initTextures()
- {
-
- texture = new QOpenGLTexture(QOpenGLTexture::Target2D);
- texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
- texture->setMagnificationFilter(QOpenGLTexture::Linear);
-
-
- texture->setWrapMode(QOpenGLTexture::Repeat);
-
- texture->setSize(this->width(), this->height());
-
- texture->allocateStorage();
- }
- void PlayerGLWidget::initializeGL()
- {
- initializeOpenGLFunctions();
- glClearColor(0, 0, 0, 0);
- glEnable(GL_TEXTURE_2D);
- initTextures();
- initShaders();
- }
- void PlayerGLWidget::resizeGL(int w, int h)
- {
-
- qreal aspect = qreal(w) / qreal(h ? h : 1);
-
- const qreal zNear = 3.0, zFar = 7.0, fov = 45.0;
-
- projection.setToIdentity();
-
- projection.perspective(fov, static_cast<float>(aspect), zNear, zFar);
- }
- void PlayerGLWidget::paintGL()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- QMatrix4x4 matrix;
- matrix.translate(0.0, 0.0, -5.0);
- program.enableAttributeArray(0);
- program.enableAttributeArray(1);
- program.setAttributeArray(0, vertices.constData());
- program.setAttributeArray(1, texCoords.constData());
- program.setUniformValue("texture", 0);
- texture->bind();
- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
- texture->release();
- texture->destroy();
- texture->create();
- }
|