#include <stdio.h> 
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>


//static GLenum spinMode = GL_TRUE;
//static GLenum singleStep = GL_FALSE;

struct timeval starttime,endtime;
static float HourOfDay = 0.0;
static float DayOfYear = 0.0;
static float AnimateIncrement = 24.0;  // Time step for animation (hours)
void OpenGLInit(void);

static void Animate(void );
static void ResizeWindow(int w, int h);
//double ge0,ge1,lsgfx;


static void Animate(void)
{
//gettimeofday(&starttime, NULL);
	//double ge0,ge1;
	int i;
	for (i=0; i<1500; i++) {
        // Clear the redering window
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//    if (spinMode) {
                // Update the animation state
        HourOfDay += AnimateIncrement;
        DayOfYear += AnimateIncrement/24.0;

        HourOfDay = HourOfDay - ((int)(HourOfDay/24))*24;
        DayOfYear = DayOfYear - ((int)(DayOfYear/365))*365;
  //              }

        // Clear the current matrix (Modelview)
    glLoadIdentity();

        // Back off eight units to be able to view from the origin.
    glTranslatef ( 0.0, 0.0, -14.0 );

        // Rotate the plane of the elliptic
        // (rotate the model's plane about the x axis by fifteen degrees)
        glRotatef( 15.0, 1.0, 0.0, 0.0 );

    // Draw the sun     -- as a yellow, wireframe sphere
        glColor3f( 1.0, 1.0, 0.0 );
    glutWireSphere( 4.0, 80, 80 );
//glutWireCube(2.0);
//glutSolidSphere( 2.0, 300, 300 );

    // Draw the Earth
        // First position it around the sun
        //              Use DayOfYear to determine its position
    glRotatef( 360.0*DayOfYear/365.0, 0.0, 1.0, 0.0 );
    glTranslatef( 4.0, .0, .0 );
    glPushMatrix();                                             // Save matrix state
        // Second, rotate the earth on its axis.
        //              Use HourOfDay to determine its rotation.
        glRotatef( 360.0*HourOfDay/24.0, 0.0, 1.0, 0.0 );
        // Third, draw the earth as a wireframe sphere.
    glColor3f( 0.2, 0.2, 1.0 );
    glutWireSphere( 0.8, 45, 45);
    glPopMatrix();                                              // Restore matrix state

        // Draw the moon.
        //      Use DayOfYear to control its rotation around the earth
        glRotatef( 360.0*12.0*DayOfYear/365.0, 0.0, 1.0, 0.0 );
    glTranslatef( 0.7, 0.0, 0.0 );
    glColor3f( 0.3, 0.7, 0.3 );
   glutWireSphere( 0.2, 30, 30 );
//glutWireTetrahedron();
//glutWireCube(0.2);

// Draw some fucking stars
//glRotatef( 0.0, 0.0, 1.0, 0.0 );
    glTranslatef( 0.0, 0.0, 0.0 );
glColor3f( 9.0, 9.0, 9.0 );
glutWireSphere( 0.01111, 4, 4 );

//glRotatef( 360.0*12.0*DayOfYear/365.0, 0.0, 1.0, 0.0 );
    glTranslatef( 0.9, 1.0, 2.0 );
glColor3f( 1.0, 1.0, 2.0 );
glutWireSphere( 0.0041, 3, 3 );

//glRotatef( 360.0*12.0*DayOfYear/365.0, 0.0, 1.0, 0.0 );
    glTranslatef( 0.5, 4.0, 2.0 );
glColor3f( 1.0, 2.0, 4.0 );
glutWireSphere( 0.0071, 4, 4 );

//glRotatef( 360.0*12.0*DayOfYear/365.0, 0.0, 1.0, 0.0 );
    glTranslatef( 0.6, 5.0, 4.0 );
glColor3f( 7.0, 6.0, 3.0 );
glutWireSphere( 0.0123, 4, 4 );

//glRotatef( 360.0*12.0*DayOfYear/365.0, 0.0, 1.0, 0.0 );
    glTranslatef( 0.8, 3.0, 2.0 );
glColor3f( 2.0, 3.0, 4.0 );
glutWireSphere( 0.00955, 4, 4 );


        // Flush the pipeline, and swap the buffers
    glFlush();
    glutSwapBuffers();

    //    if ( singleStep ) {
  //              spinMode = GL_FALSE;
        //}


        glutPostRedisplay();            // Request a re-draw for animation purposes

}	
//gettimeofday(&endtime, NULL);
//ge0=((double)(endtime.tv_sec*1000000-starttime.tv_sec*1000000+endtime.tv_usec-starttime.tv_usec))/1000000;
//       lsgfx = 150/ge0;

	//ge1 = ge0;
	//printf("\n\tTest #1:\t Wire frame animation:\t%lf\n\n", ge1);
	//return ge1;
	exit (0);
}



// Initialize OpenGL's rendering modes
void OpenGLInit(void)
{
    glShadeModel( GL_FLAT );
    glClearColor( 0.0, 0.0, 0.0, 0.0 );
    glClearDepth( 1.0 );
    glEnable( GL_DEPTH_TEST );
}


// ResizeWindow is called when the window is resized
static void ResizeWindow(int w, int h)
{
    float aspectRatio;
        h = (h == 0) ? 1 : h;
        w = (w == 0) ? 1 : w;
        glViewport( 0, 0, w, h );       // View port uses whole window
        aspectRatio = (float)w/(float)h;

        // Set up the projection view matrix (not very well!)
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective( 60.0, aspectRatio, 1.0, 30.0 );

        // Select the Modelview matrix
    glMatrixMode( GL_MODELVIEW );
}



//int Graphics1(){
//	gfxtest1(void);
//}


int main( int argc, char** argv )
{
//gettimeofday(&starttime, NULL);

// gettimeofday(&starttime, NULL);




        glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );

        // Create and position the graphics window
    glutInitWindowPosition( 0, 0 );
    glutInitWindowSize( 1600, 1200 );
    glutCreateWindow( "lsbench wire frame animation test" );

        // Initialize OpenGL.
    OpenGLInit();

        // Set up the callback function for resizing windows
    glutReshapeFunc( ResizeWindow );

        // Callback for graphics image redrawing
    glutDisplayFunc( Animate );

        // Start the main loop.  glutMainLoop never returns.
        glutMainLoop(  );

gettimeofday(&endtime, NULL);

ge0=((double)(endtime.tv_sec*1000000-starttime.tv_sec*1000000+endtime.tv_usec-starttime.tv_usec))/1000000;
        //ge1 = ge0 / 60;
        ge1 = ge0;
lsgfx = ge1 /60;
        printf("\n\tTest #1:\t Wire frame animation:\t%lf\n\n", ge0);

FILE *fp;
//lsgfx = ge1 /60;
fp=fopen("gfxdir/lsgfx.log", "w");
fprintf(fp, "%f\n", lsgfx);
fclose(fp);


// gettimeofday(&endtime, NULL);
//return lsgfx;
   exit(1);
}
