Mzsmunna
10/16/2019 - 3:15 PM

CG-Solar System-orbitPlanet


#include <iostream>
#include <stdlib.h>
#include<GL/gl.h>
#include <GL/glut.h>
#include<math.h>
#include<cstring>

using namespace std;

char a0_text[] = "SUN";
char a1_text[] = "MERCURY";
char a2_text[] = "VENUS";
char a3_text[] = "EARTH";
char a4_text[] = "MARS";
char a5_text[] = "MOON";
char instruct[] = "Check the Black console for Instruction";
char clockModeA[] = "Rotation : Anti-clock Wise";
char clockModeC[] = "Rotation : Clock Wise";
float speedE = 0.0f;
float speedMa = 0.0f;
float speedMe = 0.0f;
float speedV = 0.0f;
bool antiClock = true;
/*float savedSpeedE = 0.0f;
float savedSpeedMa = 0.0f;
float savedSpeedMe = 0.0f;
float savedSpeedV = 0.0f;
bool paused = false;*/

//Print
void Sprint( float x, float y, char *st)
{
    int l,i;


    l=strlen( st ); // see how many characters are in text string.
    glColor3f(1.0,1.0,1.0);
    //glDisable(GL_LIGHTING);
    //glRasterPos2f( x, y); // location to start printing text
    glRasterPos2f( x, y); // location to start printing text
    for( i=0; i < l; i++) // loop until i is greater then l
    {
       glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, st[i]);

    }
}

//Initializes 3D rendering
void initRendering() {
	glEnable(GL_DEPTH_TEST);
}
//Called when the window is resized
void handleResize(int w, int h) {
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}

float _angle1_1 = 30.0f;
float _angle1_2 = 30.0f;
float _angle1_3 = 30.0f;
float _angle1_4 = 30.0f;
//float _move = 0.0f;


void drawScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3d(1,0,0);
	glLoadIdentity(); //Reset the drawing perspective
	glMatrixMode(GL_MODELVIEW);

   // glRotatef(_angle, 1.0f, 0.0f, 0.0f); //Rotate the camera

    ///////////////////// Mars Planet ///////////////////////////
    glPushMatrix(); //Save the current state of transformations
	 //Move to the center of the triangle
	glRotatef(_angle1_4, 0.0, 0.0, 1.0);
	glTranslatef(0.9, 0.0, 0.0);//Rotate about the the vector (1, 2, 3)
	Sprint(0.05,0,a4_text);
	glColor3f(0.3,1.0,0.0);
	glBegin(GL_POLYGON);
	for(int i=0;i<200;i++)
	{
		float pi=3.1416;
		float A=(i*2*pi)/50 ;
		float r=0.10;
		float x = r * cos(A);
		float y = r * sin(A);
		glVertex2f(x,y );
	}
	glEnd();
    glPopMatrix();

    ///////////////////// Earth Planet ///////////////////////////
    glPushMatrix(); //Save the current state of transformations
	 //Move to the center of the triangle
	glRotatef(_angle1_3, 0.0, 0.0, 1.0);
	glTranslatef(0.7, 0.0, 0.0);//Rotate about the the vector (1, 2, 3)
	Sprint(0.05,0,a3_text);
	glColor3f(0.3,0.0,1.0);
	glBegin(GL_POLYGON);
	for(int i=0;i<200;i++)
	{
		float pi=3.1416;
		float A=(i*2*pi)/50 ;
		float r=0.09;
		float x = r * cos(A);
		float y = r * sin(A);
		glVertex2f(x,y );
	}
	glEnd();
	//glRotatef(10, 0.0, 0.0, 1.);
	//glTranslatef(0.0, 0.0, 0.0);//Rotate about the the vector (1, 2, 3)

	///////////////////// Orbit of Moon ///////////////////////////
	glColor3f(1.0,1.0,1.0);
	glBegin(GL_LINES);
	for(int i=0;i<200;i++)
	{
		float pi=3.1416;
		float A=(i*2*pi)/50 ;
		float r=0.12;
		float x = r * cos(A);
		float y = r * sin(A);
		glVertex2f(x,y );
	}
	glEnd();

	///////////////////// Moon ///////////////////////////
	glRotatef(_angle1_3, 0.0, 0.0, 1.0);
	glTranslatef(0.12, 0.0, 0.0);//Rotate about the the vector (1, 2, 3)
	Sprint(0.05,0,a5_text);
	glColor3f(1.0,1.0,1.0);
	glBegin(GL_POLYGON);
	for(int i=0;i<200;i++)
	{
		float pi=3.1416;
		float A=(i*2*pi)/50 ;
		float r=0.02;
		float x = r * cos(A);
		float y = r * sin(A);
		glVertex2f(x,y );
	}
	glEnd();
    glPopMatrix();

    ///////////////////// Venus Planet ///////////////////////////
     glPushMatrix(); //Save the current state of transformations
	 //Move to the center of the triangle
	glRotatef(_angle1_2, 0.0, 0.0, 1.0);
	glTranslatef(0.5, 0.0, 0.0);//Rotate about the the vector (1, 2, 3)
	Sprint(0.05,0,a2_text);
	glColor3f(1.0,0.0,1.0);
	glBegin(GL_POLYGON);
	for(int i=0;i<200;i++)
	{
		float pi=3.1416;
		float A=(i*2*pi)/50 ;
		float r=0.08;
		float x = r * cos(A);
		float y = r * sin(A);
		glVertex2f(x,y );
	}
	glEnd();
    glPopMatrix();

    ///////////////////// Mercury Planet ///////////////////////////
     glPushMatrix(); //Save the current state of transformations
	 //Move to the center of the triangle
	glRotatef(_angle1_1, 0.0, 0.0, 1.0);
	glTranslatef(0.3, 0.0, 0.0);//Rotate about the the vector (1, 2, 3)
	Sprint(0.05,0,a1_text);
	glColor3f(0.0,1.0,0.7);
	glBegin(GL_POLYGON);
	for(int i=0;i<200;i++)
	{
		float pi=3.1416;
		float A=(i*2*pi)/50 ;
		float r=0.06;
		float x = r * cos(A);
		float y = r * sin(A);
		glVertex2f(x,y );
	}
	glEnd();
    glPopMatrix();


    ///////////////////// Orbit 4 ///////////////////////////
    glPushMatrix();
    glColor3f(1.0,1.0,1.0);
	glBegin(GL_LINES);
	for(int i=0;i<200;i++)
	{
		float pi=3.1416;
		float A=(i*2*pi)/50 ;
		float r=0.9;
		float x = r * cos(A);
		float y = r * sin(A);
		glVertex2f(x,y );
	}
	glEnd();
    glPopMatrix();

    ///////////////////// Orbit 3 ///////////////////////////
    glPushMatrix();
    glColor3f(1.0,1.0,1.0);
	glBegin(GL_LINES);
	for(int i=0;i<200;i++)
	{
		float pi=3.1416;
		float A=(i*2*pi)/50 ;
		float r=0.7;
		float x = r * cos(A);
		float y = r * sin(A);
		glVertex2f(x,y );
	}
	glEnd();
    glPopMatrix();

