/* mekka intro               */
/* c a l o d o x   2 0 0 1   */
 
#include "main.h"
#include <math.h>
#include "3d.h"




void PUTAIN_DE_SPHERE(float rotat, float alpha)
{

	Matrix m[4];
	Vect3D nv={0.0, 0.0, 0.0};

	MAT_ident_scale(4.0, 4.0, 4.0, m[0]);
	MAT_obj_mat(rotat, rotat, rotat, nv, m[1]);
	MAT_mul(m[0], m[1], m[2]);
	
		H3D_transform_obj_matrix(&SCN_SphereMiddle, m[2]);
		H3D_render_object(&SCN_SphereMiddle, 4, temptexture, alpha);
}



void SPLINES_Draw_Parametric(float maxt, int nb_pts, int intrasteps, float zoom, float alpha, 
							 Vect2D move, int style_x, int style_y, int blendstyle, float rotat, Vect3D color);

void MANONEGRA_draw(GLfloat zoom, GLfloat displace, GLfloat rotat, GLfloat alpha, int rotatstyle)
	{

		
		glLoadIdentity();  
        if (rotatstyle&1){glRotatef(rotat,0.0f,0.0f,1.0f); }                       // Rotate On The Z Axis
		glTranslatef(displace,0.0f,zoom);
		if (rotatstyle&2){glRotatef(rotat,0.0f,0.0f,1.0f);}  
		if (rotatstyle&16){glRotatef(rotat,1.0f, 1.0f, 1.0f);}

		glColor4f(1.0f,1.0f,1.0f,alpha);		// Full Brightness, 50% Alpha ( NEW )

		glBlendFunc(GL_ZERO,GL_SRC_ALPHA);	// Blending Function For Translucency Based On Source Alpha Value

		glEnable(GL_BLEND);	
		glDisable(GL_DEPTH_TEST);

		glBegin(GL_QUADS);

		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f,  0.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f,  0.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, -1.0f,  0.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, -1.0f,  0.0f);	// Top Left Of The Texture and Quad
 
        glEnd();

		glEnable(GL_DEPTH_TEST);
		glDisable(GL_BLEND);
}

void MANONEGRA_part1(float fxinterp)
{

	

		glLoadIdentity();

		glBindTexture(GL_TEXTURE_2D, textures[0]);


		float invfxinterp=1.0-fxinterp;
	//	float alpha1=fabs(sin(fxinterp*6.28));

		/*
		MANONEGRA_draw(-12.0+sin(fxinterp*6.28)*8.0, -2.0, invfxinterp*360.0, 1.0, 3);
		MANONEGRA_draw(-12.0+cos(fxinterp*6.28)*10.0, 2.5, (fxinterp+0.333)*360.0, 1.0, 3);
		MANONEGRA_draw(-12.0+sin(fxinterp*6.28)*10.0, 3.0, (invfxinterp+0.666)*360.0, 1.0, 3);


		MANONEGRA_draw(-22.0+cos(fxinterp*6.28)*20.0, -1.0, fxinterp*360.0, 1.0, 3);
		MANONEGRA_draw(-20.0+sin(fxinterp*6.28)*18.0, 1.5, (invfxinterp+0.25)*360.0, 1.0, 2);
		MANONEGRA_draw(-18.0+cos(fxinterp*6.28*2.0)*16.0, -2.0, (fxinterp+0.75)*360.0, 1.0, 3);

		MANONEGRA_draw(-10.0+cos((fxinterp+0.3)*6.28)*10.0, 3.0, fxinterp*360.0, 1.0, 1);
		MANONEGRA_draw(-12.0+sin((fxinterp+0.15)*6.28)*10.0, -2.5, (invfxinterp+0.25)*720.0, 1.0, 2);
		MANONEGRA_draw(-12.0+cos((fxinterp+0.8)*6.28*2.0)*10.0, 1.0, (fxinterp+0.75)*720.0, 1.0, 1);
*/

		unsigned char flag=0;

		if (current_hertz>8474)
		{
		flag=16;
		}
		
		MANONEGRA_draw(-12.0+sin(fxinterp*6.28)*8.0, -2.0, invfxinterp*360.0, 1.0, 3+flag);
		MANONEGRA_draw(-12.0+cos(fxinterp*6.28)*10.0, 2.5, (fxinterp+0.333)*360.0, 1.0, 3+flag);
		MANONEGRA_draw(-12.0+sin(fxinterp*6.28)*10.0, 3.0, (invfxinterp+0.666)*360.0, 1.0, 3+flag);


		MANONEGRA_draw(-22.0+cos(fxinterp*6.28)*20.0, -1.0, fxinterp*360.0, 1.0, 3+flag);
		MANONEGRA_draw(-20.0+sin(fxinterp*6.28)*18.0, 1.5, (invfxinterp+0.25)*360.0, 1.0, 2+flag);
		MANONEGRA_draw(-18.0+cos(fxinterp*6.28*2.0)*16.0, -2.0, (fxinterp+0.75)*360.0, 1.0, 3+flag);

		MANONEGRA_draw(-10.0+cos((fxinterp+0.3)*6.28)*10.0, 3.0, fxinterp*360.0, 1.0, 1+flag);
		MANONEGRA_draw(-12.0+sin((fxinterp+0.15)*6.28)*10.0, -2.5, (invfxinterp+0.25)*720.0, 1.0, 2+flag);
		MANONEGRA_draw(-12.0+cos((fxinterp+0.8)*6.28*2.0)*10.0, 1.0, (fxinterp+0.75)*720.0, 1.0, 1+flag);


	
}





