Mzsmunna
10/16/2019 - 3:14 PM

CG-Control Planet


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

using namespace std;

char a1_text[] = "MERCURY";
char a2_text[] = "VENUS";
char a3_text[] = "EARTH";
char a4_text[] = "MARS";
char a0_text[] = "SUN";
float speedE = 0.0f;
float speedMa = 0.0f;
float speedMe = 0.0f;
float speedV = 0.0f;
/*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]);

    }


}

void specialKeys(int key, int x, int y) {
    /*switch (key) {
      case GLUT_KEY_UP:
          speed+=0.05f;
          update(0);
          break;
      case GLUT_KEY_DOWN:
      //case 'g':
          speed-=0.05f;
          update(0);
          break;

    }*/
}

//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
///////////////////// Earth ///////////////////////////
    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();

    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();
    glPopMatrix();

     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();


     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 ///////////////////////////
    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 ///////////////////////////
    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 ///////////////////////////
    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 ///////////////////////////
    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);
    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;
    }

	_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
   switch (key) {
       case 27:     // ESC key -> exit
         exit(0);
         break;
       case 77:     // M key -> Mercury +
         speedMe+=0.05f;
          update(0);
          break;
       case 69:     // E key -> Earth +
         speedE+=0.05f;
          update(0);
          break;
       case 86:     // V key -> Venus +
         speedV+=0.05f;
          update(0);
          break;
       case 65:     // A key -> Mars +
         speedMa+=0.05f;
          update(0);
          break;
        case 85:     // U key -> Mercury -
         speedMe-=0.05f;
          update(0);
          break;
       case 76:     // I key -> Earth -
         speedE-=0.05f;
          update(0);
          break;
       case 79:     // O key -> Venus -
         speedV-=0.05f;
          update(0);
          break;
       case 80:     // P key -> Mars -
         speedMa-=0.05f;
          update(0);
          break;
   }
}

int main(int argc, char** argv) {
	//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;
}