
#include "modelloader.h"


ModelLoader::ModelLoader()


{
    connect(this, SIGNAL(windowChanged(QQuickWindow*)), this, SLOT(handleWindowChanged(QQuickWindow*)));
}

ModelLoader::~ModelLoader()
{

}


void ModelLoader::initializeGL()
{

    glClearColor(1.0f, 0.f, 0.f, 1.0f);
    QOpenGLShader *vshader1 = new QOpenGLShader(QOpenGLShader::Vertex, &program);
    const char *vsrc1 =

      " #ifdef GL_ES\n"

      " precision mediump int;\n"
      "  precision mediump float;\n"
        "#endif\n"

     "   uniform mat4 mvp_matrix;\n"

      "  attribute vec4 a_position;\n"
     "   attribute vec2 a_texcoord;\n"
      "  attribute vec4 a_normal;\n"
      "  varying vec2 v_texcoord;\n"
      "  varying vec4 v_normal;\n"
      "  void main()\n"
      "  {\n"

      "      gl_Position = mvp_matrix * a_position;\n"

       "     v_texcoord = a_texcoord;\n"
      "  } \n" ;

    vshader1->compileSourceCode(vsrc1);

    QOpenGLShader *fshader1 = new QOpenGLShader(QOpenGLShader::Fragment, &program);
    const char *fsrc1 =


        "#ifdef GL_ES   \n"
       " precision mediump int;\n"
        "precision mediump float;\n"
        "#endif     \n"
        "uniform sampler2D texture;  \n"
        "varying vec2 v_texcoord;\n"
        "void main()\n"
        "{   \n"
        " gl_FragColor = texture2D(texture, v_texcoord);\n"
       " }\n";

    fshader1->compileSourceCode(fsrc1);

    program.addShader(vshader1);
    program.addShader(fshader1);
    program.link();


    vertexLocation = program.attributeLocation("a_position");
    normalLocation = program.attributeLocation("a_normal");
    texcoordLocation = program.attributeLocation("a_texcoord");

    matrixUniform1 = program.uniformLocation("mvp_matrix");


    m_tex=new QOpenGLTexture(QImage(":/waffle.jpg").mirrored());
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);



    QString str=":/model.3ds";
    model.Init(str);

}

void ModelLoader::paintGL()
{

    glDepthMask(true);
    glClearColor(1.0f, 0.f, 0.f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    QMatrix4x4 modelview;
    modelview.translate(0.0f, -0.2f, 0.0f);

    program.bind();
    program.setUniformValue(matrixUniform1, modelview);

    m_tex->bind();
    model.draw(&program, vertexLocation, normalLocation,texcoordLocation);

    program.release();


}


void ModelLoader::handleWindowChanged(QQuickWindow *win)
{
    if (win) {

        // Connect the beforeRendering signal to our paint function.
        // Since this call is executed on the rendering thread it must be
        // a Qt::DirectConnection
       connect(win,SIGNAL(sceneGraphInitialized()),this,SLOT(initializeGL()),Qt::DirectConnection);
        connect(win, SIGNAL(beforeRendering()), this, SLOT(paintGL()), Qt::DirectConnection);


        // If we allow QML to do the clearing, they would clear what we paint
        // and nothing would show.

        win->setClearBeforeRendering(false);
    }
}

