#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "veh_ogl.h"

/*--------------------------
*
*
*
*--------------------------*/



void do_transformations ()
{
        glPushMatrix();
	glLoadIdentity();
                       
	if (current_xrot >= 20)
		current_xrot = 20;
	if (current_xrot <= -20)
		current_xrot = -20;
	if (current_zrot >= 20)
		current_zrot = 20;
	if (current_zrot <= -20)
		current_zrot = -20;


  	
             glRotatef(-current_xrot, 1.0, 0.0, 0.0); 		/* roll 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);	/* lateral 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);
   /* 1, 12, 11, 10, 9, 8, 7, 4, 3, 2 */
   glVertex3f( -1.0, 0.2, 0.0 );  	
   glVertex3f( -1.0, 0.2, -1.2 ); 	
   glVertex3f( -0.7, 0.2, -1.2 );         	
   glVertex3f( -0.4, 0.2, -1.0 );        	
   glVertex3f( 0.4, 0.2, -1.0 );     	
   glVertex3f( 0.7, 0.2, -1.2  );   	
   glVertex3f( 1.0, 0.2, -1.2 );	
   glVertex3f( 1.0, 0.2, 0.0 ); 	
   glVertex3f( 0.5, 0.2, 0.8 );		
   glVertex3f( -0.5, 0.2, 0.8 );	

   glEnd( );

}

/*-------------------------------------------------------------------*/


void draw_bottom()
{
   glBegin(GL_POLYGON);

   glVertex3f( -1.0, -0.2, 0.0 );  	
   glVertex3f( -1.0, -0.2, -1.2 ); 	
   glVertex3f( -0.7, -0.2, -1.2 );
   glVertex3f( -0.4, -0.2, -1.0 );
   glVertex3f( 0.4, -0.2, -1.0 );     	
   glVertex3f( 0.7, -0.2, -1.2  );   	
   glVertex3f( 1.0, -0.2, -1.2 );	
   glVertex3f( 1.0, -0.2, 0.0 ); 	
   glVertex3f( 0.5, -0.2, 0.8 );	
   glVertex3f( -0.5, -0.2, 0.8 );	

   glEnd( );


}


/*------------------------------------------------------------------- */


void draw_front ( )
{

   /* Part I (1,2, 14, 13) */
   glBegin(GL_POLYGON);
   glVertex3f( -1.0, 0.2, 0.0 );  	
   glVertex3f( -0.5, 0.2, 0.8 );	
   glVertex3f( -0.5, -0.2, 0.8 );
   glVertex3f( -1.0, -0.2, 0.0 );	
   glEnd();

   /* Part J (2,3, 15, 14) */
   glBegin(GL_POLYGON);
   glVertex3f( -0.5, 0.2, 0.8 );	
   glVertex3f( 0.5, 0.2, 0.8 );		
   glVertex3f( 0.5, -0.2, 0.8);	
   glVertex3f( -0.5, -0.2, 0.8 );	
   glEnd();

   /* Part K (3,4,16, 15 ) */
   glBegin(GL_POLYGON);
   glVertex3f( 0.5, 0.2, 0.8 );		
   glVertex3f( 1.0, 0.2, 0.0 ); 	
   glVertex3f( 1.0, -0.2, 0.0 ); 	
   glVertex3f( 0.5, -0.2, 0.8);	
   glEnd();

}


/*-------------------------------------------------------------------*/


void draw_left ( )
{

   /* Part M (12, 6, 18, 24 ) */
   glBegin(GL_POLYGON);
   glVertex3f( -1.0, 0.2, -1.2 ); 	
   glVertex3f( -1.0, 0.2, -1.0 );     	
   glVertex3f( -1.0, -0.2, -1.0 );      	
   glVertex3f( -1.0, -0.2, -1.2 ); 	
   glEnd();

   /* Part L (6,1, 13, 18) */
   glBegin(GL_POLYGON);
   glVertex3f( -1.0, 0.2, -1.0 );     	
   glVertex3f( -1.0, 0.2, 0.0 );  	
   glVertex3f( -1.0, -0.2, 0.0 );	
   glVertex3f( -1.0, -0.2, -1.0 );      	
   glEnd();

}


/*-------------------------------------------------------------------*/


