#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <stdarg.h>
#include "togl.h"
#include "draw.h"


float current_xrot;
float current_zrot;
float move;
float accel;
float camv1;
float camh1;
float slide;
float base_rad;
double matrix_rot [16];
double cam_matrix_rot[16];



void do_transformations ()
{
             glPushMatrix();
	glLoadIdentity();
                         	
             glRotatef(current_xrot, 1.0, 0.0, 0.0); 		// move up and down --> x axis	
             glRotatef(current_zrot, 0.0, 0.0, 1.0);           	// tilt left and right --> z axis



	glGetDoublev (GL_MODELVIEW_MATRIX, matrix_rot);	
             glPopMatrix();
	
	glMultMatrixd (matrix_rot);
	
}

void cam_transformations()
{
  	glPushMatrix();
	glLoadIdentity();

 	glRotatef(camv1, 1.0, 0.0, 0.0);	// move up and down --> x axis
             glRotatef(camh1, 0.0, 1.0, 0.0);        	// turn left to right --> y axis

             glTranslatef(0.0, 0.0, -0.4);

             glGetDoublev (GL_MODELVIEW_MATRIX, cam_matrix_rot);	
             glPopMatrix();
	
	glMultMatrixd (cam_matrix_rot);

}
//-------------------------------------------------------------------



void draw_top()
{
   glBegin(GL_POLYGON);

   glVertex3f( -1.0, 0.2, 0.0 );  	// 1
   glVertex3f( -1.0, 0.2, -1.2 ); 	// 12
   glVertex3f( -0.7, 0.2, -1.2 );         	// 11
   glVertex3f( -0.4, 0.2, -1.0 );        	// 10
   glVertex3f( 0.4, 0.2, -1.0 );     	// 9
   glVertex3f( 0.7, 0.2, -1.2  );   	// 8
   glVertex3f( 1.0, 0.2, -1.2 );	// 7
   glVertex3f( 1.0, 0.2, 0.0 ); 	// 4
   glVertex3f( 0.5, 0.2, 0.8 );		// 3
   glVertex3f( -0.5, 0.2, 0.8 );	// 2

   glEnd( );

}

//-------------------------------------------------------------------


void draw_bottom()
{
   glBegin(GL_POLYGON);

   glVertex3f( -1.0, -0.2, 0.0 );  	// 13
   glVertex3f( -1.0, -0.2, -1.2 ); 	// 24
   glVertex3f( -0.7, -0.2, -1.2 );        // 23
   glVertex3f( -0.4, -0.2, -1.0 );        // 22
   glVertex3f( 0.4, -0.2, -1.0 );     	// 21
   glVertex3f( 0.7, -0.2, -1.2  );   	// 20
   glVertex3f( 1.0, -0.2, -1.2 );	// 19
   glVertex3f( 1.0, -0.2, 0.0 ); 	// 16
   glVertex3f( 0.5, -0.2, 0.8 );	// 15
   glVertex3f( -0.5, -0.2, 0.8 );	// 14

   glEnd( );


}


//-------------------------------------------------------------------


void draw_front ( )
{

   // Part I
   glBegin(GL_POLYGON);
   glVertex3f( -1.0, 0.2, 0.0 );  	// 1
   glVertex3f( -0.5, 0.2, 0.8 );	// 2
   glVertex3f( -0.5, -0.2, 0.8 );	// 14
   glVertex3f( -1.0, -0.2, 0.0 );	// 13
   glEnd();

   // Part J
   glBegin(GL_POLYGON);
   glVertex3f( -0.5, 0.2, 0.8 );	// 2
   glVertex3f( 0.5, 0.2, 0.8 );		// 3
   glVertex3f( 0.5, -0.2, 0.8);	// 15
   glVertex3f( -0.5, -0.2, 0.8 );	// 14
   glEnd();

   // Part K
   glBegin(GL_POLYGON);
   glVertex3f( 0.5, 0.2, 0.8 );		// 3
   glVertex3f( 1.0, 0.2, 0.0 ); 	// 4
   glVertex3f( 1.0, -0.2, 0.0 ); 	// 16
   glVertex3f( 0.5, -0.2, 0.8);	// 15
   glEnd();

}


