#include "Billboard.h"
#include "VertexBufferObject.h"
#include "TextureObject.h"
#include "Material.h"
#include <math.h>


Billboard::Billboard(std::string ImagePath)
{
	// Vertex-Coordinates
	Vec3f verticies[] = {
		Vec3f(	-1,	-1,	0	),	// v0
		Vec3f(	1,	-1,	0	),	// v1
		Vec3f(	1,	1,	0	),	// v2
		Vec3f(	-1,	1,	0	)	// v3
	};

	// Normal-Data
	Vec3f normals[] = {
		Vec3f(	0,	0,	1	)	// n0
	};

	// Texture-Coordinates
	Vec3f texCoords[] = {
		Vec3f(	1,	0,	0	),	// t0
		Vec3f(	1,	1,	0	),	// t1
		Vec3f(	0,	1,	0	),	// t2
		Vec3f(	0,	0,	0	)	// t3
	};


	//Creating arrays for the Vertex Buffer
	//-------------------------------------------

	// Vertex-Array-Data
	GLfloat vertexArrayData[] = {
		verticies[0].x,	 verticies[0].y,	 verticies[0].z,	//  v0 - v1 - v2
		verticies[1].x,	 verticies[1].y,	 verticies[1].z,
		verticies[2].x,	 verticies[2].y,	 verticies[2].z,

		verticies[2].x,	 verticies[2].y,	 verticies[2].z,	//  v2 - v3 - v0
		verticies[3].x,	 verticies[3].y,	 verticies[3].z,
		verticies[0].x,	 verticies[0].y,	 verticies[0].z

	};

	// Vertex-Array-Data
	GLfloat normalArrayData[] = {
		normals[0].x,	 normals[0].y,	 normals[0].z,	//  v0 - v1 - v2
		normals[0].x,	 normals[0].y,	 normals[0].z,
		normals[0].x,	 normals[0].y,	 normals[0].z,

		normals[0].x,	 normals[0].y,	 normals[0].z,	//  v2 - v3 - v0
		normals[0].x,	 normals[0].y,	 normals[0].z,
		normals[0].x,	 normals[0].y,	 normals[0].z

	};

	// Vertex-Array-Data
	GLfloat textureCoordArrayData[] = {
		texCoords[0].x,	 texCoords[0].y,		//  v0 - v1 - v2
		texCoords[1].x,	 texCoords[1].y,
		texCoords[2].x,	 texCoords[2].y,	

		texCoords[2].x,	 texCoords[2].y,		//  v2 - v3 - v0
		texCoords[3].x,	 texCoords[3].y,	
		texCoords[0].x,	 texCoords[0].y

	};

	// Color-Data
	GLfloat colorData[sizeof(vertexArrayData)];
	for(int i=0; i < sizeof(vertexArrayData); i++)
	{
		colorData[i] = 1;
	};

	vbo = new VertexBufferObject(	GL_TRIANGLES,
									vertexArrayData, sizeof(vertexArrayData),
									normalArrayData, sizeof(normalArrayData),
									textureCoordArrayData, sizeof(textureCoordArrayData),
									colorData, sizeof(colorData));

	texture = new TextureObject(ImagePath);
	mat = new Material(Vec4f(1,1,1,1), Vec4f(1,1,1,1), Vec4f(1,1,1,1), 1);
	this->ticksLeft = 0;

}
//--------------------------------------------------------------------------
Billboard::~Billboard()
{
	delete texture;
	delete vbo;
}
//--------------------------------------------------------------------------
void Billboard::showForTicks(Vec3f pos, Vec3f normal, Vec3f upDir, float width, float height, int ticks)
{
	this->width = width;
	this->height = height;
	this->ticksLeft = ticks;
	this->allTicks = ticks;
	this->pos = pos;
	this->normal = normal;
	this->up = upDir;
}

//--------------------------------------------------------------------------
void Billboard::update()
{
	if(ticksLeft > 0)
		ticksLeft--;
}
//--------------------------------------------------------------------------
void Billboard::render()
{
	if(ticksLeft == 0)
		return;

	texture->activate();

	float alpha =  sin(( static_cast<float>(ticksLeft) / allTicks) * PI );

	if(ticksLeft != -1)
		mat->activate( 1, alpha );
	else
		mat->activate();

	glPushMatrix();

		
		glTranslatef(pos.x, pos.y, pos.z);

		Vec3f right = up;
		right.crossToSelf( &normal );

		GLfloat transform[16] = {	right.x, up.x, normal.x, 0,
									right.y, up.y, normal.y, 0,
									right.z, up.z, normal.z, 0,
									0, 0, 0, 1 };
		glMultMatrixf(transform);

		glScalef(width, height, 1);
		vbo->drawVBO();

	glPopMatrix();

	texture->deactivate();
}