#include "qglslshader.h"
using namespace SOPENGL;

#include "common.h"

QGLSLShader::QGLSLShader()
{
    m_pVShader = NULL;
    m_pFShader = NULL;
    m_pProgram = NULL;
    m_pVideoTexture = NULL;

    m_fAngle = 0.0f;
    m_fScale = 1.0f;
}

QGLSLShader::~QGLSLShader()
{
    SAFE_DELETE(m_pVShader);
    SAFE_DELETE(m_pFShader);
    SAFE_DELETE(m_pProgram);

}

qreal QGLSLShader::GetAngle()
{
    return m_fAngle;
}

qreal QGLSLShader::GetScale()
{
    return m_fScale;
}

QVector<QVector3D> QGLSLShader::GetVertices()
{
    return m_Vertices;
}

QVector<QVector3D> QGLSLShader::GetNormals()
{
    return m_Normals;
}

void QGLSLShader::CreateShader(const char *pSrcVShader, const char *pSrcFShader)
{
    m_pVShader = new QOpenGLShader(QOpenGLShader::Vertex);
    m_pVShader->compileSourceCode(pSrcVShader);
    qDebug() << m_pVShader->log();

    m_pFShader = new QOpenGLShader(QOpenGLShader::Fragment);
    m_pFShader->compileSourceCode(pSrcFShader);
    qDebug() << m_pFShader->log();



    m_pProgram = new QOpenGLShaderProgram;
    m_pProgram->addShader(m_pVShader);
    m_pProgram->addShader(m_pFShader);
    m_pProgram->link();

    m_AttrVertex = m_pProgram->attributeLocation("vertex");
    m_AttrNormal = m_pProgram->attributeLocation("normal");
    m_AttrTexCoord = m_pProgram->attributeLocation("texCoord");
    m_UniformMatrix = m_pProgram->uniformLocation("matrix");
    m_UniformVideoTexture = m_pProgram->uniformLocation("tex");

    //m_ImageWidth = m_pProgram->uniformLocation("nImageWidth");
    //m_ImageHeight = m_pProgram->uniformLocation("nImageHeight");

    //m_nDisplayType = m_pProgram->uniformLocation("nDisplayType");
}

void QGLSLShader::CreateTexture(int nWidth, int nHeight, QOpenGLTexture::Target target, QOpenGLTexture::TextureFormat format)
{
    m_nTextureWidth = nWidth;
    m_nTextureHeight = nHeight;
    m_pVideoTexture = new QOpenGLTexture(target);
    m_pVideoTexture->create();
    m_pVideoTexture->setFormat(format);
    m_pVideoTexture->setSize(nWidth, nHeight);
    m_pVideoTexture->allocateStorage();

    //m_pVideoTexture->setMinMagFilters(QOpenGLTexture::LinearMipMapLinear, QOpenGLTexture::LinearMipMapLinear);



}

void QGLSLShader::Bind()
{    
    //m_pVideoTexture->setMinMagFilters(QOpenGLTexture::LinearMipMapLinear, QOpenGLTexture::LinearMipMapLinear);
    m_pProgram->bind();
}

void QGLSLShader::Release()
{
    m_pProgram->release();
}

void QGLSLShader::SetUniformValue()
{
    QMatrix4x4 modelview;

    //modelview.rotate(m_fAngle, 0.0f, 1.0f, 0.0f);
    modelview.rotate(m_fAngle, 1.0f, 0.0f, 0.0f);
    //modelview.rotate(m_fAngle, 0.0f, 0.0f, 1.0f);
    modelview.scale(m_fScale);
    modelview.translate(0.0f, 0.0f, 0.0f);

    m_pProgram->setUniformValue(m_UniformMatrix, modelview);
}

void QGLSLShader::EnableAttributeArray()
{
    //int nTextureID = m_pVideoTexture->textureId();
    //m_pProgram->setUniformValue(m_UniformVideoTexture, m_pVideoTexture->textureId()); // use texture unit 0
    m_pProgram->setUniformValue(m_UniformVideoTexture, 0); // use texture unit 0

    m_pProgram->enableAttributeArray(m_AttrVertex);
    m_pProgram->enableAttributeArray(m_AttrNormal);
    m_pProgram->enableAttributeArray(m_AttrTexCoord);
}

void QGLSLShader::SetAttributeBuffer()
{
    m_pProgram->setAttributeBuffer(m_AttrVertex, GL_FLOAT, 0, 3);
    m_pProgram->setAttributeBuffer(m_AttrTexCoord, GL_FLOAT, 6 * 3 * sizeof(GLfloat), 2);
    //m_pProgram->setAttributeBuffer(m_AttrNormal, GL_FLOAT, 6 * 5 * sizeof(GLfloat), 3);
}