//-------------------------------------------------------------------


void draw_left ( )
{

   // Part M
   glBegin(GL_POLYGON);
   glVertex3f( -1.0, 0.2, -1.2 ); 	// 12
   glVertex3f( -1.0, 0.2, -1.0 );     	// 6
   glVertex3f( -1.0, -0.2, -1.0 );      	// 18
   glVertex3f( -1.0, -0.2, -1.2 ); 	// 24
   glEnd();

   // Part L
   glBegin(GL_POLYGON);
   glVertex3f( -1.0, 0.2, -1.0 );     	// 6
   glVertex3f( -1.0, 0.2, 0.0 );  	// 1
   glVertex3f( -1.0, -0.2, 0.0 );	// 13
   glVertex3f( -1.0, -0.2, -1.0 );      	// 18
   glEnd();

}


//-------------------------------------------------------------------


void draw_right ( )
{
   // Part N
   glBegin(GL_POLYGON);
   glVertex3f( 1.0, 0.2, 0.0 ); 	// 4
   glVertex3f( 1.0, 0.2, -1.0 );	// 5
   glVertex3f( 1.0, -0.2, -1.0 );       	// 17
   glVertex3f( 1.0, -0.2, 0.0 ); 	// 16
   glEnd();

   // Part O
   glBegin(GL_POLYGON);
   glVertex3f( 1.0, 0.2, -1.0 );	// 5
   glVertex3f( 1.0, 0.2, -1.2 );	// 7
   glVertex3f( 1.0, -0.2, -1.2 );	// 19
   glVertex3f( 1.0, -0.2, -1.0 );       	// 17
   glEnd();

}


//-------------------------------------------------------------------


void draw_back( )
{
   // Part P
   glBegin(GL_POLYGON);
   glVertex3f( 1.0, 0.2, -1.2 );	// 7
   glVertex3f( 0.7, 0.2, -1.2  );   	// 8
   glVertex3f( 0.7, -0.2, -1.2  );   	// 20
   glVertex3f( 1.0, -0.2, -1.2 );	// 19
   glEnd();

   // Part Q
   glBegin(GL_POLYGON);
   glVertex3f( 0.7, 0.2, -1.2  );   	// 8
   glVertex3f( 0.4, 0.2, -1.0 );     	// 9
   glVertex3f( 0.4, -0.2, -1.0 );     	// 21
   glVertex3f( 0.7, -0.2, -1.2  );   	// 20
   glEnd();

   // Part R
   glBegin(GL_POLYGON);
   glVertex3f( 0.4, 0.2, -1.0 );     	// 9
   glVertex3f( -0.4, 0.2, -1.0 );        	// 10
   glVertex3f( -0.4, -0.2, -1.0 );        // 22
   glVertex3f( 0.4, -0.2, -1.0 );     	// 21
   glEnd();

   // Part S
   glBegin(GL_POLYGON);
   glVertex3f( -0.4, 0.2, -1.0 );        	// 10
   glVertex3f( -0.7, 0.2, -1.2 );         	// 11
   glVertex3f( -0.7, -0.2, -1.2 );        // 23
   glVertex3f( -0.4, -0.2, -1.0 );        // 22
   glEnd();

   // Part T
   glBegin(GL_POLYGON);
   glVertex3f( -0.7, 0.2, -1.2 );         	// 11
   glVertex3f( -1.0, 0.2, -1.2 ); 	// 12
   glVertex3f( -1.0, -0.2, -1.2 ); 	// 24
   glVertex3f( -0.7, -0.2, -1.2 );        // 23
   glEnd();

}

//-------------------------------------------------------------------


void draw_horizon()
{
    glColor3f(0.0, 0.0, 0.3); 	 	// blue

    // horizontal horizon

    glPushMatrix();
    glTranslatef(0.0, -current_xrot/100, 0.0);

    glBegin(GL_LINE_STRIP);
    glVertex3f( -2.5, 0.0, 0.0);
    glVertex3f( 2.5, 0.0, 0.0);
    glEnd();

    glPopMatrix();

    // Vertical horizon

    glPushMatrix();
    glTranslatef( -move/100, 0.0, 0.0);

    glBegin(GL_LINE_STRIP);
    glVertex3f( 0.0, 2.5, 0.0);
    glVertex3f( 0.0, -2.5, 0.0);
    glEnd();

    glPopMatrix();

}