#define SYNC3_key1	6879
#define SYNC3_key2  7015
#define SYNC3_key3  7607
#define SYNC3_key4  7738
#define SYNC3_key5  7932
#define SYNC3_key6  8166
#define SYNC3_key7  8345
#define SYNC3_key8  8474
#define SYNC3_key9  8668
#define SYNC3_key10  9076
#define SYNC3_key11  9212
#define SYNC3_key12  9395
#define SYNC3_key13  9623
#define SYNC3_key14  9811
#define SYNC3_key15  9950
#define SYNC3_key16  10130


#define SYNC3_k1   1.0
#define SYNC3_k2   0.5
#define SYNC3_k3   0.1
#define SYNC3_k4   1.0
#define SYNC3_k5   0.6
#define SYNC3_k6   0.3
#define SYNC3_k7   0.1
#define SYNC3_k8   1.0
#define SYNC3_k9   0.5
#define SYNC3_k10  0.1
#define SYNC3_k11  1.0
#define SYNC3_k12  0.7
#define SYNC3_k13  0.4
#define SYNC3_k14  0.1
#define SYNC3_k15  1.0
#define SYNC3_k16  0.1


float SYNC3b_whouwhou(int current_hertz)
{

	float a;

	if (current_hertz<=SYNC3_key1 || current_hertz>SYNC3_key16) return 1.0;



	if (current_hertz>SYNC3_key1 && current_hertz<=SYNC3_key2)
	{
		a=INTERPOLATE_float(SYNC3_key1,SYNC3_key2,SYNC3_k1,SYNC3_k2,current_hertz);
	}

	if (current_hertz>SYNC3_key2 && current_hertz<=SYNC3_key3)
	{
		a=INTERPOLATE_float(SYNC3_key2,SYNC3_key3,SYNC3_k2,SYNC3_k3,current_hertz);
	}

	if (current_hertz>SYNC3_key3 && current_hertz<=SYNC3_key4)
	{
		a=INTERPOLATE_float(SYNC3_key3,SYNC3_key4,SYNC3_k3,SYNC3_k4,current_hertz);
	}

	if (current_hertz>SYNC3_key4 && current_hertz<=SYNC3_key5)
	{
		a=INTERPOLATE_float(SYNC3_key4,SYNC3_key5,SYNC3_k4,SYNC3_k5,current_hertz);
	}

	if (current_hertz>SYNC3_key5 && current_hertz<=SYNC3_key6)
	{
		a=INTERPOLATE_float(SYNC3_key5,SYNC3_key6,SYNC3_k5,SYNC3_k6,current_hertz);
	}

	if (current_hertz>SYNC3_key6 && current_hertz<=SYNC3_key7)
	{
		a=INTERPOLATE_float(SYNC3_key6,SYNC3_key7,SYNC3_k6,SYNC3_k7,current_hertz);
	}

	if (current_hertz>SYNC3_key7 && current_hertz<=SYNC3_key8)
	{
		a=INTERPOLATE_float(SYNC3_key7,SYNC3_key8,SYNC3_k7,SYNC3_k8,current_hertz);
	}

	if (current_hertz>SYNC3_key8 && current_hertz<=SYNC3_key9)
	{
		a=INTERPOLATE_float(SYNC3_key8,SYNC3_key9,SYNC3_k8,SYNC3_k9,current_hertz);
	}

	if (current_hertz>SYNC3_key9 && current_hertz<=SYNC3_key10)
	{
		a=INTERPOLATE_float(SYNC3_key9,SYNC3_key10,SYNC3_k9,SYNC3_k10,current_hertz);
	}

	if (current_hertz>SYNC3_key10 && current_hertz<=SYNC3_key11)
	{
		a=INTERPOLATE_float(SYNC3_key10,SYNC3_key11,SYNC3_k10,SYNC3_k11,current_hertz);
	}

	if (current_hertz>SYNC3_key11 && current_hertz<=SYNC3_key12)
	{
		a=INTERPOLATE_float(SYNC3_key11,SYNC3_key12,SYNC3_k11,SYNC3_k12,current_hertz);
	}

	if (current_hertz>SYNC3_key12 && current_hertz<=SYNC3_key13)
	{
		a=INTERPOLATE_float(SYNC3_key12,SYNC3_key13,SYNC3_k12,SYNC3_k13,current_hertz);
	}

	if (current_hertz>SYNC3_key13 && current_hertz<=SYNC3_key14)
	{
		a=INTERPOLATE_float(SYNC3_key13,SYNC3_key14,SYNC3_k13,SYNC3_k14,current_hertz);
	}

	if (current_hertz>SYNC3_key14 && current_hertz<=SYNC3_key15)
	{
		a=INTERPOLATE_float(SYNC3_key14,SYNC3_key15,SYNC3_k14,SYNC3_k15,current_hertz);
	}

	if (current_hertz>SYNC3_key15 && current_hertz<=SYNC3_key16)
	{
		a=INTERPOLATE_float(SYNC3_key15,SYNC3_key16,SYNC3_k15,SYNC3_k16,current_hertz);
	}

return a;

}