void QGLSLShader::DisableAttributeArray()
{
    m_pProgram->disableAttributeArray(m_AttrVertex);
    m_pProgram->disableAttributeArray(m_AttrNormal);
    m_pProgram->disableAttributeArray(m_AttrTexCoord);
}

void QGLSLShader::BindTexture()
{
    m_pVideoTexture->bind();
}

void QGLSLShader::ReleaseTexture()
{
    m_pVideoTexture->release();
}

void QGLSLShader::Translate(float fX, float fY, float fZ)
{
    QMatrix4x4 modelview;

    //modelview.rotate(m_fAngle, 0.0f, 1.0f, 0.0f);
    modelview.rotate(m_fAngle, 1.0f, 0.0f, 0.0f);
    //modelview.rotate(m_fAngle, 0.0f, 0.0f, 1.0f);
    modelview.scale(m_fScale);
    modelview.translate(fX, fY, fZ);

    m_pProgram->setUniformValue(m_UniformMatrix, modelview);
}

QOpenGLTexture *QGLSLShader::GetTexture()
{
    return m_pVideoTexture;
}

void QGLSLShader::CreateGeometry()
{
    m_Vertices.clear();
    m_Normals.clear();
/*
    qreal x1 = +0.06f;
    qreal y1 = -0.14f;
    qreal x2 = +0.14f;
    qreal y2 = -0.06f;
    qreal x3 = +0.08f;
    qreal y3 = +0.00f;
    qreal x4 = +0.30f;
    qreal y4 = +0.22f;

    quad(x1, y1, x2, y2, y2, x2, y1, x1);
    quad(x3, y3, x4, y4, y4, x4, y3, x3);

    extrude(x1, y1, x2, y2);
    extrude(x2, y2, y2, x2);
    extrude(y2, x2, y1, x1);
    extrude(y1, x1, x1, y1);
    extrude(x3, y3, x4, y4);
    extrude(x4, y4, y4, x4);
    extrude(y4, x4, y3, x3);

    const qreal Pi = 3.14159f;
    const int NumSectors = 100;

    for (int i = 0; i < NumSectors; ++i) {
        qreal angle1 = (i * 2 * Pi) / NumSectors;
        qreal x5 = 0.30 * sin(angle1);
        qreal y5 = 0.30 * cos(angle1);
        qreal x6 = 0.20 * sin(angle1);
        qreal y6 = 0.20 * cos(angle1);

        qreal angle2 = ((i + 1) * 2 * Pi) / NumSectors;
        qreal x7 = 0.20 * sin(angle2);
        qreal y7 = 0.20 * cos(angle2);
        qreal x8 = 0.30 * sin(angle2);
        qreal y8 = 0.30 * cos(angle2);

        quad(x5, y5, x6, y6, x7, y7, x8, y8);

        extrude(x6, y6, x7, y7);
        extrude(x8, y8, x5, y5);
    }

    for (int i = 0;i < m_vertices.size();i++)
        m_vertices[i] *= 2.0f;
        */
}

void QGLSLShader::Quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4)
{
    m_Vertices << QVector3D(x1, y1, -0.05f);
    m_Vertices << QVector3D(x2, y2, -0.05f);
    m_Vertices << QVector3D(x4, y4, -0.05f);

    m_Vertices << QVector3D(x3, y3, -0.05f);
    m_Vertices << QVector3D(x4, y4, -0.05f);
    m_Vertices << QVector3D(x2, y2, -0.05f);

    QVector3D n = QVector3D::normal
        (QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(x4 - x1, y4 - y1, 0.0f));

    m_Normals << n;
    m_Normals << n;
    m_Normals << n;

    m_Normals << n;
    m_Normals << n;
    m_Normals << n;

    m_Vertices << QVector3D(x4, y4, 0.05f);
    m_Vertices << QVector3D(x2, y2, 0.05f);
    m_Vertices << QVector3D(x1, y1, 0.05f);

    m_Vertices << QVector3D(x2, y2, 0.05f);
    m_Vertices << QVector3D(x4, y4, 0.05f);
    m_Vertices << QVector3D(x3, y3, 0.05f);

    n = QVector3D::normal
        (QVector3D(x2 - x4, y2 - y4, 0.0f), QVector3D(x1 - x4, y1 - y4, 0.0f));

    m_Normals << n;
    m_Normals << n;
    m_Normals << n;

    m_Normals << n;
    m_Normals << n;
    m_Normals << n;
}