//-------------------------------------------------------------------


void draw_hor_thrusters( )
{
   GLUquadricObj *quadric;
   quadric = gluNewQuadric();

   // gluCylinder ( GLUquadricObj *quadric, top radius, base radius, height, slices, stacks)

   //-----------------
   // Thrusters
   //-----------------

    glColor3f(0.5, 0.5, 0.5);     	// grey

    // The left thruster
    glPushMatrix();
    glColor3f(0.7, 0.7, 0.7);
    glTranslatef(-0.65, -0.5, 0.5);
    //glRotatef(30, 0.0, 1.0, 0.0);    	// make the thruster slightily rotated
    gluCylinder(quadric, 0.15, 0.15, 0.2, 20, 2);

    /*
    if (accel != 0)
    {
         glColor3f(0.5, 0.0, 0.0);
         gluCylinder (quadric, 0.15+accel/1000, 0.15+accel/1000, 0.2, 20, 2);
    }

    */

    glPopMatrix();

    // The right thruster
    glPushMatrix();
    glColor3f(0.7, 0.7, 0.7);
    glTranslatef(0.65, -0.5, 0.5);
    gluCylinder(quadric, 0.15, 0.15, 0.2, 20, 2);

    /*
    if (accel != 0)
    {
         glColor3f(0.5, 0.0, 0.0);
         gluCylinder (quadric, 0.15+accel/1000, 0.15+accel/1000, 0.2, 20, 2);
    }

    */

    glPopMatrix();

    // draw prepellar
    glColor3f(0.5, 0.0, 0.8);

    // on the left
    glPushMatrix();
       glRotatef(90, 0.0, 1.0, 0.0 );
       glTranslatef(-0.6, -0.5, -0.65);	// (front/back, up/down, left/right)
       glPushMatrix();
          glRotatef(accel*10, 1.0, 0.0, 0.0 );
          gluCylinder(quadric, 0.02, 0.02, 0.15, 20, 2);
          //glutSwapBuffers();
       glPopMatrix();
    glPopMatrix();

    // on the right
    glPushMatrix();
       glRotatef(90, 0.0, 1.0, 0.0 );
       glTranslatef(-0.6, -0.5, 0.65);	// (front/back, up/down, left/right)
       glPushMatrix();
          glRotatef(accel*10, 1.0, 0.0, 0.0 );
          gluCylinder(quadric, 0.02, 0.02, 0.15, 20, 2);
          //glutSwapBuffers();
       glPopMatrix();
    glPopMatrix();




    // The middle thruster
    // Draw the initial middle  thruster

    glColor3f(0.5, 0.5, 0.5);
    glLineWidth(1);
    glBegin( GL_LINE_STRIP );
    glVertex3f(0.0, -0.35, 0.5);
    glVertex3f(0.0, -0.65, 0.5);
    glEnd();

    // Manipulate the middle thruster

    glPushMatrix();
    glTranslatef(0.0, -0.5, 0.5);

        if (move <= 0)		// vehicle is moving to left
        {
        	glRotatef(90.0,0.0, 1.0, 0.0);
        	gluCylinder(quadric, 0.15, 0.0, (-move) / 100, 20, 2);
        }

        else
        {
        	glRotatef(-90.0,0.0, 1.0, 0.0);
        	gluCylinder(quadric, 0.15, 0.0, move / 100, 20, 2);
       }

    glPopMatrix();
    gluDeleteQuadric(quadric);

}


//-------------------------------------------------------------------