void SYNC3_manonegra(int current_hertz)
{
		

	Vect3D cspline_for={0.0, 0.0, 0.0};
	Vect3D cspline_back={108.0/256.0, 124.0/256.0, 126.0/256.0};


	if (current_hertz >= 6819 && current_hertz<10240)
	{

		
		float g=SYNC3b_whouwhou(current_hertz);


		
		glClearColor(1.0, 1.0, 1.0, 0.0f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		float rideau=0.6;
		float rubandev;

		if (current_hertz>=6819 && current_hertz<6919)
		{
		
			rubandev=INTERPOLATE_float(6819,6919,0.0,3.28,current_hertz);
			rideau=INTERPOLATE_float(6819,6919,4.0,0.6,current_hertz);	
		}
		else
		{
			rubandev=6.28;
		}



		Vect2D move1={0.0, 0.0};
		if (current_hertz>8715)
		{
			move1.x=sin(g);
			move1.y=cos(g)*sin(g)*sin(g);
		}

		glBindTexture(GL_TEXTURE_2D, temptexture);

		SPLINES_Draw_Parametric(rubandev+g*3.28,490, 20, 
			1.0+g,	//zoom
			g,	//alpha
			move1, 0, 1, 
			1,		//blend
			ACCURATE_TIMER*0.01,	//rotation
			cspline_back);


		float u=(((current_hertz&1023))/1024.0);
		MANONEGRA_part1(u);



		int active_spline=0;
		float b;

			if (current_hertz>7242 && current_hertz<7242+120) 
			{ 
				b=INTERPOLATE_float(7242,7242+120,0.0,6.28,current_hertz);	
				active_spline=1; 
			}

			if (current_hertz>8700 && current_hertz<8700+120) 
			{ 
				b=INTERPOLATE_float(8700,8700+120,0.0,6.28,current_hertz);	
				active_spline=1; 
			}

			if (active_spline==1)
			{
				Vect2D move;
				move.x=sin(ACCURATE_TIMER*0.001)*0.3;
				move.y=cos(ACCURATE_TIMER*0.001)*0.3;
				glBindTexture(GL_TEXTURE_2D, temptexture);
				SPLINES_Draw_Parametric(b,390, 
				70, 1.0+b*0.15, (6.28-b)/6.28, move, 0, 0, 1, ACCURATE_TIMER*0.01,cspline_for);
			}


		if (current_hertz>10170 && current_hertz<10240)	//on affiche la sphere derriere
		{
			Vect3D translate={0.0, 0.0, 0.0};
			float a=INTERPOLATE_float(10170,10240,0.0,1.0,current_hertz);	

		 	PUTAIN_DE_SPHERE(ACCURATE_TIMER*0.0015,a);
		
		 ///
		 ///
		 ///
		 ///
		 ///SPHERE_print(ACCURATE_TIMER*0.0015,ACCURATE_TIMER*0.1, 4.0, translate, 1, a);
		 ///
		 ///
		 ///
		 ///
		 ///

		}

		
		glBindTexture(GL_TEXTURE_2D, textures[1]);
		MASK_draw(4.780015, 0.27, 0.0,       0.89+rideau,        0.0);
		MASK_draw(4.780015, 0.27, -0.939-rideau,    -0.129999,   90.0);
		MASK_draw(3.67, -1.777,   -2.739998-rideau, 0.040001,    90.0);
		MASK_draw(3.67, -1.777,   0.0,       2.76+rideau,        0.0);

		
	}

}

float old_nurbs_angle;

void NURBS_test_object(int sides, int points, float rotat, int texture_id, float ecarte);
void NURBS_compute(int sides, int maxpoints, float param1);


#define START_passage_meduse	12400













void SYNC4_lotus(int current_hertz)
{

	if (current_hertz>=10240 && current_hertz<12690)
	{

	
		
			float a;

			if (current_hertz>=10340) 
			{ a=1.0; }
				 else 
			{ a=INTERPOLATE_float(10240,10340,0.0,1.0,current_hertz); }


			if (current_hertz>START_passage_meduse && current_hertz<12690)
			{
			float fog_color[4]={28*0.5*INV256, 27*0.5*INV256, 87*0.5*INV256, 1.0f};
			glEnable(GL_FOG);
			glFogf(GL_FOG_MODE, GL_LINEAR);
			glFogfv(GL_FOG_COLOR, fog_color);
			
			glFogf(GL_FOG_START,INTERPOLATE_float(START_passage_meduse,12690,3.0,0.0,current_hertz));
			glFogf(GL_FOG_END, INTERPOLATE_float(START_passage_meduse,12690,4.0,0.0,current_hertz));
			}



			Vect3D translate={0.0, 0.0, 0.0};

			glClearColor(28*0.5*INV256, 27*0.5*INV256, 87*0.5*INV256, 1.0f);
			//glClearColor(0.1*114*INV256, 0.13*200*INV256, 0.1*22*INV256, 0.0f);	//efface  vert fluo
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

			PUTAIN_DE_SPHERE(ACCURATE_TIMER*0.0015,1.0-a*0.5);
			///
			///
			///
			///
			///SPHERE_print(ACCURATE_TIMER*0.0015,ACCURATE_TIMER*0.03, 4.0, translate, 1, 1.0-a*0.5);
			///
			///
			///
			///
			glClear(GL_DEPTH_BUFFER_BIT);

	

			float ecart;

			if (current_hertz<START_passage_meduse)
			{
			ecart=1.0;
			}
			else
			{

				ecart=INTERPOLATE_float(START_passage_meduse,12690,1.0,22.0,current_hertz);
			}



			glDisable(GL_FOG);
		

				//	return;

			/* nurbs */

		float angle;

			if (current_hertz>11098 && current_hertz<=11230)
			{
			angle=old_nurbs_angle;
			}
			else
			{
			angle=old_nurbs_angle=ACCURATE_TIMER/50.0;
			}
			
			NURBS_compute(10, 40, angle*0.1);			//tesselate the thing

			for (int i=0;i<8;i++)
			{

			glColor4f(1.0f,1.0f,1.0f,a*0.5);		// Full Brightness, 50% Alpha ( NEW )
			NURBS_test_object(10, 40, angle+45.0*i, temptexture, ecart);
			}

		
			for ( i=0;i<8;i++)
			{
			glColor4f(1.0f,1.0f,1.0f,a*4.0*fabs(sin(current_hertz*0.01)));		// Full Brightness, 50% Alpha ( NEW )
			NURBS_test_object(10, 40, 22.5+angle+45.0*i, textures[2], ecart);
			}
	


		


	glBindTexture(GL_TEXTURE_2D, textures[1]);
	MASK_draw(4.780015, 0.27, 0.0,       0.89+0.5,        0.0);
	MASK_draw(4.780015, 0.27, -0.939-0.5,    -0.129999,   90.0);
	MASK_draw(3.67, -1.777,   -2.739998-0.5, 0.040001,    90.0);
	MASK_draw(3.67, -1.777,   0.0,       2.76+0.5,        0.0);

	}

}




	/*	
		if (current_hertz >= 10489)
		{
			glClearColor(0.1*114*INV256, 0.13*200*INV256, 0.1*22*INV256, 0.0f);	//efface  vert fluo
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			glBindTexture(GL_TEXTURE_2D, temptexture);
			SPHERETEST();
		}

	*/