void draw_right ( )
{
   /* Part N (4, 5, 17, 16 ) */
   glBegin(GL_POLYGON);
   glVertex3f( 1.0, 0.2, 0.0 ); 	
   glVertex3f( 1.0, 0.2, -1.0 );	
   glVertex3f( 1.0, -0.2, -1.0 );       	
   glVertex3f( 1.0, -0.2, 0.0 ); 	
   glEnd();

   /* Part O (5,7,19,17) */
   glBegin(GL_POLYGON);
   glVertex3f( 1.0, 0.2, -1.0 );	
   glVertex3f( 1.0, 0.2, -1.2 );	
   glVertex3f( 1.0, -0.2, -1.2 );	
   glVertex3f( 1.0, -0.2, -1.0 );       	
   glEnd();

}


/*-------------------------------------------------------------------*/


void draw_back( )
{
   /* Part P (7, 8, 20, 19) */
   glBegin(GL_POLYGON);
   glVertex3f( 1.0, 0.2, -1.2 );	
   glVertex3f( 0.7, 0.2, -1.2  );   	
   glVertex3f( 0.7, -0.2, -1.2  );   	
   glVertex3f( 1.0, -0.2, -1.2 );	
   glEnd();

   /* Part Q (8, 9, 21, 20) */
   glBegin(GL_POLYGON);
   glVertex3f( 0.7, 0.2, -1.2  );   	
   glVertex3f( 0.4, 0.2, -1.0 );     	
   glVertex3f( 0.4, -0.2, -1.0 );     	
   glVertex3f( 0.7, -0.2, -1.2  );   	
   glEnd();

   /* Part R (9, 10, 22, 21) */
   glBegin(GL_POLYGON);
   glVertex3f( 0.4, 0.2, -1.0 );     	
   glVertex3f( -0.4, 0.2, -1.0 );        	
   glVertex3f( -0.4, -0.2, -1.0 );
   glVertex3f( 0.4, -0.2, -1.0 );     	
   glEnd();

   /* Part S (10, 11, 23,22) */
   glBegin(GL_POLYGON);
   glVertex3f( -0.4, 0.2, -1.0 );
   glVertex3f( -0.7, 0.2, -1.2 );         	
   glVertex3f( -0.7, -0.2, -1.2 );
   glVertex3f( -0.4, -0.2, -1.0 );
   glEnd();

   /* Part T (11, 12,24, 23) */
   glBegin(GL_POLYGON);
   glVertex3f( -0.7, 0.2, -1.2 );
   glVertex3f( -1.0, 0.2, -1.2 ); 	
   glVertex3f( -1.0, -0.2, -1.2 ); 	
   glVertex3f( -0.7, -0.2, -1.2 );
   glEnd();

}

/*------------------------------------------------------------------- */