void draw_camera1( )
{
   glPushMatrix();    	// this needed so that the obj below this code
   			// is not translated as well
   			
      // position of the camera
      glTranslatef( 0.0, -1.0, 0.8 );
      glRotatef(90,0.0, 1.0,0.0);

   // this to rotate the camera up/down, left/right
      glPushMatrix();                     	
   	glRotatef(camv1*10, 0.0, 0.0, 1.0);
             glRotatef(camh1*10, 0.0, 1.0, 0.0);

   	glPushMatrix();   	// to push all the camera parts
   		glColor3f (1.0,0.3,0.3);

   		// draw the camera stick
   		glPushMatrix();
   			glRotatef(90, 0.0, 1.0, .0 );
  		 	glTranslatef(0.0, 0.0, -0.4);
      	
   			GLUquadricObj *quadric;
   			quadric = gluNewQuadric();
   			gluCylinder(quadric, 0.05, 0.05, 0.3, 20, 2);
                          	gluDeleteQuadric(quadric);
   		glPopMatrix();

   		// draw the lens
   		glColor3f (1.0,0.3,0.7);
  		glPushMatrix();       	// to draw the lens which is rotated
  			glRotatef(-90, 0.0, 1.0, 0.0);
  			glutSolidCone(0.1, 0.2, 20, 2);
   		glPopMatrix();

   	glPopMatrix();     			// pop the camera rotation
      glPopMatrix();
   glPopMatrix();

}

/*
void draw_info( )
{
   glColor3f(1.0,1.0,1.0);
   glRasterPos2i(20.0,10.0);
   FontPrintf(Halvetica, 0, "Ventana");


}

*/



//-------------------------------------------------------------------


void draw_frame()
{
    glColor3f(0.4, 0.4, 0.4 );		// white frame under the vehicle	
    glLineWidth(3);

    // back frame
    glBegin(GL_LINE_STRIP);
    glVertex3f( -1.0, -0.2, 0.0 );
    glVertex3f( -1.0, -1.5, 0.0 );
    glVertex3f( 1.0, -1.5, 0.0 );
    glVertex3f( 1.0, -0.2, 0.0 );
    glEnd();

    // front frame
    glBegin(GL_LINE_STRIP);
    glVertex3f( -1.0, -0.2, -1.2 );
    glVertex3f( -1.0, -1.5, -1.2 );
    glVertex3f( 1.0, -1.5, -1.2 );
    glVertex3f( 1.0, -0.2, -1.2 );
    glEnd();

    // side frame
    glBegin (GL_LINE_STRIP);
    glVertex3f( -1.0, -1.5, 0.0 );
    glVertex3f( -1.0, -1.5, -1.2 );
    glEnd();

    glBegin(GL_LINE_STRIP);
    glVertex3f( 1.0, -1.5, 0.0 );
    glVertex3f( 1.0, -1.5, -1.2 );
    glEnd();
}


void draw_ver_thrusters()
{

   //-------------------
   // Top 2 Vertical Thrusters
   //-------------------
    glColor3f(0.7, 0.7, 0.7);		// light grey

    // The top left thruster
    glPushMatrix();
    glTranslatef(-0.7, 0.21, 0.0);
    glRotatef(90, 1.0, 0.0, 0.0);
    glutSolidCone(0.15, slide/100, 20, 2);
    glPopMatrix();

     // The top right thruster
    glPushMatrix();
    glTranslatef(0.7, 0.21, 0.0);
    glRotatef(90, 1.0, 0.0, 0.0);
    glutSolidCone(0.15, slide/100, 20, 2);
    glPopMatrix();

    // The bottom left thruster
    glPushMatrix();
    glTranslatef(-0.7, -0.21, 0.0);
    glRotatef(90, 1.0, 0.0, 0.0);
    glutSolidCone(0.15, slide/100, 20, 2);
    glPopMatrix();

     // The bottom right thruster
    glPushMatrix();
    glTranslatef(0.7, -0.21, 0.0);
    glRotatef(90, 1.0, 0.0, 0.0);
    glutSolidCone(0.15, slide/100, 20, 2);
    glPopMatrix();










}


//-------------------------------------------------------------------


void draw_sub ( )
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(1.0, 1.0, 0.0);         	// bright yellow
    draw_top();
    draw_bottom();

    glColor3f(1.0, 0.9, 0.0);   	// darker yellow
    draw_front();
    draw_back();
    draw_left();
    draw_right();

    draw_frame();
    draw_hor_thrusters();
    draw_ver_thrusters();

}