///////////////////// Orbit 2 ///////////////////////////
    glPushMatrix();
    glColor3f(1.0,1.0,1.0);
	glBegin(GL_LINES);
	for(int i=0;i<200;i++)
	{
		float pi=3.1416;
		float A=(i*2*pi)/50 ;
		float r=0.5;
		float x = r * cos(A);
		float y = r * sin(A);
		glVertex2f(x,y );
	}
	glEnd();
    glPopMatrix();


///////////////////// Orbit 1 ///////////////////////////
    glPushMatrix();
    glColor3f(1.0,1.0,1.0);
	glBegin(GL_LINES);
	for(int i=0;i<200;i++)
	{
		float pi=3.1416;
		float A=(i*2*pi)/50 ;
		float r=0.3;
		float x = r * cos(A);
		float y = r * sin(A);
		glVertex2f(x,y );
	}
	glEnd();
    glPopMatrix();
///////////////////// SUN ///////////////////////////
    glPushMatrix();
        //Sprint(0.15,0,a0_text);
        glColor3f(1.0,0.0,0.0);
        glutSolidSphere(.15,50,50);
        Sprint(0.15,0,a0_text);
        Sprint(-0.99,0.9,instruct);
        if(antiClock==true)
        {
            Sprint(-0.99,0.8,clockModeA);
        }
        else if(antiClock==false)
        {
            Sprint(-0.99,0.8,clockModeC);
        }
    glPopMatrix();

	glutSwapBuffers();
}

