#include "include.h"

const GLfloat cromoAmb[4] = {0.25f, 0.25f, 0.25f, 1.0f};
const GLfloat cromoDif[4] = {0.4f, 0.4f, 0.4f, 1.0f};
const GLfloat cromoSpe[4] = {0.774597f, 0.774597f, 0.774597f, 1.0f};
GLfloat cromoShi = 76.8f;

Model * Asteroide::modelo = NULL;

Asteroide::Asteroide() {
	this->radio = 1;
	this->velAng = (rand() % 20 + 4) / 100.0;
	this->eje = Vector3D(rand() % 40 - 20, rand() % 40 - 20, rand() % 40 - 20);
	this->eje.unitize();
	this->centro = Vector3D(rand() % 40 - 20, rand() % 40 - 20, rand() % 40 - 20);
	this->pos = Vector3D(this->centro.x + 10 + rand() % 20, this->centro.y + 10 + rand() % 20, this->centro.z + 10 + rand() % 20);
	if (!Asteroide::modelo) {
		Asteroide::modelo = Model::readOBJ("modelos/meteoro.obj");
		Asteroide::modelo->unitize();
		Asteroide::modelo->setMode(GLM_MATERIAL | GLM_SMOOTH);
	}
}

Asteroide::Asteroide(Vector3D pos, double radio) {
	this->radio = radio;
	this->velAng = (rand() % 20 + 4) / 100.0;
	this->eje = Vector3D(rand() % 40 - 20, rand() % 40 - 20, rand() % 40 - 20);
	this->eje.unitize();
	this->pos = pos;
	this->centro = Vector3D(this->pos.x + 10 + rand() % 20, this->pos.y + 10 + rand() % 20, this->pos.z + 10 + rand() % 20);
	if (!Asteroide::modelo) {
		Asteroide::modelo = Model::readOBJ("modelos/meteorus.obj");
		Asteroide::modelo->unitize();
		Asteroide::modelo->setMode(GLM_MATERIAL | GLM_SMOOTH);
	}
}

Asteroide::~Asteroide() {
}

void Asteroide::update() {
	Vector3D radio = this->pos - this->centro;
	radio.rotate(this->eje, val(this->velAng));
	this->pos = radio + this->centro;
}

void Asteroide::draw() {
	glTranslated(this->pos.x, this->pos.y, this->pos.z);
	glScaled(this->radio*2, this->radio*2, this->radio*2);

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
	if (Config::luzEnabled) {
		glColor3d(1.0, 1.0, 1.0);
		glEnable(GL_COLOR_MATERIAL);
		glColorMaterial(GL_BACK, GL_AMBIENT_AND_DIFFUSE);
		glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, cromoAmb);
		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, cromoDif);
		glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, cromoSpe);
		glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, cromoShi);
	} else {
		glColor3f(0.7, 0.3, 0.3);
	}

	Asteroide::modelo->draw();

	glDisable(GL_CULL_FACE);
	glDisable(GL_DEPTH_TEST);	
	glDisable(GL_COLOR_MATERIAL);
}
