#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
#include "globals.h"
#include "text.h"
#include "keys.h"
#include "globals.h"
#include "system.h"
#include "model.h"
#include "tunnel.h"
#include "esher.h"

void init_Blob ()
{
	GLfloat fogCol[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
	glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
	glFogfv (GL_FOG_COLOR, fogCol);
	glEnable (GL_DEPTH_TEST);
	glEnable (GL_TEXTURE_2D);
	glDisable (GL_TEXTURE_GEN_S);
	glDisable (GL_TEXTURE_GEN_T);
	glDisable (GL_BLEND);
	glEnable (GL_LIGHTING);
	glDisable (GL_FOG);
	sphere->textured = true;
	sphere2->textured = true;
	cam.directional = false;
	cam.pos[0] = 0;
	cam.pos[1] = 0;
	cam.pos[2] = 10;
	cam.tgt[0] = 0;
	cam.tgt[1] = 0;
	cam.tgt[2] = 0;
	fov = 100;
}

void part_Blobpart ()
{
	init_Blob ();

	int i;
	static bool first = true;
	static float syncscale = 1.0f;

	static float starttime = time;
	static float lasttime = 0, previoustime = time;
	static float fa = 1.0;

	float lapsedtime = time - starttime;
	float delta = lapsedtime - lasttime;
/*
	if (first)
	{
		init_Blob ();
		first = false;
	} */
	

	if (order >= 8)
	{
	if (previoustime != time)
	for (int j=0;j<2;j++)
	for (i = 0; i < 64; i+=16)
	if ( row == 0+i+j || row == 2+i+j || row == 6+i+j || row == 7+i+j || 
		 row == 9+i+j || row == 10+i+j || row == 11+i+j )
		{
		syncscale = 0.90;
		previoustime = time;
		break;
		}

	if (syncscale<1.0) syncscale += 0.005f;
 
	for (i = 4; i < 64; i+=8)
		if (row == i)
		{
		part_Esherpart();
		return;
		}
	}


	cam.pos[0] = (GLfloat)(cos(time)*220)*syncscale;
	cam.pos[1] = (GLfloat)(cos(time*0.6+1)*100)*syncscale;
	cam.pos[2] = (GLfloat)(sin(time)*220)*syncscale;

	SetProjection ();

	for (i = 0; i < sphere->nVertices; i++)
	{
		double distvalmul = (	(1 - cos (atan2(sphere->vertex[i].x, sphere->vertex[i].y) * 5 + time*4.5)) +
								(1 - cos (atan2(sphere->vertex[i].x, sphere->vertex[i].z) * 6 + time*7.2)) +
								(1 - cos (atan2(sphere->vertex[i].y, sphere->vertex[i].z) * 8 + time*8.1)) );

		sphere2->vertex[i].x = (GLfloat)(sphere->vertex[i].x + sphere->vertex[i].x * (distvalmul * 0.10));
		sphere2->vertex[i].y = (GLfloat)(sphere->vertex[i].y + sphere->vertex[i].y * (distvalmul * 0.10));
		sphere2->vertex[i].z = (GLfloat)(sphere->vertex[i].z + sphere->vertex[i].z * (distvalmul * 0.10));
	}

	glFrontFace (GL_CW);
	glDisable (GL_LIGHTING);
	sphere->sX = sphere->sY = sphere->sZ = 3.5f;
	sphere->pZ = 0;
	sphere->render ();

	glFrontFace (GL_CCW);
	glEnable (GL_LIGHTING);
	sphere2->sX = sphere2->sY = sphere2->sZ = 1.0f;//syncscale;
	sphere2->rY = 180;
	sphere2->pZ = 0;

	glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
	sphere2->render ();
	glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
	glDisable (GL_TEXTURE_2D);
	glScalef ((1.05f), (1.05f), (1.05f));
	sphere2->render ();
	glEnable (GL_TEXTURE_2D);
	glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
 /*
	glDisable(GL_DEPTH_TEST);
	nelogomodel->render();
	glEnable(GL_DEPTH_TEST);
*/
	if (lapsedtime < 2)
	{
		ledsensmiley (1.0,1.0,1.0,fa);
		fa -= delta * 0.5;
	}

}
