PlayerGLWidget2.cpp_ 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "PlayerGLWidget.h"
  2. PlayerGLWidget::PlayerGLWidget(QWidget *parent) : QOpenGLWidget(parent)
  3. {
  4. }
  5. PlayerGLWidget::~PlayerGLWidget()
  6. {
  7. }
  8. /* 刷新一帧 */
  9. void PlayerGLWidget::updateFrame(Image_YUV420& image)
  10. {
  11. // yData = image.yData;
  12. // uData = image.uData;
  13. // vData = image.vData;
  14. update();
  15. }
  16. /* 刷新一帧QImage */
  17. void PlayerGLWidget::updateFrame(Image_QImage& image)
  18. {
  19. m_image = image;
  20. texture->setData(m_image.image);
  21. /* 设置纹理细节 */
  22. texture->setLevelofDetailBias(-1);
  23. update();
  24. }
  25. void PlayerGLWidget::initShaders()
  26. {
  27. //纹理坐标
  28. texCoords.append(QVector2D(0, 1)); //左上
  29. texCoords.append(QVector2D(1, 1)); //右上
  30. texCoords.append(QVector2D(0, 0)); //左下
  31. texCoords.append(QVector2D(1, 0)); //右下
  32. //顶点坐标
  33. vertices.append(QVector3D(-1, -1, 1));//左下
  34. vertices.append(QVector3D(1, -1, 1)); //右下
  35. vertices.append(QVector3D(-1, 1, 1)); //左上
  36. vertices.append(QVector3D(1, 1, 1)); //右上
  37. QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
  38. const char *vsrc =
  39. "attribute vec4 vertex;\n"
  40. "attribute vec2 texCoord;\n"
  41. "varying vec2 texc;\n"
  42. "void main(void)\n"
  43. "{\n"
  44. " gl_Position = vertex;\n"
  45. " texc = texCoord;\n"
  46. "}\n";
  47. vshader->compileSourceCode(vsrc);//编译顶点着色器代码
  48. QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
  49. const char *fsrc =
  50. "uniform sampler2D texture;\n"
  51. "varying vec2 texc;\n"
  52. "void main(void)\n"
  53. "{\n"
  54. " gl_FragColor = texture2D(texture,texc);\n"
  55. "}\n";
  56. fshader->compileSourceCode(fsrc); //编译纹理着色器代码
  57. program.addShader(vshader);//添加顶点着色器
  58. program.addShader(fshader);//添加纹理碎片着色器
  59. program.bindAttributeLocation("vertex", 0);//绑定顶点属性位置
  60. program.bindAttributeLocation("texCoord", 1);//绑定纹理属性位置
  61. // 链接着色器管道
  62. if (!program.link())
  63. close();
  64. // 绑定着色器管道
  65. if (!program.bind())
  66. close();
  67. }
  68. void PlayerGLWidget::initTextures()
  69. {
  70. // 加载 Avengers.jpg 图片
  71. texture = new QOpenGLTexture(QOpenGLTexture::Target2D);
  72. texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
  73. texture->setMagnificationFilter(QOpenGLTexture::Linear);
  74. //重复使用纹理坐标
  75. //纹理坐标(1.1, 1.2)与(0.1, 0.2)相同
  76. texture->setWrapMode(QOpenGLTexture::Repeat);
  77. //设置纹理大小
  78. texture->setSize(this->width(), this->height());
  79. //分配储存空间
  80. texture->allocateStorage();
  81. }
  82. void PlayerGLWidget::initializeGL()
  83. {
  84. initializeOpenGLFunctions(); //初始化OPenGL功能函数
  85. glClearColor(0, 0, 0, 0); //设置背景为黑色
  86. glEnable(GL_TEXTURE_2D); //设置纹理2D功能可用
  87. initTextures(); //初始化纹理设置
  88. initShaders(); //初始化shaders
  89. }
  90. void PlayerGLWidget::resizeGL(int w, int h)
  91. {
  92. // 计算窗口横纵比
  93. qreal aspect = qreal(w) / qreal(h ? h : 1);
  94. // 设置近平面值 3.0, 远平面值 7.0, 视场45度
  95. const qreal zNear = 3.0, zFar = 7.0, fov = 45.0;
  96. // 重设投影
  97. projection.setToIdentity();
  98. // 设置透视投影
  99. projection.perspective(fov, static_cast<float>(aspect), zNear, zFar);
  100. }
  101. void PlayerGLWidget::paintGL()
  102. {
  103. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除屏幕缓存和深度缓冲
  104. QMatrix4x4 matrix;
  105. matrix.translate(0.0, 0.0, -5.0); //矩阵变换
  106. program.enableAttributeArray(0);
  107. program.enableAttributeArray(1);
  108. program.setAttributeArray(0, vertices.constData());
  109. program.setAttributeArray(1, texCoords.constData());
  110. program.setUniformValue("texture", 0); //将当前上下文中位置的统一变量设置为value
  111. texture->bind(); //绑定纹理
  112. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);//绘制纹理
  113. texture->release(); //释放绑定的纹理
  114. texture->destroy(); //消耗底层的纹理对象
  115. texture->create();
  116. }