void draw_hor_thrusters( )
{
   GLUquadricObj *quadric;
   quadric = gluNewQuadric();

   /*************** How to use gluCylinder ************************/
   /* gluCylinder ( GLUquadricObj *quadric, top radius, base radius, height, slices, stacks) */

   /*-----------------
   * Horizontal Thrusters
   *-----------------*/

    glColor3f(0.5, 0.5, 0.5);     	

    /* The left thruster */
    glPushMatrix();
    glColor3f(0.7, 0.7, 0.7);
    glTranslatef(-0.65, -0.8, 0.5);
    /*glRotatef(30, 0.0, 1.0, 0.0);    	/* make the thruster slightily rotated*/
    gluCylinder(quadric, 0.15, 0.15+ 0.03 * stbd, 0.2, 20, 2);

    /*
    if (port != 0)
    {
         glColor3f(0.5, 0.0, 0.0);
         gluCylinder (quadric, 0.15+port/1000, 0.15+port/1000, 0.2, 20, 2);
    }

    */

    glPopMatrix();

    /* The right thruster */
    glPushMatrix();
    glColor3f(0.7, 0.7, 0.7);
    glTranslatef(0.65, -0.8, 0.5);


    gluCylinder(quadric, 0.15, 0.15+ 0.03 * port, 0.2, 20, 2);

    /*
    if (stbd != 0)
    {
         glColor3f(0.5, 0.0, 0.0);
         gluCylinder (quadric, 0.15+port/1000, 0.15+stbd/1000, 0.2, 20, 2);
    }

    */

    glPopMatrix();


    /*******************/
    /* draw propellar */
    /*******************/

    glColor3f(0.5, 0.0, 0.8);

    /* on the left */
    glPushMatrix();
       glRotatef(90, 0.0, 1.0, 0.0 );
       glTranslatef(-0.6, -0.8, -0.65);	/* (front/back, up/down, left/right)*/
       glPushMatrix();
          glRotatef(port*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.8, 0.65);	/* (front/back, up/down, left/right)*/
       glPushMatrix();
          glRotatef(stbd *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 (lateral <= 0)		/* vehicle is moving to left */
        {
        	glRotatef(90.0,0.0, 1.0, 0.0);
        	gluCylinder(quadric, 0.15, 0.0, 0.1 * (-lateral), 20, 2);
        }

        else
        {
        	glRotatef(-90.0,0.0, 1.0, 0.0);
        	gluCylinder(quadric, 0.15, 0.0, 0.1 * lateral, 20, 2);
       }

    glPopMatrix();
    gluDeleteQuadric(quadric);

}


/*-------------------------------------------------------------------*/
/*					*/
/*					*/
/*-------------------------------------------------------------------*/

void draw_camera1( )
{

   GLUquadricObj *quadric;
   quadric = gluNewQuadric();


   glPushMatrix();    	/* this needed so that the obj below this code
   			 is not translated as well */
   			
      /* position of the camera */
      glTranslatef( -0.3, -0.7, -1.0 );
      glRotatef(90,0.0, 1.0,0.0);

   /* this to rotate the camera up/down, left/right */
      glPushMatrix();                     	
   	glRotatef(-camh1, 0.0, 0.0, 1.0);
             glRotatef(camv1, 0.0, 1.0, 0.0);      /* if positif angle -> CW rotation, else CCW */

   	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();

   gluDeleteQuadric(quadric);
}

/*-------------------------------------------------------------------*/
/*					*/
/*					*/
/*-------------------------------------------------------------------*/

void draw_camera2( )
{

   GLUquadricObj *quadric;
   quadric = gluNewQuadric();


   glPushMatrix();    	/* this needed so that the obj below this code
   			 is not translated as well */
   			
      /* position of the camera */
      glTranslatef( 0.3, -0.7, -1.0 );
      glRotatef(90,0.0, 1.0,0.0);

   /* this to rotate the camera up/down, left/right */
      glPushMatrix();                     	
   	glRotatef(camh2, 0.0, 0.0, 1.0);
             glRotatef(-camv2, 0.0, 1.0, 0.0);      /* if positif angle -> CW rotation, else CCW */

   	glPushMatrix();   	/* to push all the camera parts*/
   		glColor3f (0.3,1.0,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 (0.3,0.7,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();

   gluDeleteQuadric(quadric);
}

/*-------------------------------------------------------------------*/
/*					*/
/*					*/
/*-------------------------------------------------------------------*/


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()
{

   /*-------------------
   * 4 Vertical Thrusters
   *-------------------*/
    glColor3f(0.7, 0.7, 0.7);		

    /* The top left thruster */
    glPushMatrix();
    //glTranslatef(-0.4, 0.35, 0.0);
    glTranslatef(-0.4, 0.17, 0.8);
    glRotatef(90, 1.0, 0.0, 0.0);
    glutSolidCone(0.15, 0.05 * -vertical, 20, 2);
    glPopMatrix();

    /* The top right thruster  */
    glPushMatrix();
    //glTranslatef(0.4, 0.35, 0.0);
    glTranslatef(0.4, 0.2, 0.8);
    glRotatef(90, 1.0, 0.0, 0.0);
    glutSolidCone(0.15, 0.05 * -vertical, 20, 2);
    glPopMatrix();

    /* The bottom left thruster */
    glPushMatrix();
    glTranslatef(-0.4, -0.21, 0.0);
    glRotatef(90, 1.0, 0.0, 0.0);
    glutSolidCone(0.15, 0.05 * -vertical, 20, 2);
    glPopMatrix();

     /* The bottom right thruster */
    glPushMatrix();
    glTranslatef(0.4, -0.21, 0.0);
    glRotatef(90, 1.0, 0.0, 0.0);
    glutSolidCone(0.15, 0.05 * -vertical, 20, 2);
    glPopMatrix();


}


/*-------------------------------------------------------------------*/
/*  Draw the vehicle along with its thrusters and frames			   		*/
/*					*/
/*-------------------------------------------------------------------*/


void draw_sub ( )
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(1.0, 0.7, 0.0);         	/* brighter yellow */
    draw_top();
    draw_bottom();

    glColor3f(1.0, 0.5, 0.0);   	/* darker yellow */
    draw_front();
    draw_back();
    draw_left();
    draw_right();

    draw_frame();
    draw_hor_thrusters();
    draw_ver_thrusters();

}



