#include "Asteroid.h"
#include "Material.h"
#include "Stone.h"
#include "BoundingSphere.h"
#include "TextureObject.h"

//-------------------------------------------------------------------------
Asteroid::Asteroid(float radius, Vec3f position, Material* material, TextureObject* texture, bool visible)
{
	this->radius = radius;
	this->position = position;
	this->geometrie = new Stone(1, 0.3);
	this->mat = material;
	this->visible = visible;
	this->tex = texture;

	init();
}
//-------------------------------------------------------------------------
Asteroid::Asteroid(float radius, Vec3f position, Stone* geometrie, Material* material, TextureObject* texture, bool visible)
{
	this->radius = radius;
	this->position = position;
	this->geometrie = geometrie;
	this->mat = material;
	this->visible = visible;
	this->tex = texture;

	init();
}
//-------------------------------------------------------------------------
void Asteroid::init()
{
	this->rotationAxis = Vec3f(rand(),rand(),rand());
	int halfRand = (float) RAND_MAX * 0.5f;
	this->rotationAxis.sub(Vec3f(halfRand, halfRand, halfRand));
	this->rotationAxis.normalize();

	this->rotationAngle = (rand() % 360);
	this->rotationSpeed = ((float) rand() / (float)(RAND_MAX) + 1.0) * MAX_ROTATION_SPEED;

	// Boundingsphere berechnen
	bbox = new BoundingSphere(position, radius);
}
//-------------------------------------------------------------------------
Asteroid::~Asteroid()
{

}
//-------------------------------------------------------------------------
void Asteroid::setPosition(Vec3f newPos)
{
	this->position = newPos;
	bbox->setCenter(position);
}
//-------------------------------------------------------------------------
Vec3f Asteroid::getPosition()
{
	return position;
}
//-------------------------------------------------------------------------
Stone* Asteroid::getGeometrie()
{
	return geometrie;
}
//-------------------------------------------------------------------------
float Asteroid::getRadius()
{
	return radius;
}
//-------------------------------------------------------------------------
float Asteroid::getMovingSpeed()
{
	return movingSpeed;
}
//-------------------------------------------------------------------------
void Asteroid::setMovingSpeed(float newSpeed)
{
	this->movingSpeed = newSpeed;
}
//-------------------------------------------------------------------------
Vec3f Asteroid::getMovingDirection()
{
	return movingDirection;
}
//-------------------------------------------------------------------------
void Asteroid::setMovingDirection(Vec3f newDirection)
{
	this->movingDirection = newDirection;
	movingDirection.normalize();
}
//-------------------------------------------------------------------------
BoundingSphere* Asteroid::getBoundingVolume()
{
	return bbox;
}
//-------------------------------------------------------------------------
void Asteroid::update()
{
	rotationAngle += rotationSpeed;
	position.x += movingDirection.x * movingSpeed;
	position.y += movingDirection.y * movingSpeed;
	position.z += movingDirection.z * movingSpeed;
}
//-------------------------------------------------------------------------
void Asteroid::render()
{
	if(!visible)
		return;

	if(tex)
		tex->activate();
	if(mat)
		mat->activate();

	glPushMatrix();
	
		glTranslatef(position.x, position.y, position.z);
		glRotatef(rotationAngle, rotationAxis.x, rotationAxis.y, rotationAxis.z);
		glScalef(radius, radius, radius);
		geometrie->render();
		
	glPopMatrix();

	if(tex)
		tex->deactivate();
		
	//#ifdef _DEBUG
	//	bbox->render();
	//#endif
}
