#include "TexturModel.h"
#include "DXBuffer.h"
#include "Textur.h"
#include "Globals.h"
#include "TexturList.h"
#include "GraphicsApi.h"
#include "Model3DList.h"

using namespace Framework;

// Inhalt der TexturModel Klasse

// Konstruktor
TexturModel::TexturModel( GraphicsApi* zApi )
    : Model3D()
{
    if( zApi->hasModel( Standart3DTypes::texturModel ) )
        model = zApi->getModel( Standart3DTypes::texturModel );
    else
    {
        model = zApi->createModel( Standart3DTypes::texturModel );
        Vertex3D* vertecies = new Vertex3D[ 4 ];
        for( int i = 0; i < 4; i++ )
            vertecies[ i ].knochenId = 0;
        vertecies[ 0 ].pos = Vec3<float >( -50, 50, 0.f );
        vertecies[ 0 ].tPos = Vec2< float >( 0.f, 0.f );
        vertecies[ 1 ].pos = Vec3<float >( 50, 50, 0.f );
        vertecies[ 1 ].tPos = Vec2< float >( 1.f, 0.f );
        vertecies[ 2 ].pos = Vec3<float >( -50, -50, 0.f );
        vertecies[ 2 ].tPos = Vec2< float >( 0.f, 1.f );
        vertecies[ 3 ].pos = Vec3<float >( 50, -50, 0.f );
        vertecies[ 3 ].tPos = Vec2< float >( 1.f, 1.f );

        model->setVertecies( vertecies, 4 );
        Polygon3D* p = new Polygon3D();
        p->indexAnz = 6;
        p->indexList = new int[ p->indexAnz ];
        p->indexList[ 0 ] = 0;
        p->indexList[ 1 ] = 3;
        p->indexList[ 2 ] = 2;
        p->indexList[ 3 ] = 0;
        p->indexList[ 4 ] = 1;
        p->indexList[ 5 ] = 3;
        model->addPolygon( p );

        model->calculateNormals();
    }
    textur = new Model3DTextur();
}

// Setzt die Textur die angezeigt werden soll 
//  t: Die Textur
void TexturModel::setTextur( Textur* t )
{
    if( !t )
        return;
    this->textur->setPolygonTextur( 0, t );
    rend = 1;
}

// Setzt die Gr��e, in der Die Textur angezeigt wird
//  gr: Ein Vektor, der f�r x und y die breite und h�he beinhaltet
void TexturModel::setSize( Vec2< float > gr )
{
    gr /= 2;
    Vertex3D* vertecies = new Vertex3D[ 4 ];
    for( int i = 0; i < 4; i++ )
        vertecies[ i ].knochenId = 0;
    vertecies[ 0 ].pos = Vec3<float >( -gr.x, gr.y, 0.f );
    vertecies[ 0 ].tPos = Vec2< float >( 0.f, 0.f );
    vertecies[ 1 ].pos = Vec3<float >( gr.x, gr.y, 0.f );
    vertecies[ 1 ].tPos = Vec2< float >( 1.f, 0.f );
    vertecies[ 2 ].pos = Vec3<float >( -gr.x, -gr.y, 0.f );
    vertecies[ 2 ].tPos = Vec2< float >( 0.f, 1.f );
    vertecies[ 3 ].pos = Vec3<float >( gr.x, -gr.y, 0.f );
    vertecies[ 3 ].tPos = Vec2< float >( 1.f, 1.f );
    model->setVertecies( vertecies, 4 );
}

// Setzt die Gr��e, in der die Textur angezeigt wird
//  b: Die Breite, in der die Textur angezeigt wird
//  h: Die H�he, in der die Textur angezeigt wird
void TexturModel::setSize( float b, float h )
{
    b /= 2;
    h /= 2;
    Vertex3D* vertecies = new Vertex3D[ 4 ];
    for( int i = 0; i < 4; i++ )
        vertecies[ i ].knochenId = 0;
    vertecies[ 0 ].pos = Vec3<float >( -b, h, 0.f );
    vertecies[ 0 ].tPos = Vec2< float >( 0.f, 0.f );
    vertecies[ 1 ].pos = Vec3<float >( b, h, 0.f );
    vertecies[ 1 ].tPos = Vec2< float >( 1.f, 0.f );
    vertecies[ 2 ].pos = Vec3<float >( -b, -h, 0.f );
    vertecies[ 2 ].tPos = Vec2< float >( 0.f, 1.f );
    vertecies[ 3 ].pos = Vec3<float >( b, -h, 0.f );
    vertecies[ 3 ].tPos = Vec2< float >( 1.f, 1.f );
    model->setVertecies( vertecies, 4 );
}