#include "qvideowidget_rgb.h"
#include "qvideoshader.h"

QVideoWidget_RGB::QVideoWidget_RGB(QWidget* parent, Qt::WindowFlags f):QVideoWidget(parent, f)
{
    m_nTextureWidth = 1920;
    m_nTextureHeight = 1080;
}

QVideoWidget_RGB::~QVideoWidget_RGB()
{

}

void QVideoWidget_RGB::initializeGL()
{
    QOpenGLContext * pCurrent = QOpenGLContext::currentContext();

    initializeOpenGLFunctions();

    setUpdateBehavior(QOpenGLWidget::NoPartialUpdate);
    //setUpdateBehavior(QOpenGLWidget::PartialUpdate);

    GLuint nFrameBufferObject = defaultFramebufferObject();

    int i=0;

    for(i=0 ; i<18 ; i++)
    {
        m_fNormalRect[i] = -1;
    }

    GLfloat afTexCoord[] = {
        0.0f,1.0f, 1.0f,1.0f, 0.0f,0.0f,
                    1.0f,0.0f, 0.0f,0.0f, 1.0f,1.0f
    };

    memcpy(m_fNormalRectTexture, afTexCoord, sizeof(GLfloat)*2*6);

    m_nTmpDisplayType = 0;

    QSize nSize = size();

    int nWidth = nSize.width();
    int nHeight = nSize.height();

    nWidth = m_nTextureWidth;
    nHeight = m_nTextureHeight;



    m_pVideoShader = new QVideoShader;
    m_pVideoShader->CreateTexture(nWidth, nHeight, QOpenGLTexture::Target2D, QOpenGLTexture::RGBA8_UNorm);

    char *pSrcVertex =
        "#version 300 es\n"
        "in highp vec4 vertex;\n"
        "in highp vec4 texCoord;\n"
        "uniform mediump mat4 matrix;\n"
        "out highp vec4 texc;\n"
        "void main(void)\n"
        "{\n"
        "    gl_Position = matrix * vertex;\n"
        "    texc = texCoord;\n"
        "}\n";


    char *pSrcFragmentCaptureEnvent =
            "#version 300 es\n"
            "in highp vec4 texc;\n"
            "uniform sampler2D tex;\n"
            "out lowp vec4 FragColor;\n"
            "uniform lowp int nImageWidth;\n"
            "uniform lowp int nImageHeight;\n"
            "void main(void)\n"
            "{\n"
            "   int nX=0;\n"
            "   int nY=0;\n"
            "   int nIndex=0;\n"
            "   highp float fX = 0.0;\n"
            "   highp float fY = 0.0;\n"
            "   int nInternalIndex = 0;\n"
            "   highp float fImageWidth = float(nImageWidth-1);\n"
            "   highp float fImageHeight = float(nImageHeight-1);\n"
            "   fX = floor(texc.s*(fImageWidth));\n"
            "   fY = floor(texc.t*(fImageHeight));\n"
            "   nX = int(fX);\n"
            "   nY = int(fY);\n"            
            //"   highp vec4 colorY = texture2D(tex, vec2(float(nX)/fImageWidth + 0.5/fImageWidth, float(nY)/fImageHeight) + 0.5/fImageHeight);\n"
            "   highp vec4 colorY = texture2D(tex, vec2(texc.s, texc.t));\n"
            "   FragColor = colorY;\n"

            "}\n";


    m_pVideoShader->CreateShader((const char*)pSrcVertex, (const char*)pSrcFragmentCaptureEnvent);
    m_pVideoShader->CreateGeometry();



    if (!m_VBOVideo.isCreated()) {
        //float fRatioWidth = 1572.0f/1920.0f;
        float fRatioWidth = 1.0;
        GLfloat afVertices[] = {
            -1, -fRatioWidth, -1,
            1,-fRatioWidth,-1,

            -1,fRatioWidth,-1,
            1, fRatioWidth,-1,

            -1,fRatioWidth,-1,
            1,-fRatioWidth,-1
        };

        GLfloat afTexCoord[] = {
            0.0f,1.0f, 1.0f,1.0f, 0.0f,0.0f,
                        1.0f,0.0f, 0.0f,0.0f, 1.0f,1.0f
        };

        GLfloat afNormals[] = {

            0,0,1, 0,0,1, 0,0,1,
            0,0,1, 0,0,1, 0,0,1
        };

        m_VBOVideo.create();
        m_VBOVideo.bind();

        m_VBOVideo.allocate(6 * 5 * sizeof(GLfloat));
        m_VBOVideo.write(0, afVertices, 6*3*sizeof(GLfloat));
        m_VBOVideo.write(6*3*sizeof(GLfloat), afTexCoord, 6*2*sizeof(GLfloat));
        //m_VBOVideo.write(6*5*sizeof(GLfloat), afNormals, 6*3*sizeof(GLfloat));

        m_VBOVideo.release();
    }

    GLfloat afVertices[] = {
        -1,1, -1,      -0.767,1,-1,       -1,-1,-1,
         -0.767, -1,-1,  -1,-1,-1,        -0.767,1,-1
    };


    QImage image("test1.jpg");
    int nBytes = image.byteCount();

    if(nBytes>0)
    {
        image = image.convertToFormat(QImage::Format_RGB888);
        nBytes = image.byteCount();
        //QImage image("desk_blue.png");


        uint8_t* pImageData = (uint8_t*)image.bits();

        memcpy(m_QueueData[0], pImageData, nBytes);

        m_bChange = true;
        m_nIndexDisplay = -1;
        UpdateTexture();
        m_bChange = true;
    }
}