void update(int value) {

	/*_angle1_1 += 3.0f;
	_angle1_2 += 2.0f;
	_angle1_3 += 1.5f;
	_angle1_4 += 1.0f*/

    if(speedMa>1 || speedMa<0)
    {
        //cout<<speed<<endl;
        speedMa = 0.0f;
    }
    if(speedMe>1 || speedMe<0)
    {
        //cout<<speed<<endl;
        speedMe = 0.0f;
    }
    if(speedE>1 || speedE<0)
    {
        //cout<<speed<<endl;
        speedE = 0.0f;
    }
    if(speedV>1 || speedV<0)
    {
        //cout<<speed<<endl;
        speedV = 0.0f;
    }

    if(antiClock==true)
    {
        _angle1_1 += speedMe;
        _angle1_2 += speedV;
        _angle1_3 += speedE;
        _angle1_4 += speedMa;
    }
    else if(antiClock==false)
    {
        _angle1_1 -= speedMe;
        _angle1_2 -= speedV;
        _angle1_3 -= speedE;
        _angle1_4 -= speedMa;
    }

	if (_angle1_1 > 360) {
		_angle1_1 -= 360;
	}
	if (_angle1_2 > 360) {
		_angle1_2 -= 360;
	}
	if (_angle1_3 > 360) {
		_angle1_3 -= 360;
	}
	if (_angle1_4 > 360) {
		_angle1_4 -= 360;
	}

	glutPostRedisplay(); //Tell GLUT that the display has changed

	//Tell GLUT to call update again in 25 milliseconds
	glutTimerFunc(30, update, 0);
}

void keyboard(unsigned char key, int x, int y) {
//find key codes: https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

    if(key==27) // ESC Key -> Exit
    {
        exit(0);
    }
    else if(key==77) // Capital M Key -> Decrease Mercury Speed
    {
        speedMe-=0.05f;
        update(0);
    }
    else if(key==69) // Capital E Key -> Decrease Earth Speed
    {
        speedE-=0.05f;
        update(0);
    }
    else if(key==86) // Capital V Key -> Decrease Venus Speed
    {
        speedV-=0.05f;
        update(0);
    }
    else if(key==78) // Capital N Key -> Decrease Mars Speed
    {
        speedMa-=0.05f;
        update(0);
    }
    else if(key==65) // Capital A Key -> Increase Speed of ALL Planets
    {
        speedMe+=0.05f;
        speedMa+=0.05f;
        speedE+=0.05f;
        speedV+=0.05f;
        update(0);
    }
    else if(key==68) // Capital D Key -> Decrease Speed of ALL Planets
    {
        speedMe-=0.05f;
        speedMa-=0.05f;
        speedE-=0.05f;
        speedV-=0.05f;
        update(0);
    }
    else if(key==82) // Capital R Key -> Change Rotation Mode
    {
        if(antiClock==true)
        {
            antiClock=false;
        }
        else
        {
            antiClock=true;
        }
        update(0);
    }
}

void specialKeys(int key, int x, int y) {
    switch (key)
    {
        case GLUT_KEY_UP: // Arrow UP Key -> Increase Speed of Mercury Planet
            speedMe+=0.05f;
            update(0);
            break;
        case GLUT_KEY_DOWN: // Arrow Down Key -> Increase Speed of Earth Planet
            speedE+=0.05f;
            update(0);
            break;
        case GLUT_KEY_RIGHT: // Arrow Right Key -> Increase Speed of Venus Planet
            speedV+=0.05f;
            update(0);
            break;
        case GLUT_KEY_LEFT: // Arrow Left Key -> Increase Speed of Mars Planet
            speedMa+=0.05f;
            update(0);
            break;
    }
}

int main(int argc, char** argv) {
    cout<<"Instruction : \n1. Press 'Arrow Up Key' to speedUP / run Mercury Planet"<<endl;
    cout<<"2. Press 'Arrow Right Key' to speedUP / run Venus Planet"<<endl;
    cout<<"3. Press 'Arrow Down Key' to speedUP / run Earth Planet"<<endl;
    cout<<"4. Press 'Arrow Left Key' to speedUP / run Mars Planet"<<endl;
    cout<<"5. Press Capital 'A' to speedUP / run all the Planets"<<endl;
    cout<<"6. Press Capital 'D' to speedDown / Stop all the Planet"<<endl;
    cout<<"7. Press Capital 'E' to speedDown / Stop Earth Planet"<<endl;
    cout<<"8. Press Capital 'M' to speedDown / Stop Mercury Planet"<<endl;
    cout<<"9. Press Capital 'N' to speedDown / Stop Mars Planet"<<endl;
    cout<<"10. Press Capital 'V' to speedDown / Stop Venus Planet"<<endl;
    cout<<"10. Press Capital 'R' to Toggle rotation to Clock Wise or Anti-Clock wise"<<endl;
    cout<<"11. Press 'Esc' to Exit"<<endl;
	//Initialize GLUT
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(800, 800);

	//Create the window
	glutCreateWindow("Transformation");
	initRendering();

	//Set handler functions
	glutDisplayFunc(drawScene);
	glutReshapeFunc(handleResize);

	glutSpecialFunc(specialKeys); //Special Key Handler
	glutKeyboardFunc(keyboard);   //Basic keyboard key handler

	glutTimerFunc(25, update, 0); //Add a timer

	glutMainLoop();

	return 0;
}