#include "qopenglbufferbase.h"
using namespace SOPENGL;



QOpenGLBufferBase::QOpenGLBufferBase(QOpenGLContext *pContext, QSurfaceFormat surfaceformat, QObject *parent):
    QObject(parent), m_SurfaceFormatParent(surfaceformat), m_pContext(pContext)
{
    m_pShaderThumbnail = new QGLSLShader;

    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);

}

QOpenGLBufferBase::~QOpenGLBufferBase()
{

}

void QOpenGLBufferBase::initializeGL()
{
    m_pFBO = new QOpenGLFramebufferObject(m_rectDisplay.width(), m_rectDisplay.height(), QOpenGLFramebufferObject::CombinedDepthStencil, GL_TEXTURE_2D, (int)QOpenGLTexture::RGBA8_UNorm);

    m_pOffScreen = new QOffscreenSurface();
    m_pOffScreen->setFormat(m_SurfaceFormatParent);
    m_pOffScreen->create();

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

    char *pSrcFragment =
            "#version 300 es\n"
            "in highp vec4 texc;\n"
            "uniform sampler2D tex;\n"
            "uniform lowp int nImageWidth;\n"
            "uniform lowp int nImageHeight;\n"
            "uniform lowp int nDisplayType;\n"
            "out lowp vec4 FragColor;\n"
            "void main(void)\n"
            "{\n"
            "   lowp vec4 color = texture2D(tex, texc.st);\n"
            "   FragColor = vec4(color);\n"
            "}\n";

    m_pShaderThumbnail->CreateShader((const char*)pSrcVertex, (const char*)pSrcFragment);



    if (!m_VBOThumbnail.isCreated()) {
        MakeRectNormal(m_rectDisplay, QSize(1920, 1080));
        m_VBOThumbnail.create();
        m_VBOThumbnail.bind();

        m_VBOThumbnail.allocate(6 * 5 * sizeof(GLfloat));
        m_VBOThumbnail.write(0, m_fNormalRect, 6*3*sizeof(GLfloat));
        m_VBOThumbnail.write(6*3*sizeof(GLfloat), m_fNormalRectTexture, 6*2*sizeof(GLfloat));
        //m_VBOVideo.write(6*5*sizeof(GLfloat), afNormals, 6*3*sizeof(GLfloat));

        m_VBOThumbnail.release();
    }

}


float QOpenGLBufferBase::MakeNormalize(int nPos, int nRange)
{
    float fData = 0.0f;

    //fData = (float)(nPos-nRange/2.0)/(float)(nRange/2.0);

    fData = (float)nPos/(float)(nRange/2) - 1.0f;
    return fData;
}

void QOpenGLBufferBase::MakeRectNormal(const QRect &rect, QSize rectSize)
{
    float fLeft = MakeNormalize(rect.left(), rectSize.width());
    float fTop = MakeNormalize(rect.top(), rectSize.height()) * -1.0f;
    float fRight = MakeNormalize(rect.right(), rectSize.width());
    float fBottom = MakeNormalize(rect.bottom(), rectSize.height()) * -1.0f;
    //lefttop
    m_fNormalRect[0] = fLeft;
    m_fNormalRect[1] = fBottom;

    //righttop
    m_fNormalRect[3] = fRight;
    m_fNormalRect[4] = fBottom;

    //leftbottom
    m_fNormalRect[6] = fLeft;
    m_fNormalRect[7] = fTop;

    //rightbottom
    m_fNormalRect[9] = fRight;
    m_fNormalRect[10] = fTop;

    //leftbottom
    m_fNormalRect[12] = fLeft;
    m_fNormalRect[13] = fTop;

    //righttop
    m_fNormalRect[15] = fRight;
    m_fNormalRect[16] = fBottom;
}

void QOpenGLBufferBase::MakeRectNormalFlip(const QRect &rect, QSize rectSize)
{
    float fLeft = MakeNormalize(rect.left(), rectSize.width());
    float fTop = MakeNormalize(rect.top(), rectSize.height()) * 1.0f;
    float fRight = MakeNormalize(rect.right(), rectSize.width());
    float fBottom = MakeNormalize(rect.bottom(), rectSize.height()) * 1.0f;
    //lefttop
    m_fNormalRect[0] = fLeft;
    m_fNormalRect[1] = fBottom;

    //righttop
    m_fNormalRect[3] = fRight;
    m_fNormalRect[4] = fBottom;

    //leftbottom
    m_fNormalRect[6] = fLeft;
    m_fNormalRect[7] = fTop;

    //rightbottom
    m_fNormalRect[9] = fRight;
    m_fNormalRect[10] = fTop;

    //leftbottom
    m_fNormalRect[12] = fLeft;
    m_fNormalRect[13] = fTop;

    //righttop
    m_fNormalRect[15] = fRight;
    m_fNormalRect[16] = fBottom;
}

void QOpenGLBufferBase::MakeRectNormalWithOffset(const QRect &rect, QSize rectSize, QSize rectOffset)
{
    QRect rectNormal;
    int nX = 0;
    int nY = 0;
    int nWidth = 0;
    int nHeight = 0;

    nX = rect.left() - rectOffset.width();
    nY = rect.top() - rectOffset.height();
    nWidth = rect.width();
    nHeight = rect.height();

    rectNormal = QRect(nX, nY, nWidth, nHeight);

    MakeRectNormalFlip(rectNormal, rectSize);
}
