//////////////////////////////////////////////////////////////////////////
//Tom Valesky
//CS-752
//Dr. Chen, instructor
//sceneobject.cpp -- source code for the SceneObject class
//////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#include "sceneobject.h"
#include "glhdr.h"

void SceneObject::add_velocity(float exp_x, float exp_y, float exp_z, float dist, float cur_velocity)
{
	float unit_x, unit_y, unit_z;
	float temp_dx, temp_dy, temp_dz;

	printf("in add_velocity\n");
	printf("args:exp_x = %f; exp_y = %f; exp_z = %f; dist = %f; cur_velocity = %f\n",
		exp_x, exp_y, exp_z, dist, cur_velocity);
	unit_x = (this->x - exp_x )/dist;
	unit_y = (this->y - exp_y)/dist;	//find unit vector 
	unit_z = (this->z - exp_z)/dist;

	printf("unit_x = %f; unit_y = %f; unit_z = %f\n", unit_x, unit_y, unit_z);

	temp_dx = unit_x * cur_velocity;
	temp_dy = unit_y * cur_velocity;	//find delta-x in that direction
	temp_dz = unit_z * cur_velocity;

	printf("temp_dx = %f; temp_dy = %f; temp_dz = %f\n", temp_dx, temp_dy, temp_dz);

	this->dx += temp_dx;
	this->dy += temp_dy;
	this->dz += temp_dz;

	//this->dx = this->dy = this->dz = 1; //debugging -- see if movement is OK

	printf("dx = %f; dy = %f; dz = %f\n", dx, dy, dz);
}

SceneObject::SceneObject(void)
{
        printf("called SceneObject default constructor--THIS SHOULD NEVER HAPPEN\n");
		exit(0);

}
SceneObject::~SceneObject(void)
{
        printf("called SceneObject destructor\n");
}


void SceneObject::move(void)
{
   //	printf("called SceneObject.move()\n");
	x += dx;
	y += dy;
	z += dz;
	printf ("in sceneobject::move();x=%f;y=%f;z=%f;dx=%f;dy=%f;dz=%f\n", x, y, z, dx, dy, dz);
}
void SceneObject::draw(void)
{
   //	printf("called SceneObject.draw()\n");
	glColor3f(r, g, b);
	glLoadIdentity();
	glTranslatef(x, y, z);
	switch (this->object_type)
	{
		case 1:
			auxSolidSphere(radius);	
			break;
		case 2:
			auxSolidCube (radius);
			break;
		case 3:
			auxSolidTorus (radius - 1, radius);
			break;
		case 4:
			auxSolidIcosahedron (radius);
			break;
		case 5:
			auxSolidOctahedron (radius);
			break;
		case 6:
			auxSolidTetrahedron (radius);
			break;
		case 7:
			auxSolidDodecahedron (radius);
			break;
		case 8:
			auxSolidTeapot (radius);
			break;
		default:
			auxSolidSphere(radius);
			break;

	}
	printf("drawing object at x=%f, y=%f, z=%f\n", x, y, z);


}

float SceneObject::get_radius(void)
{
	return radius;
}

void SceneObject::dump(void)
{
        printf("called SceneObject::dump\n");
        printf("x = %d\n", x);
        printf("y = %d\n", y);
        printf("z = %d\n", z);
}

SceneObject::SceneObject(int x, int y, int z, float r, float g, float b, float radius, int object_type)
{
	printf("called SceneObject constructor\n");
	printf("x=%d, y=%d, z=%d, r = %f ,g = %f, b = %f, radius = %f\n", x, y, z, r, g, b, radius);

	this->x = x;
	this->y = y;
	this->z = z;
	this->r = r;
	this->g = g;
	this->b = b;
	this->dx = 0;
	this->dy = 0;
	this->dz = 0;
	this->radius = radius;
	this->object_type = object_type;
}
float SceneObject::get_distance_from(float x, float y, float z)
{
	float xval = 0, yval = 0, zval = 0, retval = 0;

	xval = this->x - x;
	yval = this->y - y;			//find initial components
	zval = this->z - z;

	xval *= xval;
	yval *= yval;				//square them
	zval *= zval;

	retval = sqrt (xval + yval + zval); //take square root

	return retval;				//return

}

